Consul簡介

What is Consul?

Consul is a service mesh solution providing a full featured control plane with service discovery, configuration, and segmentation functionality. Each of these features can be used individually as needed, or they can be used together to build a full service mesh. Consul requires a data plane and supports both a proxy and native integration model. Consul ships with a simple built-in proxy so that everything works out of the box, but also supports 3rd party proxy integrations such as Envoy. link

Consul是一個服務網格解決方案,它提供了一個功能齊全的控制平面,具有服務發現、配置和分段功能。這些特性中的每一個都可以根據需要單獨使用,也可以一起用於構建全服務網格。Consul需要一個數據平面,並支援代理和本機整合模型。Consul船與一個簡單的內建代理,使一切工作的開箱即用,但也支援第三方代理整合,如Envoy。

Consul是一套開源的分散式服務發現和配置管理系統,由HashiCorp 公司用Go語言開發。

提供了微服務系統中的服務治理、配置中心、控制匯流排等功能。這些功能中的每一個都可以根據需要單獨使用,也可以一起使用以構建全方位的服務網格,總之Consul提供了一種完整的服務網格解決方案。

它具有很多優點。包括:基於raft協議,比較簡潔;支援健康檢查,同時支援HTTP和DNS協議支援跨資料中心的WAN叢集提供圖形介面跨平臺,支援Linux、Mac、Windows。

Consul能做什麼

  • 服務發現 - 提供HTTP和DNS兩種發現方式。
  • 健康監測 - 支援多種方式,HTTP、TCP、Docker、Shell指令碼定製化
  • KV儲存 - Key、Value的儲存方式
  • 多資料中心 - Consul支援多資料中心
  • 視覺化Web介面
  • 如何使用

安裝並執行Consul

官網安裝說明

直接下載最新的,上面有下載地址,下載後解壓就一個檔案

在當然路徑進入CMD

檢視版本

consul.exe --version
Consul v1.10.1
Revision db839f18b
Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)

我的版本是最新的1.10.1

使用開發模式啟動

consul.exe agent -dev

訪問控制檯

http://localhost:8500

提供者註冊服務到Consul

新建8006工程

新建cloud-providerconsul-payment8006工程

修改POM.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>com.dance.springcloud</artifactId>
<groupId>com.dance</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>cloud-providerconsul-payment8006</artifactId> <properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties> <dependencies>
<!-- dependent on common modules -->
<dependency>
<groupId>com.dance</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--SpringCloud consul-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!-- SpringBoot整合Web元件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--日常通用jar包配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies> </project>

新增yml配置

###consul服務埠號
server:
port: 8006 spring:
application:
name: consul-provider-payment
####consul註冊中心地址
cloud:
consul:
host: localhost
port: 8500
discovery:
#hostname: 127.0.0.1
service-name: ${spring.application.name}

新增主啟動類

package com.dance.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8006 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8006.class, args);
}
}

新增Controller

package com.dance.springcloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.UUID; @RestController
@Slf4j
public class PaymentController {
@Value("${server.port}")
private String serverPort; @RequestMapping(value = "/payment/consul")
public String paymentConsul() {
return "springcloud with consul: " + serverPort + "\t " + UUID.randomUUID().toString();
}
}

測試

測試成功~

消費者註冊服務到Consul

新建80工程

新建cloud-consumerconsul-order80工程

修改POM.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>com.dance.springcloud</artifactId>
<groupId>com.dance</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>cloud-consumerconsul-order80</artifactId> <properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties> <dependencies>
<!--SpringCloud consul-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!-- SpringBoot整合Web元件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--日常通用jar包配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </project>

新增yml配置

###consul服務埠號
server:
port: 80 spring:
application:
name: cloud-consumer-order
####consul註冊中心地址
cloud:
consul:
host: localhost
port: 8500
discovery:
#hostname: 127.0.0.1
service-name: ${spring.application.name}

新增主啟動類

package com.dance.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication
@EnableDiscoveryClient //該註解用於向使用consul或者zookeeper作為註冊中心時註冊服務
public class OrderConsulMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderConsulMain80.class, args);
}
}

新增配置類

package com.dance.springcloud.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate; /**
*
*/
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}

新增Controller

package com.dance.springcloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; @RestController
@Slf4j
public class OrderConsulController {
public static final String INVOKE_URL = "http://consul-provider-payment"; @Resource
private RestTemplate restTemplate; @GetMapping(value = "/consumer/payment/consul")
public String paymentInfo() {
String result = restTemplate.getForObject(INVOKE_URL + "/payment/consul", String.class);
return result;
}
}

測試

  1. 啟動Consul
  2. 啟動提供者和消費者工程
  3. Consul控制檯測試
  4. 瀏覽器測試
    1. http://localhost/consumer/payment/consul

測試完成~

三個註冊中心對比

元件名

語言

CAP

服務健康檢查

對外暴露介面

Spring Cloud整合

Eureka

Java

AP

可配支援

HTTP

已整合

Consul

Go

CP

支援

HTTP/DNS

已整合

Zookeeper

Java

CP

支援

客戶端

已整合

CAP

C:Consistency (強一致性)

A:Availability (可用性)

P:Partition tolerance (分割槽容錯性)

最多隻能同時較好的滿足兩個。

CAP理論的核心是:一個分散式系統不可能同時很好的滿足一致性,可用性和分割槽容錯性這三個需求。

因此,根據CAP原理將NoSQL資料庫分成了滿足CA原則、滿足CP原則和滿足AP原則三大類:

  • CA - 單點叢集,滿足—致性,可用性的系統,通常在可擴充套件性上不太強大。
  • CP - 滿足一致性,分割槽容忍必的系統,通常效能不是特別高。
  • AP - 滿足可用性,分割槽容忍性的系統,通常可能對一致性要求低一些。

AP架構(Eureka)

當網路分割槽出現後,為了保證可用性,系統B可以返回舊值,保證系統的可用性。

結論:違背了一致性C的要求,只滿足可用性和分割槽容錯,即AP

CP架構(ZooKeeper/Consul)

當網路分割槽出現後,為了保證一致性,就必須拒接請求,否則無法保證一致性。

結論:違背了可用性A的要求,只滿足一致性和分割槽容錯,即CP。

CP 與 AP 對立同一的矛盾關係。

到這裡Consul就寫完了~完結撒花

作者:彼岸舞

時間:2021\08\22

內容關於:Spring Cloud H版

本文屬於作者原創,未經允許,禁止轉發