First, the Consul cluster introduction
The Consul agent has two modes of operation:Server and Client. The server and client here are only consul cluster-level distinctions, regardless of the application service built on cluster, consul agent nodes running in server mode are used to maintain the status of consul clusters, and each consul is officially recommended Cluster at least 3 or more of the agent,client nodes running in server mode are not limited.
1. server node requires three or more machines
2, client node Unlimited
Second, consul Environment preparation
Three Linux (CentOS) virtual machines (Consul Server) two Linux (CentOS) virtual machines (Consul Client) are prepared
Consul Server service IPs are:
192.168.31.175
192.168.31.176
192.168.31.177
Consul Client service IPs are:
192.168.31.178
192.168.31.179
Among them, 192.168.31.175 will act as the leader role, and the remaining two 192.168.31.176 and 192.168.31.177 will act as follower roles. Of course, the leader role in the real world will not be a fixed one, and the new leader will be picked up by the algorithm as the environment changes (such as leader outage or loss of union). Before doing the following, make sure that the three nodes can ping each other and can ping the host. In addition, 192.168.31.178 and 192.168.31.179 serve as the client role and ping each other three virtual machines.
Third, the official installation of consul
You can refer to the previous article installation method:. Netcore consul implement service registration and discovery-single node
Must ensure that the above five installation success
1. Test whether the consul is installed successfully
> consul
If it means success:
?
2. Consul Server Server-side installation ( Start and configure the consul service )
Service-side 192.168.31.175 execution
> consul agent -server -ui -bootstrap-expect=3 -data-dir=/tmp/consul -node=consul-175 -client=0.0.0.0 -bind=192.168.31.175 -datacenter=dc1
Service-side 192.168.31.176 execution
> consul agent -server -ui -bootstrap-expect=3 -data-dir=/tmp/consul -node=consul-176 -client=0.0.0.0 -bind=192.168.31.176 -datacenter=dc1 -join 192.168.31.175
Service-side 192.168.31.177 execution
> consul agent -server -ui -bootstrap-expect=3 -data-dir=/tmp/consul -node=consul-177 -client=0.0.0.0 -bind=192.168.31.177 -datacenter=dc1 -join 192.168.31.175
Note: Because it is a cluster installation, bootstrap-expect=3, the number of service-side will prevail
DATACENTER=DC1, three must be in a data center
176 and 177 of the start command, there is a sentence -join 192.168.31.175 = With this sentence, the 176 and 177 were added to the cluster of 175.
After launch, the cluster begins the process of vote (voting for leader)
Commands: View individual server scenarios:
> consul members
?
Command: View the status of all currently consul roles:
> consul operator raft list-peers
?
3. View cluster status through the UI
Consul not only provides a rich command view of the cluster situation, but also provides a webui, the default port of 8500, which we can access by accessing this URL (eg. http://192.168.31.175:8500) Get the WebUI as shown:
?
3, analog leader hang off, see the consul cluster of new elections leader
Directly stop the 192.168.31.175 service, or violence directly shut down the machine
Enter a command to view service status
> consul members
?
?
View the logs or commands for the remaining two nodes to discover that consul-176 was chosen for the new leader
We can also view the status at the time through the UI interface:
?
Although here 192.168.31.175 this original leader node hangs, but as long as more than half of the server (here is 2/3 alive) is still alive, the cluster is working, which is why like Consul, Distributed management components such as zookeeper recommend that we use 3 or 5 nodes for deployment reasons.
Note: the. Netcore project can also be deployed on Consul server, but the official recommendations Consul Client to relate, do their own things, do not affect each other.
4. Consul Client Installation
In order to conserve virtual machines, currently deployed in 192.168.31.178. Netcore Project
> mkdir /data/mvc> mkdir /data/api> cd /data/mvc/> dotnet new mvc> cd /data/api/> dotnet new webapi>dotnet run
Start and run Mvc,webapi two items, ensure normal access, normal access
?
5, will the. Netcore service is registered with Consul ( registering a service with a configuration file )
vi/etc/consul/services_config.json{"Services": [{"id": "client_service_01", "name": "M Vcclientservice "," tags ": [" urlprefix-/mvcclientservice01 "]," address ": "192.168.31.178", "Port": "Checks": [{"Name": "Clientservi Ce_check "," http ":" http://192.168.31.178:5000 "," Interval ":" 10s ", "Timeout": "5s"}]}, {"id": "client_service_02", "Nam E ":" Apiclientservice "," tags ": [" Urlprefix-/apiclientservice02 "]," add Ress ":" 192.168.31.178 "," Port ":" Checks ": [{" Name ":" Clie Ntservice_check "," http ":" Http://192.168.31.178/api/values "," Interval ":" 10s ", The timeout":" 5s "}]}]}
Run the command in Consul Client 192.168.31.178:
consul agent -config-dir=/etc/consul -data-dir=/tmp/consul -node=consul-178 -client=0.0.0.0 -bind=192.168.31.178 -datacenter=dc1 -join 192.168.31.175
As indicated by normal startup and add 192.168.31.178 to the service cluster 192.168.31.175
?
6. View Consul cluster status
You can see that 192.168.31.178 is added to the cluster, which is normal and can be seen. Netcore Two services Oh, that's normal.
?
Consul not only provides service registration, but also provides service discovery, and we can discover the IP and port of the service by invoking its provided API.
?
Iv. Summary and follow-up work
This article builds a consul service governance component based on a minimized cluster, registers the ASP. NET Core API program with Consul (through profile registration), and attempts to perform service discovery through consul. Hope to organize this article to help you some, but also hope that you put the. NET core application, in the future can run on Linux and Docker, I hope that everyone to achieve the goal early.
Later I will continue to try to build the API gateway based on Ocelot, which will be combined with Consul for further integration. In addition, an attempt is made to Polly for a fuse downgrade, Identity server Authentication
Exceptionless for distributed log Open source framework, LOG4NET,NLOG,AUTOFAC attribute injection,Consul API Interface Service registration instance and other open source address: Https://github.com/hailang2ll/DMS
?
. Netcore Consul implementing Service registration and Discovery-cluster deployment