Consul service discovery and registration

consul start

dos command 

consul path \ consul agent -dev

Service Registration 

bin directory dll file --urls dotnet project file = "http: // *: 5726" --ip = "127.0.0.1" --port = 5726  

urls release address port

ip console received parameters

console port received parameters

consul console fixed port  HTTP: // localhost: 8500 /

Registration Service code is as follows:

  string ip = configuration["ip"] ?? "Localhost";
            int port = string.IsNullOrWhiteSpace(configuration["port"]) ? 44344 : int.Parse(configuration["port"]);
            IConsulClient client = new ConsulClient(obj =>
            {
                obj.Address = new Uri("http://127.0.0.1:8500");//注册地址
                = obj.Datacenter " DC1 " ; // Register content name 
            }); 
            the Task <WriteResult> Result = client.Agent.ServiceRegister ( new new AgentServiceRegistration () 
            { 
                ID = " apiserviceTest_ " + Guid.NewGuid (), // service number, can not repeat 
                the name = " apiserviceTest " , // service name - with future calls is this 
                Address = ip, 
                Port = Port, 
                Tags = new new String [] {}, // can be used to set the weight 
                the Check = new new AgentServiceCheck () 
                { 
                    DeregisterCriticalServiceAfter = TimeSpan.FromSeconds ( . 5 ), // service long stopped unregister 
                    the Interval = TimeSpan.FromSeconds ( 10 ), // health check time interval, referred to as heartbeat interval or 
                    the HTTP $ = " HTTP: // {IP}: {Port} / API / the Value " , // health check address, 
                    the Timeout TimeSpan.FromSeconds = ( . 5 ) 
                } 
            });

Registration starts accept parameters such as accepting ip port, etc.

public static void Main(string[] args)
        {
            ///允许控制台命令
            var config = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddCommandLine(args)
                .Build();
            CreateHostBuilder(args)
                .Build()
                .Run();
        }

  

 

Guess you like

Origin www.cnblogs.com/zxp6/p/11566766.html