. NET core MicroServices based on consul for service governance

Source: Internet
Author: User
Tags cas zookeeper server hosting

First, Consul basic introduction

Consul is an open source tool launched by Hashicorp to implement service discovery and configuration for distributed systems. Compared with other distributed service registration and discovery schemes, such as Airbnb's Smartstack, Consul's solution is more "one-stop", with 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.), it is easier to use.

Consul is implemented with Golang and therefore has natural portability (support for Linux, Windows, and Mac OS X); the installation package contains only one executable file for easy deployment and works seamlessly with lightweight containers such as Docker.

More about the consul, such as the advantages, here will not repeat, the Internet a search can be found everywhere. However, a comparison with other similar software must be posted:

In addition, the architecture of the consul and related roles, as shown in:

To use the services provided by Consul to realize the registration and discovery of services, we need to establish consul Cluster. In the consul scenario, the consul agent is deployed and run on each node that provides the service, and all the collections that run the Consul agent node constitute consul Cluster. 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 top of cluster. The consul agent node running in server mode is used to maintain the state of the consul cluster, and the official recommendation is that each consul cluster at least 3 or more agent,client nodes running in server mode.

Consul supports multi-data centers, and consul cluster in each data center selects a leader node in the agent node running in server mode, which is ensured by Consul protocol, The consul data information on the server node is strongly consistent. The Consul Agent node in client mode is relatively simple and stateless, only responsible for forwarding requests to the Server Agent node.

Second, consul cluster construction 2.1 environment preparation

Here I have three Linux (CentOS) virtual machines and one Windows Server R2 virtual machine built with VMware Workstation as shown in.

Among them, 192.168.80.100 will act as the leader role, and the remaining two 192.168.80.101 and 192.168.80.102 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.80.71 will act as the client role and ping each other three virtual machines.

2.2 Downloads Consul

Consul download is very simple, go directly: https://www.consul.io/downloads.html Select the corresponding platform.

Here, our Linux virtual machine chooses the Linux version:

  

After the download is a zip file, we can transfer it to our Linux node via tools such as XFTP.

The Windows Server virtual machine chooses the version of Windows and does not repeat it.

2.3 Installation and Configuration Consul 1. Unzip Consul.zip:

Extract the commands in the Santai node, respectively:

> Unzip Consul_1.1.0_linux_386.zip

After decompression, copy the consul to our custom file directory, for example:/usr/local/consul

> CP Consul/usr/local/consul

2. Setting Environment variables

Set the environment variables in the Santai node, respectively:

> Vim/etc/profile

Add a line of Consul_home to the profile and change the path:

# Consul

Export Consul_home=/usr/local/consul

Export path= $PATH: $JAVA _home/bin:$CONSUL _home;

Make configuration effective

> Source/etc/profile

Test is in effect, test input consul on three nodes

> Consul

When you see the command prompt shown, it represents OK.

3. Start server (s)

Execute the following command on the Santai node, respectively, to start the consul

192.168.80.100>consul Agent-server-ui-bootstrap-expect=3-data-dir=/tmp/consul-node=consul-1-client=0.0.0.0- Bind=192.168.80.100-datacenter=dc1

192.168.80.101>consul Agent-server-ui-bootstrap-expect=3-data-dir=/tmp/consul-node=consul-2-client=0.0.0.0- Bind=192.168.80.101-datacenter=dc1-join 192.168.80.100

192.168.80.102>consul agent-server-ui-bootstrap-expect=3-data-dir=/tmp/consul-node=consul-3-client=0.0.0.0- Bind=192.168.80.102-datacenter=dc1-join 192.168.80.100

Note that in the start command for 101 and 102, there is a sentence of -join 192.168.80.100 = 101 and 102 are added to the cluster where 100 is located.

After launch, the cluster begins the process of vote (voting for leader), which can be seen by the following command:

  

Start on a Windows Server virtual machine:

> Consul agent-bind 0.0.0.0-client 192.168.80.71-data-dir=c:\counsul\tempdata-node EDC. DEV. Webserver-join 192.168.80.100

After starting, you will be prompted with the following:

  

4. Viewing the cluster 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.80.100:8500) Get the WebUI as shown:

You can see that three nodes are starting normally, so let's try registering our Webapi service with Consul based on ASP.

5. Simulate leader hang up and view new elections for consul cluster leader

I'm a little violent here. Shut down the leader node directly: Shutdown-h Now, you can see that our 80.100 has been hung.

  

