Spring Cloud 系列之 Netflix Eureka 註冊中心(一)

服務註冊中心是服務實現服務化管理的核心組件,類似於目錄服務的作用,主要用來存儲服務資訊,譬如提供者 url 串、路由資訊等。服務註冊中心是微服務架構中最基礎的設施之一。

  在微服務架構流行之前,註冊中心就已經開始出現在分散式架構的系統中。Dubbo 是一個在中國比較流行的分散式框架,被大量的中小型互聯網公司所採用,它提供了比較完善的服務治理功能,而服務治理的實現主要依靠的就是註冊中心。

  

什麼是註冊中心

  

  註冊中心可以說是微服務架構中的「通訊錄」,它記錄了服務和服務地址的映射關係。在分散式架構中,服務會註冊到這裡,當服務需要調用其它服務時,就到這裡找到服務的地址,進行調用。

  舉個現實生活中的例子,比如說,我們手機中的通訊錄的兩個使用場景:

當我想給張三打電話時,那我需要在通訊錄中按照名字找到張三,然後就可以找到他的手機號撥打電話。—— 服務發現

李四辦了手機號並把手機號告訴了我,我把李四的號碼存進通訊錄,後續,我就可以從通訊錄找到他。—— 服務註冊

通訊錄 —— ?什麼角色(提示:服務註冊中心)

  總結:服務註冊中心的作用就是服務的註冊服務的發現

  

常見的註冊中心

  

  • Netflix Eureka
  • Alibaba Nacos
  • HashiCorp Consul
  • Apache ZooKeeper
  • CoreOS Etcd
  • CNCF CoreDNS

  

特性 Eureka Nacos Consul Zookeeper
CAP AP CP + AP CP CP
健康檢查 Client Beat TCP/HTTP/MYSQL/Client Beat TCP/HTTP/gRPC/Cmd Keep Alive
雪崩保護
自動註銷實例 支援 支援 不支援 支援
訪問協議 HTTP HTTP/DNS HTTP/DNS TCP
監聽支援 支援 支援 支援 支援
多數據中心 支援 支援 支援 不支援
跨註冊中心同步 不支援 支援 支援 不支援
SpringCloud集成 支援 支援 支援 支援

  

為什麼需要註冊中心

  

  了解了什麼是註冊中心,那麼我們繼續談談,為什麼需要註冊中心。在分散式系統中,我們不僅僅是需要在註冊中心找到服務和服務地址的映射關係這麼簡單,我們還需要考慮更多更複雜的問題:

  • 服務註冊後,如何被及時發現
  • 服務宕機後,如何及時下線
  • 服務如何有效的水平擴展
  • 服務發現時,如何進行路由
  • 服務異常時,如何進行降級
  • 註冊中心如何實現自身的高可用

  這些問題的解決都依賴於註冊中心。簡單看,註冊中心的功能有點類似於 DNS 伺服器或者負載均衡器,而實際上,註冊中心作為微服務的基礎組件,可能要更加複雜,也需要更多的靈活性和時效性。所以我們還需要學習更多 Spring Cloud 微服務組件協同完成應用開發。

  

註冊中心解決了什麼問題

  

  • 服務管理
  • 服務的依賴關係管理

  

什麼是 Eureka 註冊中心

  

  Eureka 是 Netflix 開發的服務發現組件,本身是一個基於 REST 的服務。Spring Cloud 將它集成在其子項目 Spring Cloud Netflix 中,實現 Spring Cloud 的服務註冊與發現,同時還提供了負載均衡、故障轉移等能力。

  

Eureka 註冊中心三種角色

  

Eureka Server

  

  通過 Register、Get、Renew 等介面提供服務的註冊和發現。

  

Application Service(Service Provider)

  

  服務提供方,把自身的服務實例註冊到 Eureka Server 中。

  

Application Client(Service Consumer)

  

  服務調用方,通過 Eureka Server 獲取服務列表,消費服務。

  

  

Eureka 入門案例

  

創建項目

  

  我們創建聚合項目來講解 Eureka,首先創建一個 pom 父工程。

  

添加依賴

  

  pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="//maven.apache.org/POM/4.0.0"
         xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="//maven.apache.org/POM/4.0.0 //maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!-- 項目坐標地址 -->
    <groupId>com.example</groupId>
    <!-- 項目模組名稱 -->
    <artifactId>eureka-demo</artifactId>
    <!-- 項目版本名稱 快照版本SNAPSHOT、正式版本RELEASE -->
    <version>1.0-SNAPSHOT</version>

    <!-- 繼承 spring-boot-starter-parent 依賴 -->
    <!-- 使用繼承方式,實現復用,符合繼承的都可以被使用 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
    </parent>

    <!--
        集中定義依賴組件版本號,但不引入,
        在子工程中用到聲明的依賴時,可以不加依賴的版本號,
        這樣可以統一管理工程中用到的依賴版本
     -->
    <properties>
        <!-- Spring Cloud Hoxton.SR1 依賴 -->
        <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
    </properties>

    <!-- 項目依賴管理 父項目只是聲明依賴,子項目需要寫明需要的依賴(可以省略版本資訊) -->
    <dependencyManagement>
        <dependencies>
            <!-- spring cloud 依賴 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

  

註冊中心 eureka-server

  

  在剛才的父工程下創建 eureka-server 註冊中心的項目。

  

創建項目

  

  

添加依賴

  

  pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="//maven.apache.org/POM/4.0.0" xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="//maven.apache.org/POM/4.0.0 //maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>eureka-server</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!-- 繼承父依賴 -->
    <parent>
        <groupId>com.example</groupId>
        <artifactId>eureka-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <!-- 項目依賴 -->
    <dependencies>
        <!-- netflix eureka server 依賴 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!-- spring boot web 依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- spring boot test 依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

</project>

  

配置文件

  

  application.yml

server:
  port: 8761 # 埠

spring:
  application:
    name: eureka-server # 應用名稱

# 配置 Eureka Server 註冊中心
eureka:
  instance:
    hostname: localhost			  # 主機名,不配置的時候將根據作業系統的主機名來獲取
  client:
    register-with-eureka: false   # 是否將自己註冊到註冊中心,默認為 true
    fetch-registry: false         # 是否從註冊中心獲取服務註冊資訊,默認為 true
    service-url:                  # 註冊中心對外暴露的註冊地址
      defaultZone: //${eureka.instance.hostname}:${server.port}/eureka/

  此時如果直接啟動項目是會報錯的,錯誤資訊:com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect,這是因為 Eureka 默認開啟了將自己註冊至註冊中心從註冊中心獲取服務註冊資訊的配置,如果該應用的角色是註冊中心並是單節點的話,要關閉這兩個配置項。

  

啟動類

  

  EurekaServerApplication.java

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
// 開啟 EurekaServer 註解
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }

}

  

訪問

  

  訪問://localhost:8761/

下一篇我們講解 Eureka 集群、架構原理、自我保護、優雅停服、安全認證等功能實現。記得關注噢~

  本文採用 知識共享「署名-非商業性使用-禁止演繹 4.0 國際」許可協議

  大家可以通過 分類 查看更多關於 Spring Cloud 的文章。

  

  🤗 您的點贊轉發是對我最大的支援。

  📢 掃碼關注 哈嘍沃德先生「文檔 + 影片」每篇文章都配有專門影片講解,學習更輕鬆噢 ~

哈嘍沃德先生