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

问题描述 投票:1回答:2

我有一个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,但是用户服务的配置部分会抛出以下异常:

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服务器,但它尝试通过错误的URL(即http://localhost:8888)连接到配置服务器

Euroka Server App属性:

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

config-server 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-server docker文件

FROM java:8
EXPOSE 8761
ADD /target/eureka-service-0.0.1-SNAPSHOT.jar eureka-service.jar
CMD java -jar eureka-service.jar
docker spring-boot docker-compose microservices
2个回答
2
投票

尝试从Docker容器中运行的Spring Cloud Config Server获取配置时,我也面临同样的问题。我的Spring Boot应用尝试在http://localhost:8888上访问它,即使我已经在应用程序的application.proprties中指定了docker容器名称。

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

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

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

这是我的docker-compose.yml:

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"

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

下面是Dockerfile和脚本:

Dockerfile

FROM openjdk:8-jdk-alpine
RUN apk update && apk upgrade && apk add netcat-openbsd
RUN mkdir -p /usr/local/stock-app
ADD @[email protected] /usr/local/stock-app/
ADD run.sh run.sh
RUN chmod +x run.sh
CMD ./run.sh

润.是

#!/bin/sh

echo "********************************************************"
echo "Waiting for the database server to start on port $DATABASESERVER_PORT"
echo "********************************************************"
while ! `nc -z database $DATABASESERVER_PORT`; do sleep 3; done
echo "******** Database Server has started "

echo "********************************************************"
echo "Waiting for the configuration server to start on port $CONFIGSERVER_PORT"
echo "********************************************************"
while ! `nc -z configserver $CONFIGSERVER_PORT`; do sleep 3; done
echo "*******  Configuration Server has started"

echo "********************************************************"
echo "Starting stock-app "
echo "********************************************************"
java -Dspring.cloud.config.uri=$CONFIGSERVER_URI -jar /usr/local/stock-app/@[email protected]

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


1
投票

改变这个

eureka:
  client:
    fetchRegistry: true
    registerWithEureka: true
    serviceUrl:
      defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/

对此

eureka:
  client:
    fetchRegistry: true
    registerWithEureka: true
    serviceUrl:
      defaultZone: http://${EUREKA_HOST:localhost}:${EUREKA_PORT:8761}/eureka/

您还必须创建桥接网络,以便您的应用程序可以相互通信。可以考虑将应用程序停靠在不同计算机上运行每项服务。这意味着使用“localhost”并不能真正帮助你。

像下面的这个(来源:https://developer.okta.com/blog/2017/10/11/developers-guide-to-docker-part-3

services:
  app:
    image: sample:1.0
    container_name: sample_app
    build: .
    ports:
      - 80:3000
    environment:
      - MONGO_URI=mongodb://sampledb/sample
    depends_on:
      - db
    networks:
      - samplenet
  db:
    image: mongo:3.0.15
    container_name: sample_db
    volumes:
      - ./db:/data/db
    networks:
      samplenet:
        aliases:
          - "sampledb"
networks:
  samplenet:
    driver: bridge
© www.soinside.com 2019 - 2024. All rights reserved.