SpringCloud服務發現Eureka、zookeeper、consul


1、版本選擇

https://start.spring.io/actuator/info

 {
 "git": {
 "branch": "72506972926758dc1808eaa450281274ee85459a",
 "commit": {
 "id": "7250697",
 "time": "2020-08-17T15:37:11Z"
 }
 },
 "build": {
 "version": "0.0.1-SNAPSHOT",
 "artifact": "start-site",
 "versions": {
 "spring-boot": "2.3.3.RELEASE",
 "initializr": "0.9.2-SNAPSHOT"
 },
 "name": "start.spring.io website",
 "time": "2020-08-17T15:38:19.523Z",
 "group": "io.spring.start"
 },
 "bom-ranges": {
 "azure": {
 "2.0.10": "Spring Boot >=2.0.0.RELEASE and <2.1.0.RELEASE",
 "2.1.10": "Spring Boot >=2.1.0.RELEASE and <2.2.0.M1",
 "2.2.4": "Spring Boot >=2.2.0.M1 and <2.3.0.M1",
 "2.3.1": "Spring Boot >=2.3.0.M1"
 },
 "codecentric-spring-boot-admin": {
 "2.0.6": "Spring Boot >=2.0.0.M1 and <2.1.0.M1",
 "2.1.6": "Spring Boot >=2.1.0.M1 and <2.2.0.M1",
 "2.2.4": "Spring Boot >=2.2.0.M1 and <2.3.0.M1",
 "2.3.0": "Spring Boot >=2.3.0.M1 and <2.4.0-M1"
 },
 "solace-spring-boot": {
 "1.0.0": "Spring Boot >=2.2.0.RELEASE and <2.3.0.M1",
 "1.1.0": "Spring Boot >=2.3.0.M1"
 },
 "solace-spring-cloud": {
 "1.0.0": "Spring Boot >=2.2.0.RELEASE and <2.3.0.M1",
 "1.1.1": "Spring Boot >=2.3.0.M1"
 },
 "spring-cloud": {
 "Finchley.M2": "Spring Boot >=2.0.0.M3 and <2.0.0.M5",
 "Finchley.M3": "Spring Boot >=2.0.0.M5 and <=2.0.0.M5",
 "Finchley.M4": "Spring Boot >=2.0.0.M6 and <=2.0.0.M6",
 "Finchley.M5": "Spring Boot >=2.0.0.M7 and <=2.0.0.M7",
 "Finchley.M6": "Spring Boot >=2.0.0.RC1 and <=2.0.0.RC1",
 "Finchley.M7": "Spring Boot >=2.0.0.RC2 and <=2.0.0.RC2",
 "Finchley.M9": "Spring Boot >=2.0.0.RELEASE and <=2.0.0.RELEASE",
 "Finchley.RC1": "Spring Boot >=2.0.1.RELEASE and <2.0.2.RELEASE",
 "Finchley.RC2": "Spring Boot >=2.0.2.RELEASE and <2.0.3.RELEASE",
 "Finchley.SR4": "Spring Boot >=2.0.3.RELEASE and <2.0.999.BUILD-SNAPSHOT",
 "Finchley.BUILD-SNAPSHOT": "Spring Boot >=2.0.999.BUILD-SNAPSHOT and <2.1.0.M3",
 "Greenwich.M1": "Spring Boot >=2.1.0.M3 and <2.1.0.RELEASE",
 "Greenwich.SR6": "Spring Boot >=2.1.0.RELEASE and <2.1.17.BUILD-SNAPSHOT",
 "Greenwich.BUILD-SNAPSHOT": "Spring Boot >=2.1.17.BUILD-SNAPSHOT and <2.2.0.M4",
 "Hoxton.SR7": "Spring Boot >=2.2.0.M4 and <2.3.4.BUILD-SNAPSHOT",
 "Hoxton.BUILD-SNAPSHOT": "Spring Boot >=2.3.4.BUILD-SNAPSHOT and <2.4.0.M1",
 "2020.0.0-SNAPSHOT": "Spring Boot >=2.4.0.M1"
 },
 "spring-cloud-alibaba": {
 "2.2.1.RELEASE": "Spring Boot >=2.2.0.RELEASE and <2.3.0.M1"
 },
 "spring-cloud-services": {
 "2.0.3.RELEASE": "Spring Boot >=2.0.0.RELEASE and <2.1.0.RELEASE",
 "2.1.7.RELEASE": "Spring Boot >=2.1.0.RELEASE and <2.2.0.RELEASE",
 "2.2.3.RELEASE": "Spring Boot >=2.2.0.RELEASE and <2.3.0.M1"
 },
 "spring-statemachine": {
 "2.0.0.M4": "Spring Boot >=2.0.0.RC1 and <=2.0.0.RC1",
 "2.0.0.M5": "Spring Boot >=2.0.0.RC2 and <=2.0.0.RC2",
 "2.0.1.RELEASE": "Spring Boot >=2.0.0.RELEASE"
 },
 "vaadin": {
 "10.0.17": "Spring Boot >=2.0.0.M1 and <2.1.0.M1",
 "14.3.3": "Spring Boot >=2.1.0.M1 and <2.4.0-M1"
 },
 "wavefront": {
 "2.0.0": "Spring Boot >=2.1.0.RELEASE"
 }
 },
 "dependency-ranges": {
 "okta": {
 "1.2.1": "Spring Boot >=2.1.2.RELEASE and <2.2.0.M1",
 "1.4.0": "Spring Boot >=2.2.0.M1 and <2.4.0-M1"
 },
 "mybatis": {
 "2.0.1": "Spring Boot >=2.0.0.RELEASE and <2.1.0.RELEASE",
 "2.1.3": "Spring Boot >=2.1.0.RELEASE and <2.4.0-M1"
 },
 "geode": {
 "1.2.9.RELEASE": "Spring Boot >=2.2.0.M5 and <2.3.0.M1",
 "1.3.2.RELEASE": "Spring Boot >=2.3.0.M1 and <2.4.0-M1",
 "1.4.0-M1": "Spring Boot >=2.4.0-M1"
 },
 "camel": {
 "2.22.4": "Spring Boot >=2.0.0.M1 and <2.1.0.M1",
 "2.25.2": "Spring Boot >=2.1.0.M1 and <2.2.0.M1",
 "3.3.0": "Spring Boot >=2.2.0.M1 and <2.3.0.M1",
 "3.4.3": "Spring Boot >=2.3.0.M1 and <2.4.0-M1"
 },
 "open-service-broker": {
 "2.1.3.RELEASE": "Spring Boot >=2.0.0.RELEASE and <2.1.0.M1",
 "3.0.4.RELEASE": "Spring Boot >=2.1.0.M1 and <2.2.0.M1",
 "3.1.1.RELEASE": "Spring Boot >=2.2.0.M1 and <2.4.0-M1"
 }
 }
 }
 

