gpt4 book ai didi

java - Docker:客户端未连接到 Eureka 服务器

转载 作者:行者123 更新时间:2023-12-02 01:36:28 25 4
gpt4 key购买 nike

我想为我的客户端创建一个容器,为我的服务器创建一个容器并连接它们。

客户:

server.port=8084
spring.application.name=articlemicroservice

#already tried localhost & hostIp
eureka.client.serviceUrl.defaultZone=http://discovery:8761/eureka

eureka.client.fetchRegristry=true
eureka.instance.preferIpAddress=true

spring.h2.console.enabled=true
spring.datasource.platform=h2
spring.datasource.url=jdbc:h2:mem:shop

服务器:

server.port=8761
spring.application.name=eureka-server

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
# eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
eureka.instance.lease-expiration-duration-in-seconds: 2

我使用 docker-maven-plugin 创建了两个容器。 dockerfile 如下所示:

FROM java
ADD /articlemicroservice.jar //
ENTRYPOINT ["java", "-jar", "/articlemicroservice.jar"]

要启动我使用的容器

docker run -it -p 8761:8761 eurekaserver
docker run -it -p 8084:8084 articlemicroservice

当我启动客户端时,我只是得到:“com.netflix.discovery.shared.transport.TransportException:无法在任何已知服务器上执行请求”和

 "2019-03-15 17:54:51.439  WARN 1 --- [nfoReplicator-0] c.n.discovery.InstanceInfoReplicator     : There was a problem with the instance info replicator"

我是否忘记了属性中的某些内容?

编辑:这是 docker-maven-plugin 的配置:

    <plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.2.0</version>
<configuration>
<imageName>articlemicroservice</imageName>
<baseImage>java</baseImage>
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
<!-- copy the service's jar file from target into the root directory
of the image -->
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>

最佳答案

如上所述,单独运行容器将使用容器名称连接到 docker 桥默认网络。

由于您没有提及容器名称,并且它不会按属性文件中提到的名称解析服务

eureka.client.serviceUrl.defaultZone=http://discovery:8761/eureka

要使其通过直接运行容器来工作,请使用 eureka-server 的容器 ip

# start eureka server container
$ docker run --name eureka-server -p 8081:8081 -d -rm eureka-server

# start eureka client container
$ docker run --name eureka-client -p 8082:8082 -d --rm -e EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://172.17.0.2:8081/eureka eureka-client

检查默认桥接网络以查找附加的容器及其 ​​IP

$ docker network inspect bridge


"Containers": {
"4e4ce0e24091bda8e78805faad7ee31ba12fe5cd266c9d8a51dacf5b88813a67": {
"Name": "eureka-client",
"EndpointID": "edb2d9b333e6b974cfcf939e1585847c82c7ddebde5c44065f1e101ff12cb2bd",
"MacAddress": "02:42:ac:11:00:03",
"IPv4Address": "172.17.0.3/16",
"IPv6Address": ""
},
"c4534a928c71430414b95324a3626aa5c0bf6535a5ffce1fe38a060cfc07d0df": {
"Name": "eureka-server",
"EndpointID": "af82ea5ca05283bac6634ee877e8e3c40c7cdbe7d3c2126bfb53c9db1747d0cc",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
}
},

完美的解决方案是使用 docker compose。

docker-compose.yml

version: '3.7'
services:
eureka-server:
image: eureka-server
ports:
- "8080:8080"
eureka-client:
build: eureka-client
ports:
- "8082:8082"
environment:
EUREKA_CLIENT_SERVICEURL_DEFAULTZONE: http://eureka-server:8080/eureka


在这里阅读更多内容:

Link containers in default docker

关于java - Docker:客户端未连接到 Eureka 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55188315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com