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

SpringCloud中的Consul詳解

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

1. Consul 簡(jiǎn)介

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

2. 專業(yè)名詞

  • agent

組成 consul 集群的每個(gè)成員上都要運(yùn)行一個(gè) agent,可以通過(guò) consul agent 命令來(lái)啟動(dòng)。agent可以運(yùn)行在 server 狀態(tài)或者 client 狀態(tài)。自然的, 運(yùn)行在 server 狀態(tài)的節(jié)點(diǎn)被稱為 server 節(jié)點(diǎn),運(yùn)行在 client 狀態(tài)的節(jié)點(diǎn)被稱 為 client 節(jié)點(diǎn)。

  • server 節(jié)點(diǎn)

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

  • client 節(jié)點(diǎn)

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

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

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

3. Consul 的優(yōu)勢(shì)

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

綜合比較, Consul 作為服務(wù)注冊(cè)和配置管理的新星, 比較值得關(guān)注和研究。

4. 特性

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

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

1.一致性 Consul強(qiáng)一致性(CP)

  • 服務(wù)注冊(cè)相比Eureka會(huì)稍慢一些。因?yàn)镃onsul的raft協(xié)議要求必須過(guò)半數(shù)的節(jié)點(diǎn)都寫入成功才認(rèn) 為注冊(cè)成功
  • Leader掛掉時(shí),重新選舉期間整個(gè)consul不可用。保證了強(qiáng)一致性但犧牲了可用性。

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

  • 服務(wù)注冊(cè)相對(duì)要快,因?yàn)椴恍枰茸?cè)信息replicate到其他節(jié)點(diǎn),也不保證注冊(cè)信息是否 replicate成功
  • 當(dāng)數(shù)據(jù)出現(xiàn)不一致時(shí),雖然A, B上的注冊(cè)信息不完全相同,但每個(gè)Eureka節(jié)點(diǎn)依然能夠正常對(duì)外提 供服務(wù),這會(huì)出現(xiàn)查詢服務(wù)信息時(shí)如果請(qǐng)求A查不到,但請(qǐng)求B就能查到。如此保證了可用性但犧牲了一致性。

6. Consul 安裝

consul docker-hub

6.1 docker-compose安裝

以dev模式啟動(dòng) 且 設(shè)置client=0.0.0.0為所有ip都可以連接此服務(wù)

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

服務(wù)啟動(dòng)成功后,通過(guò)瀏覽器訪問(wèn)localhost:8500,顯示如下頁(yè)面即安裝成功。

7. Quick Start

7.1 啟動(dòng)consul服務(wù)

本文使用的是docker-compose方式管理consul服務(wù),直接啟動(dòng)即可

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

7.2.1 引入依賴坐標(biāo)

<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服務(wù)地址
      host: 127.0.0.1
      #consul服務(wù)端口
      port: 8500
      discovery:
        #是否注冊(cè)
        register: true
        #實(shí)例ID
        #        instance-id: ${spring.application.name}-${server.port}
        instance-id: ${spring.application.name}:${random.value}
        #服務(wù)實(shí)例名稱
        service-name: ${spring.application.name}
        #服務(wù)實(shí)例端口
        port: ${server.port}
        #健康檢查路徑
        healthCheckPath: /actuator/health
        #健康檢查時(shí)間間隔
        healthCheckInterval: 15s
        #開啟ip地址注冊(cè)
        prefer-ip-address: true
        #實(shí)例的請(qǐng)求ip
        ip-address: ${spring.cloud.client.ip-address}

7.2.3 添加測(cè)試方法

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)建過(guò)程和provider一樣 測(cè)試方法換一下,并且在啟動(dòng)類上添加RestTemplate Bean

7.3.1 配置application.yml

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

7.3.2 添加支持負(fù)載均衡的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 添加測(cè)試方法

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 啟動(dòng)

啟動(dòng)了兩個(gè) provider 和一個(gè) consumer

瀏覽器輸入localhost:8500 查看consul控制臺(tái),顯示服務(wù)注冊(cè)成功

測(cè)試服務(wù)調(diào)用

其中provider 輸出的 實(shí)例信息如下:

[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}]}]

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

相關(guān)文章

  • Maven配置文件pom.xml詳解

    Maven配置文件pom.xml詳解

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

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

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

    SpringBoot框架整合SwaggerUI的示例代碼

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

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

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

    淺談java 中文件的讀取File、以及相對(duì)路徑的問(wèn)題

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

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

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

    Java流處理stream使用詳解

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

    一文掌握MyBatis?Plus的條件構(gòu)造器方法

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

    Java比較兩個(gè)對(duì)象中全部屬性值是否相等的方法

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

    spring cloud升級(jí)到spring boot 2.x/Finchley.RELEASE遇到的坑

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

最新評(píng)論