Java Redisson多集群连接
场景:
需要连接多个Redis-Cluster集群进行操作
直接上源码
Maven引入Redisson依赖
-
<dependency>
-
<groupId>org.redisson</groupId>
-
<artifactId>redisson-spring-boot-starter</artifactId>
-
<version>3.17.1</version>
-
</dependency>
配置集群配置信息文件,本文采用yml配置方式
redis_one.yml
-
clusterServersConfig:
-
#连接空闲超时,单位:毫秒
-
idleConnectionTimeout: 10000
-
#连接超时,单位:毫秒
-
connectTimeout: 5000
-
#命令等待超时,单位:毫秒
-
timeout: 10000
-
#命令失败重试次数
-
retryAttempts: 5
-
#命令重试发送时间间隔,单位:毫秒
-
retryInterval: 2000
-
#redis集群密码
-
password: redisPwd
-
#单个连接最大订阅数量
-
subscriptionsPerConnection: 5
-
#从节点最小空闲连接数
-
slaveConnectionMinimumIdleSize: 32
-
#从节点连接池大小
-
slaveConnectionPoolSize: 64
-
#主节点最小空闲连接数
-
masterConnectionMinimumIdleSize: 32
-
#主节点连接池大小
-
masterConnectionPoolSize: 64
-
#读取操作的负载均衡模式
-
#默认值: SLAVE(只在从服务节点里读取)
-
#注:在从服务节点里读取的数据说明已经至少有两个节点保存了该数据,确保了数据的高可用性。
-
#设置读取操作选择节点的模式。
-
#可用值为:
-
#SLAVE - 只在从服务节点里读取。
-
#MASTER - 只在主服务节点里读取。
-
#MASTER_SLAVE - 在主从服务节点里都可以读取。
-
readMode: "SLAVE"
-
#节点地址
-
nodeAddresses:
-
- "redis://108.0.0.112:8000"
-
- "redis://108.0.0.113:8000"
-
- "redis://108.0.0.114:8000"
-
- "redis://108.0.0.115:8000"
-
- "redis://108.0.0.116:8000"
-
- "redis://108.0.0.117:8000"
-
#集群扫描间隔时间
-
scanInterval: 1000
-
#传输模式
-
#默认值:TransportMode.NIO
-
#可选参数:
-
#TransportMode.NIO,
-
#TransportMode.EPOLL - 需要依赖里有netty-transport-native-epoll包(Linux)
-
#TransportMode.KQUEUE - 需要依赖里有 netty-transport-native-kqueue包(macOS)
-
transportMode: NIO
-
#编码
-
codec: !<org.redisson.client.codec.StringCodec> {}
redis_two.yml的配置同上,只是节点地址不同
关于Redisson的具体配置内容可以参考Redisson官方文档 - 2. 配置方法-阿里云开发者社区
注意依赖版本,上方Redisson官方文档中的某些参数在此版本可能无效,启动时,redisson会报错,此yml配置文件中的内容全都可用(已实测)
读取配置文件并注入Bean
-
import org.redisson.Redisson;
-
import org.redisson.api.RedissonClient;
-
import org.redisson.config.Config;
-
import org.springframework.context.annotation.Bean;
-
import org.springframework.context.annotation.Configuration;
-
import org.springframework.core.io.DefaultResourceLoader;
-
import org.springframework.core.io.Resource;
-
import org.springframework.core.io.ResourceLoader;
-
-
import java.io.IOException;
-
-
-
public class InitializingRedis {
-
-
-
public RedissonClient getRedissonClientElectronicCard() throws IOException {
-
ResourceLoader loader = new DefaultResourceLoader();
-
Resource resource = loader.getResource("redis_one.yml");
-
Config config = Config.fromYAML(resource.getInputStream());
-
config.useClusterServers();
-
return Redisson.create(config);
-
}
-
-
-
public RedissonClient getRedissonClientRfidRectify() throws IOException {
-
ResourceLoader loader = new DefaultResourceLoader();
-
Resource resource = loader.getResource("redis_two.yml");
-
Config config = Config.fromYAML(resource.getInputStream());
-
config.useClusterServers();
-
return Redisson.create(config);
-
}
-
-
}
使用RedissonClient进行操作
-
import lombok.extern.slf4j.Slf4j;
-
import org.redisson.api.RBatch;
-
import org.redisson.api.RMapAsync;
-
import org.redisson.api.RedissonClient;
-
import org.springframework.stereotype.Component;
-
-
import javax.annotation.PostConstruct;
-
import javax.annotation.Resource;
-
-
-
-
public class RedissonTask {
-
-
-
private RedissonClient redissonClientOne;
-
-
-
private RedissonClient redissonClientTwo;
-
-
-
public void test() {
-
RBatch oneBatch = redissonClientOne.createBatch();
-
RBatch twoBatch = redissonClientTwo.createBatch();
-
RMapAsync<Object, Object> oneMap = oneBatch.getMap("onetest");
-
oneMap.putAsync("a", "1");
-
oneMap.putAsync("b", "2");
-
-
RMapAsync<Object, Object> twoMap = twoBatch.getMap("twotest");
-
twoMap.putAsync("c", "3");
-
twoMap.putAsync("d", "4");
-
oneBatch.execute();
-
twoBatch.execute();
-
}
-
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgjfefa
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13