springboot整合jedisCluster[通俗易懂]

springboot整合jedisCluster[通俗易懂]JedisClusterjedis客户端提供的一个操作集群的连接对象;底层封装了单个节点电连接对象,封装了连接池的对外使用的集群对象;测试连接代码•收集节点信息(redis-cluster可以只提供若干个节点) @Test publicvoidtest(){ //收集若干个节点信息 Set<HostAndPort>set=newHashSet<Ho…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

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);}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/181943.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • APAP论文阅读笔记[通俗易懂]

    APAP论文阅读笔记[通俗易懂]As-Projective-As-PossibleImageStitchingwithMovingDLT论文阅读笔记论文和代码可以在这个网址找到:https://cs.adelaide.edu.au/~tjchin/apap/一、全文翻译题目:使用移动DLT进行尽可能投影的图像拼接摘要:我们专注于图像拼接的任务,通常通过估计投影扭曲来解决这一问题——当场景是平面的或当视图完全因旋转而不同时,该模型是合理的。这样的条件在实践中很容易被违反,这就产生了使用重影人工制品的缝合结果,这就需要使用去

    2022年9月22日
    0
  • ORACLE11g安装包及安装过程(附安装包)

    ORACLE11g安装包及安装过程(附安装包)ORACLE11g安装包及安装过程这是一个职场小白的第一篇博文,就是随手分享一些经验,希望我可以写的尽量清楚,如果有问题,也希望和大家一起探讨。1.oracle11g安装包链接:https://pan.baidu.com/s/18lYrkqqHG8u4aDdQekHc3g提取码:fg2v一:开始安装解压文件后找到setup.exe,双击开始安装oracle(注意:设置文件路径时尽量不…

    2022年7月25日
    84
  • 数据结构(栈&堆 )

    数据结构(栈&堆 )

    2021年9月3日
    56
  • springboot整合shiro实现权限控制

    springboot整合shiro实现权限控制ApacheShiro是一个强大且易用的Java安全框架,执行身份验证、授权、密码学和会话管理。使用Shiro的易于理解的API,您可以快速、轻松地获得任何应用程序,从最小的移动应用程序到最大的网络和企业应用程序。上个月写了一个在线教育的项目用到了shiro权限控制,这几天又复盘了一下,对其进行了深入探究,来总结一下。下面所总结的有关shiro的代码已经传到我的github上,可以访问下面的……

    2022年10月22日
    0
  • android studio与eclipse_androidstudio源码网

    android studio与eclipse_androidstudio源码网 以前公司的老项目,是使用eclipse进行开发的,虽然androidstudio出来了很久,但为了避免迁移会有一些问题,一直忍着没改,但最近谷歌公司上架有要求,要求android的项目要用android8.0来编译,然后就发现eclipse+ADT已经不支持jdk1.8还有android8.0,运行就会有问题,有类似unsported52.0,还有各种莫名其妙的错误,比如无法识别27,…

    2022年10月4日
    0
  • php curl_init post/get请求

    php curl_init post/get请求publicfunctiongetCurlApi(){$url=’地址’;$headers=array(‘access_token:’.$token);$curl=curl_init();curl_setopt($curl,CURLOPT_URL,$url);//设置调用地址curl_setopt($curl,CURLOPT_HTTPHEADER,$headers);//添加头…

    2022年7月12日
    17

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号