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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • centos7安装php环境_docker搭建php开发环境

    centos7安装php环境_docker搭建php开发环境centos7php环境手动搭建:1.先安装apache:yum安装yuminstallhttpd进入配置文件vi/etc/httpd/conf/httpd.conf(/etc/httpd/conf/httpd.conf为配置文件位置)apache默认就是使用80端口防火墙开启80端口(一般例如在阿里云网站控制台直接开启即可)服务器常用指令:linux常用服务的启动、停止、重启操作服务/操作 启动 停止 重启apache systemctlstarthttpd开启

    2022年9月22日
    2
  • C语言数组初始化的三种方法[通俗易懂]

    C语言数组初始化的三种方法[通俗易懂]C语言中,数组初始化的方式主要有三种:1、声明时,使用{0}初始化;2、使用memset;3、用for循环赋值。那么,这三种方法的原理以及效率如何呢?请看下面的测试代码:#defineARRAY_SIZE_MAX(1*1024*1024)voidfunction1(){chararray[ARRAY_SIZE_MAX]={0};…

    2022年10月8日
    1
  • JAVA swing_java action

    JAVA swing_java action1.整体的结构图:2.编写GameFrame03.java的代码:packagecn.bjsxt.test;importjava.awt.Frame;importjava.awt.Graphics;importjava.awt.Image;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;publi…

    2025年6月22日
    5
  • Protel 99SE详细安装教程(附安装包)[通俗易懂]

    Protel 99SE详细安装教程(附安装包)[通俗易懂]安装步骤:安装前先关闭杀毒软件和360卫士,注意安装路径不能有中文,安装包路径也不要有中文。安装前请断网。试装系统:win1064bit1.解压安装包。2.以管理员身份运行Protel99SE文件夹里的安装程序。3.点击下一步。4.name和company随便输入,打开SerialNO.txt,将serialNO输入到安装界面的code栏中。5.选择好安装目录(不要出现中…

    2022年5月30日
    82
  • 树莓派、jetson nano 安装pycharm arm架构64 aarch64

    树莓派、jetson nano 安装pycharm arm架构64 aarch64树莓派、jetsonnano安装pycharmarm架构64aarch64

    2025年7月3日
    6
  • goland2021破解激活码【在线注册码/序列号/破解码】「建议收藏」

    goland2021破解激活码【在线注册码/序列号/破解码】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月20日
    119

发表回复

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

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