返回 导航

SpringBoot / Cloud

hangge.com

SpringBoot - Redis的整合与使用详解2(Redis集群)

作者:hangge | 2019-11-29 08:10

二、Redis 的整合与使用:Redis 集群

1,安装配置

(1)首先编辑 pom.xml 文件,添加相关依赖:
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>

(2)接着配置集群信息,我们在 resources 目录下创建一个 application.yml 配置文件,内容如下:
(1)由于本案例各个 Redis 实例的 host 都是一样的,因此这里只配置了一个 host,而 port 配置成了一个集合,这些 port 将被注入一个集合中。
(2)poolConfig 则是基本的连接池信息配置:
  • smax-active:表示 Redis 连接池的最大连接数
  • max-idle:表示 Redis 连接池中的最大空闲连接数
  • max-wait-millis:表示连接池的最大阻塞等待使用,默认为 -1,表示没有限制
  • min-idle:表示连接池最小空闲连接数
spring:
redis:
cluster:
ports:
- 8001
- 8002
- 8003
- 8004
- 8005
- 8006
host: 192.168.60.133
poolConfig:
max-total: 8
max-idle: 8
max-wait-millis: -1
min-idle: 0

(3)不同与单机版 Redis 整合 Spring BootRedis 集群整合 Spring Boot 需要开发者手动配置。我们创建 RedisConfig,完成对 Redis 的配置:
@Configuration
@ConfigurationProperties("spring.redis.cluster")
public class RedisConfig {
    /**
        通过 @ConfigurationProperties 注解声明配置文件前缀,将配置文件中定义的 ports 数组、host
        及连接池配置信息都将被注入 port、host、poolConfig 三个属性中。
     **/
    List<Integer> ports;
    String host;
    JedisPoolConfig poolConfig;

    // 配置 RedisClusterConfiguration 实例
    @Bean
    RedisClusterConfiguration redisClusterConfiguration() {
        RedisClusterConfiguration configuration = new RedisClusterConfiguration();
        List<RedisNode> nodes = new ArrayList<>();
        for (Integer port : ports) {
            nodes.add(new RedisNode(host, port));
        }
        configuration.setPassword(RedisPassword.of("123")); // 设置 Redis 登录密码
        configuration.setClusterNodes(nodes); // 设置 Redis 节点信息
        return configuration;
    }

    // 根据 RedisClusterConfiguration 实例以及连接池配置信息创建 Jedis 连接工厂 JedisConnectionFactory
    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        JedisConnectionFactory factory
                = new JedisConnectionFactory(redisClusterConfiguration(),poolConfig);
        return factory;
    }

    // 根据 JedisConnectionFactory 创建 RedisTemplate,同时配置 key 和 value 序列号方式。
    @Bean
    RedisTemplate redisTemplate() {
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(jedisConnectionFactory());
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
        return redisTemplate;
    }

    // 根据 JedisConnectionFactory 创建 StringRedisTemplate,同时配置 key 和 value 序列号方式。
    @Bean
    StringRedisTemplate stringRedisTemplate() {
        StringRedisTemplate stringRedisTemplate
                = new StringRedisTemplate(jedisConnectionFactory());
        stringRedisTemplate.setKeySerializer(new StringRedisSerializer());
        stringRedisTemplate.setKeySerializer(new StringRedisSerializer());
        return stringRedisTemplate;
    }

    public List<Integer> getPorts() {
        return ports;
    }

    public void setPorts(List<Integer> ports) {
        this.ports = ports;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public JedisPoolConfig getPoolConfig() {
        return poolConfig;
    }

    public void setPoolConfig(JedisPoolConfig poolConfig) {
        this.poolConfig = poolConfig;
    }
}

2,使用样例

(1)首先创建一个 User 实体类,内容如下:
@Getter
@Setter
@ToString
public class User implements Serializable {
    private Integer id;
    private String name;
    private Integer age;
}

(2)接着创建一个 Controller 进行测试,代码其实和上文单实例 Redis 的测试 Controller 基本一致:
(1) RedisTemplateStringRedisTemplate 实例提供了 Redis 的基本操作方法,本次由我们自己提供:
  • StringRedisTemplateRedisTemplate 的子类,StringRedisTemplate 中的 keyvalue 都是字符串,采用的序列化方案是 StringRedisSerializer
  • RedisTemplate 则可以用来操作对象,RedisTemplate 采用的序列化方案是 JdkSerializationRedisSerializer
(2)无论 StringRedisTemplate 还是 RedisTemplate,它们都是通过 opsForValueopsForZSet 或者 opsForSet 等方法先获取一个操作对象,再使用该操作对象完成数据的读写。
@RestController
public class HelloController {

    @Autowired
    RedisTemplate redisTemplate; //默认提供的用来操作对象的redis操作实例

    @Autowired
    StringRedisTemplate stringRedisTemplate; //默认提供的用来操作字符串的redis操作实例

    @RequestMapping("/test")
    public void test() {
        //保存一个字符串
        ValueOperations<String, String> ops1 = stringRedisTemplate.opsForValue();
        ops1.set("message", "欢迎访问 hangge.com");

        //读取一个字符串
        String message = ops1.get("message");
        System.out.println("读取一个字符串:" + message);

        //保存一个对象
        ValueOperations ops2 = redisTemplate.opsForValue();
        User u1 = new User();
        u1.setId(1);
        u1.setName("hangge");
        u1.setAge(100);
        ops2.set("user", u1);

        //读取一个对象
        User u2 = (User) ops2.get("user");
        System.out.println("读取一个对象:" + u2.toString());
    }
}

(3)使用浏览器访问 http://localhost:8080/test,可以看到控制台输入如下:

(4)然后登录任意一个 Redis 实例,查询数据。可以看到查询时只要登录任意一个 Redis 实例,RedisCluster 会负责将查询请求 Redirected 到相应的实例上去。
评论

全部评论(0)

回到顶部