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. DataGridView的DataGridViewComboBoxColumn列在编辑时自动弹出下拉列表

    在DataGridView的CellEnter的事件中添加如下代码即可: if (e.ColumnIndex == dataGridView1.Columns["仓库名"].Ind ...

  2. POSIX信号处理

    信号用于通知进程发生了某件事情,可以由一个进程发给另一个进程,也可以由内核发个一个进程. 每个信号都有一个关联的行为,可以通过sigaction函数来设置一个信号的行为,有3种选择: 1.定义一个信号 ...

  3. H5-考试判断题

    1.所有的元素设置了浮动后都可以设置宽高. 2.行元素都不能设置宽高跟上下边距 3.所有的css样式优先级中“!important”优先级最高(及其不推荐使用) 4.改变元素的transition值, ...

  4. html、css、js文件加载顺序及执行情况

      HTML页面加载和解析流程 1. 用户输入网址(假设是个html页面,并且是第一次访问),浏览器向服务器发出请求,服务器返回html文件. 2. 浏览器开始载入html代码,发现<head& ...

  5. 抓包工具Charles,anyproxy,mitmproxy等

    Charles:图形化界面,看着比较方便友好,也可以抓取https,不过电脑和手机都要下载证书,主要我的电脑上不能添加一添加就卡死 所以,抓取https的话,就用mitmproxy比较简单 1.安装C ...

  6. VB6.0 获取Excel文件工作表Sheet的名称

    获取Excel文件工作表Sheet的名称 '产生Excel文档 Dim xlapp, xlbook As Object Dim sSheetName As String Set xlapp = Cre ...

  7. tomcat 支持ssi功能配置

    1.SSI是Server Side Includes 的缩写,是嵌入到HTML页面的一组指令的集合.在返回请求的页面(包含SSI指令前),服务器会处理这些指令,并用处理的结果替换指令,然后把页面返回. ...

  8. 团队作业week2

    软件分析和用户需求调查 (2013) 具体内容参看邹欣老师的博客:http://www.cnblogs.com/xinz/p/3308608.html. 作业提交期限:2013年9月25日上课前.

  9. myfocus焦点库的引用

    1.在Html中引入相关的文件:引入风格文件(js/css/jq) 2.创建myFocus标准的Html的结构并填充内容 <div id="picBox"> <d ...

  10. java线程详解(三)

    java线程间通信 首先看一段代码 class Res { String name; String sex; } class Input implements Runnable { private R ...