spring之spring-data-redis集群,哨兵整合

  • 引入jar包
        
            org.springframework.data
            spring-data-redis
            1.7.11.RELEASE
        
        
            redis.clients
            jedis
            2.8.2
        
  • 配置spring-data-redis的配置文件

①注入RedisClusterConfiguration,配置JedisPoolConfig的连接信息

②配置JedisConnectionFactory

③配置RedisTemplate

集群:




    
    
        
        
        
        
        
        

        
        
        
    

    
    
        
        
        
        
        
        
        
        
    

    
    
        
            
            
            
            
            
            
        
    


实际代码使用:

/**
     * redis集群操作类
     */
    @Autowired
    private JedisCluster jedisCluster;

    @Autowired
    @Lazy
    private RedissonClient redissonClient;

哨兵:



 
    
    
 

 
    
        
        
        
        
        
        
        
        
        
        
        
        
    
 
    
        
            
                
                
            
        
        
            
                
                    
                    
                
                
                    
                    
                
                
                    
                    
                
            
        
    
    
        
        
    
    
        
        
            
        
        
            
        
    

实际使用:

@Autowired
private StringRedisTemplate redisTemplate;

private boolean insertObject(final Object obj, final String keyEnum, final long timeout) {

        if (obj == null) {
            return false;
        }

        final String value = JSONObject.toJSONString(obj);
        boolean result = redisTemplate.execute(new RedisCallback() {
            @Override
            public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
                byte[] redisKey = redisTemplate.getStringSerializer().serialize(keyEnum);
                byte[] redisValue = redisTemplate.getStringSerializer().serialize(value);
                connection.set(redisKey, redisValue);
                if (timeout > 0) {
                    redisTemplate.expire(keyEnum, timeout, TimeUnit.MILLISECONDS);
                }
                return true;
            }
        });

        return result;
    }
  • 编写redisManage管理类

redisTemplate

spring之spring-data-redis集群,哨兵整合_第1张图片

 

你可能感兴趣的:(redis)