spring整合redis(集群、主从)

使用maven整合   

整合环境ssm:

spring   4.2.6.RELEASE

Mybatis 3.2.6

注意事项:

spring、redis和spring-redis的版本,开始用spring4.0整合不成功,使用4.2整合成功

pom.xml文件:


  4.0.0
  redis-cluster
  redis-cluster
  0.0.1-SNAPSHOT
  war
  
  
  
    	UTF-8
    	1.7
  		4.1.12
  	 	4.2.6.RELEASE
  	 	0.9.1.2
  	 	1.2.2
  	 	1.6.6
		 1.2.16
  		3.2.6 
  		3.1
  		1.2
  		${4.12}
	 	1.8.1.RELEASE
		2.9.0
		1.1.41
		5.1.38
    	1.3.0
		7.0
		2.4
  
  
    
      javax.servlet
      jstl
      1.2
      provided
    
    
      javax.servlet.jsp
      jsp-api
      2.1
      provided
    
    
      org.glassfish
      javax.annotation
      3.0.1
    
    
      org.glassfish
      javax.ejb
      3.0.1
    
    
      org.jboss.weld
      weld-osgi-bundle
      1.0.1-SP3
    
    
      org.glassfish
      javax.servlet
      3.0.1
    
    
	    junit
	    junit
	    ${junit}
	    
        test
	
	
	
    
        org.springframework
        spring-core
        ${spring.version}
    
    
        org.springframework
        spring-web
        ${spring.version}
    
    
        org.springframework
        spring-oxm
        ${spring.version}
    
    
        org.springframework
        spring-tx
        ${spring.version}
    
    
        org.springframework
        spring-jdbc
        ${spring.version}
    
    
        org.springframework
        spring-webmvc
        ${spring.version}
    
    
        org.springframework
        spring-aop
        ${spring.version}
    
    
        org.springframework
        spring-context-support
        ${spring.version}
    
    
        org.springframework
        spring-test
        ${spring.version}
    
    
    
    
	
		c3p0
		c3p0
		${c3p0-version}
	
		
	
	  commons-fileupload
	  commons-fileupload
	  ${commons-fileupload-version}
	
		
	
	  org.apache.commons
	  commons-lang3
	  ${org.apache.commons-version}
	
	
	 
        commons-io
        commons-io
        ${commons-io}
    
       

	
    
        javax
        javaee-api
        ${javaee-api}
    
    
    
	
    
        org.mybatis
        mybatis
        ${mybatis.version}
    
    
    
        org.mybatis
        mybatis-spring
        ${mybatis-spring}
    
    
     
    
        mysql
        mysql-connector-java
        ${mysql-connector}
    
    	
     
   
    
        log4j
        log4j
        ${log4j.version}
    
     
        org.slf4j
        slf4j-api
        ${slf4j-version}
    
    
        org.slf4j
        slf4j-log4j12
        ${slf4j-version}
    
    
      
    
        com.alibaba
        fastjson
        ${fastjson}
    
    
  	
	
		redis.clients 
		jedis
		${jedis}
	
		
	 
         
	  org.springframework.data  
	  spring-data-redis  
	  ${spring-data-redis}  
	

	
  
  
    
      
        maven-war-plugin
      
      
        maven-compiler-plugin
        
          1.6
          1.6
        
      
    
  

redis集群主要配置


redis-cluster.properties

redis.host1=192.168.1.235
redis.port1=7001
redis.host2=192.168.1.235
redis.port2=7002
redis.host3=192.168.1.235
redis.port3=7003
redis.host4=192.168.1.235
redis.port4=7004
redis.host5=192.168.1.235
redis.port5=7005
redis.host6=192.168.1.235
redis.port6=7006

redis.maxRedirects=3
redis.maxIdle=30
redis.maxTotal=100
redis.minIdle=5
redis.maxWaitMillis=30000 
redis.testOnBorrow=true
redis.testOnReturn=true 
redis.testWhileIdle=true
redis.timeout=3000

redis-cluster.xml



  
		   
				  
				  
					  
						  
							  
							  
						  
						  
							  
							  
						  
						  
							  
							  
						  
						  
							  
							  
						  
						  
							  
							  
						  
						  
							  
							  
						  
				  
		  
	  
	
	
	  
		
        
        
        
      
	   
	  
		
		 
		   
           
		
	  
	  
	  
		
		   
              
          
          
              
          
          
              
          
          
              
          
	   
	    
  



redis 主从配置

redis-ms.properties

# Redis settings  
#sentinel1的IP和端口  
sentinel1.host=192.168.1.233
sentinel1.port=26379  
#sentinel2的IP和端口  
sentinel2.host=192.168.1.233
sentinel2.port=26378 

im.hs.server.redis.maxIdle=500  
#最大连接数,超过此连接时操作redis会报错  
im.hs.server.redis.maxTotal=5000  
im.hs.server.redis.maxWaitTime=1000  
im.hs.server.redis.testOnBorrow=true  
#最小闲置连接数,spring启动的时候自动建立该数目的连接供应用程序使用,不够的时候会申请。  
im.hs.server.redis.minIdle=300
im.hs.server.redis.sentinel.masterName=mymaster

redis-ms.xml



  
  
  
           
		          
		          
		          
		          
		          
		          
		          
   		  
   		  
		
		    
		        
		            
		        
    		
		    
		        
		            
		                
		
		                
		            
		            
		                
		
		                
		            
		        
		    
	    

		
		    	    
          	
		


		
		    
		
	    


使用:

@Service
@Transactional(readOnly=true)
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserDao userDao;
	@Autowired
	private RedisTemplate redisTemplate;
	
	@Override
	@Transactional(readOnly=false)
	public void add(final User user) {
		// TODO Auto-generated method stub
		userDao.save(user);
		System.out.println("--------------添加redis缓存---------------------------");
		

		redisTemplate.execute(new RedisCallback() {  
            @Override  
            public Object doInRedis(RedisConnection connection)  
                    throws DataAccessException {  
                connection.hSet(redisTemplate.getStringSerializer().serialize("SYS_USER_TAB"),
                        redisTemplate.getStringSerializer().serialize("USER_"+user.getId()),  
                        redisTemplate.getStringSerializer().serialize(JSONObject.toJSONString(user)));  
              System.out.println(redisTemplate.getStringSerializer().serialize("USER_"+user.getId()));
                return null;  
            }  
        });  
		
		
		
	}

} 
  

附源码下载地址,配置不成功下载下来看看:http://download.csdn.net/detail/sunqingzhong44/9829625


你可能感兴趣的:(redis)