如何将使用Eureka和云配置的spring microservices应用程序对接
我在这个spring微服务应用程序中工作,在我想对微服务进行dockerize之前,一切都正常。对docker略知一二后,我正努力让它发挥作用,以下是我迄今为止所做的: 1.Config服务器 此服务器是一个集中式服务器,包含其他服务的通用配置以及每个微服务的特殊配置 这是配置服务配置如何将使用Eureka和云配置的spring microservices应用程序对接,spring,docker,docker-compose,microservices,Spring,Docker,Docker Compose,Microservices,我在这个spring微服务应用程序中工作,在我想对微服务进行dockerize之前,一切都正常。对docker略知一二后,我正努力让它发挥作用,以下是我迄今为止所做的: 1.Config服务器 此服务器是一个集中式服务器,包含其他服务的通用配置以及每个微服务的特殊配置 这是配置服务配置 spring.profiles.active=native server.port=8888 spring.cloud.config.server.native.search-locations=classpat
spring.profiles.active=native
server.port=8888
spring.cloud.config.server.native.search-locations=classpath:/common-config
spring.application.name=config-server
#the password which apps will use to access its configurations
spring.security.user.password=1234
然后,我在src/main/resources中添加了一个名为common config的文件夹,其中包含每个服务配置:
server:
port: 8762
然后在配置服务器的根目录中,我添加了两个文件Dockerfile和docker-compose.yml:
Dockerfile
# Start with a base image containing Java runtime
FROM openjdk:8-jdk-alpine
# Add Maintainer Info
LABEL maintainer="sanchi.goyal.sg@gmail.com"
# Add a volume pointing to /tmp
VOLUME /tmp
# Make port 8888 available to the world outside this container
EXPOSE 8888
# The application's jar file
ARG JAR_FILE=./target/*.jar
# Add the application's jar to the container
ADD ${JAR_FILE} app.jar
# Run the jar file
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
version: "3"
services:
config-server:
image: config-server:0.0.1-SNAPSHOT
environment:
- JAVA_OPTS=
-DEUREKA_SERVER=http://discover-service:8762/eureka
ports:
- 8888:8888
discover-service:
image: discover-service:0.0.1-SNAPSHOT
ports:
- 8762:8762
depends_on:
- config-server
restart: on-failure
docker compose
# Start with a base image containing Java runtime
FROM openjdk:8-jdk-alpine
# Add Maintainer Info
LABEL maintainer="sanchi.goyal.sg@gmail.com"
# Add a volume pointing to /tmp
VOLUME /tmp
# Make port 8888 available to the world outside this container
EXPOSE 8888
# The application's jar file
ARG JAR_FILE=./target/*.jar
# Add the application's jar to the container
ADD ${JAR_FILE} app.jar
# Run the jar file
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
version: "3"
services:
config-server:
image: config-server:0.0.1-SNAPSHOT
environment:
- JAVA_OPTS=
-DEUREKA_SERVER=http://discover-service:8762/eureka
ports:
- 8888:8888
discover-service:
image: discover-service:0.0.1-SNAPSHOT
ports:
- 8762:8762
depends_on:
- config-server
restart: on-failure
现在在discover service应用程序中,我在根目录中添加了Dockerfile,如下所示:
# Start with a base image containing Java runtime
FROM openjdk:8-jdk-alpine
# Add Maintainer Info
LABEL maintainer="sanchi.goyal.sg@gmail.com"
# Add a volume pointing to /tmp
VOLUME /tmp
# Make port 8888 available to the world outside this container
EXPOSE 8762
# The application's jar file
ARG JAR_FILE=./target/*.jar
# Add the application's jar to the container
ADD ${JAR_FILE} app.jar
# Run the jar file
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
#This file is the entrypoint for spring to know where to look for the configuration file and hold the initial configuration
spring:
application:
name: discover-service #name of the application
cloud:
config:
uri: http://localhost:8888 #where the config-service is running
fail-fast: true #the service will not run if it can't reach the config-service
password: 1234 #password of the config-service
username: user #username of the config-service
eureka:
instance:
prefer-ip-address: true #when the application registers with eureka, it uses its IP address rather than its hostname
client:
registerWithEureka: false #tells the built-in eureka client to not register itself with eureka, because we should act as a server
fetchRegistry: false
在src/main/resources中,我添加了bootstrap.yml文件,如下所示:
# Start with a base image containing Java runtime
FROM openjdk:8-jdk-alpine
# Add Maintainer Info
LABEL maintainer="sanchi.goyal.sg@gmail.com"
# Add a volume pointing to /tmp
VOLUME /tmp
# Make port 8888 available to the world outside this container
EXPOSE 8762
# The application's jar file
ARG JAR_FILE=./target/*.jar
# Add the application's jar to the container
ADD ${JAR_FILE} app.jar
# Run the jar file
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
#This file is the entrypoint for spring to know where to look for the configuration file and hold the initial configuration
spring:
application:
name: discover-service #name of the application
cloud:
config:
uri: http://localhost:8888 #where the config-service is running
fail-fast: true #the service will not run if it can't reach the config-service
password: 1234 #password of the config-service
username: user #username of the config-service
eureka:
instance:
prefer-ip-address: true #when the application registers with eureka, it uses its IP address rather than its hostname
client:
registerWithEureka: false #tells the built-in eureka client to not register itself with eureka, because we should act as a server
fetchRegistry: false
现在的问题是,在开始dockerize这两个应用程序之前,我为每个应用程序构建了一个jar文件和docker jar文件,因此对于配置服务器应用程序,我创建了两个jar,没有问题,但是对于第二个应用程序,为了创建这两个jar,我必须在bootstrap.yml中保留以下属性,以便它知道配置服务器在哪里
uri: http://localhost:8888
现在转到docker,当我运行命令时:
docker-compose up
第一个应用程序运行正常,但第二个应用程序显示:
Fetching config from server at : http://localhost:8888
Connect Timeout Exception on Url - http://localhost:8888
org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8888/discover-service/default": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
有人能帮我吗
这是我的github存储库
我没有提交此问题中描述的更改。你会发现多个服务,但如果我能在docker中运行所描述的两个应用程序,我可以用其他应用程序来运行
编辑
我已通过将docker-compose.yml更改为以下内容来启动我的应用程序:
version: "3"
services:
config-server:
image: config-server:0.0.1-SNAPSHOT
environment:
- JAVA_OPTS=
-DEUREKA_SERVER=http://discover-service:8762/eureka
ports:
- 8888:8888
discover-service:
image: discover-service:0.0.1-SNAPSHOT
ports:
- 8762:8762
depends_on:
- config-server
restart: on-failure
networks:
host:
但奇怪的事情正在发生,例如,当我想去发现服务应用程序(这是一个eureka服务器)时,它不起作用,但当我去同一个选项卡上的另一个网站并返回时,它显示了预期的结果这是否回答了你的问题@利奥帕尔:不,没用