Spring Cloud Consul项目是针对Consul的服务治理实现。Consul是一个分布式高可用的系统,它包含多个组件,可是做为一个总体,在微服务架构中为咱们的基础设施提供服务发现和服务配置的工具。它包含了下面几个特性:html
因为Spring Cloud Consul项目的实现,咱们能够轻松的将基于Spring Boot的微服务应用注册到Consul上,并经过此实现微服务架构中的服务治理。spring
以以前实现的基于Eureka的示例(eureka-client)为基础,咱们如何将以前实现的服务提供者注册到Consul上呢?方法很是简单,咱们只须要在pom.xml
中将eureka的依赖修改成以下依赖:bootstrap
1
2
3
4
|
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
|
接下来再修改一下application.properites
,将consul须要的配置信息加入便可,好比:(下面配置是默认值)架构
1
2
|
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
|
到此为止,咱们将eureka-client转换为基于consul服务治理的服务提供者就完成了。前文咱们已经有提到过服务发现的接口DiscoveryClient
是Spring Cloud对服务治理作的一层抽象,因此能够屏蔽Eureka和Consul服务治理的实现细节,咱们的程序不须要作任何改变,只须要引入不一样的服务治理依赖,并配置相关的配置属性就能轻松的将微服务归入Spring Cloud的各个服务治理框架中。app
下面能够尝试让consul的服务提供者运行起来。这里可能读者会问,不须要建立相似eureka-server的服务端吗?因为Consul自身提供了服务端,因此咱们不须要像以前实现Eureka的时候建立服务注册中心,直接经过下载consul的服务端程序就可使用。框架
咱们能够用下面的命令启动consul的开发模式:tcp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
$consul agent -dev
==> Starting Consul agent...
==> Starting Consul agent RPC...
==> Consul agent running!
Version:
'v0.7.2'
Node name:
'Lenovo-zhaiyc'
Datacenter:
'dc1'
Server:
true (bootstrap: false)
Client Addr: 127.0.0.1 (HTTP: 8500, HTTPS: -1, DNS: 8600, RPC: 8400)
Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)
Gossip encrypt:
false, RPC-TLS: false, TLS-Incoming: false
Atlas: <disabled>
==> Log data will now stream
in as it occurs:
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}]
2017/06/22 07:50:54 [INFO] raft: Node at 127.0.0.1:8300 [Follower] entering Follower state (Leader:
"")
2017/06/22 07:50:54 [INFO] serf: EventMemberJoin: Lenovo-zhaiyc 127.0.0.1
2017/06/22 07:50:54 [INFO] consul: Adding LAN server Lenovo-zhaiyc (Addr: tcp/127.0.0.1:8300) (DC: dc1)
2017/06/22 07:50:54 [INFO] serf: EventMemberJoin: Lenovo-zhaiyc.dc1 127.0.0.1
2017/06/22 07:50:54 [INFO] consul: Adding WAN server Lenovo-zhaiyc.dc1 (Addr: tcp/127.0.0.1:8300) (DC: dc1)
2017/06/22 07:51:01 [ERR] agent: failed to sync remote state: No cluster leader
2017/06/22 07:51:02 [WARN] raft: Heartbeat timeout from
"" reached, starting election
2017/06/22 07:51:02 [INFO] raft: Node at 127.0.0.1:8300 [Candidate] entering Candidate state
in term 2
2017/06/22 07:51:02 [DEBUG] raft: Votes needed: 1
2017/06/22 07:51:02 [DEBUG] raft: Vote granted from 127.0.0.1:8300
in term 2. Tally: 1
2017/06/22 07:51:02 [INFO] raft: Election won. Tally: 1
2017/06/22 07:51:02 [INFO] raft: Node at 127.0.0.1:8300 [Leader] entering Leader state
2017/06/22 07:51:02 [INFO] consul: cluster leadership acquired
2017/06/22 07:51:02 [INFO] consul: New leader elected: Lenovo-zhaiyc
2017/06/22 07:51:02 [DEBUG] consul: reset tombstone GC to index 3
2017/06/22 07:51:02 [INFO] consul: member
'Lenovo-zhaiyc' joined, marking health alive
2017/06/22 07:51:02 [INFO] agent: Synced service
'consul'
2017/06/22 07:51:02 [DEBUG] agent: Node info
in sync
|
consul服务端启动完成以后,咱们再将以前改造后的consul服务提供者启动起来。consul与eureka同样,都提供了简单的ui界面来查看服务的注册状况:分布式
从如今开始,我这边会将近期研发的springcloud微服务云架构的搭建过程和精髓记录下来,帮助更多有兴趣研发spring cloud框架的朋友,但愿能够帮助更多的好学者。你们来一块儿探讨spring cloud架构的搭建过程及如何运用于企业项目。源码来源微服务