[英]How to configure and run Eureka Server, Microservices (Eureka Client), Zuul inside separate docker containers?
I am a newbie in Spring Boot and Docker. 我是Spring Boot和Docker的新手。 Written the below microservices.
编写了以下微服务。
I have used "Spring Boot STS" to develop the services. 我已经使用“ Spring Boot STS”来开发服务。 Also used "Spotify" dependency to build the docker images.
还使用“ Spotify”依赖项来构建docker映像。
I am able to run all the services from STS on localhost (windows) and everything is working fine. 我能够在本地主机(Windows)上从STS运行所有服务,并且一切正常。 All the three services mentioned before are getting registered in Eureka server.
前面提到的所有三个服务都已在Eureka服务器中注册。
But everything stopped working when I choose to dockerize the services. 但是,当我选择对服务进行docker化时,一切都停止了工作。 I have created four docker images of the four services.
我已经创建了四个服务的四个docker映像。
Problem: Ran the Eureka server and "User Registration Service" in two sperate containers but the service is not getting registered with Eureka. 问题:在两个单独的容器中运行Eureka服务器和“用户注册服务”,但该服务未在Eureka中注册。 In fact none of the services is getting registered with Eureka.
实际上,这些服务都没有在Eureka上注册。
What I did: 我做了什么:
Ran the Eureka server and client using below commands:
docker run --rm -it --name eurekaserver-container -p 8761:8761 eurekaserver-alpine-linux
docker运行--rm -it --name eurekaserver-container -p 8761:8761 eurekaserver-alpine-linux
docker run --rm -it -d --name user-registration-container -p 8081:8081 user-registration
docker运行--rm -it -d --name用户注册容器-p 8081:8081用户注册
It seems Eureka Client (User Registration Service) is not able to find Eureka server. 看来Eureka Client(用户注册服务)无法找到Eureka服务器。 What I found, Eureka server is running within a container with different IP (172.0...) and Eureka client is not able to connect the server.
我发现,Eureka服务器正在具有不同IP(172.0 ...)的容器中运行,而Eureka客户端无法连接该服务器。
Ran the Eureka server and client using below commands. 使用以下命令运行Eureka服务器和客户端。 Tried to link the two containers (Eureka Server and Client):
试图链接两个容器(Eureka Server和Client):
docker run --rm -it --name eurekaserver-container -p 8761:8761 eurekaserver-alpine-linux
docker运行--rm -it --name eurekaserver-container -p 8761:8761 eurekaserver-alpine-linux
docker run --rm -it -d --name user-registration-container -p 8081:8081 --link eurekaserver-container:eureka-server user-registration
docker run --rm -it -d --name用户注册容器-p 8081:8081 --link eurekaserver容器:eureka服务器用户注册
This also didn't work. 这也没有用。
Ran the Eureka server and client using below commands. 使用以下命令运行Eureka服务器和客户端。 Tried to pass the host name to Eureka image, so that Client can find the Eureka server using host name.
尝试将主机名传递给Eureka映像,以便客户端可以使用主机名找到Eureka服务器。 Also linked the two containers.
还链接了两个容器。
docker run --rm -it -d --name eurekaserver-container -h discovery-eurekaserver.com -p 8761:8761 eurekaserver-alpine-linux /bin/bash
docker运行--rm -it -d --name eurekaserver-container -h Discovery-eurekaserver.com -p 8761:8761 eurekaserver-alpine-linux / bin / bash
docker run --rm -it -d --name user-registration-container -p 8081:8081 --link eurekaserver-container:eureka-server user-registration
docker run --rm -it -d --name用户注册容器-p 8081:8081 --link eurekaserver容器:eureka服务器用户注册
This also didn't work. 这也没有用。
Can anyone please help me to understand what am I doing wrong? 谁能帮我了解我在做什么错? Whatever I have tried, Eureka clients are not able to register with Eureka server when running in different containers.
无论我尝试了什么,当在不同容器中运行时,Eureka客户端都无法向Eureka服务器注册。
My objective is to run the services (Eureka Server, Application Services etc.) in different containers and all the services will register with Eureka server, so that I can call APIs from Postman or SOAP UI. 我的目标是在不同的容器中运行服务(Eureka服务器,应用程序服务等),并且所有服务都将在Eureka服务器中注册,以便可以从Postman或SOAP UI调用API。
I suggest, you have a separate Dockerfile for each Spring boot project. 我建议,每个Spring引导项目都有一个单独的Dockerfile。
FROM maven:3.6.0-jdk-8
MAINTAINER xxx@yyyyy.com
COPY . /usr/src/project-name
WORKDIR /usr/src/project-name
CMD ["mvn","spring-boot:run"]
And a docker-compose.yml for controlling services and networks and links between containers. 还有一个docker-compose.yml用于控制服务和网络以及容器之间的链接。
For example: 例如:
version: '3.5'
services:
discovery-server:
build:
context: ./discovery-server
hostname: discovery-server
environment:
- SPRING_APPLICATION_NAME=discovery-server
- SPRING_PROFILES_ACTIVE=dev
command: ["mvn", "spring-boot:run"]
expose:
- "8761"
volumes:
- maven-home:/root/.m2
networks:
services-network-01:
aliases:
- discovery-server
user-service:
build:
context: ./user-service
hostname: user-service
environment:
- SPRING_APPLICATION_NAME=user-service
- SPRING_PROFILES_ACTIVE=dev
command: ["mvn", "spring-boot:run"]
ports:
- "8080:8080"
volumes:
- maven-home:/root/.m2
networks:
services-network-01:
aliases:
- user-service
depends_on:
- discovery-server
volumes:
maven-home:
networks:
services-network-01:
name: services-network-01
driver: bridge
Docker Compose File Refrence Docker撰写文件参考
And ultimately depends on the configuration for Eureka discovery server and Microservices. 最终取决于Eureka发现服务器和微服务的配置。
Eureka Discovery Server config in application.yml
: application.yml
Eureka Discovery Server配置:
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
region: region1
serviceUrl:
zone1: http://discovery-server:8761/eureka/
availability-zones:
region1: zone1
instance:
hostname: discovery-server
metadata-map:
zone: zone1
Eureka client config in application.yml
in Microservices: Microservices中
application.yml
中的Eureka客户端配置:
# Eureka client
eureka:
instance:
prefer-ip-address: true
metadata-map:
zone: zone1
client:
prefer-same-zone-eureka: true
register-with-eureka: true
region: region1
service-url:
zone1: http://discovery-server:8761/eureka/
availability-zones:
region1: zone1
Run docker compose: 运行docker compose:
docker-compose up -d --build --force-recreate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.