使用 localhost 对应用程序进行 Dockerizing 会导致连接被拒绝

Dockerizing application with localhost leads to connection refused

因此,当我在本地启动应用程序时,它可以正常工作。当我使用 docker-compose up 它启动时,一切顺利,然后突然连接被拒绝。我用了Spring Cloud Gateway,没用。尝试添加 Eureka,也没有用。问题可能是因为我使用本地主机,但我不知道还有什么可以放在那里。

我尝试过的东西而不是本地主机:

我想让它在 docker 化后做的基本上是,从端口 8080 调用 api 到在端口 8081 上运行的用户服务。这适用于本地启动,但不能当 docker 化时。

第一个错误日志:

user-ms           | 2022-04-04 08:22:52.331  INFO 1 --- [           main] c.n.d.s.t.d.RedirectingEurekaHttpClient  : Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http
://localhost:8087/eureka/}, exception=I/O error on GET request for "http://localhost:8087/eureka/apps/": Connect to localhost:8087 [localhost/127.0.0.1] failed: Connection refused (Con
nection refused); nested exception is org.apache.http.conn.HttpHostConnectException: Connect to localhost:8087 [localhost/127.0.0.1] failed: Connection refused (Connection refused) sta
cktrace=org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8087/eureka/apps/": Connect to localhost:8087 [localhost/127.0.0.1] faile
d: Connection refused (Connection refused); nested exception is org.apache.http.conn.HttpHostConnectException: Connect to localhost:8087 [localhost/127.0.0.1] failed: Connection refuse
d (Connection refused)

API-Gateway.yml:

server:
  port: 8080

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8087/eureka

spring:
  application:
    name: api-gateway
  main:
    web-application-type: reactive
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
        - id: user-ms
          uri: lb://USER-MS
          predicates:
            - Path=/user/**

Eureka.yml:

spring:
    application:
        name: eureka-server

server:
    port: 8087

eureka:
    client:
        registerWithEureka: false
        fetchRegistry: false
        serviceUrl:
            defaultZone: http://localhost:8087/eureka

User.yml:

spring:
  application:
    name: user-ms

server:
  port: 8081

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8087/eureka

Docker-compose.yml:

version: '3.8'

services:
  eureka-service:
    build: ./eureka-discovery-service
    restart: always
    container_name: eureka-service
    ports:
      - 8087:8087    

  user:
    build: ./user
    restart: unless-stopped
    container_name: user-ms
    ports:
    - 8081:8081
    depends_on:
      - eureka-service    

  api-gateway:
    build: ./api-gateway
    restart: always
    container_name: api-gateway
    depends_on:
      - eureka-service
    ports:
      - 8080:8080

  mongodb:
    image: mongo
    restart: always
    container_name: mongodb
    ports:
      - 27017:27017

我哪里错了?感觉 docker 内部需要一个不同的 url 而不是本地主机,但我不知道它可能是什么。

好的,基本上我发现了什么。使用 eureka-service 确实有效,但我必须 运行 命令。我不得不 运行 mvn clean package 刷新所有 jar-files 等,现在它可以工作了。我已经苦苦挣扎了 3 个星期,现在它终于奏效了。