In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you the .NET Core micro-service based on Consul how to achieve service governance, I hope you will learn something after reading this article, let's discuss it together!
1. Basic introduction of Consul
Consul is an open source tool developed by HashiCorp, which is used to realize service discovery and configuration in distributed systems. Compared with other distributed service registration and discovery solutions, 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.), and it is easy to use.
Consul is implemented in Golang, so it is naturally portable (supports Linux, windows, and Mac OS X); the installation package contains only one executable file, which is easy to deploy and works seamlessly with lightweight containers such as Docker.
More information about Consul, such as its advantages, will not be repeated here. You can find it everywhere with a search on the Internet. However, you must post a comparison with other similar software:
In addition, with regard to the architecture of Consul and related roles, as shown in the following figure:
To implement the registration and discovery of services using the services provided by Consul, we need to establish a Consul Cluster. In the Consul scenario, Consul's Client Agent is deployed and run on each node that provides services, and the collection of all running Consul Agent nodes forms the Consul Cluster. Consul Agent has two modes of operation: Server and Client. The Server and Client here are only the distinction at the Consul cluster level and have nothing to do with the application services built on top of Cluster. Consul Agent nodes running in Server mode are used to maintain the state of Consul clusters. It is officially recommended that there are at least 3 or more Agent,Client nodes running on Server Mode per Consul Cluster.
Consul supports multiple data centers, and the Consul Cluster of each data center selects a Leader node among the Agent nodes running in Server mode. The election process is guaranteed by the raft protocol implemented by Consul, and the Consul data information on multiple Server nodes is strongly consistent. The Consul Agent node in Client Mode is relatively simple and stateless, and is only responsible for forwarding the request to the Server Agent node.
2. Prepare the environment for setting up Consul cluster
Here I have prepared three Linux (CentOS) virtual machines and one Windows Server 2008 R2 virtual machine, which are built with VMware Workstation, as shown in the following figure.
Among them, 192.168.80.100 will be the leader role, and the other two 192.168.80.101 and 192.168.80.102 will be the follower role. Of course, in the real world, the leader role will not be fixed, and the new leader will be selected by the algorithm as the environment changes (such as Leader downtime or loss of contact). Before doing the following, make sure that the three nodes can ping each other and ping with the host. In addition, 192.168.80.71 will play the client role and ping each other with the remaining three virtual machines.
2.2 download Consul
Downloading Consul is easy, go directly: https://www.consul.io/downloads.html can choose the corresponding platform.
Here our linux virtual machine chooses the Linux version:
After downloading, there is a zip file, which we can transfer to our linux node through tools such as XFtp.
The Windows Server virtual machine chooses the Windows version, which will not be discussed in detail.
2.3 install and configure Consul 1. Extract the Consul.zip:
Decompress and decompress commands in three nodes:
> unzip consul_1.1.0_linux_386.zip
After decompression, copy consul to our custom file directory, such as / usr/local/consul
> cp consul / usr/local/consul
two。 Set environment variabl
Set the environment variables in the three nodes:
> vim / etc/profile
Add a line of CONSUL_HOME to profile and change the PATH:
# Consul
Export CONSUL_HOME=/usr/local/consul
Export PATH=$PATH:$JAVA_HOME/bin:$CONSUL_HOME;
Make the configuration effective
> source / etc/profile
Test whether it works or not, test the input consul on three nodes
> consul
When you see the command prompt shown in the following figure, it represents OK.
3. Start Server (s)
Execute the following command on each of the three nodes to start 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
Notice that in the startup command of 101and102, there is a sentence-join 192.168.80.100 = > that adds 101and102to the cluster where the100s are located.
After startup, the cluster starts the process of Vote (voting for Leader). You can see the situation of the cluster through the following command:
Start on the 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 startup, you will be prompted as follows:
4. View clusters through UI
Consul not only provides rich commands to view the cluster, but also provides a WebUI with a default port of 8500. We can access this URL (eg. http://192.168.80.100:8500) to get the WebUI shown in the following figure:
You can see that all three nodes start up normally, so let's try to register our ASP.NET Core-based WebAPI service with Consul.
5. Simulate Leader hanging up to view the new election Leader of the Consul cluster
Here I violently shut down the Leader node directly: shutdown-h now, and you can see that our 80.100 is dead.
Looking at the logs of the remaining two nodes, we can see that consul-3 (80.102) has been selected as the new leader:
Of course, you can also view it through the WebUI of 80.101 or 102:
You can also view the current role status of each Server with the following command:
> consul operator raft list-peers
Although 80.100 of the original leader node here is dead, the cluster can work as long as more than half of the Server (here is 2 Server 3 is still alive), which is why distributed management components such as Consul and ZooKeeper recommend that we use 3 or 5 nodes for deployment.
Third, ASP.NET Core WebAPI service registration 3.1 prepares an ASP.NET Core WebAPI program
Step1. Create an ASP.NET Core WebAPI program
Step2. Create a HealthController for the health check of Consul
[Produces ("application/json")] [Route ("api/Health")] public class HealthController: Controller {[HttpGet] public IActionResult Get () = > Ok ("ok");}
* .Consul uses call as an API to confirm the health status of Service.
Step3. Rewrite the startup code and call the Consul API registration service
(1) install the .NET client of Consul through Nuget
PM > install-package Consul
(2) write an extension method based on IApplicationBuilder to call Consul API
Public static class AppBuilderExtensions {public static IApplicationBuilder RegisterConsul (this IApplicationBuilder app, IApplicationLifetime lifetime, ServiceEntity serviceEntity) {var consulClient = new ConsulClient (x = > x.Address = new Uri ($"http://{serviceEntity.ConsulIP}:{serviceEntity.ConsulPort}"));) / / Consul address var httpCheck = new AgentServiceCheck () {DeregisterCriticalServiceAfter = TimeSpan.FromSeconds (5), / / how long after the service starts, register Interval = TimeSpan.FromSeconds (10), / / Health check interval Or heartbeat interval HTTP = $"http://{serviceEntity.IP}:{serviceEntity.Port}/api/health",// health check address Timeout = TimeSpan.FromSeconds (5)} / / Register service with consul var registration = new AgentServiceRegistration () {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 So that Fabio can recognize} ConsulClient.Agent.ServiceRegister (registration) .Wait (); / / when the service starts, the internal implementation actually registers using Consul API (initiated by HttpClient) lifetime.ApplicationStopping.Register (() = > {consulClient.Agent.ServiceDeregister (registration.ID). Wait (); / cancel registration} when the service stops); return app;}
*。 Here is a reference to the Code of pastoral crickets, see the Resources section for links.
(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 void Configure (IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime lifetime) {if (env.IsDevelopment ()) {app.UseDeveloperExceptionPage ();} app.UseMvc () / / register this service ServiceEntity serviceEntity = new ServiceEntity {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);}
The ServiceEntity class is defined as follows:
View Code
The appSettings.json configuration file is used, which is defined as follows:
View Code
*。 This code is no longer explained and can be understood at a glance. In addition, in addition to calling Consul API, you can also use a configuration file, such as the following configuration file format, which is not demonstrated here.
View Code
Step4. Keep the default created ValuesController, and the rest will not create any API, which is not the focus of this lab. Of course, you can integrate Swagger so that you can have an interface document to view.
Here I jump to healthcontroller by default:
3.2 publish to IIS
Step1. It is easy to publish in a .NET Core program, either using the configuration file originally created in VS or using the command line (for example: dotnet publish). Here I still publish in VS and get the Release file.
Step2. Copy to the Windows Server virtual machine through the Ftp tool
Step3. Here my Windows Server virtual machine is 2008 R2, need to install a Windows Server Hosting, download address: click me. If you do not install this, your IIS will not be able to run .NET Core programs.
Step4. Add a website (service) to IIS in a familiar way:
Step5. Change the. net framework version of the default application pool to unmanaged code
Step6. According to reason, this is the OK. Click Browse to visit, TMD, and give me a 502.5 error.
So after a search on the Internet, I found that I needed to make a patch (Windows6.1-KB2533623-x64.msu). Download address: click me.
Step7. After installing the patch, restart IIS, and you can successfully access = > make sure that Consul can call to the health API of our service.
3.3View Consul cluster status
Step1. Visit Consul's WebUI to check whether the service has been registered successfully: you can see that our ClientService has been successfully registered
Step2.Consul provides not only service registration, but also service discovery, and we can discover the IP and Port of the service by calling the API provided by it.
Url > http://192.168.80.100:8500/v1/catalog/service/CAS.NB.ClientService
*。 We can see the ServiceAddress and ServicePort that return ClientService, and the service invocation can be made through their composition of URL. Of course, we may deploy multiple instances of a service to form a cluster to achieve load balancing. We can set some load balancing strategies and assume that a service address is randomly selected and returned to the service consumer through modular operation.
Step3. Here, we will copy the published Release file on the Windows Server virtual machine, and change the configuration file to change its ServiceName to CAS.NB.ProductService, its Port to 8820, add another website to IIS, start it, and check the Consul cluster status through WebUI:
Step4. At this point, we will make service discovery through the following API:
Url > http://192.168.80.100:8500/v1/catalog/service/CAS.NB.ProductService
IV. Summary and follow-up work
This article mainly builds a Consul service governance component based on a minimized cluster, registers the ASP.NET Core API program with Consul, and attempts to discover services through Consul (although it does not simulate specific service consumption). This article does not elaborate on the introduction, advantages and disadvantages of Consul, because I do not have any practical experience, so I can only stand on the shoulders of other gardeners to do a small experiment. ASP.NET Core is a technology that is naturally suitable for micro-services, and I hope that with our study and promotion, the company can apply .NET Core and run on Linux and Docker in the future. This is my current goal and share with you.
In the future, I will continue to try to build an API gateway based on Ocelot, which will be further integrated with Consul. In addition, we will also try Polly to fuse and downgrade, Identity Server to verify, and Exceptionless to do distributed logs, which will basically follow the open source technologies used by NanoFabric (or the technical framework for Amway) used by Zhang Shanyou, captain of NET Core, to build a preliminary micro-service architecture for promotion and application within the company in the future. In addition, considering that the company's current microservice architecture is based on Java (Spring Cloud), it will also consider using Steeltoe to be compatible with Spring Cloud, so that Java and .net Core microservices can coexist (see the seventh cricket in Resources). Here, Amway is also studying micro-service. NET Coder, you can take a look at teacher Zhang Shanyou's NanoFabric, or teacher Yang Zhongke's. NET Core micro-service course.
After reading this article, I believe you have a certain understanding of ".NET Core Micro Services how to implement service governance based on Consul". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
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.