首页 文章

Dockerized微服务无法注册到eureka服务器

提问于
浏览
0

我使用spring boot,spring cloud netfix和docker来运行微服务 .

在非dockerized环境中一切都很好,但是一旦我将eureka服务器和微服务(例如用户服务)停靠,我发现用户服务无法注册到eureka服务器 .

我可以通过http:// :8761 / eureka /访问dockerized eureka服务器,或通过http:// :8088 / user-service访问dockerized service .

但是在docker-compose日志中,我发现了错误see the attachment我不知道为什么它一直说不知名的服务器 .

在eureka服务器网站中,没有显示应用程序实例 . 这个错误信息已经困扰了我好几天了,我已经调查了我能想到的每一种可能性 . 请告诉我任何线索 . 谢谢 .

背景:

Virtualbox: 5.1.20

Ubuntu VM: ubuntu-16

docker installed: 17.06.0

Registry: localhost:5000

Network: Bridge mode in Virtualbox for Ubuntu VM, so that the VM has a standalone ip that could be accessed by other computer

Eureka Server配置:

server:
  port: ${vcap.application.port:8761} #Http port

eureka:
  instance:
    hostname: eureka
    #prefer-ip-address:true

  client:
    registerWithEureka: false
    fetchRegistry: false
    defaultZone: http://${eureka.instance.hostname}:8761/eureka/
  #server:
    waitTimeInMsWhenSyncEmpty: 0

微服务配置:

spring:
  application:
    ## Define the service name for registering on Eureka
    name: user-service
  profiles: docker

eureka:
  client:
    serviceUrl:
      defaultZone: http://eureka:8761/eureka/
    fetch-registry: true
    register-with-eureka: true
  instance:
    prefer-ip-address: true
    metadataMap:
      instanceId: ${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${random.value}}}

endpoints:
    restart:
      enabled: true
    shutdown:
      enabled: true
    health:
      sensitive: true

Docker撰写:

discovery-eureka:
  image: localhost:5000/nicolas/eureka:0.0.1-SNAPSHOT
  ports:
   - "8761:8761"
  hostname: eureka

user-service:
  image: localhost:5000/nicolas/user:0.0.1-SNAPSHOT
  ports:
  - "8088:8088"
  links:
  - discovery-eureka

1 回答

  • 0

    问题解决了,神奇的是用户服务的application.yml中的spring profile定义 .

    我从一开始就忽略了这个定义,我之前的docker-compose.yml定义:

    discovery-eureka:
      image: localhost:5000/nicolas/eureka:0.0.1-SNAPSHOT
      ports:
      - "8761:8761"
      hostname: eureka
    
    user-service:
      image: localhost:5000/nicolas/user:0.0.1-SNAPSHOT
      ports:
      - "8088:8088"
      links:
      - discovery-eureka
    

    从该文件中,没有地方可以告诉容器(或JVM)使用用户服务的application.yml中定义的docker配置文件 .

    初始化容器后,它将使用默认配置文件,application.yml中的元数据将不会普及 .

    之后我将docker-compose.yml改为下面,一切正常 .

    discovery-eureka:
      image: localhost:5000/nicolas/eureka:0.0.1-SNAPSHOT
      ports:
      - "8761:8761"
      hostname: eureka
    
    user-service:
      image: localhost:5000/nicolas/user:0.0.1-SNAPSHOT
      ports:
      - "8088:8088"
      environment:
        - "SPRING_PROFILES_ACTIVE=docker"
      links:
      - discovery-eureka
    

    我从http://www.littlebigextra.com/use-spring-profiles-docker-containers/得到了这个想法

相关问题