简体   繁体   English

使用 docker 时,如何在 Eureka 客户端中保持 Eureka 服务器 url 动态?

[英]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.

相关问题 使用 spring 引导在 Docker 中运行 Eureka Server 和 Eureka Client - Running Eureka Server and Eureka Client in Docker using spring boot docker 中的 Eureka 客户端未与 Eureka 服务器连接 - Eureka client in docker not connecting with Eureka server 如何使用Eureka Server延迟Eureka客户注册? - How to delay Eureka client registration with Eureka Server? 在 Spring 启动 Eureka Client eureka.client.service-url.defaultZone 是如何工作的以及如何添加新的 eureka 服务器? - In Spring Boot Eureka Client How does eureka.client.service-url.defaultZone work and how to add new eureka server? 如何在单独的Docker容器中配置和运行Eureka Server,微服务(Eureka Client),Zuul? - How to configure and run Eureka Server, Microservices (Eureka Client), Zuul inside separate docker containers? 带有 Docker Compose 的 Eureka 客户端 - Eureka Client with Docker Compose 如何在 Spring Boot 的 eureka 服务器上从 Grails 注册 eureka 客户端 - How to register eureka client from Grails on eureka server in Spring boot eureka客户端如何在spring boot 1.5.2中找到eureka服务器? - How does the eureka client find the eureka server in spring boot 1.5.2? 如何使用 Eureka 服务器将配置客户端连接到配置服务器? - How to connect Config Client to Config server using Eureka server? Eureka客户端无法向Eureka服务器注册服务 - Eureka client unable to register service to eureka server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM