一、建立.net core web程序並運行node
二、在Consul中註冊該服務linux
Consul支持兩種服務註冊的方式,一種是經過Consul的服務註冊HTTP API,由服務自身在啓動後調用API註冊本身,另一種則是經過在配置文件中定義服務的方式進行註冊。Consul文檔中建議使用後面一種方式來作服務 配置和服務註冊。Consul agent在啓動時能夠經過-config-dir來指定配置文件所在目錄(引用自網絡)web
[root@linux5 ~]# consul agent -data-dir=/tmp/consul/ -node=consul5 -dc=dc1 -bind=192.168.56.105 -ui -config-dir=/tmp/consul/
在Consul啓動時會讀取-config-dir目錄下全部的json文件來加載註冊的服務,如今在該目錄下建立一個配置文件:jw.jsonjson
{ "service": { "name": "jw", "tags": ["jiewus"], "address": "192.168.56.1", "port": 1234, "checks": [ { "http": "http://192.168.56.1:1234/Home/About", "interval": "10s" } ] } }
重啓Consul以後會發現jw這個服務:網絡
2017/05/10 13:59:37 [INFO] serf: EventMemberJoin: consul3 192.168.56.103 2017/05/10 13:59:37 [INFO] serf: EventMemberJoin: consul2 192.168.56.102 2017/05/10 13:59:37 [INFO] serf: EventMemberJoin: consul1 192.168.56.101 2017/05/10 13:59:37 [INFO] serf: Re-joined to previously known node: consul1: 192.168.56.101:8301 2017/05/10 13:59:37 [INFO] consul: adding server consul3 (Addr: tcp/192.168.56.103:8300) (DC: dc1) 2017/05/10 13:59:37 [INFO] consul: adding server consul2 (Addr: tcp/192.168.56.102:8300) (DC: dc1) 2017/05/10 13:59:37 [INFO] consul: adding server consul1 (Addr: tcp/192.168.56.101:8300) (DC: dc1) 2017/05/10 13:59:38 [INFO] agent: Synced service 'jiewusWeb' 2017/05/10 13:59:41 [INFO] agent: Synced check 'service:jiewusWeb' ==> Newer Consul version available: 0.8.2 (currently running: 0.8.1)
三、使用Consul Api進行服務註冊tcp
private static void ServiceRegister(ConsulClient client) { var client = new ConsulClient(ConfigurationOverview); var result = client.Agent.ServiceRegister(new AgentServiceRegistration() { Address = "http://192.168.56.1:1234/Home/About", ID = "jw", Name = "jw", Port = 1234, Tags = new[] { "jiewus" }, Check = new AgentServiceCheck() { HTTP = "http://192.168.56.1:1234/Home/About", Interval = new TimeSpan(0, 0, 10), DeregisterCriticalServiceAfter = new TimeSpan(0, 1, 0), } }).Result; } private static void ConfigurationOverview(ConsulClientConfiguration obj) { obj.Address = new Uri("http://192.168.56.104"); obj.Datacenter = "dc1"; }