微服务无法访问Docker Compose上的Config Server

时间:2018-09-07 19:42:33

标签: docker spring-boot docker-compose microservices

我有一个Eureka服务器,配置服务器和Spring-Boot服务。我创建了一个docker compose文件,如下所示:

version: '3'

services:
 user-service:
   container_name: user-service-container
   image: user-service:latest
   build:
     context: ./user-service
     dockerfile: DockerFile
   hostname: user-service
   ports:
     - "8082:8082"
   depends_on:
      - postgresdb
      - eureka-service
      - config-server
   environment:
       management.context-path : /userservice
       hostName : user-service
       EUREKA_HOST: eureka-service
       EUREKA_PORT: 8761

 config-server:
   container_name: config-server-container
   image: config-server:latest
   build:
    context: ./config-server
    dockerfile: DockerFile
   hostname: config-server
   ports:
     - "8081:8081"
   depends_on:
       - eureka-service
   environment:
     management.context-path : /config
     hostName : config-server
     SPRING_PROFILES_ACTIVE : registration-first
     EUREKA_HOST: eureka-service
     EUREKA_PORT: 8761

 eureka-service:
   container_name: eureka-service-container
   image: eureka-service:latest
   build:
      context: ./eureka-service
      dockerfile: DockerFile
   hostname: eureka-service
   ports:
     - "8761:8761"

 postgresdb:
   container_name: postgres-db-container
   image: postgres:10.5
   hostname: postgres
   ports:
     - "5432:5432"

使用此配置,Config Server可以将自己注册到Eureka Server,但是User Service的配置部分抛出以下异常:

2018-09-07T18:53:17.869040200Z 2018-09-07 18:53:17.868  INFO 6 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
2018-09-07T18:53:18.331651100Z 2018-09-07 18:53:18.331  INFO 6 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Connect Timeout Exception on Url - http://localhost:8888. Will be trying the next url if available
2018-09-07T18:53:18.332055700Z 2018-09-07 18:53:18.331  WARN 6 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: I/O error on GET request for "http://localhost:8888/user-service/registered-config-server,dev": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
2018-09-07T18:53:18.334406300Z 2018-09-07 18:53:18.334  INFO 6 --- [           main] c.k.d.u.UserServiceApplication           : The following profiles are active: registered-config-server,dev
2018-09-07T18:53:18.371334800Z 2018-09-07 18:53:18.370  INFO 6 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@4d1c00d0: startup date [Fri Sep 07 18:53:18 UTC 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@13969fbe

从此异常中,我了解到用户服务已连接到Euroka Server,但它尝试通过错误的URL(即http://localhost:8888)连接到Config Server

Euroka Server应用程序属性:

spring.application.name=eureka-server
server.port=8761

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

配置服务器应用程序属性:

    spring:
  cloud:
    config:
      server:
        git :
          uri: https://bitbucket.org/blabla
          username: blabla
          password: ***
      discovery:
        enabled: true
        service-id: config-server
server:
  port: 8081
---
spring:
  application:
    name: config-server
  profiles: registration-first
eureka:
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/
  instance:
    appname: config-server
    hostname: ${hostName}
    statusPageUrlPath: ${management.context-path}/info
    healthCheckUrlPath: ${management.context-path}/health
    preferIpAddress: true
    metadataMap:
      instanceId: ${spring.application.name}:${server.port}

用户服务应用程序属性:

endpoints:
  enabled: false
  info:
    enabled: true
  health:
    enabled: true
  metrics:
    enabled: true
  refresh:
    enabled: true

eureka:
  client:
    fetchRegistry: true
    registerWithEureka: true
    serviceUrl:
      defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/
  instance:
    hostname: ${hostName}
    statusPageUrlPath: ${management.context-path}/info
    healthCheckUrlPath: ${management.context-path}/health
    preferIpAddress: true
    metadataMap:
      instanceId: ${spring.application.name}:${server.port}

spring.jpa.database: POSTGRESQL
spring.datasource.platform: postgres
spring.jpa.show-sql: true
spring.jpa.hibernate.ddl-auto: update
spring.database.driverClassName: org.postgresql.Driver
spring.datasource.url: jdbc:postgresql://postgresdb:5432/postgres
spring.datasource.username: postgres
spring.datasource.password: postgres

用户服务Bootstrap.yml:

    spring:
  profiles:
    active: registered-config-server,dev
  application:
    name: user-service

server.port: 8082
---
spring:
  profiles: known-config-server
  cloud:
    config:
      uri: http://localhost:8080

---
spring:
  profiles: registered-config-server
  cloud:
    config:
      discovery:
        enabled: true
        serviceId: config-server

但是当我在没有docker的情况下在本地PC上运行这些应用程序时,它们可以正常工作。

有人有什么建议吗?

编辑: UserService Docker文件:

FROM java:8
EXPOSE 8082
ADD /target/user-service-0.0.1-SNAPSHOT.jar user-service.jar
CMD java -jar user-service.jar

配置服务器docker文件

FROM java:8
EXPOSE 8081
ADD /target/config-server-0.0.1-SNAPSHOT.jar config-server.jar
CMD java -jar config-server.jar

eureka服务器docker文件

FROM java:8
EXPOSE 8761
ADD /target/eureka-service-0.0.1-SNAPSHOT.jar eureka-service.jar
CMD java -jar eureka-service.jar

2 个答案:

答案 0 :(得分:2)

当试图从运行在Docker容器中的Spring Cloud Config Server中获取配置时,我也面临着同样的问题。即使我在应用程序的select name,cntClient,sum(cntClient) over(order by orderCol rows between unbounded preceding and 1 preceding) as cumCntClient from (select a.name, count(distinct numClient) as cntClient from a group by a.name ) t 中指定了docker容器名称,我的Spring Boot应用程序仍尝试在http://localhost:8888上访问它。

下面是我的应用程序的 application.properties

application.proprties

作为一种解决方法,我在docker compose中将配置服务器URL作为参数传递。

这是我的 docker-compose.yml

spring.application.name=stock-app
spring.profiles.active=default
spring.cloud.config.uri=http://configserver:8888
server.port=8301

然后,在用于启动应用程序的shell脚本中使用此参数。

下面是 Dockerfile script

Dockerfile

version: '2'
services:
  configserver:
    image: himarg/config-server:0.0.1-SNAPSHOT
    ports:
       - "8888:8888"
    environment:
      ENCRYPT_KEY:       "IMSYMMETRIC"

  database:
    image: himarg/stock-db:latest
    ports:
      - "3307:3306"
    environment:
      MYSQL_ROOT_PASSWORD: "Welcome1"
      MYSQL_DATABASE: "inventory"

  stockapp:
    image: himarg/stock-app:0.0.1-SNAPSHOT
    ports:
      - "8301:8301"
    environment:
      PROFILE: "default"
      CONFIGSERVER_URI: "http://configserver:8888"
      CONFIGSERVER_PORT:   "8888"
      DATABASESERVER_PORT: "3306"
      ENCRYPT_KEY:       "IMSYMMETRIC"

run.sh

FROM openjdk:8-jdk-alpine
RUN apk update && apk upgrade && apk add netcat-openbsd
RUN mkdir -p /usr/local/stock-app
ADD @project.build.finalName@.jar /usr/local/stock-app/
ADD run.sh run.sh
RUN chmod +x run.sh
CMD ./run.sh

进行此更改后,尽管我不确定这种解决方法是否符合您的要求,但我能够成功运行我的应用程序。

答案 1 :(得分:1)

尝试将eureka.client属性移动到bootstrap.yml而不是配置服务器的application.properties