Spring Cloud Netflix(一)
介紹
Spring Cloud Netflix 是由 Netflix 開源的,并且由 Spring 專案集成到 Spring Cloud 中的,主要用于構建大型分布式專案,
Spring Cloud Netflix 通過自動配置來系結到 Spring 專案中,使用注解便可以快速啟用相應的功能,
Spring Cloud Netflix 主要提供以下功能
Eureka:服務發現Hystrix:斷路器Zuul:智能路由Ribbon:客戶端的負載均衡
Eureka
Eureka 主要提供服務注冊、服務發現功能,這是微服務架構中的核心功能之一,
Eureka 主要分為 Server 和 Client,
Client 是應用端,是向外提供的服務,
Server 是服務端,即注冊中心,存盤了所有已注冊 Client 的元資料資訊,例如:主機、埠、健康指標、首頁等其他資訊,
Client 會發送心跳給 Server,用于表明服務正常可用,如果 Server 不能定時接受到 Client 的心跳資訊,便會將 Client 的 Instance 移除,但是如果在兩次心跳之間服務掛掉,那么 Server 中的 Client 不會移出,有一定的資訊延遲,
1. 搭建 Eureka-Server
1.1. 依賴
Eureka-Server 的依賴是 spring-cloud-starter-netflix-eureka-server,
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
1.2. 主類
@EnableEurekaServer 注解是用來啟動 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;
/**
* @author mw118
* @version 1.0
* @date 2021/1/8 22:26
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
1.3. 配置
server.port 用于表明 Eureka 服務的訪問介面,
eureka.client.register-with-eureka 表示不在其他 Eureka 服務上注冊,因為當前只有一個節點,
eureka.client.fetch-registry 表示是否從其他 Eureka 服務上獲取注冊表資訊,
application.properties
server.port=8762
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
logging.level.com.netflix.eureka=OFF
logging.level.com.netflix.discovery=OFF
1.4. 啟動并訪問
訪問 localhost:8761 ,便會展示 Spring Eureka 的網頁資訊,
其中紅色方框內便會展示服務的實體資訊,此時還沒有服務注冊到該節點上,

2. 搭建 Eureka-Client
2.1. 依賴
Eureka-Client 的依賴是 spring-cloud-starter-netflix-eureka-client,
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.2. 主類
@EnableDiscoveryClient 是客戶端進行服務注冊的,
EurekaClientApplication.java
package com.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author mw118
* @version 1.0
* @date 2021/1/8 22:29
*/
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
2.3. 配置
spring.application.name 指明當前應用名稱,會使用該名稱的大寫作為該應用的名稱
eureka.client.serviceUrl.defaultZone 用于定位 Eureka 注冊中心
application.properties
spring.application.name=eureka-client
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
2.4.啟動
啟動專案,并重繪 localhost:8761頁面,此時在紅色方框中,便會展示剛才啟動的客戶端服務,
顯示的重要資訊有:
Application:實體名稱,默認使用客戶端的環境變數spring.application.nameAvailability Zones:實體數量,如果部署了多個相同名稱的服務,便會展示對應的數量Status:展示了當前的實體狀態(UP:表示為可用),以及訪問的URL,URL會指向實體的/actuator/info

2.5. 測驗客戶端關閉的場景
將剛剛啟動的 Eureka-Client 關閉,并等待1分鐘后,重繪 localhost:9761 的頁面,
在圖中,盡管 Eureka-Client 服務已經關閉了,但是在注冊中心的串列中還存在該實體,并且狀態為 UP,
原因參考圖中第一個紅色方框中的內容,這是由于 Eureka 的自我保護機制,如果更新的心跳次數小于預期閾值,Eurka 服務不會將實體移除,防止因為網路引起的故障,

資源
代碼
- Github Eureka
參考及參考
-
Spring Cloud Netflix 官方檔案
-
Spring Cloud Eureka Server 自我保護
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/247185.html
標籤:其他
上一篇:云原生 - 1、什么是云計算
