V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
bruce2000
V2EX  ›  Java

springboot 获取配置服务器中的 Redis 集群地址出问题

  •  
  •   bruce2000 · 2019-05-06 10:14:34 +08:00 · 6905 次点击
    这是一个创建于 1788 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我在局域网一台服务器 192.168.6.33 启动了 configServer,端口 7091,同时 6 个 docker 容器的 Redis 集群也在这台服务器上,容器网络地址是 172.16.0.31-172.16.0.36 ,Redis 端口都是 7001,映射到外面分别是 7001-7006。 为了方便调试,我在自己电脑上启动微服务,微服务名称是 web, 在 configserver 的配置文件夹中 web 服务的配置文件是 web-local.properties,其中的 Redis 集群配置是:

    spring.redis.cluster.nodes=192.168.6.33:7001,192.168.6.33:7002,192.168.6.33:7003,192.168.6.33:7004,192.168.6.33:7005,192.168.6.33:7006
    

    启动微服务获取配置文件信息的日志如下:

    2019-05-06 09:54:17.055  INFO 4076 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://192.168.6.33:7091/
    2019-05-06 09:54:17.315  INFO 4076 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=web, profiles=[local], label=null, version=null, state=null
    2019-05-06 09:54:17.315  INFO 4076 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource [name='configService', propertySources=[MapPropertySource [name='classpath:properties/web-local.properties']]]
    

    从启动日志来看,获取配置文件的路径信息是没有问题的,通过浏览器访问 http://192.168.6.33:7091/web-local.properties 获取的 redis 集群信息都是:

    192.168.6.33:7001,192.168.6.33:7002,192.168.6.33:7003,192.168.6.33:7004,192.168.6.33:7005,192.168.6.33:7006
    

    但是 web 服务后面的日志 redis 集群就报错,解析出来的地址是 docker 容器的内部地址:

    Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer' defined in class path resource [org/springframework/boot/autoconfigure/session/RedisSessionConfiguration$SpringBootRedisHttpSessionConfiguration.class]: Invocation of init method failed; nested exception is org.springframework.data.redis.ClusterStateFailureException: Could not retrieve cluster information. CLUSTER NODES returned with error.
    	- 172.16.0.32:7001 failed: Could not get a resource from the pool
    	- 172.16.0.34:7001 failed: Could not get a resource from the pool
    	- 172.16.0.33:7001 failed: Could not get a resource from the pool
    	- 172.16.0.36:7001 failed: Could not get a resource from the pool
    	- 172.16.0.35:7001 failed: Could not get a resource from the pool
    	- 172.16.0.31:7001 failed: Could not get a resource from the pool
    

    哪位大神帮忙看下是什么问题,小弟万分感谢

    7 条回复    2020-03-30 16:31:04 +08:00
    airfling
        1
    airfling  
       2019-05-06 10:29:18 +08:00
    RedisSessionConfiguration 这个初始化使用的配置是 SessionProperties,这个 SessionProperties 类的初始化应该是比你从 configserver 中获取配置来的早的,而且这个 SessionProperties 也没有加 @RefreshScope 注解,所以我建议你不要使用 RedisSessionConfiguration 的自动配置,你自己定制化一个配置比较好,自动化配置其实都是默认配置,一般使用没问题,功能复杂的话 还是自己修改下比较好
    airfling
        2
    airfling  
       2019-05-06 10:37:44 +08:00
    @airfling 刚开始没审清题,刚刚回答的只是想让你检查下配置有没有成功刷进来。后续这个解析的 ip,你应该检查下 redis 集群的配置是不是配置了 docker 内部集群 ip 的地址,没怎么用过 redis,思路只能提供这两个方面
    pmispig
        3
    pmispig  
       2019-05-06 10:49:23 +08:00
    这个不是你 java 的问题,而是 redis 这个的服务发现机制 /集群通信机制 特别扯淡。
    可以说如果你不是资深的运维,不推荐 redis 跑在 docker 里面
    了解下 cluster-announce-ip slave-announce-ip
    bruce2000
        4
    bruce2000  
    OP
       2019-05-06 16:00:50 +08:00
    @airfling 我想也是用了容器内部地址,但是我用外部地址创建一直在 Waiting for the cluster to join。。。
    bruce2000
        5
    bruce2000  
    OP
       2019-05-06 20:07:13 +08:00
    @pmispig 加个 cluster-announce-ip 可以了,艰难。。。
    eefnrowe
        6
    eefnrowe  
       2019-05-23 17:19:28 +08:00
    外部采用宿主机的 ip 加多端口
    g9g9
        7
    g9g9  
       2020-03-30 16:31:04 +08:00
    容器默认的网络模式是 bridge 模式:容器访问自己容器内部的 hosts 文件
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1049 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 22:34 · PVG 06:34 · LAX 15:34 · JFK 18:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.