Spring Cloud Consul項目是針對Consul的服務治理實現。Consul是一個分佈式高可用的系統,它包含多個組件,可是做爲一個總體,在微服務架構中爲咱們的基礎設施提供服務發現和服務配置的工具。它包含了下面幾個特性:spring
因爲Spring Cloud Consul項目的實現,咱們能夠輕鬆的將基於Spring Boot的微服務應用註冊到Consul上,並經過此實現微服務架構中的服務治理。bootstrap
以以前實現的基於Eureka的示例(eureka-client)爲基礎,咱們如何將以前實現的服務提供者註冊到Consul上呢?方法很是簡單,咱們只須要在pom.xml
中將eureka的依賴修改成以下依賴:架構
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency>
接下來再修改一下application.properites
,將consul須要的配置信息加入便可,好比:(下面配置是默認值)app
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的服務端程序就可使用。tcp
咱們能夠用下面的命令啓動consul的開發模式:分佈式
$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架構的搭建過程及如何運用於企業項目。工具