• Springboot 集成 Redis集群配置公网IP连接报私网IP连接失败问题


    1、问题:在Springboot 集成 Redis集群配置公网IP连接报私网IP连接失败,一直报私有IP连接失败

    14 14:57:49.180  WARN 22012 --- [ioEventLoop-6-4] i.l.c.c.topology.ClusterTopologyRefresh  : Unable to connect to [192.168.0.19:6384]: connection timed out: /192.168.0.19:6384
    2020-08-14 14:57:49.180  WARN 22012 --- [ioEventLoop-6-3] i.l.c.c.topology.ClusterTopologyRefresh  : Unable to connect to [192.168.0.19:6383]: connection timed out: /192.168.0.19:6383
    2020-08-14 14:57:49.182  WARN 22012 --- [ioEventLoop-6-2] i.l.c.c.topology.ClusterTopologyRefresh  : Unable to connect to [192.168.0.19:6382]: connection timed out: /192.168.0.19:6382
    2020-08-14 14:57:49.182  WARN 22012 --- [ioEventLoop-6-1] i.l.c.c.topology.ClusterTopologyRefresh  : Unable to connect to [192.168.0.19:6381]: connection timed out: /192.168.0.19:6381
    2020-08-14 14:57:49.190  WARN 22012 --- [ioEventLoop-6-1] i.l.c.c.topology.ClusterTopologyRefresh  : Unable to connect to [192.168.0.19:6385]: connection timed out: /192.168.0.19:6385
    2020-08-14 14:57:49.191  WARN 22012 --- [ioEventLoop-6-2] i.l.c.c.topology.ClusterTopologyRefresh  : Unable to connect to [192.168.0.19:6386]: connection timed out: /192.168.0.19:6386
    2020-08-14 14:57:59.389  WARN 22012 --- [ioEventLoop-6-3] i.l.core.cluster.RedisClusterClient      : connection timed out: /192.168.0.19:6382
    2020-08-14 14:58:09.391  WARN 22012 --- [ioEventLoop-6-4] i.l.core.cluster.RedisClusterClient      : connection timed out: /192.168.0.19:6381
    2020-08-14 14:58:19.393  WARN 22012 --- [ioEventLoop-6-1] i.l.core.cluster.RedisClusterClient      : connection timed out: /192.168.0.19:6383
    2020-08-14 14:58:29.396  WARN 22012 --- [ioEventLoop-6-2] i.l.core.cluster.RedisClusterClient      : connection timed out: /192.168.0.19:6384
    2020-08-14 14:58:39.399  WARN 22012 --- [ioEventLoop-6-3] i.l.core.cluster.RedisClusterClient      : connection timed out: /192.168.0.19:6386
    2020-08-14 14:58:49.402  WARN 22012 --- [ioEventLoop-6-4] i.l.core.cluster.RedisClusterClient      : connection timed out: /192.168.0.19:6385
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2、配置文件

    创建6个配置文件:redis-6381.conf,redis-6382.conf,redis-6383.conf,redis-6384.conf,redis-6385.conf,
    redis-6386.conf。配置文件内容如下:

    # 配置文件进行了精简,完整配置可自行和官方提供的完整conf文件进行对照。端口号自行对应修改
    
    #默认是 protected-mode yes,即开启保护模式, no=关闭
    #在redis的配置文件中会遇到protected-mode,它直译为保护模式。
    #如果设置为yes,那么只允许我们在本机的回环连接,其他机器无法连接。
    #线上Redis服务,为了安全,我们建议将protected-mode设置为yes。
    #protected-mode设置为yes的情况下,为了我们的应用服务可以正常访问Redis,我们需要设置Redis的bind参数或者密码参数#requirepass。
    protected-mode yes
    #端口号
    port 6381
    # IP绑定,redis不建议对公网开放,这里绑定了服务器私网IP及环回地址
    bind 192.168.0.19 127.0.0.1
    requirepass 123456
    # redis数据文件存放的目录
    dir /redis/workdata
    # 日志文件
    logfile "/redis/logs/cluster-node-6381.log"
    # 开启AOF
    appendonly yes
    #后台启动
    daemonize yes 
     # 开启集群
    cluster-enabled yes
    # 集群持久化配置文件,内容包含其它节点的状态,持久化变量等,会自动生成在上面配置的dir目录下
    cluster-config-file cluster-node-6381.conf
    # 集群节点不可用的最大时间(毫秒),如果主节点在指定时间内不可达,那么会进行故障转移
    cluster-node-timeout 5000
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    3、springboot集成redis集群有以下配置,二选一:

    1:代码配置

    @Configuration
    public class RedisClusterConfig {
    
      @Bean
      public RedisConnectionFactory redisConnectionFactory() {
        // 客户端读写分离配置
        LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
                .readFrom(ReadFrom.REPLICA_PREFERRED)
                .build();
        RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(Arrays.asList(
                "42.192.119.238:6381",
                "42.192.119.238:6382",
                "42.192.119.238:6383",
                "42.192.119.238:6384",
                "42.192.119.238:6385",
                "42.192.119.238:6386"));
        return new LettuceConnectionFactory(redisClusterConfiguration, clientConfig);
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    2:yml 配置

    #集群模式
    spring:
      redis:
        cluster:
          max-redirects: 3
          nodes:
            - 42.192.119.238:6381
            - 42.192.119.238:6382
            - 42.192.119.238:6383
            - 42.192.119.238:6384
            - 42.192.119.238:6385
            - 42.192.119.238:6386
        database: 0
    #    host: 42.192.119.238
    #    port: 6380
        password: 
        timeout: 5000s #连接超时时长
        # 连接池最大连接数(使用负值表示没有限制)
        jedis:
          pool:
            max-active: 8 #连接池最大连接数量,负数表示无限,默认为8
            max-idle: 8 #连接池最大空闲数量,默认8
            min-idle: 0 #连接池最小空闲数量,默认0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    4、解决链接报错问题

    让Redis暴露公网IP其实在redis.conf配置文件里是能找到的,这里我们可以手动指定Redis的公网IP、端口以及总线端口(默认服务端口加10000)。
    手动指定了公网ip后,Redis集群中的节点会通过公网IP进行通信,也就是外网访问。因此相关的总线端口,如下面的16381等总线端口必须在云服务器中的安全组中放开

    # 配置文件进行了精简,完整配置可自行和官方提供的完整conf文件进行对照。端口号自行对应修改
    
    #默认是 protected-mode yes,即开启保护模式, no=关闭
    #在redis的配置文件中会遇到protected-mode,它直译为保护模式。
    #如果设置为yes,那么只允许我们在本机的回环连接,其他机器无法连接。
    #线上Redis服务,为了安全,我们建议将protected-mode设置为yes。
    #protected-mode设置为yes的情况下,为了我们的应用服务可以正常访问Redis,我们需要设置Redis的bind参数或者密码参数#requirepass。
    protected-mode yes
    #端口号
    port 6381
    # IP绑定,redis不建议对公网开放,这里绑定了服务器私网IP及环回地址
    bind 192.168.0.19 127.0.0.1
    requirepass 123456
    # redis数据文件存放的目录
    dir /redis/workdata
    # 日志文件
    logfile "/redis/logs/cluster-node-6381.log"
    # 开启AOF
    appendonly yes
    #后台启动
    daemonize yes 
     # 开启集群
    cluster-enabled yes
    # 集群持久化配置文件,内容包含其它节点的状态,持久化变量等,会自动生成在上面配置的dir目录下
    cluster-config-file cluster-node-6381.conf
    # 集群节点不可用的最大时间(毫秒),如果主节点在指定时间内不可达,那么会进行故障转移
    cluster-node-timeout 5000
    
    # 云服务器上部署需指定公网ip
    cluster-announce-ip 42.192.119.238
    # Redis总线端口,用于与其它节点通信
    cluster-announce-bus-port 16381
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    根据以上配置修改每一个redis节点的配置,注意端口不能相同

    在src 目录下执行命令:

    ./redis-cli -c -p 6381 -h 192.168.0.19 -a 123456 cluster nodes
    
    • 1
    Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    6297ab04ff4bbfcad778b80315619defc2f6e513 42.192.119.238:6382@16382 master - 0 1696924087000 2 connected 5461-10922
    19d3955d4bacd04892c13e7a05e13c7744085896 42.192.119.238:6381@16381 myself,master - 0 1696924087000 1 connected 0-5460
    3a21d6c05255229741593340a781affbdcad6236 42.192.119.238:6385@16385 slave df0858e942b03f5b3c848d1acb2a4fde1f70e290 0 1696924088000 3 connected
    546c8528a07abee29a1e383a3130d4f306447f0e 42.192.119.238:6386@16386 slave 19d3955d4bacd04892c13e7a05e13c7744085896 0 1696924086000 1 connected
    56b730c5631515b2359bbf9b6d4306460da8502c 42.192.119.238:6384@16384 slave 6297ab04ff4bbfcad778b80315619defc2f6e513 0 1696924088460 2 connected
    df0858e942b03f5b3c848d1acb2a4fde1f70e290 42.192.119.238:6383@16383 master - 0 1696924089469 3 connected 10923-16383
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    可以发现,各节点暴露的IP全是公网IP了。

    5、故障转移期间Lettuce客户端连接问题

    解决方式:
    1、yml指定使用jedis

    2、代码配置
    1)、更换Redis客户端

    @Configuration
    public class RedisClusterConfig {
    
      @Bean
      public RedisConnectionFactory redisConnectionFactory() {
        RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(Arrays.asList(
               "42.192.119.238:6381",
                "42.192.119.238:6382",
                "42.192.119.238:6383",
                "42.192.119.238:6384",
                "42.192.119.238:6385",
                "42.192.119.238:6386"));
        return new JedisConnectionFactory(redisClusterConfiguration);
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    参考链接:

    https://github.com/lettuce-io/lettuce-core/wiki/Redis-Cluster
    https://github.com/lettuce-io/lettuce-core/wiki/Client-options#cluster-specific-options
    
    • 1
    • 2

    2)、更改实现配置

    @Configuration
    public class RedisClusterConfig {
    
      @Bean
      public RedisConnectionFactory redisConnectionFactory() {
        // 开启自适应集群拓扑刷新和周期拓扑刷新,不开启相应槽位主节点挂掉会出现服务不可用,直到挂掉节点重新恢复
        ClusterTopologyRefreshOptions clusterTopologyRefreshOptions =  ClusterTopologyRefreshOptions.builder()
                .enableAllAdaptiveRefreshTriggers() // 开启自适应刷新,自适应刷新不开启,Redis集群变更时将会导致连接异常
                .adaptiveRefreshTriggersTimeout(Duration.ofSeconds(30)) //自适应刷新超时时间(默认30秒),默认关闭开启后时间为30秒
                .enablePeriodicRefresh(Duration.ofSeconds(20))  // 默认关闭开启后时间为60秒 
                .build();
        ClientOptions clientOptions = ClusterClientOptions.builder()
                .topologyRefreshOptions(clusterTopologyRefreshOptions)
                .build();
        // 客户端读写分离配置
        LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
                .clientOptions(clientOptions)
                .build();
        RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(Arrays.asList(
               "42.192.119.238:6381",
                "42.192.119.238:6382",
                "42.192.119.238:6383",
                "42.192.119.238:6384",
                "42.192.119.238:6385",
                "42.192.119.238:6386"));
        return new LettuceConnectionFactory(redisClusterConfiguration, clientConfig);
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    参考文档:https://developer.aliyun.com/article/1167633

  • 相关阅读:
    服务器远程管理-Windows远程桌面协议实操
    基础-MVP标定-棋盘格标定算子
    GJB 5000B简介
    基于OpenCV实现对图片及视频中感兴趣区域颜色识别
    java毕业设计盘山县智慧项目管理系统源码+lw文档+mybatis+系统+mysql数据库+调试
    智慧公路筑基者!天翼云打造全栈能力新底座
    HuggingFace——Accelerate的使用
    类和对象12:容器方法
    要体验 AI 编程助手吗?
    页面滚动组件
  • 原文地址:https://blog.csdn.net/qq_26898033/article/details/133747433