欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringCloud中的Consul詳解

 更新時間:2022年03月23日 14:19:54   作者:張鐵牛  
這篇文章主要介紹了SpringCloud中的Consul知識,本文使用的是docker-compose方式管理consul服務,直接啟動即可,需要的朋友可以參考下

1. Consul 簡介

Consul是 HashiCorp 公司推出的開源工具,用于實現(xiàn)分布式系統(tǒng)的服務發(fā)現(xiàn)與配置。與其它分布式服 務注冊與發(fā)現(xiàn)的方案,Consul 的方案更“一站式”,內(nèi)置了服務注冊與發(fā)現(xiàn)框 架、分布一致性協(xié)議實 現(xiàn)、健康檢查、Key/Value 存儲、多數(shù)據(jù)中心方案,不再需要依賴其它工具(比如 ZooKeeper 等)。 使用起來也較為簡單。Consul 使用 Go 語言編寫,因此具有天然可移植性(支持Linux、windows和 Mac OS X);安裝包僅包含一個可執(zhí)行文件,方便部署,與 Docker 等輕量級容器可無縫配合。

2. 專業(yè)名詞

  • agent

組成 consul 集群的每個成員上都要運行一個 agent,可以通過 consul agent 命令來啟動。agent可以運行在 server 狀態(tài)或者 client 狀態(tài)。自然的, 運行在 server 狀態(tài)的節(jié)點被稱為 server 節(jié)點,運行在 client 狀態(tài)的節(jié)點被稱 為 client 節(jié)點。

  • server 節(jié)點

負責組成 cluster 的復雜工作(選舉server 自行選舉一個 leader、狀態(tài)維 護、轉(zhuǎn)發(fā)請求到 leader),以及 consul 提供的服務(響應RPC 請求),以及存放和復制數(shù)據(jù)??紤]到容錯和可用性,一般部署 3 ~ 5 個比較合適。

  • client 節(jié)點

負責轉(zhuǎn)發(fā)所有的 RPC 到 server 節(jié)點。本身無狀態(tài),且輕量級,因此,可以部署大量的client 節(jié)點。

  • 數(shù)據(jù)中心

雖然數(shù)據(jù)中心的定義似乎很明顯,但仍有一些細微的細節(jié)必須考慮。我們 將一個數(shù)據(jù)中心定義為一個私有、低延遲和高帶寬的網(wǎng)絡環(huán)境。這不包括通過公共互聯(lián)網(wǎng)的通信,但是為了我們的目的,單個EC2 (aws云主機)區(qū)域內(nèi)的多個可用區(qū)域?qū)⒈灰暈閱蝹€數(shù)據(jù)中心的一部分。

3. Consul 的優(yōu)勢

  • 使用 Raft 算法來保證一致性, 比復雜的 Paxos 算法更直接. 相比較而言, zookeeper 采用的是 Paxos, 而 etcd 使用的則是 Raft。
  • 支持多數(shù)據(jù)中心,內(nèi)外網(wǎng)的服務采用不同的端口進行監(jiān)聽。 多數(shù)據(jù)中心集群可以避免單數(shù)據(jù)中心 的單點故障,而其部署則需要考慮網(wǎng)絡延遲, 分片等情況等。 zookeeper 和 etcd 均不提供多數(shù)據(jù)中 心功能的支持。
  • 支持健康檢查。 etcd 不提供此功能。
  • 支持 http 和 dns 協(xié)議接口。 zookeeper 的集成較為復雜, etcd 只支持 http 協(xié)議。 官方提供 web 管理界面, etcd 無此功能。

綜合比較, Consul 作為服務注冊和配置管理的新星, 比較值得關注和研究。

4. 特性

  • 服務發(fā)現(xiàn)
  • 健康檢查
  • Key/Value 存儲
  • 多數(shù)據(jù)中心

5. Consul與Eureka的區(qū)別

1.一致性 Consul強一致性(CP)

  • 服務注冊相比Eureka會稍慢一些。因為Consul的raft協(xié)議要求必須過半數(shù)的節(jié)點都寫入成功才認 為注冊成功
  • Leader掛掉時,重新選舉期間整個consul不可用。保證了強一致性但犧牲了可用性。

2.Eureka保證高可用和最終一致性(AP)

  • 服務注冊相對要快,因為不需要等注冊信息replicate到其他節(jié)點,也不保證注冊信息是否 replicate成功
  • 當數(shù)據(jù)出現(xiàn)不一致時,雖然A, B上的注冊信息不完全相同,但每個Eureka節(jié)點依然能夠正常對外提 供服務,這會出現(xiàn)查詢服務信息時如果請求A查不到,但請求B就能查到。如此保證了可用性但犧牲了一致性。

