docker spring cloud 无法访问 consul 求助

2022-09-25 22:53:00 +08:00
 ccppgo

之前公司要么是单体架构, 要么是入职的时候已经配置好了微服务, 上家是 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 目录

也试过在 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)

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

1133 次点击
所在节点    Docker
3 条回复
ccppgo
2022-09-25 22:54:47 +08:00
mmdsun
2022-09-27 08:04:15 +08:00
127.0.0.1 都换成你电脑内网 IP 地址试试看。
ccppgo
2022-09-28 10:32:33 +08:00
@mmdsun 后来发现问题了.. docker 内部网络是连容器名, 当时估计抽风了记混了,记成网络名, 以前也是知道的...

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/882876

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX