【问题标题】:Eureka Client with Docker Compose使用 Docker Compose 的 Eureka 客户端
【发布时间】:2018-12-26 07:22:57
【问题描述】:

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

下面是dockerfile的内容

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 run -e 选项和 Docker Compose environment: 部分相同,但您在两个路径上拼写环境变量的方式不同。如果双向传递相同的环境变量,它会起作用吗?
  • @DavidMaze 我在这两个地方都尝试了两种方式,但不适用于其他方式。我在某处读到撰写文件不接受“。”在传递的环境变量中,变量名之间应该有“_”。 SPRING_PROFILES_ACTIVE: staging,XYZServerName 在 docker compose 中有效,但是 spring.profiles.active=staging,XYZServerName 在 compose 文件中无效。

标签: docker spring-boot docker-compose dockerfile


【解决方案1】:

您最好将 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 将显示您是否一切正常。

【讨论】:

  • 用连字符添加环境变量也不起作用,它无法选择信息。环境: - 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 = username123:password123@10.XXX.21.161:31120/eureka/,http://… - SPRING_Z
  • 你能发布完整的 docker-compose.yml 文件、完整的 .env 文件和 docker-compose config 的输出吗?然后我们可以通过测试看看是怎么回事。
猜你喜欢
  • 2016-03-04
  • 1970-01-01
  • 2020-12-25
  • 1970-01-01
  • 2019-08-06
  • 2020-01-01
  • 2020-10-01
  • 2017-11-25
  • 2016-06-03
相关资源
最近更新 更多