image-20200820145837517

2月份最近,版本選擇依據官網!

2、停更組件

停更不停用:被動修復bugs,不再接受合並請求,不再發布新版本。停課不停學,666

以前:

image-20200820150209100

目前:cloud升級

image-20200820151257339

3、父工廠Project空間

步驟:

image-20200820164525170

走起

dependencyManagement和dependency的區別

dependencyManagement是父pom。

能讓所有子項目中引用一個依賴而不是顯示的列出版本號,Maven為沿着父子層次向上走,知道找到一個擁有dependencyManagement元素的項目,然后它會使用這個dependencyManagement元素中指定的版本號。

好處:

如果多個項目子項目都引用同一個依賴,則可以避免在每個使用的子項目里都聲明一個版本號,這樣當你想升級或者切換到另外一個版本時,只需要在頂層父容器里更新,而不需要一個一個子項目的修改;另外如果某個子項目需要另外一個版本,只需要聲明version即可。

dependencyManagement 只是聲明依賴,而不實現引入,因此子項目需要顯示的聲明需要的依賴,也就是dependency。

如果不在子項目聲明依賴,是不會從父項目中繼承下來的。只有在子項目中寫了該依賴項,並且沒有指定具體版本,才會從父項目中繼承該項,並且version和scope都讀取自父pom。

如果子項目中指定了版本號,那么會使用子項目中指定的jar版本。

如何跳過test單元測試

image-20200820174529419

 

