Explain how to use aspnetcore API and consumer under Windows

Time:2019-12-4

1. Concept: what is consumer

Consul is a whole composed of multiple components, which functions as Eureka and zookeeper, and is used for service discovery and governance.

Features of consul:

1. Service discovery: consumer can provide registered services to users, or actively discover services. In the client interface of consumer, users can get service information, such as IP port and other information. In the client interface, it is easy to find registered services,

2. Health check: here, health check is to check whether the service is running well. Here, you can judge whether the service is running well by using the status of web service or the utilization status of the server. You can judge those services that are running well and those that are not healthy according to these statuses () here, the utilization rate of the server is not healthy )According to these judgments, the route is far away from unhealthy services.

3. Kvstore: it is the storage of key / value. In use, many things can be done according to the storage function of kV, such as dynamic configuration, marking of some features, coordination and management election. The API of kV storage is based on HTTP.

4. Multiple data centers: consumer supports multiple data centers, which means that we don’t need to worry about doing more work to expand in the process of consumer.

Consul is a distributed and highly available system. Every service registered with the consumer will run an agents in the service itself. The ultimate purpose of running the agents is to check the health of the current service, so as to inform the consul system of the health of the current node. We have given an example in the feature (health check) of the consumer.

Each data center runs a consular server cluster. When a cross datacenter service discovery or configuration request is made, the local (agents) server forwards the request to the remote datacenter and returns the result.

(in white) the purpose of health check is to determine whether the node service can be used, whether the service of this node is the best of all known nodes, and whether it is the best to replace it. In the process of using each server, it is recommended to simulate multiple scenarios that can lead to service failure and data loss, and then recommend each service as a secure server. In this process, one of the biggest contributions is agents, which realizes the communication with the server.

Learn more: https://www.consumer.io/intro/index.html

II. Installation of consul:

1. Download of consul:

Address: https://www.consumer.io/

 

Click download to enter the download page as follows:

I downloaded the win version and decompressed it. The size is very small

2. Installation and operation:

The installation command is: consul.exe agent dev developer mode installation Ctrl + C can stop running, as shown in the following figure:

The developer mode installation is only applicable to the environment of a single server, not for production, and the developer mode does not save any state.

After the installation is successful, we can see that the output log information contains. Client addr: the client address is 127.0.0.1:8500, and the DNS is 8600. We can access it in the browser,

The results are as follows:

In the page, we can see that our service and consumer will register themselves as a service node by default. The interface shows us their running status, IP server and other information

We can also view the service information through the command

Command: the following figure is the output:

In the figure, we output our own more detailed information, address, status, role, version information

In the later stage, we will add many services. At this time, the output may be more, because the service information is obtained and registered through the communication protocol. In order to avoid the service status in the consumer view and keep consistent with the status on the server, we need to send the service status to the Management (agent) in the consumer through httpapi in combination with the above consumer We can better understand 3kvstore.

Official translation

After you interrupt the agent, you should see it leave the cluster and shut down. By gracefully leaving, the consul informs other cluster member nodes that they have left. If you force the agent process to terminate, other members of the cluster will detect node failures. When a member leaves, its services and checks are removed from the directory. When a member fails, its health status is simply marked as critical, but it is not removed from the directory. The consul will automatically attempt to reconnect to the failed node, allowing it to recover from certain network conditions without contacting the left node. In addition, if the agent is running as a server, elegant vacations are important to avoid interruptions that may affect the availability of the consensus protocol.

3. Simple use of consul:

1. Create a new aspnetcoreapi project:

The following picture:

 

2. Implement registration in the configure method of startup in the project:

 

The code is as follows:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  {
   if (env.IsDevelopment())
   {
    app.UseDeveloperExceptionPage();
   }

   app.UseMvc();
   String IP = configuration ["IP"]; // when deployed to different servers, it cannot be written as 127.0.0.1 or 0.0.0.0, because this is the address for service consumers to call
   Int port = int.parse (configuration ["port"); // get the service port
   Var client = new consulclient (configurationoverview); // callback get
   var result = client.Agent.ServiceRegister(new AgentServiceRegistration()
   {
    Id = "servernamefirst" + guid. Newguid(), // the service number is guaranteed not to be duplicate
    Name = "serverfirst", // the name of the service
    Address = IP, // service IP address
    Port = port, // service port
    Check = new agentservicecheck // health check
    {
     Deregistercriticalserviceafter = timespan. Fromseconds (5), // how long before the service starts
     Interval = timespan.fromseconds (10), // health check interval, or heartbeat interval (regularly check whether the service is healthy)
     HTTP = $"http: // {IP}: {port} / API / health", // health check address
     Timeout = timespan. Fromseconds (5) // service registration time
    }
   });
  }
/// <summary>
  ///Get an instance of consul
  /// </summary>
  /// <param name="obj"></param>
  private static void ConfigurationOverview(ConsulClientConfiguration obj)
  {
   //Address of consumer
   obj.Address = new Uri("http://127.0.0.1:8500");
   //Data center naming
   obj.Datacenter = "dc1";
  }

3. Startup: first, make sure that consul has been started, and we run project registration

The steps are as follows:

After API compilation is successful, we run it on the command line:

Execute command: dotnet project DLL — IP setting IP — port setting port (dotnet framework. Webapi. DLL — IP 127.0.0.1 — port 5001) hit enter to run the project

In the same project, we can run multiple ports when the command is running

Refresh the consumer client and we can see that our service registration is successful

The results are as follows:

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.