服务发现 - 客户端应用进程向注册中心发起查询,来获取服务的位置。服务发现的一个重要作用就是提供一个可用的服务列表
服务定义的格式类似如下:
{ "service":{ "id": "jetty", "name": "jetty", "address": "192.168.1.200", "port": 8080, "tags": ["dev"], "checks": [ { "http": "http://192.168.1.200:8080/health", "interval": "5s" } ] } }
check必须是script或者TTL类型的,如果是script类型,则script和interval变量必须被提供,如果是TTL类型,则ttl变量必须被提供
script是consul主动去检查服务的健康状况,ttl是服务主动向consul报告自己的健康状况
以下是几种配置方式
script check
{ "check": { "id": "mem-util", "name": "Memory utilization", "script": "/usr/local/bin/check_mem.py", "interval": "10s", "timeout": "1s" } }
{ "check": { "id": "api", "name": "HTTP API on port 5000", "http": "http://localhost:5000/health", "interval": "10s", "timeout": "1s" } }
{ "check": { "id": "ssh", "name": "SSH TCP on port 22", "tcp": "localhost:22", "interval": "10s", "timeout": "1s" } }
{ "check": { "id": "web-app", "name": "Web App Status", "notes": "Web app does a curl internally every 10 seconds", "ttl": "30s" } }
{ "service":{ "id": "jetty", "name": "jetty", "address": "192.168.1.200", "port": 8080, "tags": ["dev"], "checks": [ { "http": "http://192.168.1.200:8080/health", "interval": "5s" } ] } }
如:
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的值,这点需要注意
3:使用程序实现服务的注册和发现(Java)
首先加入consul client的依赖
<dependency> <groupId>com.orbitz.consul</groupId> <artifactId>consul-client</artifactId> <version>0.12.3</version> </dependency>
package com.pp.cnosul; 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; public class ConsulDemo { static Consul consul = Consul.builder().withHostAndPort(HostAndPort.fromString("192.168.1.246:8500")).build(); /** * 服务注册 */ public static void serviceRegister() { AgentClient agent = consul.agentClient(); //健康检测 ImmutableRegCheck check = ImmutableRegCheck.builder().http("http://192.168.1.104:9020/health").interval("5s").build(); ImmutableRegistration.Builder builder = ImmutableRegistration.builder(); builder.id("tomcat1").name("tomcat").addTags("v1").address("192.168.1.104").port(8080).addChecks(check); agent.register(builder.build()); } /** * 服务获取 */ public static void serviceGet() { HealthClient client = consul.healthClient(); String name = "tomcat"; //获取所有服务 System.out.println(client.getAllServiceInstances(name).getResponse().size()); //获取所有正常的服务(健康检测通过的) client.getHealthyServiceInstances(name).getResponse().forEach((resp) -> { System.out.println(resp); }); } public static void main(String[] args) { serviceRegister(); serviceGet(); } }
当然了,还可以使用如下consul api
<dependency> <groupId>com.ecwid.consul</groupId> <artifactId>consul-api</artifactId> <version>1.1.10</version> </dependency>
import com.ecwid.consul.v1.ConsulClient; import com.ecwid.consul.v1.ConsulRawClient; import com.ecwid.consul.v1.agent.model.Service; public class App { public static void main(String[] args) { ConsulRawClient client = new ConsulRawClient("192.168.1.100", 8500); ConsulClient consul = new ConsulClient(client); //获取所有服务 Map<String, Service> map = consul.getAgentServices().getValue(); } }