原文:http://chentian114.iteye.com/blog/2292323

1、通过spring-data-redis集成redis

pom.xml依赖包

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.chen</groupId>
  5. <artifactId>test_redis02</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>war</packaging>
  8. <build />
  9. <properties>
  10. <spring.version>3.1.2.RELEASE</spring.version>
  11. </properties>
  12.  
  13. <dependencies>
  14. <dependency>
  15. <groupId>junit</groupId>
  16. <artifactId>junit</artifactId>
  17. <version>4.12</version>
  18. </dependency>
  19. <!-- spring -->
  20. <dependency>
  21. <groupId>org.springframework</groupId>
  22. <artifactId>spring-context-support</artifactId>
  23. <version>${spring.version}</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework</groupId>
  27. <artifactId>spring-webmvc</artifactId>
  28. <version>${spring.version}</version>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework</groupId>
  32. <artifactId>spring-orm</artifactId>
  33. <version>${spring.version}</version>
  34. </dependency>
  35.  
  36. <dependency>
  37. <groupId>redis.clients</groupId>
  38. <artifactId>jedis</artifactId>
  39. <version>2.8.1</version>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework.data</groupId>
  43. <artifactId>spring-data-redis</artifactId>
  44. <version>1.7.1.RELEASE</version>
  45. </dependency>
  46. <dependency>
  47. <groupId>org.apache.commons</groupId>
  48. <artifactId>commons-pool2</artifactId>
  49. <version>2.4.2</version>
  50. </dependency>
  51. </dependencies>
  52. </project>

spring application-reids.xml配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
  10. ">
  11.  
  12. <!-- 加载配置文件 -->
  13. <context:property-placeholder location="classpath:redis.properties"/>
  14. <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
  15. <property name="maxIdle" value="${redis.maxIdle}" />
  16. <!-- <property name="maxActive" value="${redis.maxActive}" />
  17. <property name="maxWait" value="${redis.maxWait}" />
  18. <property name="testOnBorrow" value="${redis.testOnBorrow}" /> -->
  19. </bean>
  20.  
  21. <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
  22. p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/>
  23.  
  24. <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
  25. <property name="connectionFactory" ref="connectionFactory" />
  26. </bean>
  27.  
  28. </beans>

UserDaoImpl.java

  1. package com.chen.dao;
  2.  
  3. import java.io.Serializable;
  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.dao.DataAccessException;
  7. import org.springframework.data.redis.connection.RedisConnection;
  8. import org.springframework.data.redis.core.RedisCallback;
  9. import org.springframework.data.redis.core.RedisTemplate;
  10. import org.springframework.stereotype.Repository;
  11.  
  12. import com.chen.pojo.User;
  13. @Repository
  14. public class UserDaoImpl implements UserDao {
  15.  
  16. @Autowired
  17. private RedisTemplate<Serializable,Serializable> redisTemplate;
  18.  
  19. public void saveUser(final User user) {
  20. redisTemplate.execute(new RedisCallback<Object>(){
  21. @Override
  22. public Object doInRedis(RedisConnection connection)
  23. throws DataAccessException {
  24. String str= "user.uid."+user.getId();
  25. byte[] key = redisTemplate.getStringSerializer().serialize(str);
  26. connection.set(key,redisTemplate.getStringSerializer().serialize(user.getName()));
  27. return null;
  28. }
  29. });
  30. }
  31.  
  32. public User getUser(final long id) {
  33. return redisTemplate.execute(new RedisCallback<User>(){
  34. @Override
  35. public User doInRedis(RedisConnection connection) throws DataAccessException {
  36. byte[] key = redisTemplate.getStringSerializer().serialize("user.uid." + id);
  37. if(connection.exists(key)) {
  38. byte[] value = connection.get(key);
  39. String name = redisTemplate.getStringSerializer().deserialize(value);
  40. User user = new User();
  41. user.setName(name);
  42. user.setId(id);
  43. return user;
  44. }
  45. return null;
  46. }
  47. });
  48. }
  49.  
  50. }

2、通过jedis集成redis

