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 入門案例視頻(獲取更多請關注公眾號「哈嘍沃德先生」)

  eureka-demo 聚合工程。SpringBoot 2.2.4.RELEASESpring Cloud Hoxton.SR1

  

創建項目

  

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

  

添加依賴

  

  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">
    <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="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.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: http://${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);
    }

}

  

訪問

  

  訪問:http://localhost:8761/

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

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

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

  

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

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


免責聲明!

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



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