點擊這個按鈕即可。

 
 <?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">
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>com.fage</groupId>
    <artifactId>springclouddemo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
 
 
    <name>發哥</name>
 
    <!-- 同意管理jar包版本 -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <junit.version>4.12</junit.version>
        <log4j.version>1.2.17</log4j.version>
        <lombok.version>1.16.18</lombok.version>
        <mysql.version>5.1.47</mysql.version>
        <druid.version>1.1.16</druid.version>
        <mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
    </properties>
 
    <!-- 子模塊繼承之后,提供作用:鎖定版本+子module不用謝groupId和version -->
    <dependencyManagement>
        <dependencies>
            <!-- spring-boot 2.2.2 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.2.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- spring-cloud Hoxton.SR7 spring cloud 是倫敦 地鐵站 從 a到z -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR7</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- spring-cloud-alibaba 2.1.0-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.1.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druid.version}</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis.spring.boot.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
 
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <addResources>true</addResources>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
 </project>
 

4、構建支付模塊

image-20200820180328484

1、cloud-provider-payment8001

微服務提供支付的module模塊

image-20200820205722751

 

微服務模塊:

1、建module

2、改pom

3、寫yml

4、主啟動

5、業務類

編寫業務:

image-20200820212524435

一步一步走

idea運行:

image-20200820223630764

設置為run dashBoard 模式

在 idea目錄下 workspace.xml末尾增加:

     <component name="RunDashboard">
         <option name="ruleStates">
             <list>
                 <RuleState>
                     <option name="name" value="ConfigurationTypeDashboardGroupingRule" />
                 </RuleState>
                 <RuleState>
                     <option name="name" value="StatusDashboardGroupingRule" />
                 </RuleState>
             </list>
         </option>
         <option name="contentProportion" value="0.22874807" />
         <option name="configurationTypes">
             <set>
                 <option value="SpringBootApplicationConfigurationType" />
             </set>
         </option>
     </component>
 

測試:

image-20200820224230975

開啟熱部署:

image-20200820224427996

1、pom依賴

         <!-- 熱部署 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

2、pom插件

     <build>
        <plugins>
            <!-- 配置熱部署 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <addResources>true</addResources>
                </configuration>
            </plugin>
        </plugins>
    </build>

3、設置idea編譯檢測功能

image-20200820224645851

3、設置idea功能

ctrl+shift+alt+/

image-20200820224902837

設置image-20200820225114242

image-20200820225153596

5、重啟即可

5、構建消費者訂單模塊

cloud-consumer-order80

RestTemplate 提供了多種便捷訪問遠程http服務的方法。

和httpClient和webservice同樣的操作

使用:

image-20200820231140915

config配置類:

     @Bean
     public RestTemplate restTemplate() {
         return new RestTemplate();
    }

代碼重構

將冗余的代碼重組,通過依賴使用。

新建 cloud-api-commons模塊

6、服務發現

服務注冊中心:

CAP理論:

為甚需要注冊中心:

比如:服務生產者多個服務,ip和端口

在服務調用者調用生產者接口不方便

1、Eureka

image-20200821100246476

避免單點故障,使用集群方式。

image-20200821101027409

實踐

單機Eureka服務端

a:新建模塊 cloud-eureka-server7001

b:改pom

         <!--eureka-server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
 <!--       <dependency>-->
 <!--           <groupId>org.springframework.cloud</groupId>-->
 <!--           <artifactId>spring-cloud-netflix-eureka-server</artifactId>-->
 <!--           <version>${spring.boot.version}</version>-->
 <!--       </dependency>-->

 

image-20200821101424914

c: 配置文件

 server:
  port: 7001
 
 eureka:
  instance:
    hostname: localhost # # eureka服務端的實例名稱
  client:
     # false 表示 不會向注冊中心 注冊自己
    register-with-eureka: false
     # false 表示自己端 就是注冊中心,我的職責就是維護服務實例,並不需要去檢索服務
    fetch-registry: false
    service-url:
       # 設置與eureka server 交互的地址查詢服務 和 注冊服務都需要依賴這個地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
 

d:啟動類

e:測試

http://localhost:7001/

調整cloud-provider-payment8001的服務,修改為eureka的client

pom修改

image-20200821111747460

啟動類增加

