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 ...
随机推荐
- Mybatis技术一数据库连接池配置(druid)
只简单叙述,网上相关的内容很多,这里只是给出参考: 数据库连接池druid配置列表: 配置 缺省值 说明 name 配置这个属性的意义在于,如果存在多个数据源,监控的时候可以通过名字来区分开来.如 ...
- abp radio表单元素 消失了
框架将原本的元素都隐藏掉了,取而代之的是根据label定义了自己的样式,如下: [type="radio"]:not(:checked)+label { padding-le ...
- rsync同步备份
一.服务器端.备份客户端安装 rsync 服务. 1.环境: CentOS 主 IP:172.16.3.18 备 IP:172.16.3.19 2.安装 rsync 软件 #yum install r ...
- bzoj3073: [Pa2011]Journeys 线段树优化建图
bzoj3073: [Pa2011]Journeys 链接 BZOJ 思路 区间和区间连边.如何线段树优化建图. 和单点连区间类似的,我们新建一个点,区间->新点->区间. 又转化成了单点 ...
- openjudge1.1
目录 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.1.10 1.1.1 描述 对于大部分编程语言来说,编写一个能够输出"H ...
- 04-树5 Root of AVL Tree (25 分)
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...
- 【CSP膜你赛】大逃亡
题目描述 魔王撒旦为了建立魔物的乐土,率领亚多拉玛雷克.艾谢尔.路西菲尔.以 及马纳果达这四位恶魔大元帅进攻人类世界.然而此时手持圣剑的勇者艾米莉亚出现了. 战败的魔王逃跑时穿越到了地球,以真奥贞夫 ...
- (转载)如何在当前目录下快速打开cmd
很多时候我们需要打开命令行然后进入到相应目录进行一些操作. 常规的做法是: Win+R打开运行窗口 输入"cmd"回车打开命令行窗口 假如我们要进入的是D盘foo文件夹下的一个ba ...
- tomcat 启动中文乱码
1.情景展示 从Apache官网下载的tomcat,启动后中文日志信息显示乱码. 启动startup.bat后 2.原因分析 通过修改日志输出的字符集来解决. 3.解决方案 tomcat安装目 ...
- shell之批量新增用户脚本(http-basic-auth)
user.txt(用户名记录文件) test001@.com test002@.com user.sh(shell脚本): for line in `cat user.txt` do echo $li ...