问题
[Spring boot Microservies]
I have a microservices includes 2 services: ConfigService and DiscoveryService
- ConfigService is enabled ConfigServer, keep files config for microservice
- DiscoveryService is EurekaServer. It will get config file from ConfigService
When run 2 service on local (not docker), everything is good
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
but when run 2 service on 2 container (docker), ConfigService run normal, but DiscoveryService have some error (can not connect to 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
- ConfigService
EplConfigServiceApplication.java
Blockquote
@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
Link source code: https://github.com/stomer90/epl-spring-cloud-microservice
Please help me resolve this issue
回答1:
So you have correctly specified the order in which container needs to be started but this doesn't guarantee that previous container (in your case config server is healthy or not) since ur docker-compose version is 3.1 you can define healthchecks in your compose file
eg:
registry:
build:
context: ../service-registry
dockerfile: Dockerfile
container_name: registry
links:
- configuration-server
depends_on:
configuration-server:
condition: service_healthy
and
configuration-server:
build:
context: ../configuration-server
dockerfile: Dockerfile
image: xyz/configuration-server
container_name: configuration-server
environment:
- SPRING_PROFILES_ACTIVE=dev
- SPRING_CLOUD_STREAM_KAFKA_BINDER_BROKER=kafka
- SPRING_CLOUD_STREAM_KAFKA_BINDER_BROKER_PORT=9092
- SPRING_CLOUD_STREAM_KAFKA_BINDER_ZKNODE=zookeeper
- SPRING_CLOUD_STREAM_KAFKA_BINDER_ZKPORT=2181
depends_on:
kafka:
condition: service_healthy
zookeeper:
condition: service_healthy
healthcheck:
test: "exit 0"
notice the healthcheck in config server which would be invoked from registry server (condition:service healthy) you can implement your own custom healthcheck to something more sophesticated like
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost"]
interval: 1m30s
timeout: 10s
retries: 3
start_period: 40s
refer this:https://docs.docker.com/compose/compose-file/compose-file-v2/#healthcheck
i guess that should be enough to kickstart your containers, let me know if it works
来源:https://stackoverflow.com/questions/50161783/cant-get-config-files-when-run-configserver-and-eurekaserver-on-docker-containe