@EnableEurekaClient

刷新 7001服務端口頁面

紅色EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.是Eureka 的自我保護機制。

出錯:

image-20200821113050279

image-20200821113043326

yml對文件縮進要求嚴格。

集群改進

實現高可用(避免單點故障)

image-20200821113239233

步驟如上

工作流程:

image-20200821113316759

搭建 Eureka注冊中心集群,實現負載均衡+故障容錯。

互相注冊,互相守望

image-20200821122130126

客戶端訪問集群

service-url: defaultZone: 使用逗號隔開

hosts文件修改

 C:\Windows\System32\drivers\etc
 127.0.0.1 eureka7001.com
 127.0.0.1 eureka7002.com
 127.0.0.1 eureka7003.com

結果

image-20200821125107988

調用接口:http://localhost/consumer/payment/get/6

image-20200821125127076

完美

給服務提供者做集群。

新建cloud-provider-payment8002模塊

使用負載均衡:

ribbon 和 eureka 整合使用

1、RestTemplate配置增加

輪詢機制:

@LoadBalanced

2、路徑使用應用名稱(spring.application.name)

cloud-payment-service

actuator(圖形化顯示)

image-20200821132745594

去掉 主機名稱,和ip顯示問題

yml增加:

eureka:
  instance:
    ## 主機名稱 
    instance-id: payment8001
    ## 瀏覽器上 浮動名稱 左下角顯示 ip
    prefer-ip-address: true

查看健康狀態:

http://localhost:8001/actuator/health

服務發現Discovery

image-20200821133810043

白話就是將注冊到eureka的客戶端的服務顯示一下。

啟動類增加

@EnableDiscoveryClient

增加controller方法;

@Resource
private DiscoveryClient discoveryClient;

    @GetMapping(value = "/consumer/discovery")
    public CommonResult<Object> getDiscoveryClient() {
        List<String> services = discoveryClient.getServices();
        services.forEach(s -> {
            log.info("\n service = " + s);
            List<ServiceInstance> instances = discoveryClient.getInstances(s);
            instances.forEach(serviceInstance -> {
                log.info(serviceInstance.getInstanceId() + "\t" + serviceInstance.getUri());
            });
        });
        return new CommonResult<>(200, "success", discoveryClient);
    }

Eureka 自我保護

故障現象:

http://eureka7001.com:7001/ 紅字信息

原因:

某時刻某一個微服務不可用了,Eureka不會立刻清理,依舊會對該微服務的信息進行保存,屬於CAP原則里面的AP分支

高可用的一種機制

image-20200821140552245

image-20200821140611739

在自我保護模式中,Eureka Server 會保護服務注冊表中的信息,不再注銷任何服務實例。不盲目刪除,盡可能的保留實例。

怎么禁止自我保護?

eureka服務端

 eureka:
  server:
     # 關閉自我保護機制,保證不可用服務及時剔除
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 2000
 

eureka客戶端

 eureka:
  instance:
    instance-id: order80
     ##訪問路徑顯示ip地址
    prefer-ip-address: true
     ## Eureka 客戶端向服務器發送心跳的時間間隔 ( 默認 為 30秒)
    lease-renewal-interval-in-seconds: 1
     ## Eureka 服務端在收到最后一次心跳后等待時間上線,單位為秒 (默認為90秒) ,超時將剔除該服務
    lease-expiration-duration-in-seconds: 2

訪問eureka服務頁面

顯示THE SELF PRESERVATION MODE IS TURNED OFF. THIS MAY NOT PROTECT INSTANCE EXPIRY IN CASE OF NETWORK/OTHER PROBLEMS.

測試: 將啟動后的服務關閉,看服務頁面中的實例是否消失,即可測試完成。

Eureka停更不停用。。。就別用了

Zookeper替換Eureka

Zookeper是一個分布式的協調工具,可以實現注冊中心,關閉Linux服務器防火牆后啟動Zookeper服務器

安裝Zookeper

docker pull zookeeper

啟動容器 
docker run --privileged=true -d --name zookeeper --publish 2181:2181  -d zookeeper:latest

docker ps 

2020年8月21日15:26:26
看啟動日志 version  3.6.1

查看容器是否啟動
docker ps

