SpringBoot注解的方式使用redis集群模式

 

​ 使用SpringBoot都不喜欢写xml配置文件,下面我就分享一下使用配置类的方式将redis切换到集群模式


1、添加依赖

​ spring-boot-starter-parent已经将redis的依赖定义好了,我们不需要定义版本号:


    org.springframework.data
    spring-data-redis

2、编写集群配置属性类

package com.fcbox.sflowapp.config;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * Redis集群属性
 * @author 002158
 */
@Getter
@Setter
@ConfigurationProperties(prefix = "redis.cluster")
public class ClusterRedisProperties {
    /**
     * 最大连接数
     */
    private int maxTotal;
    /**
     * 最大空闲数
     */
    private int maxIdle;
    /**
     * 初始化连接数
     */
    private int minIdle;

    /**
     * 建立连接时最大等待时间,单位毫秒
     */
    private long maxWait;

    /**
     * 客户端超时时间,单位毫秒
     */
    private int timeout;

    /**
     * 指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
     */
    private boolean testOnBorrow;

    /**
     * 在return给pool时,是否提前进行validate操作
     */
    private boolean testOnReturn;

    /**
     * 如果为true,表示有一个idle object evitor线程对idle object进行扫描,如果validate失败,此object会被从pool中drop掉;
     */
    private boolean testWhileIdle;

    /**
     * 配合testWhileIdle使用,表示idle object evitor两次扫描之间要sleep的毫秒数
     */
    private long timeBetweenEvictionRunsMillis;

    /**
     * 集群信息
     */
    private String nodes;

    private int maxRedirects;

    private String password;
}

3、编写配置类

package com.fcbox.sflowapp.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.MapPropertySource;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashMap;
import java.util.Map;

/**
 * Redis集群配置类
 * @author 002158
 */
@Configuration
@EnableConfigurationProperties(ClusterRedisProperties.class)
public class RedisConfig {
    private static final String REDIS_CLUSTER_NODES_CONFIG_PROPERTY = "spring.redis.cluster.nodes";
    private static final String REDIS_CLUSTER_MAX_REDIRECTS_CONFIG_PROPERTY = "spring.redis.cluster.max-redirects";
    private static final String REDIS_CLUSTER_PROPERTIES = "redis.properties";

    @Autowired
    private ClusterRedisProperties clusterRedisProperties;

    /**
     * 配置jedis连接池
     * @return
     */
    @Bean
    public JedisPoolConfig jedisPoolConfig(){
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(clusterRedisProperties.getMaxTotal());
        jedisPoolConfig.setMaxIdle(clusterRedisProperties.getMaxIdle());
        jedisPoolConfig.setMinIdle(clusterRedisProperties.getMinIdle());
        jedisPoolConfig.setMaxWaitMillis(clusterRedisProperties.getMaxWait());
        jedisPoolConfig.setTestOnBorrow(clusterRedisProperties.isTestOnBorrow());
        jedisPoolConfig.setTestOnReturn(clusterRedisProperties.isTestOnReturn());
        jedisPoolConfig.setTestWhileIdle(clusterRedisProperties.isTestWhileIdle());
        jedisPoolConfig.setTimeBetweenEvictionRunsMillis(clusterRedisProperties.getTimeBetweenEvictionRunsMillis());
        return jedisPoolConfig;
    }

    /**
     * redis集群配置
     * @return
     */
    @Bean
    public RedisClusterConfiguration redisClusterConfiguration(){
        Map clusterPropertie = new HashMap<>();
        clusterPropertie.put(REDIS_CLUSTER_NODES_CONFIG_PROPERTY, clusterRedisProperties.getNodes());
        clusterPropertie.put(REDIS_CLUSTER_MAX_REDIRECTS_CONFIG_PROPERTY, clusterRedisProperties.getMaxRedirects());
        MapPropertySource mapPropertySource = new MapPropertySource(REDIS_CLUSTER_PROPERTIES, clusterPropertie);

        RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(mapPropertySource);
        return redisClusterConfiguration;
    }

    /**
     * 配置jedis连接工厂
     * @param redisClusterConfiguration
     * @param jedisPoolConfig
     * @return
     */
    @Bean
    public JedisConnectionFactory jedisConnectionFactory(RedisClusterConfiguration redisClusterConfiguration, JedisPoolConfig jedisPoolConfig){
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisClusterConfiguration,jedisPoolConfig );
        jedisConnectionFactory.setTimeout(clusterRedisProperties.getTimeout());
        jedisConnectionFactory.setPassword(clusterRedisProperties.getPassword());
        return jedisConnectionFactory;
    }

    /**
     * 配置RedisTemplate
     * @param factory
     * @return
     */
    @Bean
    public RedisTemplate redisTemplate(JedisConnectionFactory factory){
        RedisTemplate redisTemplate = new RedisTemplate();
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        redisTemplate.setConnectionFactory(factory);
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setValueSerializer(stringRedisSerializer);
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        redisTemplate.setHashValueSerializer(stringRedisSerializer);
        return redisTemplate;
    }
}

4、在disconf上传redis.propertie配置文件

redis.cluster.max-total=16
redis.cluster.max-idle=3
redis.cluster.min-idle=1
redis.cluster.max-wait=1000
redis.cluster.timeout=1000
redis.cluster.test-on-borrow=true
redis.cluster.test-on-return=false
redis.cluster.test-while-idle=true
redis.cluster.time-between-eviction-runs-millis=160000
redis.cluster.max-redirects=3
#redis.cluster.nodes=127.0.0.1:6379
#redis.cluster.password=
# sit2
redis.cluster.nodes=10.204.58.11:8080,10.204.58.12:8080,10.204.58.13:8080,10.204.58.14:8080,10.204.58.15:8080,10.204.58.16:8080
redis.cluster.password=K6CJDjjnE9d0OxRjYNKZ
这样就完成了集群模式的切换了,下面测试一下:

编写一个Controller

@RequestMapping(value = "test")
@RestController
public class TestController {
  @Autowired
  private RedisTemplate redisTemplate;
  @RequestMapping("/push")
  public Object testRedisPush(String password){
    redisTemplate.opsForValue().set("password", password);
    return "OK";
  }
  @RequestMapping("/pop")
  public Object testRedisPop(String password){
    return redisTemplate.opsForValue().get("password");
  }
}

你可能感兴趣的:(java)