docker - Eureka 客户端与 Docker Compose

标签 docker spring-boot docker-compose dockerfile

我正在尝试使用 docker 将我的一项微服务容器化。

下面是dokerfile内容

FROM openjdk:8-jdk-alpine   
LABEL maintainer="shardajaiswal@gmail.com"

RUN mkdir -p /test/service/master/input/
RUN mkdir -p /test/service/master/output/
VOLUME /test/service/master/input/
VOLUME /test/service/master/output/

EXPOSE 8080
ARG JAR_FILE=target/master-1.1-SNAPSHOT.jar
ADD ${JAR_FILE} master-1.1-SNAPSHOT.jar
ENTRYPOINT ["java", "-jar","master-1.1-SNAPSHOT.jar"]

我已经成功地使用maven插件创建了图像。

现在,当我尝试在 docker CLI 上使用以下命令运行图像时,服务已启动并运行良好

docker run -p 7070:8080 -e spring.cloud.config.username=username123
-e \spring.cloud.config.password=password123
-e \spring.cloud.config.failFast=true
-e \spring.cloud.config.discovery.enabled=true
-e \eureka.client.serviceUrl.defaultZone=http://username123:password123@10.XXX.21.161:31120/eureka/,http://username123:password123@10.XXX.147.171:31120/eureka/ 
-e \spring.profiles.active=staging,XYZServerName shardaspj:master-1.1-SNAPSHOT

通过命令行中的上述命令,它能够连接到配置服务器并能够向 Eureka 注册。

现在我想使用 docker-compose.yml 文件运行该服务,因为我想将某些其他参数也添加到配置中。

我的 docker-compose.yml 文件如下所示:-

version: '3.0'
services:
 master:
  container_name: master
  image: shardaspj:master-1.1-SNAPSHOT
  environment:
   SPRING_CLOUD_CONFIG_USERNAME: username123
   SPRING_CLOUD_CONFIG_PASSWORD: password123
   SPRING_CLOUD_CONFIG_FAILFAST: 'true'
   SPRING_CLOUD_CONFIG_DISCOVERY_ENABLED: 'true'
   EUREKA_CLIENT_SERVICE-URL_DEFAULTZONE: http://username123:password123@10.XXX.21.161:31120/eureka/,http://username123:password123@10.XXX.147.171:31120/eureka/
   SPRING_PROFILES_ACTIVE: staging,XYZServerName
 ports:
   - "7070:8080"

但是在命令行上运行命令 docker-compose up 时,我收到以下错误并且无法连接以在 eureka 上注册,也无法连接到配置服务器以获取配置文件。

master    | 2018-07-18 11:18:51.867  WARN [master,,,] 1 --- [           main] c.n.d.s.t.d.RetryableEurekaHttpClient    : Request execution failed with message: java.net.ConnectException: Connectio
n refused (Connection refused)
master    | 2018-07-18 11:18:51.873 ERROR [master,,,] 1 --- [           main] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_MASTER/cd656dee7969:master - was unable to refresh its cach
e! status = Cannot execute request on any known server
master    |
master    | com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
master    |     at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:111)
master    |     at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134)
master    |     at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$6.execute(EurekaHttpClientDecorator.java:137)
master    |     at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77)
master    |     at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134)
master    |     at com.netflix.discovery.DiscoveryClient.getAndStoreFullRegistry(DiscoveryClient.java:1022)
master    |     at com.netflix.discovery.DiscoveryClient.fetchRegistry(DiscoveryClient.java:936)
master    |     at com.netflix.discovery.DiscoveryClient.<init>(DiscoveryClient.java:412)
master    |     at com.netflix.discovery.DiscoveryClient.<init>(DiscoveryClient.java:267)
master    |     at org.springframework.cloud.netflix.eureka.CloudEurekaClient.<init>(CloudEurekaClient.java:61)
master    |     at org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration$EurekaClientConfiguration.eurekaClient(EurekaClientAutoConfiguration.java:234)
master    |     at org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration$EurekaClientConfiguration$$EnhancerBySpringCGLIB$$f480408d.CGLIB$eurekaClient$0(<generated>)
master    |     at org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration$EurekaClientConfiguration$$EnhancerBySpringCGLIB$$f480408d$$FastClassBySpringCGLIB$$cdb46f4f.invoke(<gener
ated>)

