zoukankan      html  css  js  c++  java
  • Springboot2.x集成Redis集群模式

    Springboot2.x集成Redis集群模式

    说明

    Redis集群模式是Redis高可用方案的一种实现方式,通过集群模式可以实现Redis数据多处存储,以及自动的故障转移。如果想了解更多集群模式的相关知识介绍,欢迎往上爬楼。

    准备条件

    pom.xml中引入相关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>
    

    application.yml集群模式配置属性示例。

    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
        cluster:
          nodes:
          - 192.168.8.121:7000
          - 192.168.8.121:7001
          - 192.168.8.121:7002
          - 192.168.8.121:7003
          - 192.168.8.121:7004
          - 192.168.8.121:7005
          - 192.168.8.121:7006
          - 192.168.8.121:7007
    

    nodes节点读取。因为nodes是集合方式,所以spring中的@value$("xxx.xxx.xx")是无法读取的,本文提供了一种获取改节点属性的方式。

    RedisClusterNodesCfg.java 获取nodes节点数据的代码示例。

    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;
    		}
    	
    }
    
    

    集群模式下的整合教程

    Jedis客户端整合

    JedisClusterConfig.java 相关配置

    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客户端整合

    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通过RedisClusterConfiguration来统一了连接集群的方式,区别Jedis客户端是通过JedisConnectionFactory进行初始化,而Lettuce客户端是通过LettuceConnectionFactory初始化。

    单元测试

    Jedis单元测试

    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 单元测试

    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);
    	}
    
    }
    
    

    好了以上就是Springboot2.x集成Redis集群模式的代码示例,希望对你能有所帮助。谢谢阅读。

  • 相关阅读:
    美国队长清晰TC中字 迅雷下载+ 美国队长 漫画 复仇者迅雷下载
    【转】腾讯、人人、新浪社交网络优劣势分析(转自月光博客)
    【技术贴】NVIDIA控制面板设,显示屏输入信号超出范围
    【技术贴】怎么拖动vs2008的控件
    SQL Server 2000/2005检测存储过程名是否存在,存在删除
    asp.net生成HTML
    gridview列 数字、货币和日期 显示格式
    用C#编写托盘程序
    判断地址栏参数,为空或null
    C# 读取计算机CPU,HDD信息
  • 原文地址:https://www.cnblogs.com/enjoyitlife/p/12057404.html
Copyright © 2011-2022 走看看