1:經過配置文件的方式靜態註冊
2:經過HTTP API接口來動態註冊(spring cloud使用方式,spring cloud中使用的是consul api)
3:使用consul client或consul api(程序)實現服務的註冊和發現(Java非spring boot,cloud項目)java
1.1.一、建立文件夾/etc/consul.d,node
說明:.d表示一系列配置文件的存放目錄(directory)web
1.1.二、建立服務並寫入上述文件夾中的一個文件spring
說明:json
注意:若是上述文件夾沒有權限進行操做,先改變文件夾權限,bootstrap
我在window上,演示:ubuntu
1.1.三、先啓動consul進程,帶上config-dir參數windows
切換屏幕-->api
window上:D:\soft\worksoft\consul_1.0.6_windows_amd64>consul agent -dev -config-dir etc/consul.d/數組
說明:
直接調用/v1/agent/service/register接口註冊便可,須要注意的是:http method爲PUT提交方式
如:
curl -X PUT -d '{"id": "jetty","name": "jetty","address": "192.168.1.200","port": 8080,"tags": ["dev"],"checks": [{"http": "http://192.168.1.104:9020/health","interval": "5s"}]}' http://192.168.1.100:8500/v1/agent/service/register
注意,這種方式,和上面的註冊方式有一點不同,body的參數,是上面service的值,這點須要注意。
結果:
首先加入consul client的依賴
<dependency> <groupId>com.orbitz.consul</groupId> <artifactId>consul-client</artifactId> <version>0.15.1</version> </dependency>
主類:ConsulClientDemo.java
package com.dxz.Consul_client; import java.util.List; import com.google.common.net.HostAndPort; import com.orbitz.consul.AgentClient; import com.orbitz.consul.Consul; import com.orbitz.consul.HealthClient; import com.orbitz.consul.model.agent.ImmutableRegCheck; import com.orbitz.consul.model.agent.ImmutableRegistration; import com.orbitz.consul.model.health.ServiceHealth; public class ConsulClientDemo { static Consul consul = Consul.builder().withHostAndPort(HostAndPort.fromString("localhost:8500")).withPing(false).build(); /** * 服務註冊 */ public static void serviceRegister() { AgentClient agent = consul.agentClient(); //健康檢測 ImmutableRegCheck check = ImmutableRegCheck.builder().http("http://localhost:9020/health").interval("5s").build(); ImmutableRegistration.Builder builder = ImmutableRegistration.builder(); builder.id("consul-server3").name("consul-server").addTags("v1").address("localhost").port(8080).addChecks(check); agent.register(builder.build()); } /** * 服務獲取 */ public static void serviceGet() { HealthClient client = consul.healthClient(); String name = "consul-server"; //獲取全部服務 System.out.println(client.getAllServiceInstances(name).getResponse().size()); //獲取全部正常的服務(健康檢測經過的) List<ServiceHealth> responses = client.getHealthyServiceInstances(name).getResponse(); for(ServiceHealth sh : responses ) { System.out.println(sh.getService()); } } public static void main(String[] args) { serviceRegister(); serviceGet(); System.exit(0); } }
上面的註冊後,看consul控制檯以下:
固然了,還可使用以下consul api
<dependency> <groupId>com.ecwid.consul</groupId> <artifactId>consul-api</artifactId> <version>1.2.2</version> </dependency>
主類:ConsulApiDemo.java
package com.dxz.Consul_client; import java.util.List; import java.util.Map; import com.ecwid.consul.v1.ConsulClient; import com.ecwid.consul.v1.ConsulRawClient; import com.ecwid.consul.v1.agent.model.Service; import com.ecwid.consul.v1.health.model.HealthService; public class ConsulApiDemo { public static void serviceApiGet() { ConsulRawClient client = new ConsulRawClient("localhost", 8500); ConsulClient consul = new ConsulClient(client); //獲取全部服務 Map<String, Service> map = consul.getAgentServices().getValue(); List<HealthService> list = consul.getHealthServices("consul-server", false, null).getValue(); System.out.println(map.size()+"," +map); System.out.println("list" + list); } public static void main(String[] args) { serviceApiGet(); System.exit(0); } }
啓動時的日誌片斷:
6,map={application=Service{id='application', service='application', tags=[], address='192.168.5.6', port=8080}, consul=Service{id='consul', service='consul', tags=[], address='', port=8300}, consul-client1=Service{id='consul-client1', service='consul-client', tags=[], address='DESKTOP-PPSFCNC', port=8501}, consul-server1=Service{id='consul-server1', service='consul-server', tags=[], address='DESKTOP-PPSFCNC', port=8503}, consul-server2=Service{id='consul-server2', service='consul-server', tags=[], address='DESKTOP-PPSFCNC', port=8504}, consul-server3=Service{id='consul-server3', service='consul-server', tags=[v1], address='localhost', port=8080}}
3,list=[HealthService{node=Node{node='DESKTOP-PPSFCNC', address='127.0.0.1'}, service=Service{id='consul-server1', service='consul-server', tags=[], address='DESKTOP-PPSFCNC', port=8503}, checks=[Node{node='DESKTOP-PPSFCNC', checkId='serfHealth', name='Serf Health Status', status=PASSING, notes='', output='Agent alive and reachable', serviceId='', serviceName=''}, Node{node='DESKTOP-PPSFCNC', checkId='service:consul-server1', name='Service 'consul-server' check', status=CRITICAL, notes='', output='parse http://DESKTOP-PPSFCNC:8503${management.contextPath}/health: invalid character "{" in host name', serviceId='consul-server1', serviceName='consul-server'}]}, HealthService{node=Node{node='DESKTOP-PPSFCNC', address='127.0.0.1'}, service=Service{id='consul-server2', service='consul-server', tags=[], address='DESKTOP-PPSFCNC', port=8504}, checks=[Node{node='DESKTOP-PPSFCNC', checkId='serfHealth', name='Serf Health Status', status=PASSING, notes='', output='Agent alive and reachable', serviceId='', serviceName=''}, Node{node='DESKTOP-PPSFCNC', checkId='service:consul-server2', name='Service 'consul-server' check', status=CRITICAL, notes='', output='parse http://DESKTOP-PPSFCNC:8504${management.contextPath}/health: invalid character "{" in host name', serviceId='consul-server2', serviceName='consul-server'}]}, HealthService{node=Node{node='DESKTOP-PPSFCNC', address='127.0.0.1'}, service=Service{id='consul-server3', service='consul-server', tags=[v1], address='localhost', port=8080}, checks=[Node{node='DESKTOP-PPSFCNC', checkId='serfHealth', name='Serf Health Status', status=PASSING, notes='', output='Agent alive and reachable', serviceId='', serviceName=''}, Node{node='DESKTOP-PPSFCNC', checkId='service:consul-server3', name='Service 'consul-server' check', status=CRITICAL, notes='', output='Get http://localhost:9020/health: dial tcp [::1]:9020: connectex: No connection could be made because the target machine actively refused it.', serviceId='consul-server3', serviceName='consul-server'}]}]
其中,spring cloud 使用的就是第二種consul api。
兩種查詢方式:DNS和HTTP
訪問的服務名字:
說明:
在安裝好consul的ubuntu虛擬機上啓動consul server,以server方式啓動:
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul agent -ui -server -data-dir=/data -bootstrap-expect 1 -bind 10.200.110.100
使用-ui參數啓動server成功後,能夠在瀏覽器中輸入:http://localhost:8500/ui 看到以下界面
在本機上啓動consul client
consul agent -data-dir /data -node=duan -advertise=10.200.110.13 -join=10.200.110.100
加入server節點成功後的截圖以下
此時在web ui中查看節點,會發現多了一個節點,可是沒有任何服務
4.一、每個服務註冊到一個文件
假設如今又建立了一個secondservice服務,我會將該服務寫入secondservice.json文件中去,以下:
使用http去訪問:
說明:按照服務名去訪問。
4.二、多個服務寫在同一個json文件中
說明:
使用http去訪問:
說明:按照服務名去訪問。
注意:在實際開發中,微服務數量衆多,
若是每一個文件都放在一個文件裏,文件會很是多,很差!
若是全部微服務都放在一個文件裏,文件太大,也很差!
因此,須要兩者結合。例如,假設有100個微服務,放在10個json文件中去,每一個json文件存放10個服務。