Spring Cloud Spring Boot mybatis 企业分布式微服务云(二)服务注册与发现(Consul)

Spring Cloud Consul

Spring Cloud Consul项目是针对Consul的服务治理实现。Consul是一个分布式高可用的系统,它包含多个组件,但是作为一个整体,在微服务架构中为我们的基础设施提供服务发现和服务配置的工具。它包含了下面几个特性:

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

由于Spring Cloud Consul项目的实现,我们可以轻松的将基于Spring Boot的微服务应用注册到Consul上,并通过此实现微服务架构中的服务治理。

以之前实现的基于Eureka的示例(eureka-client)为基础,我们如何将之前实现的服务提供者注册到Consul上呢?方法非常简单,我们只需要在pom.xml中将eureka的依赖修改为如下依赖:

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency>

接下来再修改一下application.properites,将consul需要的配置信息加入即可,比如:(下面配置是默认值)

spring.cloud.consul.host=localhost spring.cloud.consul.port=8500

到此为止,我们将eureka-client转换为基于consul服务治理的服务提供者就完成了。前文我们已经有提到过服务发现的接口DiscoveryClient是Spring Cloud对服务治理做的一层抽象,所以可以屏蔽Eureka和Consul服务治理的实现细节,我们的程序不需要做任何改变,只需要引入不同的服务治理依赖,并配置相关的配置属性就能轻松的将微服务纳入Spring Cloud的各个服务治理框架中。

下面可以尝试让consul的服务提供者运行起来。这里可能读者会问,不需要创建类似eureka-server的服务端吗?由于Consul自身提供了服务端,所以我们不需要像之前实现Eureka的时候创建服务注册中心,直接通过下载consul的服务端程序就可以使用。

我们可以用下面的命令启动consul的开发模式:

1$consul agent -dev 2==> Starting Consul agent... 3==> Starting Consul agent RPC... 4==> Consul agent running! 5 Version: 'v0.7.2' 6 Node name: 'Lenovo-zhaiyc' 7 Datacenter: 'dc1' 8 Server: true (bootstrap: false) 9 Client Addr: 127.0.0.1 (HTTP: 8500, HTTPS: -1, DNS: 8600, RPC: 8400) 10 Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302) 11 Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false 12 Atlas: <disabled> 13 14==> Log data will now stream in as it occurs: 15 16 2017/06/22 07:50:54 [INFO] raft: Initial configuration (index=1): [{Suffrage:Voter ID:127.0.0.1:8300 Address:127.0.0.1:8300}] 17 2017/06/22 07:50:54 [INFO] raft: Node at 127.0.0.1:8300 [Follower] entering Follower state (Leader: "") 18 2017/06/22 07:50:54 [INFO] serf: EventMemberJoin: Lenovo-zhaiyc 127.0.0.1 19 2017/06/22 07:50:54 [INFO] consul: Adding LAN server Lenovo-zhaiyc (Addr: tcp/127.0.0.1:8300) (DC: dc1) 20 2017/06/22 07:50:54 [INFO] serf: EventMemberJoin: Lenovo-zhaiyc.dc1 127.0.0.1 21 2017/06/22 07:50:54 [INFO] consul: Adding WAN server Lenovo-zhaiyc.dc1 (Addr: tcp/127.0.0.1:8300) (DC: dc1) 22 2017/06/22 07:51:01 [ERR] agent: failed to sync remote state: No cluster leader 23 2017/06/22 07:51:02 [WARN] raft: Heartbeat timeout from "" reached, starting election 24 2017/06/22 07:51:02 [INFO] raft: Node at 127.0.0.1:8300 [Candidate] entering Candidate state in term 2 25 2017/06/22 07:51:02 [DEBUG] raft: Votes needed: 1 26 2017/06/22 07:51:02 [DEBUG] raft: Vote granted from 127.0.0.1:8300 in term 2. Tally: 1 27 2017/06/22 07:51:02 [INFO] raft: Election won. Tally: 1 28 2017/06/22 07:51:02 [INFO] raft: Node at 127.0.0.1:8300 [Leader] entering Leader state 29 2017/06/22 07:51:02 [INFO] consul: cluster leadership acquired 30 2017/06/22 07:51:02 [INFO] consul: New leader elected: Lenovo-zhaiyc 31 2017/06/22 07:51:02 [DEBUG] consul: reset tombstone GC to index 3 32 2017/06/22 07:51:02 [INFO] consul: member 'Lenovo-zhaiyc' joined, marking health alive 33 2017/06/22 07:51:02 [INFO] agent: Synced service 'consul' 34 2017/06/22 07:51:02 [DEBUG] agent: Node info in sync

consul服务端启动完成之后,我们再将之前改造后的consul服务提供者启动起来。consul与eureka一样,都提供了简单的ui界面来查看服务的注册情况:

源码来源

点赞
收藏

评论区

加载中...

相关推荐

Consul微服务的配置中心体验篇

SpringCloudConsul项目是针对Consul的服务治理实现。Consul是一个分布式高可用的系统,具有分布式、高可用、高扩展性ConsulConsul是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置。与其他分布式服务注册与发现的方案,Consul

Nacos作为注册中心和配置中心,爱不释手的感觉

在使用SpringCloud做分布式微服务架构时,注册中心是必不可少的一个组件。目前可以用的主要有:Eureka、Consul、Zookeeper。今天,我们就来说一下Alibaba的Nacos怎么样?下载与安装下载地址https://github.com/alibaba/nacos/releases安装:

Mysql半同步加orchestrator

Github基于Orchestrator,Consul和GLB实现高可用性目标。1.orchestrator用来运行故障监听和故障恢复。我们使用了如下图所示的一个跨数据中心的orchestrator/raft。2.Hashicorp公司的用于服务发现的Consul。使用Consul的KV存储器写入集群主节点的身份。对于每个集群,都有一套KV记录

SpringCloud consul 微服务(注册到主机名的问题)

目前项目在使用consul做服务注册与发现,做SpringSecurityOAuth2权限认证的authorization\_code模式的时候发现一个异常坑爹的问题这是开始的服务注册代码块bootstrap.yml:spring:cloud:consul:port:8500

SpringCloud注册中心高可用搭建

SpringCloud的注册中心可以由Eureka、Consul、Zookeeper、ETCD等来实现,这里推荐使用SpringCloudEureka来实现注册中心,它基于Netfilix的Eureka做了二次封装,完成分布式服务中服务治理的功能,微服务系统中的服务注册与发现都通过这个注册中心来进行管理。引入EurekaServer依赖

Spring Cloud(二) Consul 服务治理实现

SpringCloudConsul项目是针对Consul的服务治理实现。Consul是一个分布式高可用的系统,具有分布式、高可用、高扩展性。Consul简介Consul是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置。与其他分布式服务注册与发现的方案,Consul的方案更“一站式”,内