Springboot2.x integrated cluster model Redis

Springboot2.x integrated cluster model Redis

Explanation

Redis Redis trunked mode is a way to achieve high availability solution, you can achieve multiple Redis data storage, and automatic failover through the cluster mode. If you want to learn more knowledge cluster model introduction, welcome to climb buildings.

Ready condition

pom.xml introduced the relevant jar

        <!-- 集成Redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
    
        <!-- Jedis 客户端 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
    
        <!-- lettuce客户端需要使用到 -->
          <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-pool2</artifactId>
           </dependency>

Sentinel mode configuration example application.yml properties.

spring:
  redis:
    host: 192.168.8.121
    port: 6379
    password: enjoyitlife
    timeout: 30000
    jedis:
      pool:
        max-active: 256
        max-wait: 30000
        max-idle: 64
        min-idle: 32
    lettuce:
      pool:
        max-active: 256
        max-idle: 64
        max-wait: 30000
        min-idle: 32
    sentinel:
      master: mymaster
      nodes: 192.168.8.121:26379, 192.168.8.121:26380,192.168.8.121:26381

nodes node reads. Because nodes are pooled, the spring in @value $ ( "xxx.xxx.xx") can not be read, provided herein is a way of obtaining modified node attributes.

Get Code Example RedisClusterNodesCfg.java nodes of node data.

package top.enjoyitlife.redis;

import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "spring.redis.cluster")
public class RedisClusterNodesCfg {

        private List<String> nodes;

        public List<String> getNodes() {
            return nodes;
        }

        public void setNodes(List<String> nodes) {
            this.nodes = nodes;
        }
    
}

Integration tutorials in cluster mode

Jedis client integration

JedisClusterConfig.java configuration

package top.enjoyitlife.redis.jedis;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import top.enjoyitlife.redis.RedisClusterNodesCfg;

@Configuration
@Profile("JedisCluster")
public class JedisClusterConfig {
    
    @Autowired
     private RedisClusterNodesCfg redisClusterNodesCfg;
    
     @Bean
     public JedisConnectionFactory redisPoolFactory()  throws Exception{
            RedisClusterConfiguration rcc=new RedisClusterConfiguration();
            List<String> nodesList=redisClusterNodesCfg.getNodes();
            String host=null;
            int port=0;
            for(String node:nodesList) {
                host=node.split(":")[0];
                port=Integer.valueOf(node.split(":")[1]);
                rcc.addClusterNode(new RedisNode(host,port));
            }
            return new JedisConnectionFactory(rcc);
     }
    
     @Bean
        public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
            RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
            template.setKeySerializer(new StringRedisSerializer());
            template.setValueSerializer(new StringRedisSerializer());
            template.setConnectionFactory(redisConnectionFactory);
            return template;
        }
     
     
}

Lettuce client integration

package top.enjoyitlife.redis.lettuce;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import top.enjoyitlife.redis.RedisClusterNodesCfg;

@Configuration
@Profile("lettuceCluster")
public class LettuceClusterConfig {
    
    @Autowired
     private RedisClusterNodesCfg redisClusterNodesCfg;
    
    
    @Bean
    public LettuceConnectionFactory redisConnectionFactory() {
        RedisClusterConfiguration rcc=new RedisClusterConfiguration();
        List<String> nodesList=redisClusterNodesCfg.getNodes();
        String host=null;
        int port=0;
        for(String node:nodesList) {
            host=node.split(":")[0];
            port=Integer.valueOf(node.split(":")[1]);
            rcc.addClusterNode(new RedisNode(host,port));
        }
        return new LettuceConnectionFactory(rcc);
    }
    

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new StringRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}

Springboot to unify the way through RedisClusterConfiguration connection to the cluster, the client is the difference between Jedis by JedisConnectionFactory initialize and Lettuce client through LettuceConnectionFactory initialization.

unit test

Jedis unit testing

package top.enjoyitlife.redis.jedis;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ActiveProfiles;

@SpringBootTest
@ActiveProfiles("JedisCluster")
class JedisClusterTest {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Test
    void contextLoads() {
        String name=redisTemplate.opsForValue().get("name").toString();
        redisTemplate.opsForValue().set("hahha", "enjoyitlife2020");
        System.out.println(name);
    }
}

Lettuce unit testing

package top.enjoyitlife.redis.lettuce;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ActiveProfiles;


@SpringBootTest
@ActiveProfiles("lettuceCluster")
class LettuceClusterTest {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    
    
    @Test
    void contextLoads() {
        String name = redisTemplate.opsForValue().get("name").toString();
        System.out.println(name);
    }

}

Well, above is the code example Springboot2.x integrated Redis cluster model, I hope you can help. thanks for reading.

Guess you like

Origin www.cnblogs.com/enjoyitlife/p/12057404.html