日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringCloud微服务:Eureka组件之服务注册与发现

發布時間:2025/3/17 javascript 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringCloud微服务:Eureka组件之服务注册与发现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、Eureka基本架構

1、Eureka角色結構圖


角色職責如下:
1)、Register:服務注冊中心,它是一個Eureka Server ,提供服務注冊和發現功能。
2)、Provider:服務提供者,它是一個Eureka Client ,提供服務。
3)、Consumer:服務消費者,它是一個Eureka Cient ,消費服務。

2、Eureka中幾個核心概念

1)、Registe服務注冊
當Client向Server 注冊時,Client 提供自身的元數據,比如IP 地址、端口、運行狀況指標的Uri 、主頁地址等信息。
2)、Renew服務續約
Client 在默認的情況下會每隔30 秒發送一次心跳來進行服務續約。通過服務續約來告知Server該Client仍然可用。正常情況下,如果Server在90 秒內沒有收到Client 的心跳,Server會將Client 實例從注冊列表中刪除。官網建議不要更改服務續約的間隔時間。
3)、Fetch Registries獲取服務注冊列表信息
Client 從Server 獲取服務注冊表信息,井將其緩存在本地。Client 會使用服務注冊列表信息查找其他服務的信息,從而進行遠程調用。該注冊列表信息定時(每30 秒) 更新一次。
4)Cancel服務下線
Client 在程序關閉時可以向Eureka Server 發送下線請求。發送請求后,該客戶端的實例信息將從Server 的服務注冊列表中刪除。該下線請求不會自動完成,需要在程序關閉時調用以下代碼:

DiscoveryManager.getinstance().shutdownComponent();

5) Eviction服務下線
在默認情況下,當Client 連續90 秒沒有向Server 發送服務續約(即心跳〉時,Server 會將該服務實例從服務注冊列表刪除,即服務下線。

二、Eureka案例代碼

1、項目基本結構圖

主要包括兩個注冊中心(集群)
node01-eureka-7001
node01-eureka-7002
一個服務提供方
node01-provider-8001
一個服務消費方
node01-consume-8002

2、配置本機的Host文件

# cloud host 127.0.0.1 registry01.com 127.0.0.1 registry02.com 127.0.0.1 provider-8001.com

3、注冊中心代碼分解

1)Eureka注冊中心依賴

<dependencies><!--eureka-server服務端 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId></dependency> </dependencies>

2)核心配置文件

server:port: 7001 spring:application:name: node01-eureka-7001 eureka:instance:hostname: registry01.comprefer-ip-address: trueclient:# false表示不向注冊中心注冊自己register-with-eureka: false# false表示該端就是注冊中心,維護服務實例,不去檢索服務fetch-registry: falseservice-url:# 單點注冊中心# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/# 集群注冊中心defaultZone: http://registry02.com:7002/eureka/

這里采用集群的配置,如果有多個集群,逗號分隔,如下寫法就好:

defaultZone: http://registry02.com:7002/eureka/, http://registry02.com:7002/eureka/

3)啟動類注解

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer // 注冊中心注解 public class Application_7001 {public static void main(String[] args) {SpringApplication.run(Application_7001.class,args) ;} }

這樣注冊中心代碼完成。
4)啟動項目,如圖

暫時沒有服務注冊進來。

4、服務提供方代碼分解

1)核心依賴

<dependencies><!-- 將微服務provider側注冊進eureka --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency> </dependencies>

2)配置文件如下

server:port: 8003 spring:application:name: node01-provider-8001 eureka:instance:hostname: provider-8001prefer-ip-address: trueclient:service-url:# 集群注冊中心defaultZone: http://registry01.com:7001/eureka/,http://registry02.com:7002/eureka/

3)提供一個接口服務

@RestController public class ProviderController {@RequestMapping("/getInfo")public Map<String,String> getInfo (){Map<String,String> infoMap = new HashMap<>() ;infoMap.put("作者:","知了一笑") ;infoMap.put("時間:","2019-05-18") ;infoMap.put("主題:","SpringCloud微服務框架") ;return infoMap ;} }

4)啟動類上需要注解

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication @EnableEurekaClient // 本服務啟動后會自動注冊進eureka服務中 @EnableDiscoveryClient public class Application_8001 {public static void main(String[] args) {SpringApplication.run(Application_8001.class,args) ;} }

5、服務消費方代碼分解

服務消費方和提供方的代碼邏輯基本一致。
1)配置文件
這里不需要注冊自己,只是單純從注冊中心獲取服務提供方的消息。

server:port: 8002 spring:application:name: node01-consume-8002 eureka:client:register-with-eureka: falseservice-url:# 集群注冊中心defaultZone: http://registry01.com:7001/eureka/,http://registry02.com:7002/eureka/

2)消費服務代碼
注意這里的【server_name】就服務提供方的

spring:application:name: node01-provider-8001

使用RestTemplate調用服務。

@RestController public class ConsumeController {@Autowiredprivate RestTemplate restTemplate ;String server_name = "node01-provider-8001" ;@RequestMapping("/showInfo")public Map<String,String> showInfo (){return restTemplate.getForObject("http://"+server_name+":8001/getInfo",Map.class) ;} }

到這里,一個基于Eureka的服務注冊與發現就完成了。
3)四個服務全部啟動

4)訪問如下地址

http://localhost:8002/showInfo

獲取服務接口結果

{"主題:": "SpringCloud微服務框架","作者:": "知了一笑","時間:": "2019-05-18" }

這樣案例就結束了。
歡迎關注公眾號:【知了一笑】
文章持續更新中。

新人創作打卡挑戰賽發博客就能抽獎!定制產品紅包拿不停!

總結

以上是生活随笔為你收集整理的SpringCloud微服务:Eureka组件之服务注册与发现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。