6. Consul 安裝

consul docker-hub

6.1 docker-compose安裝

以dev模式啟動 且 設置client=0.0.0.0為所有ip都可以連接此服務

version: '2'
services:
  consul-container:
    image: consul
    container_name: consul-dev
    environment:
      - CONSUL_BIND_INTERFACE=eth0
    ports:
      - "8500:8500"
    volumes:
      - "./config:/consul/config/"
      - "./data/:/consul/data/"
    command: agent -dev -client=0.0.0.0

服務啟動成功后,通過瀏覽器訪問localhost:8500,顯示如下頁面即安裝成功。

7. Quick Start

7.1 啟動consul服務

本文使用的是docker-compose方式管理consul服務,直接啟動即可

7.2 創(chuàng)建客戶端-provider

7.2.1 引入依賴坐標

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!--actuator用于心跳檢查-->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

7.2.2 配置application.yml

server:
  port: ${port:8082}

spring:
  application:
    name: provider
  cloud:
    consul:
      #consul服務地址
      host: 127.0.0.1
      #consul服務端口
      port: 8500
      discovery:
        #是否注冊
        register: true
        #實例ID
        #        instance-id: ${spring.application.name}-${server.port}
        instance-id: ${spring.application.name}:${random.value}
        #服務實例名稱
        service-name: ${spring.application.name}
        #服務實例端口
        port: ${server.port}
        #健康檢查路徑
        healthCheckPath: /actuator/health
        #健康檢查時間間隔
        healthCheckInterval: 15s
        #開啟ip地址注冊
        prefer-ip-address: true
        #實例的請求ip
        ip-address: ${spring.cloud.client.ip-address}

7.2.3 添加測試方法

package com.ldx.provider.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class TestController {
    @Autowired
    private DiscoveryClient discoveryClient;
    @Value("${server.port}")
    private String port;
    @GetMapping("products")
    public String products(){
        List<ServiceInstance> list = discoveryClient.getInstances("consumer");
        if(list != null && list.size() > 0 ) {
            ServiceInstance serviceInstance = list.get(0);
            System.out.println(serviceInstance);
        }
        return "Hello World:" + port;
    }
}

7.3 創(chuàng)建客戶端-comsumer

創(chuàng)建過程和provider一樣 測試方法換一下,并且在啟動類上添加RestTemplate Bean

7.3.1 配置application.yml

server:
  port: ${port:8081}
spring:
  application:
    name: consumer
  cloud:
    consul:
      #consul服務地址
      host: 127.0.0.1
      #consul服務端口
      port: 8500
      discovery:
        #是否注冊
        register: true
        #實例ID
        #        instance-id: ${spring.application.name}-${server.port}
        instance-id: ${spring.application.name}:${random.value}
        #服務實例名稱
        service-name: ${spring.application.name}
        #服務實例端口
        port: ${server.port}
        #健康檢查路徑
        healthCheckPath: /actuator/health
        #健康檢查時間間隔
        healthCheckInterval: 15s
        #開啟ip地址注冊
        prefer-ip-address: true
        #實例的請求ip
        ip-address: ${spring.cloud.client.ip-address}
        metadata:
          #添加自定義元數(shù)據(jù)
          my-name: zhangtieniu-consumer

7.3.2 添加支持負載均衡的RestTemplate

