SpringCloud+Consul 服务注册与服务发现

1. 服务注册:

在Spring.factories有一段:

# Discovery Client Configuration
org.springframework.cloud.client.discovery.EnableDiscoveryClient=\
org.springframework.cloud.consul.discovery.ConsulDiscoveryClientConfiguration

这是SpringCloud时Consul实现服务注册的关键。

发现有一个ConsulLifecycle的bean注入:

@Bean
@ConditionalOnMissingBean(search = SearchStrategy.CURRENT)
public ConsulLifecycle consulLifecycle(ConsulDiscoveryProperties discoveryProperties,
HeartbeatProperties heartbeatProperties) {
ConsulLifecycle lifecycle = new ConsulLifecycle(consulClient, discoveryProperties, heartbeatProperties);
if (this.ttlScheduler != null) {
lifecycle.setTtlScheduler(this.ttlScheduler);
}
if (this.servletContext != null) {
lifecycle.setServletContext(this.servletContext);
}
if (this.serverProperties != null && this.serverProperties.getPort() != null && this.serverProperties.getPort() > 0) {
// no need to wait for events for this to start since the user has explicitly set the port.
lifecycle.setPort(this.serverProperties.getPort());
}
return lifecycle;
}

ConsulLifecycle继承自AbstractDiscoveryLifecycle,而AbstractDiscoveryLifecycle实现了ApplicationListener接口,即在容器初始化完成后会调用onApplicationEvent方法。会调用start方法。最终调用register方法注册服务。注意stat()方法的定义:

@Override
@Retryable(interceptor = "consulRetryInterceptor")
public void start() {
super.start();
}

是支持重新注册的。

//第一个配置
spring:
cloud:
consul:
host: localhost
port: 8500
discovery:
tags: dev
instance-id: cosulservice
service-name: app
application:
name: cosulservice
server:
port: 8088
//第二个配置
spring:
cloud:
consul:
host: localhost
port: 8500
discovery:
# health-check-path: /health
# health-check-interval: 15s
tags: dev
instance-id: cosulservice2
service-name: app
application:
name: cosulservice
server:
port: 8088

运行两个应用,注册,查看consul中相关服务:

app
Tags dev
Nodes
node-client-v-5-1 172.17.0.8 2 passing Service 'app' check service:cosulservice2
passing
Serf Health Status serfHealth
passing node-client-v-5-1 172.17.0.8 2 passing Service 'app' check service:cosulservice
passing
Serf Health Status serfHealth

注册两个服务。服务均为app。至此已经两个服务已经注册。

2. 服务发现:

@Bean
@ConditionalOnMissingBean
public ConsulDiscoveryClient consulDiscoveryClient(ConsulLifecycle consulLifecycle,
ConsulDiscoveryProperties discoveryProperties) {
ConsulDiscoveryClient discoveryClient = new ConsulDiscoveryClient(consulClient,
consulLifecycle, discoveryProperties);
discoveryClient.setServerProperties(serverProperties); //null ok
return discoveryClient;
}
@RequestMapping("/services")
public Object services() {
return discoveryClient.getInstances("app");
}

简单的单元测试:

@Test
public void testServicess() throws Exception{
mockMvc.perform(MockMvcRequestBuilders.get("/services").contentType(MediaType.APPLICATION_JSON_UTF8)).andDo(new ResultHandler() {
@Override
public void handle(MvcResult result) throws Exception {
System.out.println(result.getResponse().getContentAsString());
}
});
}
[{"serviceId":"app","host":"192.168.1.103","port":8088,"secure":false,"metadata":{"dev":"dev"},"uri":"http://192.168.1.103:8088"},{"serviceId":"app","host":"192.168.1.103","port":8080,"secure":false,"metadata":{"dev":"dev"},"uri":"http://192.168.1.103:8080"}]

