首页 > 解决方案 > 将挂载 Spring 微服务与 Docker 绑定导致 Eureka 未发现其他服务

问题描述

我之前为每个 Spring 微服务构建了一个 Docker 映像,在docker-compose.yml文件中,这是我为启动它们所做的,并通过覆盖defaultZone传递容器的名称让 Eureka 的服务注册了一些其他服务:

version: '3'

services:
  eureka-discovery:
    image: eureka-discovery-service:0.0.1
    ports:
      - 8761:8761

  zuul-gateway:
    image: zuul-gateway-service:0.0.1
    environment:
      - eureka.client.serviceUrl.defaultZone=http://eureka-discovery:8761/eureka
    depends_on:
      - eureka-discovery
    ports:
      - 8765:8765

有效。现在,我做了一些不同的事情,将我的项目绑定到本地开发容器中:

version: '3.1'

services:
  eureka-discovery-service:
    container_name: eureka-discovery-service
    image: openjdk:11.0.9.1-jdk
    ports:
      - 8761:8761
    volumes:
      - ./eureka-discovery-service:/usr/src/app
    working_dir: /usr/src/app
    command: ./mvnw spring-boot:run

  zuul-gateway-service:
    container_name: zuul-gateway-service
    image: openjdk:11.0.9.1-jdk
    environment:
      - eureka.client.serviceUrl.defaultZone=http://eureka-discovery-service:8761/eureka
    ports:
      - 8765:8765
    volumes:
      - ./zuul-gateway-service:/usr/src/app
    working_dir: /usr/src/app
    command: ./mvnw spring-boot:run
    depends_on:
      - eureka-discovery-service
    links:
      - eureka-discovery-service

但是有了这样的事情,看起来zuul-gateway-service不像eureka-discovery-service以前那样。

2020-12-17 23:26:29.997 错误 78 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_ZUUL-GATEWAY-SERVICE/7c580b412063:zuul-gateway-service:8765 - 无法刷新其缓存!status = 无法在任何已知服务器上执行请求

我注意到它使用的是 Eureka 容器的 ID 而不是它的名称。

标签: springdockerdocker-compose

解决方案


command我设法通过更改并覆盖那里的属性而不是 on来使其工作environments

command: ./mvnw spring-boot:run -Dspring-boot.run.arguments=--eureka.client.serviceUrl.defaultZone=http://eureka-discovery-service:8761/eureka

如果有更好的方法,我想知道。


推荐阅读