一、SpringCloud 微服务基础介绍
二、SpringCloud 微服务项目构建
三、SpringCloud 微服务之Eureka 服务注册与发现
四、SpringCloud 微服务之 Zookeeper 服务注册与发现
五、SpringCloud 微服务之Consul 服务注册与发现
六、SpringCloud 微服务之 Ribbon 负载均衡服务调用
七、SpringCloud 微服务之 OpenFeign 服务接口调用
八、SpringCloud 微服务之 Hystrix 断路器
九、Spring Cloud 微服务之 Gateway
十、SpringCloud 微服务之SpringCloud Config 分布式配置中心
十一、SpringCloud 微服务之SpringCloud Bus 消息总线
十二、SpringCloud 微服务之SpringCloud Stream 消息驱动
十三、Spring Cloud 微服务之 SpringCloud Sleuth 分布式请求链路跟踪
十四、SpringCloud Alibaba Nacos 服务注册和配置中心
十五、SpringCloud Alibaba Sentinel 实现熔断与限流
十六、SpringCloud Alibaba Seata 处理分布式事务
Consul 服务注册与发现
Consul 官网文档:https://www.consul.io/docs
Consul 下载地址:https://www.consul.io/downloads
1. Consul 简介
什么是 Consul?
https://www.consul.io/docs/intro
Consul 是一套开源的分布式服务发现和配置管理系统,由 HashiCorp 公司用 Go 语言开发。提供了微服务系统中的服务治理、配置中心、控制总线等功能。这些功能中的每一个都可以根据需要单独使用,也可以一起使用以构建全方位的服务网格,总之Consul提供了一种完整的服务网格解决方案。
它具有很多优点。包括:基于 raft 协议,比较简洁;支持健康检查,同时支持 HTTP 和 DNS 协议,支持跨数据中心的 WAN 集群,提供图形界面,跨平台,支持 Linux、Mac、Windows
能干什么?
- 服务发现
提供HTTP/DNS两种发现方式- 健康检测
支持多种方式,HTTP、TCP、Docker、shell脚本定制化- KV存储
Key、Value的存储方式- 多数据中心
Consul支持多数据中心- 可视化界面
2. 安装并运行 Consul
参考:选择自己需要的版本下载安装
https://www.consul.io/downloads
安装使用 Consul
# 更新 yum-utils 工具
yum install -y yum-utils
# 添加 Consul 到仓库
yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
# 下载安装 Consul
yum -y install consul
# 启动 Consul 开发环境
[root@graph2 ~]# consul agent -dev
# 启动 Consul 开发环境,使可以外网访问 Consul UI
[root@graph2 ~]# consul agent -dev -ui -node=consul-dev -client=0.0.0.0
# 后台启动 Consul
[root@graph2 ~]# consul agent -dev -ui -node=consul-dev -client=0.0.0.0 &
# 关闭 Consul
[root@graph2 ~]# consul leave
Graceful leave complete
Consul UI 访问地址
http://47.107.124.79:8500
3. 服务提供者
在父项目中创建 cloud-provider-consul-payment8006 支付服务module
修改 pom 文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud</artifactId>
<groupId>com.zzx</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-provider-consul-payment8006</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- SpringCloud consul -server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId