This article is a mirror article of machine translation, please click here to jump to the original article.

View: 55716|Reply: 3

[Source] 【Architecture】ASP.NET Core is highly available based on Consul services

[Copy link]
Posted on 2020-10-16 18:33:07 | | | |
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.

[Architecture]. net/c# implements distributed configuration of Key/Value store based on Consul
https://www.itsvse.com/thread-9420-1-1.html

Architecture: ASP.NET Core dynamically configures hot updates based on Consul
https://www.itsvse.com/thread-9421-1-1.html


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, pleaseReply




Previous:Architecture: ASP.NET Core dynamically configures hot updates based on Consul
Next:DMP file import is exceptional
Posted on 2020-11-30 19:57:38 |
w174w174w174
Posted on 2021-7-23 09:31:37 |
Thanks for sharing
Posted on 2021-9-3 09:05:16 |
software, import tens of thousands of emails, do not fake death, do not freeze!!!
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com