In the development of Internet projects, it is necessary to interact with various services, and users obtain data from the server, and the high availability of service interfaces is particularly important, especially in e-commerce and payment systems, where high availability also measures whether the system is robust. In the case of clusters, many services are also difficult to maintain.
Consul review
Consul is an open-source tool developed in Go language launched by HashiCorp, which is used to realize service discovery and configuration of distributed systems, with built-in service registration and discovery framework, distribution consistency protocol implementation, health check, key/value storage, and multi-data center solutions, which is relatively simple to use.
Consul service discovery
How to use Consul Service Discovery:
- Run a Consul agent in Client Mode on each computer, which is only responsible for efficiently exchanging the latest registration information with the Consul Cluster (not participating in the leader election).
- The service service on each computer registers the service name and the URL of the service with the local consul agent
- When a program deployed on Computer1 needs to call ServiceB, the program ServiceA directly obtains the access address of ServiceB from the local Consul Agent through the service name, and then directly makes a request to the URL of ServiceB.
The code is as follows:
In the Startup class, call as follows:
Health check-up
The health check interface only needs to return a 200 status code to consul, and the code is as follows:
Start the project, sign up for the service
The controller can define a test method as follows:
Let's go to the bin directory of the project and start it through the command line, the code is as follows:
View the link via your browser:
http://127.0.0.1:8500/ui/dc1/services/test.itsvse
http://127.0.0.1:8500/v1/catalog/service/test.itsvse
The json data is as follows:
[ { "ID": "bb644359-6b2a-a27e-7a0a-a1950b8e515f", "Node": "DESKTOP-EB7B69D", "Address": "127.0.0.1", "Datacenter": "dc1", "TaggedAddresses": { "lan": "127.0.0.1", "wan": "127.0.0.1" }, "NodeMeta": { "consul-network-segment": "" }, "ServiceKind": "", "ServiceID": "507338bf-a5a6-4013-9675-c43a40a2c1a0", "ServiceName": "test.itsvse", "ServiceTags": [ "test", "itsvse" ], "ServiceAddress": "127.0.0.1", "ServiceWeights": { "Passing": 1, "Warning": 1 }, "ServiceMeta": {}, "ServicePort": 8083, "ServiceEnableTagOverride": false, "ServiceProxy": { "MeshGateway": {}, "Expose": {} }, "ServiceConnect": {}, "CreateIndex": 1317, "ModifyIndex": 1317 }, { "ID": "bb644359-6b2a-a27e-7a0a-a1950b8e515f", "Node": "DESKTOP-EB7B69D", "Address": "127.0.0.1", "Datacenter": "dc1", "TaggedAddresses": { "lan": "127.0.0.1", "wan": "127.0.0.1" }, "NodeMeta": { "consul-network-segment": "" }, "ServiceKind": "", "ServiceID": "bb21f150-7219-4eda-bc91-54686a750228", "ServiceName": "test.itsvse", "ServiceTags": [ "test", "itsvse" ], "ServiceAddress": "127.0.0.1", "ServiceWeights": { "Passing": 1, "Warning": 1 }, "ServiceMeta": {}, "ServicePort": 8082, "ServiceEnableTagOverride": false, "ServiceProxy": { "MeshGateway": {}, "Expose": {} }, "ServiceConnect": {}, "CreateIndex": 1314, "ModifyIndex": 1314 }
]
Consul service call
Create a new .NET Core console project with the following code:
I am randomly calling the registered test.itsvse service and the result is as follows:
Finally, source code download:
Tourists, if you want to see the hidden content of this post, please Reply |