首页 > 代码库 > 使用consul实现服务的注册和发现
使用consul实现服务的注册和发现
服务注册 - 服务进程在注册中心注册自己的位置。它通常注册自己的主机和端口号,有时还有身份验证信息,协议,版本号,以及运行环境的详细资料。
服务发现 - 客户端应用进程向注册中心发起查询,来获取服务的位置。服务发现的一个重要作用就是提供一个可用的服务列表
服务定义的格式类似如下:
- {
- "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是用来做服务的健康检查的,可以有多个,也可以没有,支持多种方式的检查
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"
- }
- }
HTTP check:
- {
- "check": {
- "id": "api",
- "name": "HTTP API on port 5000",
- "http": "http://localhost:5000/health",
- "interval": "10s",
- "timeout": "1s"
- }
- }
TCP check:
- {
- "check": {
- "id": "ssh",
- "name": "SSH TCP on port 22",
- "tcp": "localhost:22",
- "interval": "10s",
- "timeout": "1s"
- }
- }
TTL check:
- {
- "check": {
- "id": "web-app",
- "name": "Web App Status",
- "notes": "Web app does a curl internally every 10 seconds",
- "ttl": "30s"
- }
- }
注册服务,有三种方式,
1:通过配置文件的方式静态注册
创建文件夹/etc/consul.d
.d代表有许多配置文件在里面
vim /etc/consul.d/jetty.json 内容如下:
- {
- "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"
- }
- ]
- }
- }
重启consul,并将配置文件的路径给consul(指定参数:-config-dir /etc/consul.d)
2:通过HTTP API接口来动态注册
直接调用/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的值,这点需要注意
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();
- }
- }
其中,spring cloud 使用的就是第二种consul api
使用consul实现服务的注册和发现