<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] Spring Cloud Consul 項目是針對Consul的服務治理實現。Consul是一個分布式高可用的系統,具有分布式、高可用、高擴展性。 # Consul 簡介 Consul 是 HashiCorp 公司推出的開源工具,用于實現分布式系統的服務發現與配置。與其他分布式服務注冊與發現的方案,Consul的方案更“一站式” ,內置了服務注冊與發現框 架、**具有以下性質:** - 分布一致性協議實現、 - 健康檢查、 - Key/Value存儲、 - 多數據中心方案, 不再需要依賴其他工具(比如ZooKeeper等)。 使用起來也較 為簡單。Consul使用Go語言編寫,因此具有天然可移植性(支持Linux、windows和Mac OS X);安裝包僅包含一個可執行文件,方便部署,與Docker等輕量級容器可無縫配合 。 基于 Mozilla Public License 2.0 的協議進行開源. Consul 支持健康檢查,并允許 HTTP 和 DNS 協議調用 API 存儲鍵值對. 一致性協議采用 Raft 算法,用來保證服務的高可用. 使用 GOSSIP 協議管理成員和廣播消息, 并且支持 ACL 訪問控制. ## Consul 的使用場景 - docker 實例的注冊與配置共享 - coreos 實例的注冊與配置共享 - vitess 集群 - SaaS 應用的配置共享 - 與 confd 服務集成,動態生成 nginx 和 haproxy 配置文件 ## Consul 的優勢 使用 Raft 算法來保證一致性, 比復雜的 Paxos 算法更直接. 相比較而言, zookeeper 采用的是 Paxos, 而 etcd 使用的則是 Raft. 支持多數據中心,內外網的服務采用不同的端口進行監聽。 多數據中心集群可以避免單數據中心的單點故障,而其部署則需要考慮網絡延遲, 分片等情況等. zookeeper 和 etcd 均不提供多數據中心功能的支持. 支持健康檢查. etcd 不提供此功能. 支持 http 和 dns 協議接口. zookeeper 的集成較為復雜, etcd 只支持 http 協議. 官方提供web管理界面, etcd 無此功能. ## Consul 的角色 client: 客戶端, 無狀態, 將 HTTP 和 DNS 接口請求轉發給局域網內的服務端集群.server: 服務端, 保存配置信息, 高可用集群, 在局域網內與本地客戶端通訊, 通過廣域網與其他數據中心通訊. 每個數據中心的 server 數量推薦為 3 個或是 5 個. 由于Spring Cloud Consul項目的實現,我們可以輕松的將基于Spring Boot的微服務應用注冊到Consul上,并通過此實現微服務架構中的服務治理。 # 搭建環境 ## Windows 下安裝 Consul 去官網下載:[https://www.consul.io/downloads.html](http://link.zhihu.com/?target=https%3A//www.consul.io/downloads.html) 得到一個 zip 壓縮包 在你想要安裝的位置解壓就行,只有一個 consul.exe 文件(我的解壓位置是:`E:\consul_1.4.0_windows_amd64`) 設置環境變量(在 path 中新增一條): ``` E:\consul_1.4.0_windows_amd64 ``` cmd 命令窗口啟動: ~~~csharp consul agent -dev ~~~ consul 自帶 UI 界面,打開網址:[http://localhost:8500](http://link.zhihu.com/?target=http%3A//localhost%3A8500/),可以看到當前注冊的服務界面。 ## linux下安裝 Consul **參考** - [Spring Cloud 官方文檔](http://cloud.spring.io/spring-cloud-consul/ ) - [Consul 官方文檔 ](https://www.consul.io/intro/getting-started/install.html) 要想利用Consul提供的服務實現服務的注冊與發現,我們需要搭建Consul Cluster 環境。 在Consul方案中,每個提供服務的節點上都要部署和運行Consul的agent,所有運行Consul agent節點的集合構成Consul Cluster。 Consul agent有兩種運行模式:Server和Client。這里的Server和Client只是Consul集群層面的區分,與搭建在Cluster之上 的應用服務無關。 以Server模式運行的Consul agent節點用于維護Consul集群的狀態,官方建議每個Consul Cluster至少有3個或以上的運行在Server mode的Agent,Client節點不限。 **環境配置如下:** Centos 7.3 | 主機名稱| IP | 作用 | 是否允許遠程訪問 | -------- | -------- | -------- |-------- | | node1 | 192.168.252.121| consul server | 是 | | node2 | 192.168.252.122| consul client | 是 | | node3 | 192.168.252.123| consul client | 是 | **關閉防火墻** ```sh systemctl stop firewalld.service ``` Consul 最新版的下載地址: [https://releases.hashicorp.com/consul/1.0.1/consul_1.0.1_linux_amd64.zip](https://releases.hashicorp.com/consul/1.0.1/consul_1.0.1_linux_amd64.zip) **下載,然后unzip 解壓,得到唯一,一個可執行文件** ```sh cd /opt/ wget https://releases.hashicorp.com/consul/1.0.1/consul_1.0.1_linux_amd64.zip unzip consul_1.0.1_linux_amd64.zip cp consul /usr/local/bin/ ``` **查看是否安裝成功** ```sh [root@node1 opt]# consul ``` 出現如下結果,表示安裝成功 ```sh Usage: consul [--version] [--help] <command> [<args>] Available commands are: agent Runs a Consul agent catalog Interact with the catalog event Fire a new event exec Executes a command on Consul nodes force-leave Forces a member of the cluster to enter the "left" state info Provides debugging information for operators. join Tell Consul agent to join cluster keygen Generates a new encryption key keyring Manages gossip layer encryption keys kv Interact with the key-value store leave Gracefully leaves the Consul cluster and shuts down lock Execute a command holding a lock maint Controls node or service maintenance mode members Lists the members of a Consul cluster monitor Stream logs from a Consul agent operator Provides cluster-level tools for Consul operators reload Triggers the agent to reload configuration files rtt Estimates network round trip time between nodes snapshot Saves, restores and inspects snapshots of Consul server state validate Validate config files/directories version Prints the Consul version watch Watch for changes in Consul ``` **檢查版本** ```sh [root@node1 opt]# consul version ``` ```sh Consul v1.0.1 Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents) ``` ## Consul常用命令 | 命令| 解釋 | 示例 | | -------- | -------- | -------- | | agent | 運行一個consul agent | consul agent -dev | | join | 將agent加入到consul集群 |consul join IP | | members | 列出consul cluster集群中的members |consul members | | leave | 將節點移除所在集群 | consul leave | ## consul agent 命令的常用選項 **-data-dir** - 作用:指定agent儲存狀態的數據目錄 - 這是所有agent都必須的 - 對于server尤其重要,因為他們必須持久化集群的狀態 **-config-dir** - 作用:指定service的配置文件和檢查定義所在的位置 - 通常會指定為”某一個路徑/consul.d”(通常情況下,.d表示一系列配置文件存放的目錄) **-config-file** - 作用:指定一個要裝載的配置文件 - 該選項可以配置多次,進而配置多個配置文件(后邊的會合并前邊的,相同的值覆蓋) **-dev** - 作用:創建一個開發環境下的server節點 - 該參數配置下,不會有任何持久化操作,即不會有任何數據寫入到磁盤 - 這種模式不能用于生產環境(因為第二條) **-bootstrap-expect** - 作用:該命令通知consul server我們現在準備加入的server節點個數,該參數是為了延遲日志復制的啟動直到我們指定數量的server節點成功的加入后啟動。 **-node** - 作用:指定節點在集群中的名稱 - 該名稱在集群中必須是唯一的(默認采用機器的host) - 推薦:直接采用機器的IP **-bind** - 作用:指明節點的IP地址 - 有時候不指定綁定IP,會報`Failed to get advertise address: Multiple private IPs found. Please configure one.` 的異常 **-server** - 作用:指定節點為server - 每個數據中心(DC)的server數推薦至少為1,至多為5 - 所有的server都采用raft一致性算法來確保事務的一致性和線性化,事務修改了集群的狀態,且集群的狀態保存在每一臺server上保證可用性 - server也是與其他DC交互的門面(gateway) **-client** - 作用:指定節點為client,指定客戶端接口的綁定地址,包括:HTTP、DNS、RPC - 默認是127.0.0.1,只允許回環接口訪問 - 若不指定為-server,其實就是-client **-join** - 作用:將節點加入到集群 **-datacenter**(老版本叫-dc,-dc已經失效) - 作用:指定機器加入到哪一個數據中心中 ## 啟動服務 我們嘗試一下: `-dev表示開發模式運行,使用-client 參數可指定允許客戶端使用什么ip去訪問,例如-client 192.168.252.121 表示可以使用` [http://192.168.252.121:8500/ui/ 去訪問。](http://192.168.252.121:8500/ui/) ```sh consul agent -dev -client 192.168.252.121 ``` ![Consul Cluster][1] ## Consul 的高可用 Consul Cluster集群架構圖如下: ![Consul Cluster集群架構][2] 這邊準備了三臺Centos 7.3的虛擬機,主機規劃如下,供參考: | 主機名稱| IP | 作用 | 是否允許遠程訪問 | -------- | -------- | -------- |-------- | | node1 | 192.168.252.121| consul server | 是 | | node2 | 192.168.252.122| consul client | 是 | | node3 | 192.168.252.123| consul client | 是 | ## 搭建步驟 命令參數,參看上面詳細介紹 **在 node1 機器上啟動 Consul** ```sh cd /opt/ mkdir data consul agent -data-dir /opt/data -node=192.168.252.121 -bind=0.0.0.0 -datacenter=dc1 -ui -client=192.168.252.121 -server -bootstrap-expect 1 > /dev/null 2>&1 & ``` **在 node2 機器上啟動 Consul,并且將node2節點加入到node1節點上** ```sh cd /opt/ mkdir data consul agent -data-dir /opt/data -node=192.168.252.122 -bind=0.0.0.0 -datacenter=dc1 -ui -client=192.168.252.122 -join=192.168.252.121 > /dev/null 2>&1 & ``` **在 node3 機器上啟動 Consul,并且將node3節點加入到node1節點上** ```sh cd /opt/ mkdir data consul agent -data-dir /opt/data -node=192.168.252.123 -bind=0.0.0.0 -datacenter=dc1 -ui -client=192.168.252.123 -join=192.168.252.121 > /dev/null 2>&1 & ``` 在node1上查看當前集群節點: ```sh consul members -rpc-addr=192.168.252.123:8400 consul leave -rpc-addr=192.168.252.123:8400 ``` [http://192.168.252.121:8500/ui/ 去訪問。](http://192.168.252.121:8500/ui/) ![Consul Cluster集群 nodes][3] # 項目示例 新建項目:`spring-cloud-consul-client` ## 添加依賴 在項目 `spring-cloud-consul-client` `pom.xml`中引入需要的依賴內容: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> ``` ## 開啟服務注冊 客戶端注冊Consul時,它提供有關自身的元數據,如主機和端口,ID,名稱和標簽。默認情況下,將創建一個HTTP 檢查,每隔10秒Consul命中/health端點。如果健康檢查失敗,則服務實例被標記為關鍵。 ```java package io.ymq.example.consul; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableDiscoveryClient @RestController public class ConsulApplication { @RequestMapping("/") public String home() { return "Hello world"; } public static void main(String[] args) { SpringApplication.run(ConsulApplication.class, args); } } ``` ## 配置文件 在`application.yml`配置文件中增加如下信息:如果Consul客戶端位于localhost:8500以外,則需要配置來定位客戶端 ```sh spring: application: name: consul-client cloud: consul: host: 192.168.252.121 port: 8500 discovery: healthCheckPath: / healthCheckInterval: 5s ``` 如果Consul客戶端位于localhost:8500以外的位置,則需要配置來定位客戶端。例: ``` host: 192.168.252.121 port: 8500 ``` **HTTP健康檢查路徑** INSTALL “10s”和“1m”分別表示10秒和1分 ``` discovery: healthCheckPath: ${management.context-path}/health healthCheckInterval: 15s ``` ## 啟動服務 到`spring-cloud-consul-client` 項目根目錄下,執行`mvn clean package`,把`target` 目錄下 生成的 jar `spring-cloud-consul-client-0.0.1-SNAPSHOT.jar` 上傳服務器,發布項目 打包命令 ```sh mvn clean package ``` 發布命令 ```sh nohup java -jar spring-cloud-consul-client-0.0.1-SNAPSHOT.jar > /dev/null 2>&1 & ``` ## 訪問服務 [http://192.168.252.121:8500/ui/#/dc1/nodes/192.168.252.121](http://192.168.252.121:8500/ui/#/dc1/nodes/192.168.252.121) ![Consul Cluster 集群 服務注冊情況][4] ![Consul Cluster集群 服務注冊情況][5] ![Consul Cluster集群 服務注冊情況][6] **通過上圖HTTP健康檢查,可以看到服務檢測正常** ## 源碼下載 **GitHub:**[https://github.com/souyunku/spring-cloud-examples/tree/master/spring-cloud-consul](https://github.com/souyunku/spring-cloud-examples/tree/master/spring-cloud-consul) **碼云:**[https://gitee.com/souyunku/spring-cloud-examples/tree/master/spring-cloud-consul](https://gitee.com/souyunku/spring-cloud-examples/tree/master/spring-cloud-consul) [1]: https://www.souyunku.com/images/2017/SpringCloud/consul/1.png [2]: https://www.souyunku.com/images/2017/SpringCloud/consul/2.png [3]: https://www.souyunku.com/images/2017/SpringCloud/consul/3.png [4]: https://www.souyunku.com/images/2017/SpringCloud/consul/44.png [5]: https://www.souyunku.com/images/2017/SpringCloud/consul/55.png [6]: https://www.souyunku.com/images/2017/SpringCloud/consul/66.png
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看