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. Centos7 php 5.6.19编译安装

    0x01  前言 在php官网下载php-5.6.19.tar.gz源代码(php7虽然说性能提升很大,但是小菜菜还是先用着这个先吧),解压后根目录有个INSTALL文件,里面有安装教程了,目录如下: ...

  2. Sql Server 2008修改Sa密码

    1.用Windows验证模式进入数据库管理器 右键根目录 >>>>属性>>>>左边的安全性 选择sql server 和windows 验证(SQL S ...

  3. 20145225《Java程序设计》 实验四 Android开发基础

    20145225<Java程序设计> 实验四 Android开发基础 实验报告 实验内容 安装Android Studio 运行安卓AVD模拟器 使用安卓运行出虚拟手机并显示HelloWo ...

  4. vnc 登录后只有终端 没有桌面 黑屏

    1, start vnc server: vncserver :1 issue: connect it with pc and only display one terminal. 2, stop v ...

  5. Visual studio 非常好的插件

    1. Productive power tools2015 2. Visual studio spell checker

  6. cocos2d-x源码分析(1)

    class CC_DLL CCCopying { public: virtual CCObject* copyWithZone(CCZone* pZone); }; class CC_DLL CCZo ...

  7. (Python )运算符

    这一节,将学习运算符,主要是算术运算符和逻辑运算符 1.算术运算符 除法运算,整数/整数=整数,浮点数/整数=浮点数,整数/浮点数=浮点数: >>> 17/35>>> ...

  8. ueditor .net版本上传不了图片问题

    百度的Ueditor适合中国人,所以,选择了它,可是恶梦才刚刚开始,最头痛的就是图片死活就是上传不成功,悲剧中.. 现在来分析下为什么报错:什么也不说了,上图

  9. Js之Dom学习-三种获取页面元素的方式、事件、innerText和innerHTML的异同

    一.三种获取页面元素的方式: getElementById:通过id来获取 <body> <input type="text" value="请输入一个 ...

  10. Android 如何制造低内存环境

    我们在复现问题的时候有时需要低内存的环境,此时我们可以在有root的手机中,往 /mnt/obb 目录下 push 文件,直到满足需要. 原理:/mnt/obb目录下挂载的是tmpfs文件系统,该文件 ...