Eureka: example of creating 3 nodes Eureka Server with docker

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/yexianyi/article/details/91884851

Abstract:

In this artical, it will state how to create 3 nodes Eureka Server with docker.

Step1. Create Eureka Server

Source: https://github.com/yexianyi/Chukonu/tree/master/chukonu-springcloud-eureka-server

We will create 3 Eureka Servers which is named as "peer1", "peer2", "peer3" respectively, with register order as peer1 -> peer2, peer2 -> peer3 and peer3 -> peer1

docker network create eureka-net
docker run -it --name peer_host_1 --net eureka-net --rm -e EUREKA_SERVER_HOSTNAME=peer_host_1 -e EUREKA_SERVER_PORT=8761 -e EUREKA_SERVER_PEER_URL=http://192.168.1.10:8762/eureka,http://192.168.1.10:8763/eureka -p 8761:8761 -d yexianyi/eureka-server
docker run -it --name peer_host_2 --net eureka-net --rm -e EUREKA_SERVER_HOSTNAME=peer_host_2 -e EUREKA_SERVER_PORT=8762 -e EUREKA_SERVER_PEER_URL=http://192.168.1.10:8761/eureka,http://192.168.1.10:8763/eureka -p 8762:8762 -d yexianyi/eureka-server
docker run -it --name peer_host_3 --net eureka-net --rm -e EUREKA_SERVER_HOSTNAME=peer_host_3 -e EUREKA_SERVER_PORT=8763 -e EUREKA_SERVER_PEER_URL=http://192.168.1.10:8761/eureka,http://192.168.1.10:8762/eureka -p 8763:8763 -d yexianyi/eureka-server

Explaination:

1. 192.168.1.10 is my local host which installed docker engine

2. EUREKA_SERVER_HOSTNAME: this name will be mapping to attribute "eureka.instance.hostname" . Must be unique, otherwise eureka replicas will not be functional.

3. In order to access Eureka Dashboard of each node, each docker container exposed EUREKA_SERVER_PORT.

Once all these 3 server start up, we can visit their dashboards via http://192.168.1.10:8761 http://192.168.1.10:8762 and http://192.168.1.10:8763. Each of dashboard should be similiar to following screenshot:

 

Step 2. Register Service

After Eureka Server Cluster we created is done, we can register some services on it. Here, I will use an existing demo service to show it.

source: https://github.com/yexianyi/Chukonu/tree/master/chukonu-springcloud-eureka-provider

docker run --rm -it --net eureka-net -e EUREKA_PROVIDER_PORT=8800 -e EUREKA_SERVER_PEER_URL=http://192.168.1.10:8761/eureka -d yexianyi/eureka-provider
docker run --rm -it --net eureka-net -e EUREKA_PROVIDER_PORT=8801 -e EUREKA_SERVER_PEER_URL=http://192.168.1.10:8762/eureka -d yexianyi/eureka-provider
docker run --rm -it --net eureka-net -e EUREKA_PROVIDER_PORT=8802 -e EUREKA_SERVER_PEER_URL=http://192.168.1.10:8763/eureka -d yexianyi/eureka-provider

Now, we can see that 3 copy of "HELLO-SERVICE" have been registered to the registry with port from 8800 to 8802. And these update have been populated to 2 other eureka server peer_host_2 and peer_host_3.

 

 

Step 3. Develop Eureka Service Consumer

Now, we will deploy an eureka consumer to access HELLO-SERVICE.

Source: https://github.com/yexianyi/Chukonu/tree/master/chukonu-springcloud-eureka-consumer

docker run --rm -it -e EUREKA_CONSUMER_PORT=8900 -e EUREKA_SERVER_PEER_URL=http://192.168.1.10:8761/eureka -p 8900:8900 -d yexianyi/eureka-consumer

 After that, we will see the consumer "RIBBON-CONSUMER" has been registered in registry.

 

Step 4. Testing

Given that we have registered service on Eureka, let's test it now.

curl -X GET -i 'http://localhost:8900/ribbon-consumer?message=abc'
curl -X GET -i 'http://localhost:8900/ribbon-consumer?message=abc'
curl -X GET -i 'http://localhost:8900/ribbon-consumer?message=abc'

We will see the response is :

b5fe6b02f0e3:8800 - recv: 'abc'

e152e3d76ecb:8801 - recv: 'abc'

e152e3d76ecb:8802 - recv: 'abc'

 

Note:

Since we created an user-defined network in docker to run all the eureka container in our case, we need to enable ipv4 forwarding function. Otherwise, our container cannot be accessed from outside world.

MAC: sudo sysctl -w net.inet.ip.forwarding=1

 

展开阅读全文

没有更多推荐了,返回首页