Nacos注冊(cè)中心的幾種調(diào)用方式詳解
1.什么是注冊(cè)中心?
注冊(cè)中心(Registry)是一種用于服務(wù)發(fā)現(xiàn)和服務(wù)注冊(cè)的分布式系統(tǒng)組件。它是在微服務(wù)架構(gòu)中起關(guān)鍵作用的一部分,用于管理和維護(hù)服務(wù)實(shí)例的信息以及它們的狀態(tài)。
它的執(zhí)行流程如下圖所示:
注冊(cè)中心充當(dāng)了服務(wù)之間的中介和協(xié)調(diào)者,它的主要功能有以下這些:
- 服務(wù)注冊(cè):服務(wù)提供者將自己的服務(wù)實(shí)例信息(例如 IP 地址、端口號(hào)、服務(wù)名稱等)注冊(cè)到注冊(cè)中心。通過注冊(cè)中心,服務(wù)提供者可以將自己的存在告知其他服務(wù)。
- 服務(wù)發(fā)現(xiàn):服務(wù)消費(fèi)者通過向注冊(cè)中心查詢服務(wù)信息,獲取可用的服務(wù)實(shí)例列表。通過注冊(cè)中心,服務(wù)消費(fèi)者可以找到并連接到需要調(diào)用的服務(wù)。
- 健康檢查與負(fù)載均衡:注冊(cè)中心可以定期檢查注冊(cè)的服務(wù)實(shí)例的健康狀態(tài),并從可用實(shí)例中進(jìn)行負(fù)載均衡,確保請(qǐng)求可以被正確地轉(zhuǎn)發(fā)到可用的服務(wù)實(shí)例。
- 動(dòng)態(tài)擴(kuò)容與縮容:在注冊(cè)中心中注冊(cè)的服務(wù)實(shí)例信息可以方便地進(jìn)行動(dòng)態(tài)的增加和減少。當(dāng)有新的服務(wù)實(shí)例上線時(shí),可以自動(dòng)地將其注冊(cè)到注冊(cè)中心。當(dāng)服務(wù)實(shí)例下線時(shí),注冊(cè)中心會(huì)將其從服務(wù)列表中刪除。
使用注冊(cè)中心有以下優(yōu)勢(shì)和好處:
- 服務(wù)自動(dòng)發(fā)現(xiàn)和負(fù)載均衡:服務(wù)消費(fèi)者無需手動(dòng)配置目標(biāo)服務(wù)的地址,而是通過注冊(cè)中心動(dòng)態(tài)獲取可用的服務(wù)實(shí)例,并通過負(fù)載均衡算法選擇合適的實(shí)例進(jìn)行調(diào)用。
- 服務(wù)彈性和可擴(kuò)展性:新的服務(wù)實(shí)例可以動(dòng)態(tài)注冊(cè),并在發(fā)生故障或需要擴(kuò)展時(shí)快速提供更多的實(shí)例,從而提供更高的服務(wù)彈性和可擴(kuò)展性。
- 中心化管理和監(jiān)控:注冊(cè)中心提供了中心化的服務(wù)管理和監(jiān)控功能,可以對(duì)服務(wù)實(shí)例的狀態(tài)、健康狀況和流量等進(jìn)行監(jiān)控和管理。
- 降低耦合和提高靈活性:服務(wù)間的通信不再直接依賴硬編碼的地址,而是通過注冊(cè)中心進(jìn)行解耦,使得服務(wù)的部署和變更更加靈活和可控。
常見的注冊(cè)中心包括 ZooKeeper、Eureka、Nacos 等。這些注冊(cè)中心可以作為微服務(wù)架構(gòu)中的核心組件,用于實(shí)現(xiàn)服務(wù)的自動(dòng)發(fā)現(xiàn)、負(fù)載均衡和動(dòng)態(tài)擴(kuò)容等功能。
2.方法概述
當(dāng) Nacos 中注冊(cè)了 Restful 接口時(shí)(一種軟件架構(gòu)風(fēng)格,它是基于標(biāo)準(zhǔn)的 HTTP 協(xié)議和 URI 的一組約束和原則),其調(diào)用方式主要有以下兩種:
- 使用 RestTemplate + Spring Cloud LoadBalancer
使用 OpenFeign + Spring Cloud LoadBalancer
3.RestTemplate+LoadBalancer調(diào)用
此方案的實(shí)現(xiàn)有以下 3 個(gè)關(guān)鍵步驟:
- 添加依賴:nacos + loadbalancer
- 設(shè)置配置文件
- 編寫調(diào)用代碼
具體實(shí)現(xiàn)如下。
3.1 添加依賴
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency>
3.2 設(shè)置配置文件
spring: application: name: nacos-discovery-business cloud: nacos: discovery: server-addr: localhost:8848 username: nacos password: nacos register-enabled: false
3.3 編寫調(diào)用代碼
此步驟又分為以下兩步:
- 給 RestTemplate 增加 LoadBalanced 支持
使用 RestTemplate 調(diào)用接口
3.3.1 RestTemplate添加LoadBalanced
在 Spring Boot 啟動(dòng)類上添加“@EnableDiscoveryClient”注解,并使用“@LoadBalanced”注解替換 IoC 容器中的 RestTemplate,具體實(shí)現(xiàn)代碼如下:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableDiscoveryClient public class BusinessApplication { @LoadBalanced @Bean public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(BusinessApplication.class, args); } }
3.3.2 使用RestTemplate
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController @RequestMapping("/business") public class BusinessController2 { @Autowired private RestTemplate restTemplate; @RequestMapping("/getnamebyid") public String getNameById(Integer id){ return restTemplate.getForObject("http://nacos-discovery-demo/user/getnamebyid?id="+id, String.class); } }
4.OpenFeign+LoadBalancer調(diào)用
此步驟又分為以下 5 步:
- 添加依賴:nacos + openfeign + loadbalancer
- 設(shè)置配置文件
- 開啟 openfeign 支持
- 編寫 service 代碼
- 調(diào)用 service 代碼
具體實(shí)現(xiàn)如下。
4.1 添加依賴
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency>
4.2 設(shè)置配置文件
spring: application: name: nacos-discovery-business cloud: nacos: discovery: server-addr: localhost:8848 username: nacos password: nacos register-enabled: false
4.3 開啟OpenFeign
在 Spring Boot 啟動(dòng)類上添加 @EnableFeignClients 注解。
4.4 編寫Service
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Service @FeignClient(name = "nacos-producer") // name 為生產(chǎn)者的服務(wù)名 public interface UserService { @RequestMapping("/user/getinfo") // 調(diào)用生產(chǎn)者的接口 String getInfo(@RequestParam String name); }
4.5 調(diào)用Service
import com.example.consumer.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class OrderController { @Autowired private UserService userService; @RequestMapping("/order") public String getOrder(@RequestParam String name){ return userService.getInfo(name); } }
5.獲取本文源碼
因平臺(tái)不能上傳附件,所以想要獲取本文完整源碼,請(qǐng)聯(lián)系我:gg_stone,備注:Nacos 源碼,不然不予通過。
6.版本說明
本文案例基于以下版本:
- JDK 17
- Spring Boot 3.x
- Spring Cloud Alibaba 2022.0.0.0
- Nacos 2.2.3
7.小結(jié)
注冊(cè)中心作為微服務(wù)中不可或缺的重要組件,在微服務(wù)中充當(dāng)著中介和協(xié)調(diào)者的作用。而 Nacos 作為近幾年來,國內(nèi)最熱門的注冊(cè)中心,其 Restf 接口調(diào)用有兩種方式:RestTemplate + LoadBalancer 和 OpenFeign + LoadBalancer,開發(fā)者可以根據(jù)自己的實(shí)際需求,選擇相應(yīng)的調(diào)用方式。
以上就是Nacos注冊(cè)中心有幾種調(diào)用方式?的詳細(xì)內(nèi)容,更多關(guān)于Nacos注冊(cè)中調(diào)用方式的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
使用jquery 的ajax 與 Java servlet的交互代碼實(shí)例
這篇文章主要介紹了使用jquery 的ajax 與 Java servlet的交互代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09maven?scope?provided和runtime的例子說明
這篇文章主要介紹了maven?scope?provided和runtime的例子說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12Maven學(xué)習(xí)----Maven安裝與環(huán)境變量配置教程
這篇文章主要給大家介紹了關(guān)于如何利用Maven入手Spring Boot第一個(gè)程序的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06servlet基礎(chǔ)知識(shí)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了servlet基礎(chǔ)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Struts2實(shí)現(xiàn)單文件或多文件上傳功能
這篇文章主要為大家詳細(xì)介紹了Struts2實(shí)現(xiàn)單文件或多文件上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03解決java調(diào)用dll報(bào)Unable to load library錯(cuò)誤的問題
這篇文章主要介紹了解決java調(diào)用dll報(bào)Unable to load library錯(cuò)誤的問題。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11Java服務(wù)端性能優(yōu)化之JVM垃圾回收策略詳解
JVM垃圾回收策略涵蓋了基本原理、常見策略(如SerialGC、ParallelGC、CMS、G1GC)以及優(yōu)化建議,選擇合適的策略和調(diào)整參數(shù),如堆大小和GC日志,可以提高應(yīng)用性能和響應(yīng)速度,持續(xù)監(jiān)控和分析是關(guān)鍵步驟2025-03-03