spring applicationContext.xml配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
  10. ">
  11.  
  12. <!-- 加载配置文件 -->
  13. <context:property-placeholder location="classpath:redis.properties"/>
  14. <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
  15. <property name="maxIdle" value="${redis.maxIdle}" />
  16. <!-- <property name="maxActive" value="${redis.maxActive}" />
  17. <property name="maxWait" value="${redis.maxWait}" />
  18. <property name="testOnBorrow" value="${redis.testOnBorrow}" /> -->
  19. </bean>
  20. <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool" scope="singleton">
  21. <constructor-arg index="0" ref="jedisPoolConfig" />
  22. <constructor-arg index="1">
  23. <list>
  24. <bean class="redis.clients.jedis.JedisShardInfo">
  25. <constructor-arg name="host" value="${redis.host}" />
  26. <constructor-arg name="port" value="${redis.port}" />
  27. <!-- <constructor-arg name="timeout" value="${redis.timeout}" /> -->
  28. </bean>
  29. </list>
  30. </constructor-arg>
  31. </bean>
  32.  
  33. </beans>

RedisDataSourceImpl.java

  1. package com.chen.redis;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Repository;
  5.  
  6. import redis.clients.jedis.ShardedJedis;
  7. import redis.clients.jedis.ShardedJedisPool;
  8. @Repository("redisDataSource")
  9. public class RedisDataSourceImpl implements RedisDataSource {
  10.  
  11. @Autowired
  12. private ShardedJedisPool shardedJedisPool;
  13.  
  14. public ShardedJedis getRedisClient() {
  15. try{
  16. ShardedJedis shardJedis = shardedJedisPool.getResource();
  17. return shardJedis;
  18. }catch(Exception e){
  19. e.printStackTrace();
  20. }
  21. return null;
  22. }
  23.  
  24. public void returnResource(ShardedJedis shardedJedis) {
  25. shardedJedisPool.returnResource(shardedJedis);
  26. }
  27.  
  28. public void returnResource(ShardedJedis shardedJedis, boolean broken) {
  29. if(broken){
  30. shardedJedisPool.returnBrokenResource(shardedJedis);
  31. }else{
  32. shardedJedisPool.returnResource(shardedJedis);
  33. }
  34. }
  35.  
  36. }

RedisClientTemplate.java

  1. package com.chen.redis;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Repository;
  5.  
  6. import redis.clients.jedis.ShardedJedis;
  7.  
  8. @Repository("redisClientTemplate")
  9. public class RedisClientTemplate {
  10. @Autowired
  11. private RedisDataSource redisDataSource;
  12.  
  13. public void disconnect(){
  14. ShardedJedis shardedJedis = redisDataSource.getRedisClient();
  15. shardedJedis.disconnect();
  16. }
  17.  
  18. public String set(String key,String val){
  19. String result = null ;
  20. ShardedJedis jedis = redisDataSource.getRedisClient();
  21. if(jedis ==null)return null;
  22. boolean broken = false;
  23. try{
  24. result = jedis.set(key, val);
  25. }catch(Exception e){
  26. e.printStackTrace();
  27. broken = true ;
  28. }finally{
  29. redisDataSource.returnResource(jedis,broken);
  30. }
  31. return result;
  32. }
  33.  
  34. public String get(String key){
  35. String result = null;
  36. ShardedJedis jedis = redisDataSource.getRedisClient();
  37. if(jedis==null)return result;
  38. boolean broken = false;
  39. try{
  40. result = jedis.get(key);
  41. }catch(Exception e){
  42. e.printStackTrace();
  43. broken= true;
  44. }finally{
  45. redisDataSource.returnResource(jedis,broken);
  46. }
  47. return result;
  48. }
  49. }

DemoTest.java

