Docker:客户端未连接到Eureka Server

时间:2019-03-15 17:56:43

标签: java spring maven docker netflix-eureka

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

客户:

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

启动Cient时,我只会得到:“ 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>

1 个答案:

答案 0 :(得分:1)

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

因为您没有提到容器名称,并且它不能通过属性文件中提到的名称来解析服务

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

检查默认网桥网络以查找连接的容器及其ips

$ 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