springboot 获取配置服务器中的 Redis 集群地址出问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
bruce2000
V2EX    Java

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

  •  
  •   bruce2000 May 6, 2019 7978 views
    This topic created in 2550 days ago, the information mentioned may be changed or developed.

    我在局域网一台服务器 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 replies    2020-03-30 16:31:04 +08:00
    airfling
        1
    airfling  
       May 6, 2019
    RedisSessionConfiguration 这个初始化使用的配置是 SessionProperties,这个 SessionProperties 类的初始化应该是比你从 configserver 中获取配置来的早的,而且这个 SessionProperties 也没有加 @RefreshScope 注解,所以我建议你不要使用 RedisSessionConfiguration 的自动配置,你自己定制化一个配置比较好,自动化配置其实都是默认配置,一般使用没问题,功能复杂的话 还是自己修改下比较好
    airfling
        2
    airfling  
       May 6, 2019
    @airfling 刚开始没审清题,刚刚回答的只是想让你检查下配置有没有成功刷进来。后续这个解析的 ip,你应该检查下 redis 集群的配置是不是配置了 docker 内部集群 ip 的地址,没怎么用过 redis,思路只能提供这两个方面
    pmispig
        3
    pmispig  
       May 6, 2019
    这个不是你 java 的问题,而是 redis 这个的服务发现机制 /集群通信机制 特别扯淡。
    可以说如果你不是资深的运维,不推荐 redis 跑在 docker 里面
    了解下 cluster-announce-ip slave-announce-ip
    bruce2000
        4
    bruce2000  
    OP
       May 6, 2019
    @airfling 我想也是用了容器内部地址,但是我用外部地址创建一直在 Waiting for the cluster to join。。。
    bruce2000
        5
    bruce2000  
    OP
       May 6, 2019
    @pmispig 加个 cluster-announce-ip 可以了,艰难。。。
    eefnrowe
        6
    eefnrowe  
       May 23, 2019
    外部采用宿主机的 ip 加多端口
    g9g9
        7
    g9g9  
       Mar 30, 2020
    容器默认的网络模式是 bridge 模式:容器访问自己容器内部的 hosts 文件
    About     Help     Advertise     Blog     API     FAQ     Solana     1032 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 34ms UTC 22:33 PVG 06:33 LAX 15:33 JFK 18:33
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86