Spring Boot 2.1.3.RELEASE 整合Redis集群

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qmqm011/article/details/88745151

Redis集群搭建,详见Redis 集群

一、pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.wuychn</groupId>
    <artifactId>spring-boot-redis-cluster</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-redis-cluster</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.54</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

二、application.yml

server:
  port: 8080
spring:
  application:
    name: spring-boot-data-redis
  redis:
    database: 0
    cluster:
      nodes:
      - 192.168.115.128:6380
      - 192.168.115.128:6381
      - 192.168.115.128:6382
      - 192.168.115.128:6383
      - 192.168.115.128:6384
      - 192.168.115.128:6385
    jedis:
      pool:
        max-idle: 8
        min-idle: 0
        max-active: 8
        max-wait: -1
    timeout: 10000

Redis 的配置,不同的SpringBoot版本可能不同,可以参照RedisProperties的源码进行配置。

三、RedisConfig

package com.wuychn.config;

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);
        GenericFastJsonRedisSerializer genericFastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
        redisTemplate.setValueSerializer(genericFastJsonRedisSerializer);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

}

四、启动类

package com.wuychn;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class SpringBootRedisClusterApplication {

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @GetMapping("/set")
    public String set(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
        return key;
    }

    @GetMapping("/get")
    public String get(String key) {
        return (String) redisTemplate.opsForValue().get(key);
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringBootRedisClusterApplication.class, args);
    }

}

启动程序,在浏览器中访问http://localhost:8080/set?key=age&value=23,然后在Redis的命令行客户端中查看,发现key为age的值被存储到了6380节点上,并且6380的从数据库(6384)也存储了相应的数据:

127.0.0.1:6380> keys *
1) "age"
127.0.0.1:6384> keys *
1) "age"

而其他节点并没有存储age这个key:

127.0.0.1:6382> keys *
(empty list or set)

在测试过程中,遇到了如下的问题:

io.lettuce.core.RedisConnectionException: Unable to connect to 127.0.0.1:6380
......

最后在这篇文章中找到了解决办法。

猜你喜欢

转载自blog.csdn.net/qmqm011/article/details/88745151