Simple use of. NET CORE and Consul under Windows

Source: Internet
Author: User

1) Download

https://www.consul.io/

2) Setting Environment variables

Copy the file path corresponding to the. exe in the environment variable path

3) Start the service in CMD

Consul Agent-dev

4) Open vs

5) New Web API Project

6) Under Startup.cs, modify the Configure function as follows

    Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env) {if(env. Isdevelopment ()) {app.            Usedeveloperexceptionpage (); } app.            Usemvc (); String IP= configuration["IP"];//when deploying to a different server, you cannot write 127.0.0.1 or 0.0.0.0 because this is the address that the service consumer calls            intPort =int. Parse (configuration["Port"]);//Get Service Port            varClient =NewConsulclient (Configurationoverview);//Callback gets            varresult = client. Agent.serviceregister (Newagentserviceregistration () {ID="Servernamefirst"+ Guid.NewGuid (),//Service number guaranteed not to repeatName ="Serverfirst",//name of the serviceAddress = IP,//Service IP AddressPort = port,//Service PortCheck =NewAgentservicecheck//Health Check{Deregistercriticalserviceafter= Timespan.fromseconds (5),//how long after the service is started, anti-registrationInterval = Timespan.fromseconds (Ten),//Health check interval, or heartbeat interval (the health of the Timing check service)HTTP = $"Http://{ip}:{port}/api/health",//Health Check AddressTimeout = Timespan.fromseconds (5)//time of registration of the service                }            }); }
<summary>////        Get an instance of Consul///</summary>//        <param name= "obj" ></param>        private static void Configurationoverview (Consulclientconfiguration obj)        {            //consul the address of            obj. Address = new Uri ("http://127.0.0.1:8500");            Data center named            obj. Datacenter = "DC1";        }

7) Set up the Health check controller HealthController.cs

    [Route ("Api/[controller]")]    public class Healthcontroller:controller    {        [httpget] public        iactionresult Get () = OK ("OK");    }

8) Build the project

9) Open the project corresponding folder->bin->debug->netcoreapp2.0 path on the input cmd directly into the corresponding directory and open cmd

10) Run command dotnet PeerApi.dll--ip localhost--port 14446 explanation: PeerApi.dll Replace with your DLL localhost instead of IP address note: If you do not put on IIS, enter the LO Calhost instead of 127.0.0.1 other IP addresses 14446 is the port number to replace your ports

11) Open Http://localhost:8500/ui/dc1/services "Consul's own monitoring page" to see the specific registered services

12) Service Discovery

Later

Simple use of. NET CORE and Consul under Windows

Related Article

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.