失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > SpringCloud教程-注册中心(Consul)(SpringCloud版本Greenwich.SR4)

SpringCloud教程-注册中心(Consul)(SpringCloud版本Greenwich.SR4)

时间:2021-04-08 16:11:56

相关推荐

SpringCloud教程-注册中心(Consul)(SpringCloud版本Greenwich.SR4)

文章目录

consul简介Consul 的优势:创建工程consul-client

代码地址:github-spring-cloud地址

consul简介

Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置。与其它分布式服务注册与发现的方案,Consul 的方案更“一站式”,内置了服务注册与发现框 架、分布一致性协议实现、健康检查、Key/Value 存储、多数据中心方案,不再需要依赖其它工具(比如 ZooKeeper 等)。使用起来也较 为简单。Consul 使用 Go 语言编写,因此具有天然可移植性(支持Linux、windows和Mac OS X);安装包仅包含一个可执行文件,方便部署,与 Docker 等轻量级容器可无缝配合。

Consul 的优势:

使用 Raft 算法来保证一致性, 比复杂的 Paxos 算法更直接. 相比较而言, zookeeper 采用的是 Paxos, 而 etcd 使用的则是 Raft。支持多数据中心,内外网的服务采用不同的端口进行监听。 多数据中心集群可以避免单数据中心的单点故障,而其部署则需要考虑网络延迟, 分片等情况等。 zookeeper 和 etcd 均不提供多数据中心功能的支持。支持健康检查。 etcd 不提供此功能。支持 http 和 dns 协议接口。 zookeeper 的集成较为复杂, etcd 只支持 http 协议。官方提供 web 管理界面, etcd 无此功能。

特性

服务发现健康检查Key/Value 存储多数据中心

client: 客户端, 无状态, 将 HTTP 和 DNS 接口请求转发给局域网内的服务端集群。

server: 服务端, 保存配置信息, 高可用集群, 在局域网内与本地客户端通讯, 通过广域网与其它数据中心通讯。 每个数据中心的 server 数量推荐为 3 个或是 5 个。

创建工程consul-client

新增pom文件

<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"><parent><artifactId>spring-cloud-learn</artifactId><groupId>com.sl.learn.cloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><groupId>com.sl.learn.cloud</groupId><artifactId>consul-client</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-consul-discovery</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies></project>

配置文件application.yml

spring:cloud:consul:host: localhostport: 8500discovery:healthCheckPath: ${management.contextPath}/healthhealthCheckInterval: 15sinstance-id: consul-clientapplication:name: consul-clientserver:port: 8056

启动类Application

@SpringBootApplication@EnableDiscoveryClientpublic class ConsulClientApplication {public static void main(String[] args) {SpringApplication.run(ConsulClientApplication.class,args);}}

访问:http://localhost:8500/ 可以看到服务已经注册了

如果觉得《SpringCloud教程-注册中心(Consul)(SpringCloud版本Greenwich.SR4)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。