1. 程式人生 > 其它 >(五)、Eureka服務註冊中心

(五)、Eureka服務註冊中心

EureKa基礎知識

什麼是服務治理

Spring Cloud封裝了Netflix 公司開發的Eureka模組來實現服務治理

在傳統的RPC遠端呼叫框架中,管理每個服務與服務之間依賴關係比較複雜,管理比較複雜,所以需要使用服務治理,管理服務於服務之間依賴關係,可以實現服務呼叫、負載均衡、容錯等,實現服務發現與註冊。

什麼是服務註冊與發現

Eureka採用了CS的設計架構,Eureka Sever作為服務註冊功能的伺服器,它是服務註冊中心。而系統中的其他微服務,使用Eureka的客戶端連線到 Eureka Server並維持心跳連線。這樣系統的維護人員就可以通過Eureka Server來監控系統中各個微服務是否正常執行。

在服務註冊與發現中,有一個註冊中心。當伺服器啟動的時候,會把當前自己伺服器的資訊比如服務地址通訊地址等以別名方式註冊到註冊中心上。另一方(消費者服務提供者),以該別名的方式去註冊中心上獲取到實際的服務通訊地址,然後再實現本地RPC呼叫RPC遠端呼叫框架核心設計思想:在於註冊中心,因為使用註冊中心管理每個服務與服務之間的一個依賴關係(服務治理概念)。在任何RPC遠端框架中,都會有一個註冊中心存放服務地址相關資訊(介面地址)

Eureka包含兩個元件:Eureka Server和Eureka Client

Eureka Server提供服務註冊服務

各個微服務節點通過配置啟動後,會在EurekaServer中進行註冊,這樣EurekaServer中的服務登錄檔中將會儲存所有可用服務節點的資訊,服務節點的資訊可以在介面中直觀看到。

EurekaClient通過註冊中心進行訪問

它是一個Java客戶端,用於簡化Eureka Server的互動,客戶端同時也具備一個內建的、使用輪詢(round-robin)負載演算法的負載均衡器。在應用啟動後,將會向Eureka Server傳送心跳(預設週期為30秒)。如果Eureka Server在多個心跳週期內沒有接收到某個節點的心跳,EurekaServer將會從服務登錄檔中把這個服務節點移除(預設90秒)

單機EureKa構建步驟

1.Idea生成Server端服務註冊中心

建Module

建立名為cloud-eureka-server7001的Maven工程

改Pom

<?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>cloud2021</artifactId>
        <groupId>com.ylc.cloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-eureka-server7001</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <!--eureka-server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!-- 引入自己定義的api通用包,可以使用Payment支付Entity -->
        <dependency>
            <groupId>com.ylc.cloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--boot web actuator-->
        <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>
        <!--一般通用配置-->
        <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>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>
</project>

寫Yml

server:
  port: 7001

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

主啟動

@EnableEurekaServer標記這是EureKa的服務註冊中心

package com.ylc.cloud;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

測試

測試執行EurekaMain7001,瀏覽器輸入http://localhost:7001/回車,會檢視到Spring Eureka服務主頁

顯示空白,還沒有任何服務註冊進來

2.支付微服務8001入駐進EurekaServer

EureKa服務端已進入駐,現在開始入駐客戶端

修改cloud-provider-payment8001

新增spring-cloud-starter-netflix-eureka-client依賴

改POM

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

改YML

eureka:
  client:
    #表示是否將自己註冊進Eurekaserver預設為true。
    register-with-eureka: true
    #是否從EurekaServer抓取已有的註冊資訊,預設為true。單節點無所謂,叢集必須設定為true才能配合ribbon使用負載均衡
    fetchRegistry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

主啟動

增加註解標記@EnableEurekaClient

測試

啟動cloud-provider-payment8001和cloud-eureka-server7001工程

進入http://localhost:7001/,主頁內的Instances currently registered with Eureka會顯示cloud-provider-payment8001的配置檔案application.yml設定的應用名cloud-payment-service

6.自我保護機制

EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY’RE NOT. RENEWALS ARELESSER THAN THRESHOLD AND HENCFT ARE NOT BEING EXPIRED JUST TO BE SAFE.

緊急情況!EUREKA可能錯誤地聲稱例項在沒有啟動的情況下啟動了。續訂小於閾值,因此例項不會為了安全而過期。

3.訂單微服務80入駐進EurekaServer

cloud-consumer-order80

改POM

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

改YML

server:
  port: 80
  
spring:
  application:
    name: cloud-order-service

eureka:
  client:
    #表示是否將自己註冊進Eurekaserver預設為true。
    register-with-eureka: true
    #是否從EurekaServer抓取已有的註冊資訊,預設為true。單節點無所謂,叢集必須設定為true才能配合ribbon使用負載均衡
    fetchRegistry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

主啟動

增加註解標記@EnableEurekaClient

測試

啟動cloud-provider-payment8001、cloud-eureka-server7001和cloud-consumer-order80這三工程。
瀏覽器輸入 http://localhost:7001 , 在主頁的Instances currently registered with Eureka將會看到cloud-provider-payment8001、cloud-consumer-order80兩個工程名。

注意

application.yml配置中層次縮排和空格,兩者不能少,否則,會丟擲異常Failed to bind properties under 'eureka.client.service-url' to java.util.Map <java.lang.String, java.lang.String>