進入容器
docker exec -it f76b7e25baa9 /bin/bash

進入bin目錄
cd bin

登錄server
zkCli.sh -server 127.0.0.1:2181

查看目錄
ls /

搭建Zookeper服務提供者

zookeeper版本如果高,pom中可以使用低一點的。

如果zookeeper版本低,pom中不可以使用高版本

1、pom

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.zookeeper</groupId>
                    <artifactId>zookeeper</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.8</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

2、yml

server:
  port: 8004

spring:
  application:
    name: cloud-payment-service
  cloud:
    zookeeper:
      connect-string: 192.168.71.134:2181

zookeeper服務:

image-20200821145824365

接口層:

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/payment/zk")
    public CommonResult<Object> getPaymentById() {
        List<String> services = discoveryClient.getServices();
        services.forEach(s -> {
            log.info(" service = " + s);
            List<ServiceInstance> instances = discoveryClient.getInstances(s);
            instances.forEach(serviceInstance -> {
                log.info(serviceInstance.getInstanceId() + "\t" + serviceInstance.getUri());
            });
        });
        return new CommonResult<>(200, "Spring cloud  with zookeeper ,提供者port:" + port, UUID.randomUUID().toString());
    }

調用結果:

2020-08-21 15:27:40.190 INFO 40864 --- [nio-8004-exec-1] c.f.s.controller.PaymentController : service = cloud-payment-service 2020-08-21 15:27:40.195 INFO 40864 --- [nio-8004-exec-1] c.f.s.controller.PaymentController : 85b4c21c-cf51-47bd-bce5-89480035ec76 http://localhost:8004

[zk: 127.0.0.1:2181(CONNECTED) 3] ls /
[services, zookeeper]
[zk: 127.0.0.1:2181(CONNECTED) 4] ls /services
[cloud-payment-service]
[zk: 127.0.0.1:2181(CONNECTED) 5] ls /zookeeper
[config, quota]
[zk: 127.0.0.1:2181(CONNECTED) 6] ls /services/cloud-payment-service
[85b4c21c-cf51-47bd-bce5-89480035ec76]
[zk: 127.0.0.1:2181(CONNECTED) 7] ls /services/cloud-payment-service/85b4c21c-cf51-47bd-bce5-89480035ec76
[]
[zk: 127.0.0.1:2181(CONNECTED) 8] get  /services/cloud-payment-service/85b4c21c-cf51-47bd-bce5-89480035ec76
{"name":"cloud-payment-service","id":"85b4c21c-cf51-47bd-bce5-89480035ec76","address":"localhost","port":8004,"sslPort":null,"payload":{"@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance","id":"application-1","name":"cloud-payment-service","metadata":{"instance_status":"UP"}},"registrationTimeUTC":1597994727162,"serviceType":"DYNAMIC","uriSpec":{"parts":[{"value":"scheme","variable":true},{"value":"://","variable":false},{"value":"address","variable":true},{"value":":","variable":false},{"value":"port","variable":true}]}}
[zk: 127.0.0.1:2181(CONNECTED) 9] 

見到有 json字符串 即成功

Zookeeper : 臨時還是持久節點

測試:將服務提供者關閉, 在zookeeper的zk工具查看services。

停了之后不會直接刪除。過了一段時間將其刪除了(40000ms)。

刪除的序列號

[85b4c21c-cf51-47bd-bce5-89480035ec76]

zookeeper 里面 以序列號作為節點。~!

消費zookeeper服務提供者模塊

cloud-consumer-zk-order80

spring:
  application:
    name: cloud-payment-zk-service
  cloud:
    zookeeper:
      connect-string: 192.168.71.134:2181,192.168.170.131:2181
      ## 集群 使用逗號隔開就行
@LoadBalanced
輪訓機制

Consul

學習 方法:是什么,能干嘛,去那下,怎么做

理論、實操、小筆記

是一套開源的分布式服務發現和配置管理系統。

提供了服務治理(注冊於發現)、健康檢測、配置中心、KV存儲、控制總線、多數據中心,可視化Web界面。

中文文檔:

http://springcloud.cc/spring-cloud-consul.html

1、安裝consul

# 設置yum源

yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum makecache fast

# 安裝Docker-CE

yum -y install docker-ce