Looking at the logs for the remaining two nodes, Consul-3 (80.102) was selected for the new leader:

  

Of course, the WebUI can also be viewed by 80.101 or 102:

You can also view the current role status of each server by using the following command:

> Consul operator Raft list-peers

  

Although here 80.100 of the 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 the distributed management components like Consul, zookeeper recommend us to use 3 or 5 nodes for the reason of deployment.

ASP. NET Core WEBAPI Service Registration 3.1 preparing an ASP. NET Core WEBAPI Program

Step1. Creating an ASP. NET Core Webapi Program

  

Step2. Creating a Healthcontroller health check for consul

    [Produces ("application/json")]    [Route ("api/health")]      Public class Healthcontroller:controller    {        [httpget]        public iactionresult Get () = OK ("OK"  );    }

*. Consul will confirm the health status of the service by using the call API .

Step3. Rewriting the startup code, invoking the Consul API registration service

(1) Installing the consul. NET client through NuGet

Pm> Install-package Consul

(2) Write an extension method based on Iapplicationbuilder to invoke the consul API

     Public Static classappbuilderextensions { Public StaticIapplicationbuilder Registerconsul ( Thisiapplicationbuilder app, Iapplicationlifetime lifetime, serviceentity serviceentity) {            varConsulclient =NewConsulclient (x = x.address =NewUri ($"Http://{serviceentity.consulip}:{serviceentity.consulport}"));//Consul Address to request registration            varHttpcheck =NewAgentservicecheck () {Deregistercriticalserviceafter= Timespan.fromseconds (5),//how long after the service starts to registerInterval = Timespan.fromseconds (Ten),//Health check interval, or heartbeat intervalHTTP = $"Http://{serviceentity.ip}:{serviceentity.port}/api/health",//Health Check AddressTimeout = Timespan.fromseconds (5)            }; //Register Service with consul            varRegistration =Newagentserviceregistration () {Checks=New[] {httpcheck}, ID=Guid.NewGuid (). ToString (), Name=Serviceentity.servicename, Address=Serviceentity.ip, Port=Serviceentity.port, Tags=New[] { $"Urlprefix-/{serviceentity.servicename}"}//add tag tags in urlprefix-/servicename format for Fabio identification            }; ConsulClient.Agent.ServiceRegister (registration). Wait ();//when the service starts up, the internal implementation is actually registered using the Consul API (HttpClient initiated)Lifetime. Applicationstopping.register (() ={consulClient.Agent.ServiceDeregister (registration.id). Wait ();//Cancel Registration When the service is stopped            }); returnapp; }

*. here is a reference to the Pastoral Cricket Code, the link to see the Resources section .

(3) In the Configure method of the Starup class, call this extension method

        //This method gets called by the runtime. Use this method to configure the HTTP request pipeline.         Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env, iapplicationlifetime lifetime) {if(env. Isdevelopment ()) {app.            Usedeveloperexceptionpage (); } app.            Usemvc (); //Register this serviceServiceentity serviceentity =Newserviceentity {IP=networkhelper.localipaddress, Port= Convert.ToInt32 (configuration["Service:port"]), ServiceName= configuration["Service:name"], Consulip= configuration["Consul:ip"], Consulport= Convert.ToInt32 (configuration["Consul:port"])            }; App.        Registerconsul (lifetime, serviceentity); }

Where the Serviceentity class is defined as follows:

     Public classserviceentity { Public stringIP {Get;Set; }  Public intPort {Get;Set; }  Public stringServiceName {Get;Set; }  Public stringConsulip {Get;Set; }  Public intConsulport {Get;Set; } }
View Code

It uses the Appsettings.json configuration file, which is defined as follows:

{"  Service": {    "Name": "CAS.NB". ClientService ",    " port ":" 8810 "  },  " Consul ": {    " IP ":" 192.168.80.101 ",    " port ":" 8500 "  },  "Logging": {    "includescopes": false,    "Debug": {      "LogLevel": {        "Default": "Warning "      }    },    " Console ": {"      LogLevel ": {        " Default ":" Warning "      }  }}}
View Code

*. This code is no longer explained and can be understood at a glance. In addition, in addition to calling the Consul API, you can also configure the file, such as the following configuration file format, which is no longer demonstrated here.

{"  service": {    "name": "Hwapp_web",    "tags": ["Master"],    "address": "10.9.10.173",    "port": 5000,    "Checks": [      {        "http": "Http://10.9.10.173:5000/health",        "interval": "10s"      }    ]}  }
View Code

Step4. Keep the Valuescontroller created by default, and the rest no longer create any APIs, not the focus of this experiment. Of course, you can integrate the swagger so that there is an interface document to look at.

Here I jump to Healthcontroller by default:

  

3.2 Publishing to IIS

Step1. Publishing in a. NET core program is simple, either by creating a profile in VS or using the command line (for example: Dotnet publish), where I publish in VS, get the release file

Step2. Copy to a Windows Server virtual machine by using the FTP tool

Step3. Here my Windows Server virtual machine is R2, need to install a Windows Server Hosting,: Point me to point me. If you do not install this, your IIS is not running up. NET core programs.

Step4. Add a Web site (service) to IIS in a way that you are familiar with:

  

Step5. Change the default application pool version of the. NET Framework to "unmanaged code".

  

Step6. Supposedly, get here OK, click Browse Access, TMD, gave me a 502.5 error

  

As a result of a search on the internet, found to need to make a patch (WINDOWS6.1-KB2533623-X64.MSU): Point me to point me.

Step7. After installing the patch, restart IIS to successfully access the health API, which ensures that consul can call to our service.

3.3 View Consul cluster status

Step1. Visit Consul WebUI to see if the service is registered successfully: you can see that our ClientService has successfully registered

Step2.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.

Url>http://192.168.80.100:8500/v1/catalog/service/cas.nb.clientservice

*. we can see that the ClientService serviceaddress and Serviceport are returned, and the service invocation can be made through its constituent URLs. of course, we may deploy multiple instances of a service to form a cluster for load balancing. We can set up some load balancing strategies, assuming that a service address is randomly selected and returned to the service consumer through modulo operations .

Step3. Here we will publish the release file on the Windows Server virtual machine copy a copy, and change the configuration file, Change its servicename to CAS.NB.ProductService, its port to 8820, add a Web site to IIS, start up, and view Consul cluster status through WebUI:

Step4. Then we discover the service through the following APIs:

Url>http://192.168.80.100:8500/v1/catalog/service/cas.nb.productservice

  

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 to Consul, and attempts to perform service discovery through Consul (although no specific service consumption is simulated). This article did not carefully tell Consul's introduction, advantages, shortcomings, because I do not have any practical experience, so can only stand on the shoulders of other friends to do a small experiment. ASP. NET Core is a natural technology for micro-services, but also in the hope that with our learning and promotion, let the company to put the "."-core application, will be able to run on Linux and Docker, this is my current goal, with everyone.

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, you will try to Polly the fuse downgrade, Identity server Authentication, exceptionless as a distributed log, and basically follow the. NET Core Captain Zhang Shanyu's poc-nanofabric used (or gave us an Amway technical framework) Those open source technologies to build an initial microservices architecture that could later be promoted and applied within the company. In addition, considering that the company's current microservices architecture is Java-based (Spring Cloud), it will also consider Steeltoe to be compatible with spring cloud so that Java and. Net core microservices can coexist (7th in reference, The pastoral crickets). Here, too, Amway is learning microservices. NET coder, you can look at Zhang Shanyu Teacher's nanofabric, or Yang Zhengko Teacher's. NET core micro-service course.

  

Recently a lot of friends asked why I no longer write technical text, always write some feedback, reading, and so on, one because read those books is also my personal okr this year, and the other six months did not want to read technical books and materials, and finally because of physical reasons can not stay up too late. Now learning. NET Core has been deeply attracted by it, like to find a girl that we chased after the year, think it is all beautiful, want to go on with it to continue to find it more beautiful, so will also take advantage of micro-service learning to share more articles with you.

  

Accessories download

Sample code: Click I download

Resources

(1) Pastoral crickets, "Docker & Consul & Fabio & ASP." Cross-platform practices

(2) Not small, "service discovery-Introduction, deployment and use of consul"

(3) Erhu trough, "I am the consul of services-consul introduction to service discovery and registration Tools"

(4) chief officer, "Consul distributed cluster Building & Simple function Testing & failure Recovery"

(5) 94cool, "Win2008server R2 x64 deploying. NET Core to IIS"

(6) Yang Zhengko ". NET core micro-service Courseware"

(7) Pastoral crickets, ". NET Core microservices Architecture Steeltoe use (based on Spring Cloud)"

Zhou Xurong

Source: http://edisonchou.cnblogs.com

The copyright of this article is owned by the author and the blog Park, welcome reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to give the original link.

. NET core MicroServices based on consul for service governance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.