• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Java Redisson多集群连接

武飞扬头像
胡小妹滴陈小钿
帮助1

场景:

需要连接多个Redis-Cluster集群进行操作


直接上源码

Maven引入Redisson依赖

  1.  
    <dependency>
  2.  
    <groupId>org.redisson</groupId>
  3.  
    <artifactId>redisson-spring-boot-starter</artifactId>
  4.  
    <version>3.17.1</version>
  5.  
    </dependency>

配置集群配置信息文件,本文采用yml配置方式

学新通

redis_one.yml

  1.  
    clusterServersConfig:
  2.  
    #连接空闲超时,单位:毫秒
  3.  
    idleConnectionTimeout: 10000
  4.  
    #连接超时,单位:毫秒
  5.  
    connectTimeout: 5000
  6.  
    #命令等待超时,单位:毫秒
  7.  
    timeout: 10000
  8.  
    #命令失败重试次数
  9.  
    retryAttempts: 5
  10.  
    #命令重试发送时间间隔,单位:毫秒
  11.  
    retryInterval: 2000
  12.  
    #redis集群密码
  13.  
    password: redisPwd
  14.  
    #单个连接最大订阅数量
  15.  
    subscriptionsPerConnection: 5
  16.  
    #从节点最小空闲连接数
  17.  
    slaveConnectionMinimumIdleSize: 32
  18.  
    #从节点连接池大小
  19.  
    slaveConnectionPoolSize: 64
  20.  
    #主节点最小空闲连接数
  21.  
    masterConnectionMinimumIdleSize: 32
  22.  
    #主节点连接池大小
  23.  
    masterConnectionPoolSize: 64
  24.  
    #读取操作的负载均衡模式
  25.  
    #默认值: SLAVE(只在从服务节点里读取)
  26.  
    #注:在从服务节点里读取的数据说明已经至少有两个节点保存了该数据,确保了数据的高可用性。
  27.  
    #设置读取操作选择节点的模式。
  28.  
    #可用值为:
  29.  
    #SLAVE - 只在从服务节点里读取。
  30.  
    #MASTER - 只在主服务节点里读取。
  31.  
    #MASTER_SLAVE - 在主从服务节点里都可以读取。
  32.  
    readMode: "SLAVE"
  33.  
    #节点地址
  34.  
    nodeAddresses:
  35.  
    - "redis://108.0.0.112:8000"
  36.  
    - "redis://108.0.0.113:8000"
  37.  
    - "redis://108.0.0.114:8000"
  38.  
    - "redis://108.0.0.115:8000"
  39.  
    - "redis://108.0.0.116:8000"
  40.  
    - "redis://108.0.0.117:8000"
  41.  
    #集群扫描间隔时间
  42.  
    scanInterval: 1000
  43.  
    #传输模式
  44.  
    #默认值:TransportMode.NIO
  45.  
    #可选参数:
  46.  
    #TransportMode.NIO,
  47.  
    #TransportMode.EPOLL - 需要依赖里有netty-transport-native-epoll包(Linux)
  48.  
    #TransportMode.KQUEUE - 需要依赖里有 netty-transport-native-kqueue包(macOS)
  49.  
    transportMode: NIO
  50.  
    #编码
  51.  
    codec: !<org.redisson.client.codec.StringCodec> {}
学新通

redis_two.yml的配置同上,只是节点地址不同

关于Redisson的具体配置内容可以参考Redisson官方文档 - 2. 配置方法-阿里云开发者社区 

注意依赖版本,上方Redisson官方文档中的某些参数在此版本可能无效,启动时,redisson会报错,此yml配置文件中的内容全都可用(已实测)

读取配置文件并注入Bean

  1.  
    import org.redisson.Redisson;
  2.  
    import org.redisson.api.RedissonClient;
  3.  
    import org.redisson.config.Config;
  4.  
    import org.springframework.context.annotation.Bean;
  5.  
    import org.springframework.context.annotation.Configuration;
  6.  
    import org.springframework.core.io.DefaultResourceLoader;
  7.  
    import org.springframework.core.io.Resource;
  8.  
    import org.springframework.core.io.ResourceLoader;
  9.  
     
  10.  
    import java.io.IOException;
  11.  
     
  12.  
    @Configuration
  13.  
    public class InitializingRedis {
  14.  
     
  15.  
    @Bean(name = "redissonClientOne")
  16.  
    public RedissonClient getRedissonClientElectronicCard() throws IOException {
  17.  
    ResourceLoader loader = new DefaultResourceLoader();
  18.  
    Resource resource = loader.getResource("redis_one.yml");
  19.  
    Config config = Config.fromYAML(resource.getInputStream());
  20.  
    config.useClusterServers();
  21.  
    return Redisson.create(config);
  22.  
    }
  23.  
     
  24.  
    @Bean(name = "redissonClientTwo")
  25.  
    public RedissonClient getRedissonClientRfidRectify() throws IOException {
  26.  
    ResourceLoader loader = new DefaultResourceLoader();
  27.  
    Resource resource = loader.getResource("redis_two.yml");
  28.  
    Config config = Config.fromYAML(resource.getInputStream());
  29.  
    config.useClusterServers();
  30.  
    return Redisson.create(config);
  31.  
    }
  32.  
     
  33.  
    }
学新通

使用RedissonClient进行操作

  1.  
    import lombok.extern.slf4j.Slf4j;
  2.  
    import org.redisson.api.RBatch;
  3.  
    import org.redisson.api.RMapAsync;
  4.  
    import org.redisson.api.RedissonClient;
  5.  
    import org.springframework.stereotype.Component;
  6.  
     
  7.  
    import javax.annotation.PostConstruct;
  8.  
    import javax.annotation.Resource;
  9.  
     
  10.  
    @Component
  11.  
    @Slf4j
  12.  
    public class RedissonTask {
  13.  
     
  14.  
    @Resource
  15.  
    private RedissonClient redissonClientOne;
  16.  
     
  17.  
    @Resource
  18.  
    private RedissonClient redissonClientTwo;
  19.  
     
  20.  
    @PostConstruct
  21.  
    public void test() {
  22.  
    RBatch oneBatch = redissonClientOne.createBatch();
  23.  
    RBatch twoBatch = redissonClientTwo.createBatch();
  24.  
    RMapAsync<Object, Object> oneMap = oneBatch.getMap("onetest");
  25.  
    oneMap.putAsync("a", "1");
  26.  
    oneMap.putAsync("b", "2");
  27.  
     
  28.  
    RMapAsync<Object, Object> twoMap = twoBatch.getMap("twotest");
  29.  
    twoMap.putAsync("c", "3");
  30.  
    twoMap.putAsync("d", "4");
  31.  
    oneBatch.execute();
  32.  
    twoBatch.execute();
  33.  
    }
  34.  
    }
学新通

 

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgjfefa
系列文章
更多 icon
同类精品
更多 icon
继续加载