package com.ldx.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class ConsumerApplication {
   @Bean
   @LoadBalanced 
   public RestTemplate loadbalancedRestTemplate(){
      return new RestTemplate();
   }
   public static void main(String[] args) {
      SpringApplication.run(ConsumerApplication.class, args);
}

7.3.3 添加測試方法

package com.ldx.consumer.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
public class TestController {
    @Autowired
    private RestTemplate restTemplate;
    @GetMapping()
    public String consumer(){
        return this.restTemplate.getForObject("http://provider/products", String.class);
    }
}

7.4 啟動

啟動了兩個 provider 和一個 consumer

瀏覽器輸入localhost:8500 查看consul控制臺,顯示服務注冊成功

測試服務調(diào)用

其中provider 輸出的 實例信息如下:

[ConsulServiceInstance@4c2b7437 instanceId = 'consumer-6cfd981c90545313155d1f43c3ed23a5', serviceId = 'consumer', host = '192.168.0.101', port = 8081, secure = false, metadata = map['my-name' -> 'zhangtieniu-consumer', 'secure' -> 'false'], uri = http://192.168.0.101:8081, healthService = HealthService{node=Node{id='3fe6ea9e-3846-ff8d-b01f-a6528caaa3fd', node='44a66c1caa9c', address='172.26.0.2', datacenter='dc1', taggedAddresses={lan=172.26.0.2, lan_ipv4=172.26.0.2, wan=172.26.0.2, wan_ipv4=172.26.0.2}, meta={consul-network-segment=}, createIndex=11, modifyIndex=13}, service=Service{id='consumer-6cfd981c90545313155d1f43c3ed23a5', service='consumer', tags=[], address='192.168.0.101', meta={my-name=zhangtieniu-consumer, secure=false}, port=8081, enableTagOverride=false, createIndex=275, modifyIndex=275}, checks=[Check{node='44a66c1caa9c', checkId='serfHealth', name='Serf Health Status', status=PASSING, notes='', output='Agent alive and reachable', serviceId='', serviceName='', serviceTags=[], createIndex=11, modifyIndex=11}, Check{node='44a66c1caa9c', checkId='service:consumer-6cfd981c90545313155d1f43c3ed23a5', name='Service 'consumer' check', status=PASSING, notes='', output='HTTP GET http://192.168.0.101:8081/actuator/health: 200  Output: {"status":"UP"}', serviceId='consumer-6cfd981c90545313155d1f43c3ed23a5', serviceName='consumer', serviceTags=[], createIndex=275, modifyIndex=278}]}]

到此這篇關于SpringCloud-Consul的文章就介紹到這了,更多相關SpringCloud Consul內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Maven配置文件pom.xml詳解

    Maven配置文件pom.xml詳解

    什么是POM?這篇文章主要介紹了Maven的配置文件pom.xml,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • 一篇文章帶你深入了解Java線程池

    一篇文章帶你深入了解Java線程池

    這篇文章主要介紹了Java 線程池的相關資料,文中講解非常細致,幫助大家更好的理解和學習,感興趣的朋友可以了解下,希望能給你帶來幫助
    2021-08-08
  • SpringBoot框架整合SwaggerUI的示例代碼

    SpringBoot框架整合SwaggerUI的示例代碼

    項目中使用了很多現(xiàn)成的框架,都是項目經(jīng)理、架構師帶來的,從來沒有自己整合過,今天給大家介紹下SpringBoot框架整合SwaggerUI的過程,感興趣的朋友跟隨小編一起看看吧
    2022-02-02
  • 如何使用BufferedReader循環(huán)讀文件

    如何使用BufferedReader循環(huán)讀文件

    這篇文章主要介紹了如何使用BufferedReader循環(huán)讀文件的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 淺談java 中文件的讀取File、以及相對路徑的問題

    淺談java 中文件的讀取File、以及相對路徑的問題

    今天小編就為大家分享一篇淺談java 中文件的讀取File、以及相對路徑的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Java實現(xiàn)文件上傳與文件下載的示例代碼

    Java實現(xiàn)文件上傳與文件下載的示例代碼

    在開發(fā)中項目難免會遇到文件上傳和下載的情況,這篇文章主要為大家詳細介紹了Java中實現(xiàn)文件上傳與文件下載的示例代碼,希望對大家有所幫助
    2023-07-07
  • Java流處理stream使用詳解

    Java流處理stream使用詳解

    Java8的另一大亮點Stream,它與java.io包里的InputStream和OutputStream是完全不同的概念,下面這篇文章主要給大家介紹了關于Java8中Stream詳細使用方法的相關資料,需要的朋友可以參考下
    2022-10-10
  • 一文掌握MyBatis?Plus的條件構造器方法

    一文掌握MyBatis?Plus的條件構造器方法

    這篇文章主要介紹了MyBatis?Plus的條件構造器,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-02-02
  • Java比較兩個對象中全部屬性值是否相等的方法

    Java比較兩個對象中全部屬性值是否相等的方法

    本文主要介紹了Java比較兩個對象中全部屬性值是否相等的方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • spring cloud升級到spring boot 2.x/Finchley.RELEASE遇到的坑

    spring cloud升級到spring boot 2.x/Finchley.RELEASE遇到的坑

    這篇文章主要介紹了spring cloud升級到spring boot 2.x/Finchley.RELEASE遇到的坑,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08

最新評論