Docker 部署 Spring cloud 时 Eureka 客户端向 服务端注册失败


Docker 部署

docker 部署后客户端注册失败

docker 以 dockerfile 形式创建出 eureka 服务端容器后.
使用 docker exec -it 容器id/容器名称 ip addr 获取到当前 eureka 服务端的ip地址
将客户端的 defaultZone: http://127.0.0.1:9001/eureka ip地址修改为上面查出的ip既可

docker-compose 部署后客户端注册失败

eureka 服务端 application.yml
# 配置端口号
server:
  port: 9001
# 配置 运行实例 名称
spring:
  application:
    name: eureka-server
# 配置 eureka 相关
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:9001/eureka
    register-with-eureka: false
    fetch-registry: false
  instance:
    lease-renewal-interval-in-seconds: 30
    lease-expiration-duration-in-seconds: 90
  server:
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 1000
eureka 客户端 application.yml
# 我这里以网关为例
server:
  port: 9003
spring:
  application:
    name: cloud-gateway
  cloud:
    gateway:
      globalcors:
        cors-configurations:
          '[/**]':
            allowed-origin-patterns: "*"
            #            allowed-origins: "*"
            allowed-headers: "*"
            allow-credentials: true
            allowed-methods:
              - GET
              - POST
              - DELETE
              - PUT
              - OPTION
      routes:
        - id: server-list
#          uri: lb://cloud-bookkeeing
#          uri: http://127.0.0.1:9002
          uri: http://192.168.243.132:9002
          predicates:
            - Path=/api/bill/**
          filters:
            - StripPrefix=1
#            - name: CircuitBreaker
#              args:
#                name: backendA
#                fallbackUri: forward:/fallbackA



eureka:
  client:
    service-url:
      defaultZone:  ${EUREKASERVER_URI:http://127.0.0.1:9001/eureka}

resilience4j:
  circuitbreaker:
    configs:
      default:
        failureRateThreshold: 30
        slidingWindowSize: 10
        minimumNumberOfCalls: 5
        slidingWindowType: TIME_BASED
        permittedNumberOfCallsInHalfOpenState: 3
        automaticTransitionFromOpenToHalfOpenEnabled: true
        waitDurationInOpenState: 2s
        recordExceptions:
          - java.lang.Exception
      instances:
        backendA:
          baseConfig: default

重点注意: defaultZone: ${EUREKASERVER_URI:http://127.0.0.1:9001/eureka}

docker-compose 的编排文件 docker-compose.yml
version: '3'
services:
    nginx:
        image: nginx
        ports:
            - 80:80
        volumes:
            - ./nginx/conf/nginx.conf:/etc/nginx/nginx.conf
            - ./nginx/logs:/var/log/nginx
            - ./nginx/html:/usr/share/nginx/html
    mysql:
        image: mysql:8.0.23
        ports:
            - 3306:3306
        volumes:
            - ./mysql/conf:/etc/mysql/conf.d
            - ./mysql/logs:/logs
            - ./mysql/data:/var/lib/mysql
    eureka:
        image: eureka
        ports:
            - 9001:9001

    gateway:
        image: gateway
        ports:
            - 9003:9003
        links:
            - eureka
        environment:
            EUREKASERVER_URI: "http://eureka:9001/eureka/"
    cloud_config:
        image: cloud_config
        ports:
            - 9004:9004
        links:
            - eureka
        environment:
            EUREKASERVER_URI: "http://eureka:9001/eureka/"
    bookkeeping:
        image: bookkeeping
        ports:
            - 9002:9002 
        links:
            - eureka
        environment:
            EUREKASERVER_URI: "http://eureka:9001/eureka/"
# 链接 eureka 容器
links:
     - eureka
# "http://eureka:9001/eureka/"
# 通过容器的9001端口向 eureka 进行注册
environment:
     EUREKASERVER_URI: "http://eureka:9001/eureka/"

相关