Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to communicate between Service

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

How to communicate between Services, I believe many inexperienced people are helpless about this, this article summarizes the causes and solutions of the problem, through this article I hope you can solve this problem.

An application of microservices architecture consists of several services. For example, there is a web front end running httpd, memcached that provides caching, mysql that stores data, and each layer is a service of swarm, and each service runs several containers. In such an architecture, services must communicate with each other.

service discovery

One way to do this is to publish all services and access them via routing mesh. But the obvious drawback is that memcached and mysql are also exposed to the external network, increasing security risks.

If it does not publish, then swarm provides a mechanism to:

Give services easy access to other services.

When the IP of a copy of a service changes, access to other services of that service is not affected.

When the number of copies of a service changes, access to other services of that service is not affected.

This is service discovery. Docker Swarm natively provides this feature, through service discovery, service consumers do not need to know where the service runs, how many IP, how many copies, can communicate with the service. Below we begin to practice.

Create overlay network

To use service discovery, services that need to communicate with each other must belong to the same overlay network, so we first have to create a new overlay network.

docker network create --driver overlay myapp_net

Can I use ingress directly?

Unfortunately, ingress does not currently provide service discovery and must create its own overlay network.

deploy service overlay

Deploy a web service and mount it to the newly created overlay network.

docker service create --name my_web --replicas=3 --network myapp_net httpd

Deploy a util service for testing, mounted to the same overlay network.

docker service create --name util --network myapp_net busybox sleep 10000000

Sleep 1000000 keeps the busybox container running so we can access service my_web from the container.

verification

Confirm that the node where util is located is swarm-worker1 through docker service ps util.

Log in to swarm-worker1 and ping the service my_web in container util.1.

You can see that the IP of my_web is 10.0.0.2. Which copy of IP is this?

In fact, the IP of any copy is not. 10.0.0.2 is a VIP of my_web service (Virtual IP), swarm will Load Balancer access to VIP to each copy.

We can execute the following command to view the IP of each copy.

docker exec util.1.bpon0vvbgve6lz3fp08sgana6 nslookup tasks.my_web

10.0.0.3 10.0.0.4 and 10.0.0.5 are the IP addresses of each replica. However, for the consumer of the service (util.1 in this case), there is no need to know the IP of the copy of my_web or the VIP of my_web at all, just use the name of the service my_web to access the service.

After reading the above content, do you know how to communicate between Services? If you still want to learn more skills or want to know more related content, welcome to pay attention to the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report