spring boot2X整合Consul一服务注册与发现
Consul
是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置。
关键特性:
服务注册/发现
数据强一致性保证
多数据中心
健康检查
key/value存储
1.下载 https://www.consul.io/downloads.html
eg:下载的文件consul_1.6.1_windows_amd64.zip
解压
在path添加consul.exe所在路径
2.启动
启动就是运行一个Consul Agent实例
可以作为Server或Client角色运行
consul agent -dev
-dev 表示开发模式运行, -server 表示服务模式运行
说明:
启动dev模式需要使用到的端口有
8300 服务间通信(tcp)
8301 lan cossip的端口
8302 wan gossip的端口
8500 web ui界面的端口
8600 dns通信
浏览器打开http://localhost:8500

3.spring boot 整合
spring boot 版本 2.2.1.RELEASE
(1)添加依赖
修改pom.xml
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
(2)修改配置
application.properties
server.port=8010 spring.application.name=provider
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.health-check-path=/actuator/health
spring.cloud.consul.discovery.service-name=service-provider
spring.cloud.consul.discovery.heartbeat.enabled=true management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
说明:
spring.cloud.consul.discovery.service-name是指注册到 Consul 的服务名称,后期客户端会根据这个名称来进行服务调用
(3)修改启动类
开启服务发现支持,添加注解 @EnableDiscoveryClient
package com.xyz.provider; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient
@SpringBootApplication
public class ProviderApplication { public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
} }
(4)添加测试方法
package com.xyz.provider.controller; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class demoController {
@RequestMapping("/hello")
public String Hello(){
return "hello,provider";
} }
浏览器打开http://localhost:8500

spring boot2X整合Consul一服务注册与发现的更多相关文章
- spring boot2X整合Consul一使用RestTemplate实现服务调用
Consul可以用于实现分布式系统的服务发现与配置 服务调用有两种方式: A.使用RestTemplate 进行服务调用 负载均衡——通过Ribbon注解RestTemplate B.使用Feign ...
- 一起来学Spring Cloud | 第二章:服务注册和发现组件 (Eureka)
本篇文章,很浅显的一步步讲解如何搭建一个能运行的springcloud项目(带所有操作截图).相信!看完本篇之后,你会觉得springcloud搭建如此简单~~~~ 一. Eureka简介: 1.1 ...
- .netcore consul实现服务注册与发现-集群完整版
原文:.netcore consul实现服务注册与发现-集群完整版 一.Consul的集群介绍 Consul Agent有两种运行模式:Server和Client.这里的Server和Clien ...
- .netcore consul实现服务注册与发现-单节点部署
原文:.netcore consul实现服务注册与发现-单节点部署 一.Consul的基础介绍 Consul是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置.与其他分 ...
- .netcore consul实现服务注册与发现-集群部署
一.Consul的集群介绍 Consul Agent有两种运行模式:Server和Client.这里的Server和Client只是Consul集群层面的区分,与搭建在Cluster之上的应用服务无关 ...
- Spring Cloud Consul 实现服务注册和发现
Spring Cloud 是一个基于 Spring Boot 实现的云应用开发工具,它为基于 JVM 的云应用开发中涉及的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁.决策竞选.分布 ...
- Spring Cloud 入门教程 - Eureka服务注册与发现
简介 在微服务中,服务注册与发现对管理各个微服务子系统起着关键作用.随着系统水平扩展的越来越多,系统拆分为微服务的数量也会相应增加,那么管理和获取这些微服务的URL就会变得十分棘手,如果我们每新加一个 ...
- Consul初探-服务注册和发现
前言 经过上一篇的学习,现在已经来到了服务注册发现环节:Consul 的核心功能就是服务注册和发现,Consul 客户端通过将自己注册到 Consul 服务器集群,然后等待调用方去发现服务,实现代理转 ...
- 一个故事,一段代码告诉你如何使用不同语言(Golang&C#)提供相同的能力基于Consul做服务注册与发现
目录 引言 什么是微服务 传统服务 微服务 什么是服务注册与服务发现 为什么要使用不同的语言提供相同的服务能力 服务协调器 服务注册 Golang C#(.NetCore3.1) 服务发现 通过Htt ...
随机推荐
- wordpress调用指定分类除外的置顶文章
我们有时需要根据实际需要进行一些设置,比如wordpress调用指定分类除外的置顶文章,如何实现呢?随ytkah一起来看看吧,用如下代码插入到需要调取的位置 <div class="r ...
- 小程序SetData
- JS的ES6扩展
1.字符串扩展 1. includes(str) : 判断是否包含指定的字符串 2. startsWith(str) : 判断是否以指定字符串开头 3. endsWith(str) : 判断是否以指定 ...
- ES5新增的数组方法
ES5新增:(IE9级以上支持)1.forEach():遍历数组,无返回值,不改变原数组.2.map():遍历数组,返回一个新数组,不改变原数组.3.filter():过滤掉数组中不满足条件的值,返回 ...
- pycharm进行个性化设置
1. 设置主题:Monokai Ctrl+Alt+S: Editor->Color&Fonts->Python 2. 修改[选中内容]颜色 因为用了Monokai主题后,选中内容的 ...
- A*G`C002
AGC002 A Range Product 不会,弃疗了/kk https://agc002.contest.atcoder.jp/submissions/7908938 B Box and Bal ...
- uni-app 图片上传实战
uni.uploadFile()将本地资源上传到开发者服务器客户端发起一个post请求content-type multipart/form-data 通过uni.chooseImage获取一个本地资 ...
- python: str()
tx1 = '中国' tx2 = u'中国' print tx1 print tx2 print type(tx1) print type(tx2) #<type 'str'> str() ...
- HustOJ二次开发之修改数据库连接池
有的时候我们会因为某种业务需要的情况下,需要修改hustoj默认的数据库连接池之类的. 修改数据库连接池步骤 进入到对应的目录 /home/judge/src/web/include 找到db_inf ...
- eclipse tomcat 热加载 免除重启
Tomcat的热部署(以后就不用重起了) 1. tomcat上的部署问题,有时候也是个麻烦的问题,要是不采用热部署,我们就只能每次对原来的文件做一次改动的时候就要重新部署, 而每次重新部署都 ...