springboot整合jedisCluster

JedisCluster
jedis客户端提供的一个操作集群的连接对象;
底层封装了单个节点电连接对象,
封装了连接池的对外使用的集群对象;

测试连接代码 •

收集节点信息(redis-cluster可以只提供若干个节点)

	@Test
	public void test(){
		//收集若干个节点信息
		Set<HostAndPort> set=new HashSet<HostAndPort>();
		set.add(new HostAndPort("10.9.39.13", 8000));
		set.add(new HostAndPort("10.9.39.13", 8001));
		//jedisCluster对象的构造需要连接池的配置对象
		JedisPoolConfig config=new JedisPoolConfig();
		config.setMaxTotal(200);
		config.setMaxIdle(8);
		config.setMinIdle(2);
		JedisCluster cluster
		=new JedisCluster(set, config);
		//调用方法,发送redis命令到集群执行;
		//name对应的slot是5798
		cluster.set("name", "王翠花");
		System.out.println(cluster.get("gender"));}

• 框架使用jedisCluster
○ 配置集群的连接信息

	#redis集群的配置
	spring.redis.cluster.nodes=10.9.39.13:8000,10.9.39.13:8001,10.9.39.13:8002
	spring.redis.cluster.maxTotal=200
	spring.redis.cluster.maxIdle=8
	spring.redis.cluster.minIdle=2

○ 配置类初始化构造一个JedisCluster对象

	package com.jt.easymall.config;
	import java.util.HashSet;
	import java.util.Set;
	import org.springframework.boot.context.properties.ConfigurationProperties;
	import org.springframework.context.annotation.Bean;
	import org.springframework.context.annotation.Configuration;
	/**
	 * 每个configuration的代码,都对应xml一部分配置
	 * @author admin
	 */
	import redis.clients.jedis.HostAndPort;
	import redis.clients.jedis.JedisCluster;
	import redis.clients.jedis.JedisPoolConfig;
	import redis.clients.jedis.JedisShardInfo;
	import redis.clients.jedis.ShardedJedisPool;
	@Configuration
	@ConfigurationProperties(prefix="spring.redis.cluster")
	public class RedisClusterConfig {
		private String nodes;
		private Integer maxTotal;
		private Integer maxIdle;
		private Integer minIdle;
		public String getNodes() {
			return nodes;
		}
		public void setNodes(String nodes) {
			this.nodes = nodes;
		}
		public Integer getMaxTotal() {
			return maxTotal;
		}
		public void setMaxTotal(Integer maxTotal) {
			this.maxTotal = maxTotal;
		}
		public Integer getMaxIdle() {
			return maxIdle;
		}
		public void setMaxIdle(Integer maxIdle) {
			this.maxIdle = maxIdle;
		}
		public Integer getMinIdle() {
			return minIdle;
		}
		public void setMinIdle(Integer minIdle) {
			this.minIdle = minIdle;
		}
	
		
		@Bean//初始化方法构造一个jedisCluster对象
		public JedisCluster init(){
			try{
				Set<HostAndPort> set=new HashSet<HostAndPort>();
				//"10.9.39.13:8000,10.9.39.13:8001"
				String[] node = nodes.split(",");
				for (String hostAndPort : node) {
					//"10.9.39.13:8000",解析ip和port
					String host=hostAndPort.split(":")[0];
					int port=Integer.parseInt
							(hostAndPort.split(":")[1]);
					set.add(new HostAndPort(host,port));
				}
				//利用其它属性,编写config对象
				JedisPoolConfig config=new JedisPoolConfig();
				config.setMaxTotal(maxTotal);
				config.setMaxIdle(maxIdle);
				config.setMinIdle(minIdle);
				return new JedisCluster(set,config);
			}catch(Exception e){
				//说明构造过程出现了一些问题,一般是因为没有提供
				//redis相关配置
				return null;}}}

○ 二次封装
○ 注入对象使用

测试jediscluster的高可用代码能力
编写一个简单的访问redis-cluster集群的功能;
一个访问,直接调用set/get方法

		@Autowired
		private JedisCluster cluster;
		@RequestMapping("cluster")
		public String setAndGet(String key){
			cluster.set(key, "测试数据");
			return cluster.get(key);}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鱼鱼大头鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值