[英]How do I keep the Eureka server url dynamic in the Eureka Client while using docker?
I am using the fabric8 docker-maven-plugin to build image for my Spring boot microservices.我正在使用 fabric8 docker-maven-plugin 为我的 Spring 启动微服务构建映像。
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
The problem is that while running the application in docker containers I have to specify the Eureka Server Container name to Eureka Client.问题是在 docker 容器中运行应用程序时,我必须为 Eureka Client 指定 Eureka Server Container 名称。 But if I run it directly as a "Spring Boot APP" I have to use "Localhost:8761/Eureka".但是如果我直接将它作为“Spring Boot APP”运行,我必须使用“Localhost:8761/Eureka”。 Is there a way to make it work both with/without docker something like given below?有没有办法让它在有/没有 docker 的情况下都可以工作,如下所示?
eureka:
client:
service-url:
defaultZone: ${EUREKA_SERVER:http://localhost:8761/eureka}
I am not able to pass the value of "EUREKA_SERVER" from the fabrib8 plugin.我无法从 fabrib8 插件传递“EUREKA_SERVER”的值。 I have tried the below code to pass the value but it does not work.我试过下面的代码来传递值,但它不起作用。
<docker.env.JAVA_OPTS>-DEUREKA_SERVER=http://discovery:8761/eureka</docker.env.JAVA_OPTS>
Spring can pickup Environment Variables. Spring 可以拾取环境变量。 So if you add Environment Variables to the Docker Container that Spring Boot is running in, they will work.因此,如果您将环境变量添加到 Spring Boot 正在运行的 Docker 容器中,它们将起作用。 This avoids the need to provide a static URL up front.这避免了预先提供 static URL 的需要。
If you use Docker Compose, it could look like this:如果您使用 Docker Compose,它可能如下所示:
services:
eureka:
image: springcloud/eureka
container_name: eureka
ports:
- "8761:8761"
networks:
- "discovery"
environment:
- EUREKA_INSTANCE_PREFERIPADDRESS=true
spring:
build:
context: .
dockerfile: ./src/main/docker/Dockerfile
depends_on:
- eureka
container_name: spring
ports:
- "8080:8080"
networks:
- "discovery"
environment:
- EUREKA_SERVICE_URL=http://eureka:8761 // This overrides your Spring Property
- EUREKA_INSTANCE_PREFER_IP_ADDRESS=true
- LOGGING_FILE=/tmp/admin.log
Note: Since Environment Variables are not YAML, you need to change the format a bit.注意:由于环境变量不是 YAML,因此您需要稍微更改格式。 https://docs.spring.io/spring-boot/docs/1.5.5.RELEASE/reference/html/boot-features-external-config.html#boot-features-external-config-relaxed-binding https://docs.spring.io/spring-boot/docs/1.5.5.RELEASE/reference/html/boot-features-external-config.html#boot-features-external-config-relaxed-binding
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.