还记下了出现错误的日志:-

2018-07-18 11:18:51.876  WARN [master,,,] 1 --- [           main] com.netflix.discovery.DiscoveryClient    : Using default backup registry implementation which does not do anything.
2018-07-18 11:18:51.886  INFO [master,,,] 1 --- [           main] com.netflix.discovery.DiscoveryClient    : Not registering with Eureka server per configuration
2018-07-18 11:18:51.951  INFO [master,,,] 1 --- [           main] com.netflix.discovery.DiscoveryClient    : Discovery Client initialized at timestamp 1531912731950 with initial instances count : 0

2018-07-18 11:18:53.381  WARN [master,,,] 1 --- [           main] lientConfigServiceBootstrapConfiguration : Could not locate configserver via discovery

java.lang.IllegalStateException: No instances found of configserver (registry)
    at org.springframework.cloud.config.client.ConfigServerInstanceProvider.getConfigServerInstance(ConfigServerInstanceProvider.java:25)
    at org.springframework.cloud.config.client.DiscoveryClientConfigServiceBootstrapConfiguration.refresh(DiscoveryClientConfigServiceBootstrapConfiguration.java:80)
    at org.springframework.cloud.config.client.DiscoveryClientConfigServiceBootstrapConfiguration.startup(DiscoveryClientConfigServiceBootstrapConfiguration.java:66)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.context.event.ApplicationListenerMethodAdapter.doInvoke(ApplicationListenerMethodAdapter.java:256)
    at org.springframework.context.event.ApplicationListenerMethodAdapter.processEvent(ApplicationListenerMethodAdapter.java:177)
    at org.springframework.context.event.ApplicationListenerMethodAdapter.onApplicationEvent(ApplicationListenerMethodAdapter.java:140)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)

请帮助我正确设置 docker-compose.yml。

最佳答案

您最好将 docker-compose 文件中环境变量的所有值清空,然后将它们放入与 docker-compose 文件处于同一级别的 .env 文件中。这个文件的格式是key=value,一行一个。这样您就可以在不同的环境中重复使用您的撰写文件。

然后执行 docker-compose 配置以查看环境变量如何插入到您的 docker-compose 设置中。

我认为您遇到的问题是缺少连字符。而不是这个:

environment:
   SPRING_CLOUD_CONFIG_USERNAME
   SPRING_CLOUD_CONFIG_PASSWORD
   SPRING_CLOUD_CONFIG_FAILFAST

你需要这样的东西:

environment:
   - SPRING_CLOUD_CONFIG_USERNAME
   - SPRING_CLOUD_CONFIG_PASSWORD
   - SPRING_CLOUD_CONFIG_FAILFAST

在将值移动到 .env 后运行 docker-compose config 将显示是否一切正常。

关于docker - Eureka 客户端与 Docker Compose,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51400896/

相关文章:

python - 无法在 GitLab CI 上的 docker 镜像中导入 django

docker - 在 HTTPS 上运行 docker 服务

laravel - Minio 与 laravel,存储桶名称作为本地服务器上的子域前缀

docker - 在 docker-compose run 命令中使用 docker 环境变量

mysql - 去+MySql : how easy is to migrate to GKE (Google Cloud Container Engine)?

ruby-on-rails - QSslSocket : cannot resolve SSLv3_client_method RAILS

python docker SDK;获取有关悬挂 docker 图像的信息

spring-boot-starter-quartz jdbc 示例

java - 将spring boot maven项目作为依赖添加到另一个项目(本地)

java - Spring boot应用程序创建两个数据库行