#設置開機自啟動

systemctl enable docker 
systemctl restart docker 

#驗證

docker run hello-world

#指定consul版本、這邊與開發確認后獲取最新版本以及單節點即可

docker pull consul

#期望容器中的配置文件、數據、日志再容器異常關閉或主動刪除后保留,則需要配置掛載

#容器對應配置文件路徑都是從https://hub.docker.com/_/consul上對應的官網操作中確認過的

mkdir -p /data/consul/{conf,data}

#運行容器、映射到宿主機,掛載Volume

docker run \
--name consul \
-p 8599:8500 \
-v /data/consul/conf/:/consul/conf/ \
-v /data/consul/data/:/consul/data/ \
-d consul

# 不使用掛載目錄

 --restart=no / always 不自動/自動開啟docker容器

docker run --restart=no --name consul -p 8500:8500 -d consul:latest 

創建consul服務提供者模塊

cloud-provider-consul-payment8006

pom依賴:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

yml配置

 server:
  port: 8006
 spring:
  application:
    name: cloud-payment-consul-service
  cloud:
    consul:
      host: 192.168.170.131
      port: 8500
      discovery:
         ## 服務發現 注冊
        service-name: ${spring.application.name}

main方法

 @SpringBootApplication
 @EnableDiscoveryClient
 public class PaymentConsulApplication {
 
     public static void main(String[] args) {
         SpringApplication.run(PaymentConsulApplication.class, args);
    }
 }

啟動發現:

docker 運行日志

 

web頁面顯示

consul啟動到管理頁面時,出現一個Service Checks = false

這個是由於actuator/health無法檢測,因為項目中的consul需要檢測服務的健康狀態。

image-20200822122956911

訪問consul的健康檢測

image-20200822123101960

 

 

解決

server:
  port: 8006
spring:
  application:
    name: cloud-payment-consul-service
  cloud:
    consul:
      host: 192.168.170.131
      port: 8500
      discovery:
        ## 服務發現 注冊
        service-name: ${spring.application.name}
        enabled: true
        # 使用 consul 服務器 IP, 而不是 hostname, 需要搭配 prefer-ip-address 屬性
        ip-address: 127.0.0.1
        # 在注冊時使用 consul IP, 而不是 hostname
        prefer-ip-address: true
        # 制定檢查的ip
        health-check-url: http://${localhost}:${server.port}/actuator/health
## 本地ip
localhost: 192.168.0.4

結果:

image-20200822123600729

image-20200822123628842image-20200822123629237

沒有再爆紅,調用提供者服務也正常

調用consul消費者模塊

cloud-consumer-consul-order80

類似提供者和其他消費者。

集群: 修改提供者端口即可。消費者使用@LoadBalanced做到輪詢

三者對比

image-20200822125658955

c:Consistency (強一致性)數據一致

a:Availability (可用性)高可用

p:Partution tolerance(分區容錯性)集群

CAP理論關注粒度是數據,而不是整體系統設計的策略。

最多只能同時較好的滿足倆個。

image-20200822130046453

AP Eureka :

當網絡分區出現后,為了保證可用性,系統B可以返回值,保證系統的可用性,結論:違背了一致性C的要求,只滿足可用性和分區容錯,即AP。

image-20200822130245170

CP Zookeeper/Consul :

當網絡分區出現后,為了保證一致性,就必須拒接請求,否則無法保證一致性。結論:違背了可用性A的要求,只滿足一致性和分區容錯,即CP。

image-20200822130526966

 

公眾號發哥講

這是一個稍偏基礎和偏技術的公眾號,甚至其中包括一些可能閱讀量很低的包含代碼的技術文,不知道你是不是喜歡,期待你的關注。

代碼分享

https://gitee.com/naimaohome/springclouddemo.git

微信公眾號 點擊關於我,加入QQ群,即可獲取到代碼以及高級進階視頻和電子書!!

img

如果你覺得文章還不錯,就請點擊右上角選擇發送給朋友或者轉發到朋友圈~

● 掃碼關注我們

據說看到好文章不推薦的人,服務器容易宕機!

本文版權歸 發哥講博客園 共有,原創文章,未經允許不得轉載,否則保留追究法律責任的權利。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM