国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Spring Cloud 系列之注冊中心 Eureka詳解

瀏覽:25日期:2023-08-04 10:41:58
1.1 簡介 1.1.1 概述

  Netflix Eureka 是由 Netflix 開源的一款基于 REST 的服務(wù)發(fā)現(xiàn)組件,包括 Eureka Server 及 Eureka Client。2012 年 9 月在 GitHub 上發(fā)布 1.1.2 版本,目前 Netflix 以宣布閉源,所以市面上還是以 1.x 版本為主。Eureka 提供基于 REST 的服務(wù),在集群中主要用于服務(wù)管理。Eureka 提供了基于 Java 語言的客戶端組件,客戶端組件實現(xiàn)了負(fù)載均衡的功能,為業(yè)務(wù)組件的集群部署創(chuàng)造了條件。使用該框架,可以將業(yè)務(wù)組件注冊到 Eureka 容器中,這些組件可進(jìn)行集群部署,Eureka 主要維護(hù)這些服務(wù)的列表并自動檢查它們的狀態(tài)。Spring Cloud Netflix Eureka 是 Pivotal 公司為了將 Netflix Eureka 整合于 Spring Cloud 生態(tài)系統(tǒng)提供的版本。  Eureka 包含兩個組件:Eureka Server 和 Eureka Client, Eureka Server 提供服務(wù)注冊服務(wù)。各個微服務(wù)節(jié)點通過配置啟動后,會在 EurekaServer 中進(jìn)行注冊,這樣 EurekaServer 中的服務(wù)注冊表中將會存儲所有可用服務(wù)節(jié)點的信息,服務(wù)節(jié)點的信息可以在界面中直觀看到。EurekaClient 通過注冊中心進(jìn)行訪問。它是一個 Java 客戶端,用于簡化 Eureka Server 的交互,客戶端同時也具備一個內(nèi)置的、使用輪詢(round-robin)負(fù)載算法的負(fù)載均衡器。在應(yīng)用啟動后,將會向 Eureka Server 發(fā)送心跳(默認(rèn)周期為30秒)。如果 Eureka Server 在多個心跳周期內(nèi)沒有接收到某個節(jié)點的心跳,EurekaServer 將會從服務(wù)注冊表中把這個服務(wù)節(jié)點移除(默認(rèn)90秒)

Spring Cloud 系列之注冊中心 Eureka詳解

1.1.2 原理圖

  一個簡單的 Eureka 集群,需要一個 Eureka 服務(wù)器、若干個服務(wù)提供者。我們可以將業(yè)務(wù)組件注冊到 Eureka 服務(wù)器中,其他客戶端組件可以向服務(wù)器獲取服務(wù)并且進(jìn)行遠(yuǎn)程調(diào)用。Eureka:就是服務(wù)注冊中心(可以是一個集群),對外暴露自己的地址;提供者:啟動后向 Eureka 注冊自己信息(地址,提供什么服務(wù));消費(fèi)者:向 Eureka 訂閱服務(wù),Eureka 會將對應(yīng)服務(wù)的所有提供者地址列表發(fā)送給消費(fèi)者,并且定期更新;心跳(續(xù)約):提供者定期通過 http 方式向 Eureka 刷新自己的狀態(tài)。

Spring Cloud 系列之注冊中心 Eureka詳解

1.1.3 相關(guān)依賴

<!-- 需要確定 Spring Cloud 版本 --><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency>1.2 搭建 EurekaServer 1.2.1 相關(guān)依賴

  現(xiàn)在都是子父工程,我們將子模塊中都需要用的依賴放到父工程的 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 https://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <packaging>pom</packaging> <modules> <module>eureka</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.10.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.software</groupId> <artifactId>spring-cloud</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-cloud</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR8</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- 演示就不使用數(shù)據(jù)庫了 --><!--<dependency>--><!--<groupId>org.springframework.boot</groupId>--><!--<artifactId>spring-boot-starter-data-jpa</artifactId>--><!--</dependency>--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- H 版 Spring Cloud 將 server 與 client 分開了,需要導(dǎo)入兩個坐標(biāo) --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>1.2.2 聲明為 Eureka Server

  在 eureka 服務(wù)的啟動類上使用 @EnableEurekaServer,聲明當(dāng)前應(yīng)用為 Eureka 服務(wù)。

/** * Created with IntelliJ IDEA. * * @author Demo_Null * @date 2020/10/29 * @description Eureka 啟動類 */@SpringBootApplication@EnableEurekaServerpublic class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); }}1.2.3 配置文件

server: port: 8081spring: application: name: eurekaServer # 應(yīng)用名稱,在 Eureka 中作為 id 標(biāo)識eureka: client: register-with-eureka: false # 不注冊自己 fetch-registry: false # 不拉取自己 service-url: defaultZone: http://127.0.0.1:8081/eureka/ # EurekaServer 的地址,如果是集群,需要加上其它 Server 的地址1.2.4 啟動服務(wù)

  啟動服務(wù)訪問對應(yīng)的端口就可以看到以下界面,現(xiàn)在是一個服務(wù)都沒有注冊上來??梢园?register-with-eureka,fetch-registry 兩個配置取消就可以看到 eureka 自己了。

Spring Cloud 系列之注冊中心 Eureka詳解

Spring Cloud 系列之注冊中心 Eureka詳解

1.3 提供者1.3.1 聲明為 Eureka Client

  在服務(wù)提供者啟動類中使用 @EnableDiscoveryClient,讓 Eureka 能夠發(fā)現(xiàn),掃描到該服務(wù)。@EnableEurekaClient 注解也能實現(xiàn)但是該注解只支持 Eureka 作為注冊中心,@EnableDiscoveryClient 可以是其他注冊中心,建議使用 @EnableDiscoveryClient

/** * Created with IntelliJ IDEA. * * @author Demo_Null * @date 2020/10/29 * @description 服務(wù)提供者啟動類 */@SpringBootApplication@EnableDiscoveryClientpublic class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); }}1.3.2 配置文件

server: port: 8082spring: application: name: ProviderServer # 應(yīng)用名稱,在 Eureka 中作為 id 標(biāo)識eureka: client: service-url: defaultZone: http://127.0.0.1:8081/eurake/1.3.3 提供服務(wù)

/** * Created with IntelliJ IDEA. * * @author Demo_Null * @date 2020/10/29 * @description */@RestController@RequestMapping('/provider')public class ProviderController { @GetMapping('/get') public Object get() { return '你已經(jīng)消費(fèi)了'; }}1.3.4 啟動服務(wù)

  啟動服務(wù)之后,會自動將自己注冊到 Eureka 中

Spring Cloud 系列之注冊中心 Eureka詳解

1.4 消費(fèi)者1.4.1 聲明為 Eureka Client

/** * Created with IntelliJ IDEA. * * @author Demo_Null * @date 2020/10/29 * @description 消費(fèi)者啟動類 */@SpringBootApplication@EnableDiscoveryClientpublic class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); }// 將 RestTemplate 交由容器管理 @Bean public RestTemplate getRestTemplate() { return new RestTemplate(); }}1.4.2 配置文件

server: port: 8083spring: application: name: ConsumerServer # 應(yīng)用名稱,在 Eureka 中作為 id 標(biāo)識eureka: client: service-url: defaultZone: http://127.0.0.1:8081/eureka1.4.3 消費(fèi)服務(wù)

  我們之前使用 RestTemplate 需要自己寫 URI,這樣很不利于維護(hù),而且容易出錯,現(xiàn)在只需要確定應(yīng)用名稱,利用應(yīng)用名稱從 Eureka 中就可以獲取到詳細(xì)信息。

/** * Created with IntelliJ IDEA. * * @author gaohu9712@163.com * @date 2020/10/29 * @description */@RestController@RequestMapping('/consumer')public class ConsumerController { @Autowired private DiscoveryClient discoveryClient; @GetMapping('/go') public void go() { List<ServiceInstance> providerServer = discoveryClient.getInstances('ProviderServer'); if (0 == providerServer.size()) { return; } ServiceInstance serviceInstance = providerServer.get(0); String host = serviceInstance.getHost(); int port = serviceInstance.getPort(); URI uri = serviceInstance.getUri(); System.out.println('主機(jī):' + host); System.out.println('端口:' + port); System.out.println('uri:' + uri); RestTemplate restTemplate = new RestTemplate(); String str = restTemplate.getForObject(uri + '/provider/get', String.class); System.out.println(str); }}1.4.4 啟動服務(wù)

Spring Cloud 系列之注冊中心 Eureka詳解

1.4.5 請求服務(wù)

Spring Cloud 系列之注冊中心 Eureka詳解

1.4.6 執(zhí)行流程

 ♞ 先啟動 eureka 注冊中心 ♞ 啟動服務(wù)提供者 provider ♞ 服務(wù)提供者啟動后會把自身信息(比如服務(wù)地址以別名方式注冊進(jìn) eureka) ♞ 消費(fèi)者 consumer 服務(wù)在需要調(diào)用接口時,使用服務(wù)別名去注冊中心獲取實際的 RPC 遠(yuǎn)程調(diào)用地址 ♞ 消費(fèi)者獲得調(diào)用地址后,底層實際是利用 HttpClient 技術(shù)實現(xiàn)遠(yuǎn)程調(diào)用 ♞ 消費(fèi)者獲得服務(wù)地址后會緩存在本地 jvm 內(nèi)存中,默認(rèn)每間隔 30 秒更新一次服務(wù)調(diào)用地址

1.5 補(bǔ)充配置1.5.1 actuator 信息完善

  我們現(xiàn)在的服務(wù)注冊到 Eureka 上面是沒有 ip 地址的,以后等服務(wù)搭建集群是很不方便的,所以我們需要讓他顯示自己的 ip 地址;第二個就是服務(wù)名稱為主機(jī) + 服務(wù)名 + 端口,這樣就暴露了主機(jī)名,我們可以指定顯示的名稱。

Spring Cloud 系列之注冊中心 Eureka詳解

在配置文件中添加如下配置,通過健康檢查(http://ip:port/actuator/health)查看是否修改成功。

eureka: instance: # 實例名稱 instance-id: consumer-01 # 地址中顯示 ip prefer-ip-address: true

Spring Cloud 系列之注冊中心 Eureka詳解

1.5.2 自我保護(hù)機(jī)制 ☞ 概述

  我們可以看到 Eureka 上有一行紅色的英文 EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY’RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.它代表了 Eureka 保護(hù)模式的開啟。一旦進(jìn)入保護(hù)模式,Eureka Server 將會嘗試保護(hù)其服務(wù)注冊表中的信息,不再刪除服務(wù)注冊表中的數(shù)據(jù),也就是不會注銷任何微服務(wù)。  默認(rèn)情況下,如果 Eureka Server 在一定時間內(nèi)沒有接收到某個微服務(wù)實例的心跳,Eureka Server 將會注銷該實例(默認(rèn)90秒)。但是當(dāng)網(wǎng)絡(luò)分區(qū)故障發(fā)生(延時、卡頓、擁擠)時,微服務(wù)與 Eureka Server 之間無法正常通信,以上行為可能變得非常危險了——因為微服務(wù)本身其實是健康的,此時本不應(yīng)該注銷這個微服務(wù)。為了防止 Eureka Client 可以正常運(yùn)行,但是與 Eureka Server 網(wǎng)絡(luò)不通情況下,Eureka Server 不會立刻將 Eureka Client 服務(wù)剔除。

☞ 關(guān)閉自我保護(hù)機(jī)制

  我們之前的 Eureka 截圖中可以看到 DESKTOP-GL7GS52:ConsumerServer:8083,consumer-01 兩個同時存在,這明明是一個服務(wù),修改完配置之后前面的沒有剔除,這就是因為自我保護(hù)機(jī)制打開了。

# Eureka Server 配置eureka: server: # 關(guān)閉自我保護(hù)模式, 默認(rèn)為打開 enable-self-preservation: false # 續(xù)期時間,即掃描失效服務(wù)的間隔時間 eviction-interval-timer-in-ms: 5000 # Eureka Client 配置eureka: instance: # Eureka Client 給 Eureka Server 發(fā)送心跳的時間間隔,默認(rèn) 30 單位是 s lease-renewal-interval-in-seconds: 1 # Eureka Server 最后一次收到心跳的等待上限,超時剔除服務(wù),默認(rèn) 90 單位是 s lease-expiration-duration-in-seconds: 21.6 Eureka 高可用1.6.1 Eureka 集群搭建

  在之前的單體中我們的端口是隨意的,但是搭建集群我們需要對端口進(jìn)行規(guī)劃,例如將 808X 端口作為 Eureka 集群的端口。先來看下配置有什么區(qū)別,起初我們是將自己注冊到自己上,現(xiàn)在我們需要將自己注冊到其他 Eureka 上,有多個則用 , 隔開。

server: port: 8081spring: application: name: eurekaServereureka: client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://127.0.0.1:8082/eureka server: enable-self-preservation: false eviction-interval-timer-in-ms: 5000

server: port: 8082spring: application: name: eurekaServer_backeureka: client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://127.0.0.1:8081/eureka server: enable-self-preservation: false eviction-interval-timer-in-ms: 5000

Spring Cloud 系列之注冊中心 Eureka詳解Spring Cloud 系列之注冊中心 Eureka詳解

1.6.2 Privoder 集群

  服務(wù)提供者的集群配置了多個 Eureka 地址,會將自己同時注冊到多個 Eureka 上,除了配置文件以外其他的服務(wù)代碼完全一致,也可以加以區(qū)分是哪個提供的服務(wù)。需要注意的是 Eureka 集群的應(yīng)用名稱可以不一致甚至不寫,但是服務(wù)提供者的應(yīng)用名稱必須保持一致,否則會被認(rèn)為不是一個服務(wù)。

server: port: 8091spring: application: name: ProviderServer # 應(yīng)用名稱,在 Eureka 中作為 id 標(biāo)識eureka: client: service-url: defaultZone: http://127.0.0.1:8081/eureka, http://127.0.0.1:8082/eureka instance: instance-id: provider-prim prefer-ip-address: true lease-renewal-interval-in-seconds: 1 lease-expiration-duration-in-seconds: 2

server: port: 8092spring: application: name: ProviderServereureka: client: service-url: defaultZone: http://127.0.0.1:8081/eureka, http://127.0.0.1:8082/eureka instance: instance-id: provider-back prefer-ip-address: true lease-renewal-interval-in-seconds: 1 lease-expiration-duration-in-seconds: 2

Spring Cloud 系列之注冊中心 Eureka詳解

1.6.3 遠(yuǎn)程調(diào)用

/** * Created with IntelliJ IDEA. * * @author Demo_Null * @date 2020/10/29 * @description */@RestController@RequestMapping('/consumer')public class ConsumerController { @Autowired private DiscoveryClient discoveryClient; @GetMapping('/go') public void go() { List<ServiceInstance> providerServer = discoveryClient.getInstances('ProviderServer'); if (0 == providerServer.size()) { return; } RestTemplate restTemplate = new RestTemplate(); for (ServiceInstance instance : providerServer) { System.out.print(instance.getUri() + '---'); String url = instance.getUri() + '/provider/get'; System.out.println(restTemplate.getForObject(url, String.class)); } }}

Spring Cloud 系列之注冊中心 Eureka詳解

  咱們可以使用服務(wù)發(fā)現(xiàn) DiscoveryClient 來獲取服務(wù)信息,但是無法自動選擇使用那個服務(wù),這里就涉及到 Ribbon 負(fù)載均衡了。我們可以將 RestTemplate 交由 Ioc 管理,在注入時使用 @LoadBalanced 注解進(jìn)行負(fù)載均衡。

☞ 源碼

到此這篇關(guān)于Spring Cloud 系列之注冊中心 Eureka的文章就介紹到這了,更多相關(guān)Spring Cloud 注冊中心Eureka內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 寻乌县| 老河口市| 高邑县| 游戏| 平和县| 托克逊县| 承德县| 临夏市| 平山县| 库尔勒市| 兴宁市| 泰安市| 江安县| 北安市| 理塘县| 巍山| 石楼县| 金湖县| 鄱阳县| 玉环县| 西林县| 梓潼县| 宾川县| 都兰县| 佛坪县| 岐山县| 咸宁市| 土默特右旗| 合山市| 泰宁县| 绥滨县| 綦江县| 讷河市| 察隅县| 襄垣县| 康保县| 利辛县| 巴彦淖尔市| 南华县| 攀枝花市| 页游|