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