微服务无法在同一网络中使用 docker 获取 Spring 配置服务器数据?

Microservices can't fetch Spring Config Server data using docker in the same network?

我有微服务应用程序,我正在使用 Spring 云和 Spring boot v2.3。我依赖于配置服务器和尤里卡,在 IDE 的本地机器上一切正常,但是当我在 docker 上部署堆栈时,我的应用程序 Auth-Service 无法从 spring 配置服务器容器中获取,并给我以下堆栈跟踪

2020-07-05 02:23:52.888  INFO [auth-service,,,] 1 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
2020-07-05 02:23:53.091  INFO [auth-service,,,] 1 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Connect Timeout Exception on Url - http://localhost:8888. Will be trying the next url if available
2020-07-05 02:23:53.092  WARN [auth-service,,,] 1 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: I/O error on GET request for "http://localhost:8888/auth-service/default": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)

我认为问题只是 waiting until or container-network as mentioned here and this ,但我确保容器在同一个网络中,为了模拟,我先启动了配置,但得到了同样的错误.

这是我正在使用的配置

version: '3.8'
services:
    eureka-service:
      build: 
        context: ./eureka
      image: eureka-service:latest
      container_name: eureka
      ports:
        - 8761:8761
      hostname: eureka
      networks: 
        - mynetwork
    config-service:
      build: 
        context: ./configServer
      image: config-server:latest
      ports:
        - 8888:8888 
      networks: 
        - mynetwork
    proxy-service:
      build: 
        context: ./web-cv-proxy
      image: zuul-service:latest
      ports:
        - 8081:8081
      depends_on: 
        - config-service
      networks: 
        - mynetwork
    auth-service:
      build: 
        context: ./web-based-cv/auth-service
      image: auth-service:latest
      ports:
        - 8060:8060
      depends_on: 
        - config-service
        - eureka-service
      restart: on-failure
      networks: 
        - mynetwork
    portal-service:
      build: 
        context: ./web-based-cv/cv-portal
      image: cv-portal:latest
      ports:
        - 9090:9090
      depends_on: 
        - config-service
        - eureka-service
      restart: on-failure
      networks: 
        - mynetwork
networks: 
  mynetwork:
    driver: bridge     

配置服务器docker文件

FROM java:openjdk-8-alpine
LABEL version="1.0"
LABEL description="configuration server"
COPY ./target/configServer-0.0.1-SNAPSHOT.jar ./
EXPOSE 8888
CMD ["java", "-jar", "configServer-0.0.1-SNAPSHOT.jar"]

申请docker文件

FROM java:openjdk-8-alpine
LABEL version="1.0"
LABEL description="cv authintication service"
COPY ./target/auth-service-1.0.0-exec.jar auth-service-1.0.0-exec.jar
EXPOSE 8060
CMD ["java", "-jar", "/auth-service-1.0.0-exec.jar"]

应用bootstrap文件

spring:
  datasource:
    jpa:
      properties:
        hibernate:
          format_sql: true
          ddl-auto: none
  application:
    name: auth-service
    bus:
      refresh:
        enabled: true
    profiles:
      active: jdbc
  cloud:
    retry:
      initial-interval: 1500
      multiplier: 1.5
      max-attempts: 10000
      max-interval: 1000

server:
  servlet:
    context-path: /api/auth
  port: 8060

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
    register-with-eureka: true
    fetch-registry: true
  instance:
    prefer-ip-address: true

management:
  endpoints:
    web:
      exposure:
        include: ["health","info","refresh", "bus-refresh"]

非常感谢您的建议,并提前致谢。

您需要将 auth-service 配置从 localhost:8888 更改为 http://config-service:8888,因为错误似乎没有获得正确的端点。

docker-compose networking

error on GET request for "http://localhost:8888/auth-service/default": Connection refused (Connection refused);

localhost:8888 表示此容器(auth-service)不是 spring 配置服务器。

出于某种原因,我的容器无法使用直接 uri 配置进行连接,尽管我已将它们添加到同一网络。

I solved the issue with depending on Eureka itself, and connecting to Spring config through eureka discovery with the following config in client apps

spring 
  config:
    discovery:
      enabled: true
      service-id: configServer

我认为这是将控制权交给尤里卡的好方法,但我希望我能知道为什么它不能与 uri 一起使用?顺便说一下,如果我部署在多个集群上,这种方式会给我更多的灵活性。

  • 记住一件事,当您在 docker 上部署 micro-services 时, 本地主机将无法在那里工作,因为您的微服务 运行 在容器中 在不同的虚拟网络中。所以它不会被识别使用 本地主机。
  • 所以在你的 docker 撰写文件中,你必须提供你的名字 您的 Eureka 服务器的服务。

例如-

services:
  discovery:
    image: <Eureka server image name>
    ports:
      - 8761:8761
  ConfigServerService:
    image: <Config server image name>
    environment:
      - JAVA_OPTS=
        -DEUREKA_SERVER=http://discovery:8761/eureka
    depends_on:
      - discovery
    links:
      - discovery   
    ports:
      - 8888:8888  
  Microservice1:
    image: <Microservice1 image name>
    environment:
      - JAVA_OPTS=
        -DEUREKA_SERVER=http://discovery:8761/eureka
    depends_on:
      - discovery
      - ConfigServerService
    links:
      - discovery
      - ConfigServerService
    ports:
      - 8000:8000