【发布时间】:2018-10-14 03:58:21
【问题描述】:
[Spring Boot 微服务]
我有一个微服务包括 2 个服务:ConfigService 和 DiscoveryService
- ConfigService 已启用 ConfigServer,为微服务保留文件配置
- DiscoveryService 是 EurekaServer。它将从 ConfigService 中获取配置文件
在本地(不是 docker)上运行 2 服务时,一切都很好
Fetching config from server at: http://localhost:8088
Located environment: name=epl-discovery-service, profiles=[default], label=null, version=3f6887b5b355381341e02ad03615f2415d6a566d, state=null
Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://github.com/stomer90/epl-config-server.git/epl-discovery-service.yml'}]}
No active profile set, falling back to default profiles: default
但是当在 2 个容器(docker)上运行 2 个服务时,ConfigService 运行正常,但是 DiscoveryService 有一些错误(无法连接到 ConfigService)
Fetching config from server at: http://localhost:8088
Could not locate PropertySource: I/O error on GET request for "http://localhost:8088/epl-discovery-service/default": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
No active profile set, falling back to default profiles: default
- 配置服务
EplConfigServiceApplication.java
块引用
@SpringBootApplication
@EnableConfigServer
public class EplConfigServiceApplication {
public static void main(String[] args) {
SpringApplication.run(EplConfigServiceApplication.class, args);
}
}
bootstrap.yml
server:
port: 8088
spring:
application:
name: eplconfigserver
cloud:
config:
server:
git:
uri: https://github.com/stomer90/epl-config-server.git
Dockerfile
FROM openjdk:8-jdk-alpine
MAINTAINER Phong Nguyen
VOLUME /tmp
# Add Spring Boot app.jar to Container
ADD ./target/epl-config-service-0.0.1-SNAPSHOT.jar app.jar
RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar " ]
* DiscoveryService
EplDiscoveryServiceApplication.java
@SpringBootApplication
@EnableEurekaServer
public class EplDiscoveryServiceApplication {
public static void main(String[] args) {
SpringApplication.run(EplDiscoveryServiceApplication.class, args);
}
}
bootstrap.yml
spring:
application:
name: epl-discovery-service
cloud:
config:
uri: http://localhost:8088
Dockerfile
FROM openjdk:8-jdk-alpine
MAINTAINER Phong Nguyen
VOLUME /tmp
# Add Spring Boot app.jar to Container
ADD ./target/epl-discovery-service-0.0.1-SNAPSHOT.jar app.jar
RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
- Docker-compose.yml
version: '3.1' services: epl-config-service: build: ./epl-config-service ports: - "8088:8088" restart: unless-stopped epl-discovery-service: build: ./epl-discovery-service ports: - "8061:8061" environment: - REGISTRY_HOST=epl-config-service depends_on: - epl-config-service restart: unless-stopped
链接源代码:https://github.com/stomer90/epl-spring-cloud-microservice
请帮我解决这个问题
【问题讨论】:
标签: spring-boot docker-compose microservices spring-cloud-netflix