In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail the sample analysis of .NET Core + Consul service registration and discovery. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
In the distributed architecture, service governance is a problem that must be faced. If there is no simple and effective governance scheme, the service relationship between services can only be managed through human flesh configuration. When the service relationship changes, it becomes extremely troublesome and error-prone.
Consul [1] is an open source tool for service discovery and configuration in distributed systems. It has built-in service registration and discovery framework, distributed consistency protocol implementation, health check, Key/Value storage, multi-data center solution, no longer need to rely on other tools (such as ZooKeeper, etc.), and is relatively easy to use.
Consul architecture
The Consul cluster supports multiple data centers. In the figure above, there are two DataCenter interconnected through Internet. In order to improve communication efficiency, only Server nodes participate in cross-data center communication. In a single data center, Consul is divided into Client and Server nodes (all nodes are also known as Agent). Server nodes save data, Client is responsible for health check and forwarding data requests to Server, and does not save registration information; it is recommended that Server nodes have one Leader and multiple Follower,Leader nodes to synchronize data to Follower,Server nodes. It is recommended that 3 or 5 nodes synchronize data to Follower,Server nodes. When the Leader is hung up, the election mechanism will be activated to generate a new Leader.
Consul cluster building
Here, we use Docker to build 3 Server nodes + 1 Client node, and the API service registers and discovers services through the Client node.
Pull Consul image docker pull consul from Docker Hub
Start 3 Server nodes + 1 Client node
Docker-compose.yaml is as follows:
Version:'3'
Services:
Cs1:
Image: consul
Command: agent-server-client=0.0.0.0-bootstrap-expect=3-node=cs1-data-dir=/data
Volumes:
-/ usr/local/docker/consul/data/cs1:/data
Cs2:
Image: consul
Command: agent-server-client=0.0.0.0-retry-join=cs1-node=cs2-data-dir=/data
Volumes:
-/ usr/local/docker/consul/data/cs2:/data
Depends_on:
-cs1
Cs3:
Image: consul
Command: agent-server-client=0.0.0.0-retry-join=cs1-node=cs3-data-dir=/data
Volumes:
-/ usr/local/docker/consul/data/cs3:/data
Depends_on:
-cs1
Cc1:
Image: consul
Command: agent-client=0.0.0.0-retry-join=cs1-ui-node=cc1-data-dir=/data
Ports:
-8500 purl 8500
Volumes:
-/ usr/local/docker/consul/data/cc1:/data
Depends_on:
-cs2
-cs3
Description of main parameters:
Parameter name interpretation-server is set to Server type node, otherwise it is the IP operated by a series of clients such as Client type node-client registration or query. The default is the number of Server nodes expected by the 127.0.0.1-bootstrap-expect cluster. Only when this value is reached will Leader-node specify the node name-data-dir data storage location-retry-join specify the node address to join (build cluster)-ui enables UI interface cluster status.
(e002ca62ac24 is the name of the container, which can be viewed through docker ps | grep consul. You can select any one)
View node status and type docker exec-t e002ca62ac24 consul members
Currently there are 3 Server type nodes and 1 Client type node.
View the Server node type docker exec-t e002ca62ac24 consul operator raft list-peers
Currently cs1 is leader, you can test that cs1 stop will observe the re-election of leader.
Check the Consul node status through the http://192.168.124.9:8500 UI interface as follows: (192.168.124.9 is the external access to the IP of the consul container)
.net Core connects to Consul
Create .NET Core WebAPI (3. 1) service ServiceA (2 instances) and ServiceB
NuGet install Consul
The core code to register with Consul is as follows (source download [2]):
Public static class ConsulBuilderExtensions
{
Public static IApplicationBuilder RegisterConsul (this IApplicationBuilder app, IHostApplicationLifetime lifetime, ConsulOption consulOption)
{
Var consulClient = new ConsulClient (x = >
{
X.Address = new Uri (consulOption.Address)
});
Var registration = new AgentServiceRegistration ()
{
ID = Guid.NewGuid () .ToString ()
Name = consulOption.ServiceName,// service name
Address = consulOption.ServiceIP, / / Service binding IP
Port = consulOption.ServicePort, / / Service binding port
Check = new AgentServiceCheck ()
{
DeregisterCriticalServiceAfter = TimeSpan.FromSeconds (5), / / how long will it take to register after the service starts
Interval = TimeSpan.FromSeconds (10), / / Health check interval
HTTP = consulOption.ServiceHealthCheck,// health check address
Timeout = TimeSpan.FromSeconds (5)
}
}
/ / Service registration
ConsulClient.Agent.ServiceRegister (registration) .Wait ()
/ / when the application is terminated, the service is unregistered
Lifetime.ApplicationStopping.Register (() = >
{
ConsulClient.Agent.ServiceDeregister (registration.ID) .Wait ()
});
Return app
}
}
Add the configuration as follows:
"Consul": {
"ServiceName": "service-a"
"ServiceIP": "192.168.124.11", / / current service access IP
"ServicePort": 8000
"ServiceHealthCheck": "http://192.168.124.11:8000/healthCheck","
"Address": "http://192.168.124.9:8500""
}
The results of successful registration are as follows:
This is the end of the sample analysis on the registration and discovery of .NET Core + Consul services. I hope the above content can be of some help and learn more. If you think the article is good, you can share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.