spring集成redis,集成redis集群的更多相关文章

  1. Redis+Twemproxy+HAProxy集群(转) 干货

    原文地址:Redis+Twemproxy+HAProxy集群  干货 Redis主从模式 Redis数据库与传统数据库属于并行关系,也就是说传统的关系型数据库保存的是结构化数据,而Redis保存的是一 ...

  2. redis:哨兵集群配置

    最少配置1主2从3哨兵 一.引言 上一篇文章我们详细的讲解了Redis的主从集群模式,其实这个集群模式配置很简单,只需要在Slave的节点上进行配置,Master主节点的配置不需要做任何更改,但是有一 ...

  3. Redis 高可用集群

    Redis 高可用集群 Redis 的集群主从模型是一种高可用的集群架构.本章主要内容有:高可用集群的搭建,Jedis连接集群,新增集群节点,删除集群节点,其他配置补充说明. 高可用集群搭建 集群(c ...

  4. Redis存储Tomcat集群的Session

    Redis存储Tomcat集群的Session 如何 做到把新开发的代码推送到到生产系统中部署,生产系统要能够零宕机.对使用用户零影响. 设想 是使用集群来搞定,通过通知负载均衡Nginx,取下集群中 ...

  5. Redis 3.0 集群搭建

    Redis 3.0 集群搭建 开启两个虚拟机 分别在两个虚拟机上开启3个Redis实例 3主3从两个虚拟机里的实例互为主备 下面分别在两个虚拟机上安装,网络设置参照codis集群的前两个主机 分别关闭 ...

  6. Redis 3.0集群 Window搭建方案

    Redis 3.0集群 Window搭建方案 1.集群安装前准备 安装Ruby环境,安装:rubyinstaller-2.3.0-x64.exe http://dl.bintray.com/onecl ...

  7. Redis+Tomcat+Nginx集群实现Session共享,Tomcat Session共享

    Redis+Tomcat+Nginx集群实现Session共享,Tomcat Session共享 ============================= 蕃薯耀 2017年11月27日 http: ...

  8. Redis进阶实践之十 Redis主从复制的集群模式

    一.引言        Redis的基本数据类型,高级特性,与Lua脚本的整合等相关知识点都学完了,说是学完了,只是完成了当前的学习计划,在以后的时间还需继续深入研究和学习.从今天开始来讲一下有关Re ...

  9. Redis进阶实践之十一 Redis的Cluster集群搭建

    一.引言      本文档只对Redis的Cluster集群做简单的介绍,并没有对分布式系统的详细概念做深入的探讨.本文只是提供了有关如何设置集群.测试和操作集群的说明,而不涉及Redis集群规范中涵 ...

  10. Redis进阶实践之十二 Redis的Cluster集群动态扩容

    一.引言     上一篇文章我们一步一步的教大家搭建了Redis的Cluster集群环境,形成了3个主节点和3个从节点的Cluster的环境.当然,大家可以使用 Cluster info 命令查看Cl ...

随机推荐

  1. 全排列---(dfs)

    全排列输入一个数n,按字典序输出1-n的全排列 #include "cstdio" #include "cstring" ],ans[],n; void dfs ...

  2. 【51NOD】1486 大大走格子

    [算法]动态规划+组合数学 [题意]有一个h行w列的棋盘,定义一些格子为不能走的黑点,现在要求从左上角走到右下角的方案数. [题解] 大概能考虑到离散化黑点后,中间的空格子直接用组合数计算. 然后解决 ...

  3. python3 uper(),继承实现原理,封装

    抽象类:本身不能被实例化,也不应该不实例化,它的作用就定义标准,并不用具体实现 import abc class Parent(metaclass=abc.ABCMeta): x=1 @abc.abs ...

  4. Kuangbin 带你飞 KMP扩展KMP Manacher

    首先是几份模版 KMP void kmp_pre(char x[],int m,int fail[]) { int i,j; j = fail[] = -; i = ; while (i < m ...

  5. 利用cron监视后台进程状态

    利用cron监视后台进程状态 http://blog.csdn.net/dyx810601/article/details/72967758 1. 利用cron监视后台进程状态,如果进程死掉或服务器重 ...

  6. linux服务与进程

    linux服务与进程 http://www.cnblogs.com/jamesbd/p/3567654.html linux服务与进程 1.应用程序 2.服务脚本 3.配置文件 4.查看进程 5.查看 ...

  7. 一步步疑难解析 —— Python 异步编程构建博客

    声明:该项目学习资源主要来自廖雪峰的Python教程,参见 http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6 ...

  8. 胖AP与瘦AP区别

    一.胖AP组网方案 1.漫游问题 用户从一个胖AP的覆盖区域走到另一个胖AP的覆盖区域,会重新连接信号强的一个胖AP,重新进行认证,重新获取IP地址,存在断网现象: 2.无法保证WLAN的安全性 为了 ...

  9. Spring的自动装配

    在Spring中对自定义的引用类型注入时可以实现自动赋值.但是必须依赖set方法:  自动装配功能有两种: <!-- autowire:"byType" --根据class匹 ...

  10. 巧用nth_element求容器前n%大小的那个数

    #include <algorithm> #include <vector> #include <iostream> #include <string> ...