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

docker spring cloud 无法访问 consul 求助

  •  
  •   ccppgo · 2022-09-25 22:53:00 +08:00 · 1130 次点击
    这是一个创建于 776 天前的主题,其中的信息可能已经有所发展或是发生改变。

    之前公司要么是单体架构, 要么是入职的时候已经配置好了微服务, 上家是 nacos+ dubbo, 楼主在想学习一下 docker-comopose 部署 spring cloud , 后期转入 k8s.

    楼主在 wsl2 运行 consul+ spring-cloud 的一个 provider 模块, docker-compose 里的 consul 跑得很成功, windows 的 provider 也跑得很成功, 但 docker-compose 里的 provider 死活都无法访问 consul, 下面贴出 dockerfile 和 docker-compose.yml

    version: '3'
    
    services:
    consul-node1:
    image: bitnami/consul
    environment:
    - CONSUL_BOOTSTRAP_EXPECT=3
    - CONSUL_CLIENT_LAN_ADDRESS=0.0.0.0
    - CONSUL_DISABLE_KEYRING_FILE=true
    - CONSUL_RETRY_JOIN_ADDRESS=consul-node1
      ports:
    - '8300:8300'
    - '8301:8301'
    - '8301:8301/udp'
    - '8500:8500'
    - '8600:8600'
    - '8600:8600/udp'
      volumes:
    - 'consul-node1_data:/bitnami'
    
    consul-node2:
    image: bitnami/consul
    environment:
    - CONSUL_BOOTSTRAP_EXPECT=3
    - CONSUL_CLIENT_LAN_ADDRESS=0.0.0.0
    - CONSUL_DISABLE_KEYRING_FILE=true
    - CONSUL_RETRY_JOIN_ADDRESS=consul-node1
    - CONSUL_ENABLE_UI=false
      volumes:
    - 'consul-node2_data:/bitnami'
    
    consul-node3:
    image: bitnami/consul
    environment:
    - CONSUL_BOOTSTRAP_EXPECT=3
    - CONSUL_CLIENT_LAN_ADDRESS=0.0.0.0
    - CONSUL_DISABLE_KEYRING_FILE=true
    - CONSUL_RETRY_JOIN_ADDRESS=consul-node1
    - CONSUL_ENABLE_UI=false
      volumes:
    - 'consul-node3_data:/bitnami'
    
    provider-impl:
    container_name: provider-impl
    build:
    context: ./jar/provider-impl
    dockerfile: Dockerfile
    ports:
    
    - "8504:8504"
      command: "--spring.config.location=/app/config/application.properties"
    
    volumes:
    consul-node1_data:
    driver: local
    consul-node2_data:
    driver: local
    consul-node3_data:
    driver: local
    
    

    provider 的配置文件

    provider.name=p1
    
    server.port=8504
    spring.application.name=consul-provider
    spring.cloud.consul.i=127.0.0.1
    spring.cloud.consul.port=8500
    spring.cloud.consul.discovery.prefer-ip-address=true
    spring.cloud.consul.discovery.heartbeat.enabled=true
    spring.cloud.consul.discovery.health-check-path=health
    spring.cloud.consul.discovery.service-name=service-provider
    
    spring.datasource.driver-class-name=com.p6spy.engine.spy.P6SpyDriver
    spring.datasource.url=jdbc:p6spy:mysql://127.0.0.1:3306/blog?allowPublicKeyRetrieval=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    spring.datasource.name=mysql
    spring.datasource.username=root
    spring.datasource.password=root
    mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.slf4j.Slf4jImpl
    mybatis-plus.global-config.db-config.logic-delete-field=deletedAt
    mybatis-plus.global-config.db-config.logic-not-delete-value=null
    mybatis-plus.global-config.db-config.logic-delete-value=UNIX_TIMESTAMP()
    
    FROM adoptopenjdk:11-jdk-hotspot-focal
    
    VOLUME /tmp
    
    ADD c-consul-provider-impl*.jar /app/provider-impl.jar
    ADD config /app/config
    
    ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app/provider-impl.jar"]
    
    

    docker-compose 目录 xEqrz8.png

    也试过在 docker-compose.yml 添加网络, 但也无法生效,报错信息如下: com.ecwid.consul.transport.TransportException: org.apache.http.conn.HttpHostConnectException: Connect to 127.0.0.1:8500 [/127.0.0.1] failed: Connection refused (Connection refused)

    xEqLw9.md.png

    搞了一晚上三个多小时都没解决...要说连不上 127.0.0.1:8500, 不知道为什么能连上 mysql, mysql 是跑在 wsl 上没有使用 docker, 求大佬帮助

    3 条回复    2022-09-28 10:32:33 +08:00
    ccppgo
        1
    ccppgo  
    OP
       2022-09-25 22:54:47 +08:00
    mmdsun
        2
    mmdsun  
       2022-09-27 08:04:15 +08:00 via iPhone
    127.0.0.1 都换成你电脑内网 IP 地址试试看。
    ccppgo
        3
    ccppgo  
    OP
       2022-09-28 10:32:33 +08:00
    @mmdsun 后来发现问题了.. docker 内部网络是连容器名, 当时估计抽风了记混了,记成网络名, 以前也是知道的...
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1644 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 16:50 · PVG 00:50 · LAX 08:50 · JFK 11:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.