SpringCloud+Consul 服务注册与服务发现的更多相关文章

  1. consul服务注册与服务发现的巨坑

    最近使用consul作为项目的服务注册与服务发现的基础功能.在塔建集群使用中遇到一些坑,下面一个个的记录下来. consul集群多node consul集群的node也就是我们所说的consul实例. ...

  2. 【转】用 Consul 来做服务注册与服务发现

    原文:https://segmentfault.com/a/1190000018731395?utm_source=tag-newest ------------------------------- ...

  3. SpringCloud系列(一):Eureka 服务注册与服务发现

    上一篇,我们介绍了服务注册中心,光有服务注册中心没有用,我们得发服务注册上去,得从它那边获取服务.下面我们注册一个服务到服务注册中心上去. 我们创建一个 hello-service 的 spring ...

  4. SpringCloud之eureka服务注册和服务发现

    服务注册中心 :eureka-server 作用:服务注册中心提供服务注册功能 服务提供方:eureka-client 作用:注册服务到服务注册中心 服务注册中心 :eureka-server 创建 ...

  5. Go微服务框架go-kratos实战04:kratos中服务注册和服务发现的使用

    一.简介 关于服务注册和服务发现介绍,我前面的文章有介绍过 - 服务注册和发现的文章. 作为服务中心的软件有很多,比如 etcd,consul,nacos,zookeeper 等都可以作为服务中心. ...

  6. SpringCloud实战之初级入门(二)— 服务注册与服务调用

    目录 1.环境介绍 2.服务提供 2.1 创建工程 2.2 修改配置文件 2.3 修改启动文件 2.5 亲测注意事项 3.服务调用 3.1 创建工程 3.2 修改配置文件 3.3 修改启动文件 3.4 ...

  7. dubbo2.7.X版本带来的服务注册和服务调用方式改变

    参考地址:https://www.cnblogs.com/alisystemsoftware/p/13064620.html 注册中心数据结构格式改变(service:接口服务,application ...

  8. Consul 服务注册与服务发现

    上一篇:Mac OS.Ubuntu 安装及使用 Consul 1. 服务注册 对 Consul 进行服务注册之前,需要先部署一个服务站点,我们可以使用 ASP.NET Core 创建 Web 应用程序 ...

  9. SpringCloud服务注册与服务发现之Eureka

    Eureka是SpringCloud Netflix的子模块之一,用于云端的服务发现,服务定位,实现云端中间层服务发现和故障转移.服务注册与发现对于微服务系统来说十分的重要,有了服务注册与发现,就省去 ...

随机推荐

  1. Selenium2+python自动化14-iframe

    前言 有很多小伙伴在拿163作为登录案例的时候,发现不管怎么定位都无法定位到,到底是什么鬼呢,本篇详细介绍iframe相关的切换 以http://mail.163.com/登录页面10为案例,详细介绍 ...

  2. coursera 机器学习课程 GraphLab环境准备

    在网上看到coursera有机器学习的课程,正好再学习学习,温固一下,还有很多其他的课程也很好.收费的哟! 手机APP和网站收取的费用有差异,网站上要便宜一下,费用差的挺多的,果断在网站上支付了. 有 ...

  3. NVelocity 表格行奇偶样式变换

    #foreach($test in $tests) #even <tr class="Test1"> #odd <tr class="Test2&quo ...

  4. Shi-Tomasi角点检测

    代码示例: #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #inc ...

  5. String 与 byte[]相互转换

    1. 从byte[]转换成string string result = System.Text.Encoding.UTF8.GetString(byteArray); 2.从string 转换成byt ...

  6. Problem 2136 取糖果---FUOJ (线段树+维护)

    http://acm.fzu.edu.cn/problem.php?pid=2136 题目大意: 给你n个袋子每个袋子里都装有糖果,然后呢你可以每次抽取一个连续的一个区间的袋子,然后带走里面最多糖果数 ...

  7. centos 安装flash插件

    方法一: 1.选择合适的yum源http://get.adobe.com/cn/flashplayer/进入此网址选择 “YUM,适用于Linux(YUM)”,下载adobe源http://101.9 ...

  8. Linux-FTP

    VSFTPd:Very Secure FTP Daemon FTP服务安全吗?1.用户名和密码不加密2.所传输的文件也不加密 FTP协议原生不安全 TFTP和FTP的区别 1.FTP用TCP协议,TF ...

  9. LeetCode:Permutations, Permutations II(求全排列)

    Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...

  10. python基础整理笔记(二)

    一. 列表 1. 创建实例: a = [1,2,3] b = list() 2. 主要支持的操作及其时间复杂度如下: 3. 其他 python中的列表,在内存中实际存储的形式其实是分散的存储,比较类似 ...