在Eureka中, 使用docker 部署高可用的时候,就会出现 容器之间 循环依赖
解决方案很多, 这里使用 网络桥接设置
compose的方式如下
version: "2"
services:
peer1: # 默认情况下,其他服务可以使用服务名称连接到该服务。因此,对于peer2的节点,它需要连接http://peer1:8761/eureka/,因此需要配置该服务的名称是peer1。
image: itmuch/microservice-discovery-eureka-ha:0.0.1-SNAPSHOT
networks:
- eureka-net
ports:
- "8761:8761"
environment:
- spring.profiles.active=peer1
peer2:
image: itmuch/microservice-discovery-eureka-ha:0.0.1-SNAPSHOT
hostname: peer2
networks:
- eureka-net
ports:
- "8762:8762"
environment:
- spring.profiles.active=peer2
networks:
eureka-net:
driver: bridge
如果不想使用compose 可用这样
1 、创建一个桥接网络
$ docker network create -d bridge --subnet 172.25.0.0/16 isolated_nw
06a62f1c73c4e3107c0f555b7a5f163309827bfbbf999840166065a8f35455a8
2、把容器加入网络,重启下容器
$ docker network connect isolated_nw container2
$ docker network inspect isolated_nw
[
{
"Name": "isolated_nw",
"Id": "06a62f1c73c4e3107c0f555b7a5f163309827bfbbf999840166065a8f35455a8",
"Scope": "local",
"Driver": "bridge",
"IPAM": {
"Driver": "default",
"Config": [
{
"Subnet": "172.25.0.0/16",
"Gateway": "172.25.0.1/16"
}
]
},
"Containers": {
"90e1f3ec71caf82ae776a827e0712a68a110a3f175954e5bd4222fd142ac9428": {
"Name": "container2",
"EndpointID": "11cedac1810e864d6b1589d92da12af66203879ab89f4ccd8c8fdaa9b1c48b1d",
"MacAddress": "02:42:ac:19:00:02",
"IPv4Address": "172.25.0.2/16",
"IPv6Address": ""
}
},
"Options": {}
}]
3、或者run的时候设置网络
$ docker run --network=isolated_nw --ip=172.25.3.3 -itd --name=container3 busybox
467a7863c3f0277ef8e661b38427737f28099b61fa55622d6c30fb288d88c551