consul使用小结
来源:互联网 发布:java system类 编辑:程序博客网 时间:2024/05/22 16:08
项目中用到consul作为服务注册/发现、配置下发、负载均衡,保证了服务的高可用,最近简单学习了下它的原理。
关于consul的安装就不细说了,启几个server组成集群,再启俩个agent。
这里就有俩中方式注册服务: 1. 在启动agent的时候可以在consul的配置文件里直接注册服务 2. 也可以先不注册,之后服务通过http API向agent注册。我们选的是第二种方式,这样agent不需要关注具体业务。
服务注册
要用到开源项目consul-clinet,文档上有使用案例 github地址
<dependencies> <dependency> <groupId>com.orbitz.consul</groupId> <artifactId>consul-client</artifactId> <version>0.16.3</version> </dependency></dependencies>
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.health.ServiceHealth;import org.springframework.util.StringUtils;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.annotation.PostConstruct;import java.net.MalformedURLException;import java.net.URI;import java.util.List;@RestControllerpublic class ConsulController { private static final String serviceName = "demo"; private static final String serviceTag = "dev"; private static final String host = "192.168.21.253"; private static final int port = 8804; private static final String serviceId = host + port; private Consul consul; //注册服务名、tag和健康检查地址 @PostConstruct public void consulInit() { consul = Consul.builder().withHostAndPort(HostAndPort.fromParts("localhost", 8500)).build(); AgentClient agentClient = consul.agentClient(); try { agentClient.register(port, URI.create("http://" + host + ":" + port + "/health").toURL(), 5L, serviceName, serviceId, serviceTag); } catch (MalformedURLException e) { e.printStackTrace(); } System.out.println("consul init"); } //获取健康的服务 @RequestMapping("/servicelist") public String serviceList() { HealthClient healthClient = consul.healthClient(); List<ServiceHealth> list = healthClient.getHealthyServiceInstances(serviceName).getResponse(); StringBuffer buffer = new StringBuffer(); for (ServiceHealth serviceHealth : list) { String address = serviceHealth.getService().getAddress(); if (StringUtils.isEmpty(address)) { address = serviceHealth.getNode().getAddress(); } int port = serviceHealth.getService().getPort(); buffer.append(String.format("service (%s) address is (%s) port is (%d) \n", serviceName, address, port)); } return buffer.toString(); }}
这样就完成了服务的注册与发现,具体的发现规则,可以是随机、轮询、一致性哈希、哈希、权重。这个可以自己定义。
阅读全文
0 0
- consul使用小结
- Consul 使用
- consul ACL配置使用
- consul
- consul
- Consul 原理和使用简介
- Consul 原理和使用简介
- 使用consul、consul-template、registrator进行容器负载
- Consul 简介、安装、常用命令的使用
- springcloud使用consul作为配置中心
- golang使用服务发现系统consul
- 单机使用host网络安装consul、registrator、consul-template构建高可用demo
- 【Consul】CONSUL调研
- 【Consul】CONSUL环境部署
- 【Consul】Consul架构-简介
- 【Consul】Consul架构-简介
- 使用Consul快速搭建简易分布式服务监控系统
- 使用Consul打造弹性可扩展的PaaS平台
- Java ConcurrentModificationException异常解决
- eclipse中使用git
- h264查看码率
- JMeterPlugin 的下载安装
- java jdbc 连接hive
- consul使用小结
- X Chen笔记--- 解决virt-manager启动管理器出错:unsupported format character
- Android View 的滚动原理和 Scroller、VelocityTracker 类的使用
- JavaWeb-Servlet编程 Part1
- JavaScript的学习(七)--事件
- Servlet --cookie编码保存中文信息
- 欧拉函数
- Java String类与StringBuffer类的区别
- 关闭应用程序池之后,重启失败