天天看點

SpringCloud--Eureka

Eureka服務注冊與發現

  • Eureka基礎知識
  • 服務治理
  • 服務注冊
  • Eureka兩組建件

注冊中心-Eureka

介紹

又稱服務中心,管理各種服務功能包括服務的注冊、發現、熔斷、負載、降級等。

==什麼是注冊與發現==

  • 在服務注冊與發現中,有一個注冊中心。當伺服器啟動的時候,會把目前自己伺服器的資訊,比如 服務位址通訊位址等以别名等方式注冊到注冊中心上,另一方(消費者),以該别名的方式去注冊中心上擷取到實際的服務通訊位址,然後在實作本地RPC調用RPC遠端調用架構核心設計思想:自阿雨注冊中心,因為使用注冊中心管理每個服務與服務之間的一個依賴關系(==服務治理概念==)。在任何rpc遠端架構中,都會有一個注冊中心(存放接口位址)

Spring Cloud 封裝了 Netflix 公司開發的 一個基于 REST 服務的,服務注冊與發現的元件。Eureka 采用了 C-S 的設計架構。Eureka Server 作為服務注冊功能的伺服器,它是服務注冊中心。而系統中的其他微服務,使用 Eureka 的用戶端連接配接到 Eureka Server,并維持心跳連接配接。這樣系統的維護人員就可以通過 Eureka Server 來監控系統中各個微服務是否正常運作。Spring Cloud 的一些其他子產品(比如Zuul)就可以通過 Eureka Server 來發現系統中的其他微服務,并執行相關的邏輯。

SpringCloud--Eureka

它主要包括兩個元件:Eureka Server 和 Eureka Client

  • Eureka Server:提供服務注冊和發現的能力,各個微服務節點通過配置啟動後,會在Eureka Server 中進行注冊,這樣EurekaServer中服務系統資料庫中将會存儲所有可用節點的資訊,在ui界面可以看到(通常就是微服務中的注冊中心)
  • Eureka Client:一個Java用戶端,用于簡化與 Eureka Server 的互動,用戶端同時也具備一個内置的、使用輪詢(round-robin)負載算法的負載均衡器。在啟動後,将會向Eureka Server發送心跳(預設周期30秒),如果Eureka Server在多個心跳周期内沒有接收到某個節點的心跳,Eureka Server将會從服務系統資料庫中把這個節點移除(預設90秒)(通常就是微服務中的用戶端和服務端)

單機版的eureka

1.建立Maven工程 POM 添加 eureka-server

<!-- eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <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>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>           

2.編寫yml 檔案

#微服務建議一定要寫服務端口号和微服務名稱
server:
  #端口号
  port: 7001

eureka:
  instance:
    hostname: localhost
  client:
    #false表示不向注冊中心注冊自己(想注冊也可以,不過沒必要)
    register-with-eureka: false
    #false表示自己端就是注冊中心,職責就是維護服務執行個體,并不需要去檢索服務
    fetch-registry: false
    service-url:
      #設定與eurekaServer互動的位址查詢服務和注冊服務都需要依賴這個位址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
           

3.編寫啟動類

package com.yxl.cloud;

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

@EnableEurekaServer //表示此項目是eureka的服務注冊中心
@SpringBootApplication
public class EurekaMain7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaMain7001.class, args);
    }
}           

4.測試

啟動項目,在浏覽器輸入

http://localhost:7001/
SpringCloud--Eureka
将EurekaClient端 注冊進EurekaServer成為服務提供者provider

1.建立Maven工程 引入POM

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

2。建立yml 添加

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

3.在主配置類上加上@EnableEurekaClient注解,表示這個項目是eureka的用戶端。

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

4.啟動項目,然後重新整理頁面,成功注冊進注冊中心。

SpringCloud--Eureka
SpringCloud--Eureka