Spring Cloud Alibaba Nacos 作為近幾年最熱門的注冊中心和配置中心,也被國內無數公司所使用,今天我們就來看下 Nacos 作為注冊中心時,調用它的接口有幾種方式?
注冊中心(Registry)是一種用于服務發現和服務注冊的分布式系統組件。它是在微服務架構中起關鍵作用的一部分,用于管理和維護服務實例的信息以及它們的狀態。
它的執行流程如下圖所示:
注冊中心充當了服務之間的中介和協調者,它的主要功能有以下這些:
使用注冊中心有以下優勢和好處:
常見的注冊中心包括 ZooKeeper、Eureka、Nacos 等。這些注冊中心可以作為微服務架構中的核心組件,用于實現服務的自動發現、負載均衡和動態擴容等功能。
當 Nacos 中注冊了 Restful 接口時(一種軟件架構風格,它是基于標準的 HTTP 協議和 URI 的一組約束和原則),其調用方式主要有以下兩種:
此方案的實現有以下 3 個關鍵步驟:
具體實現如下。
<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>
spring: application: name: nacos-discovery-business cloud: nacos: discovery: server-addr: localhost:8848 username: nacos password: nacos register-enabled: false
此步驟又分為以下兩步:
在 Spring Boot 啟動類上添加“@EnableDiscoveryClient”注解,并使用“@LoadBalanced”注解替換 IoC 容器中的 RestTemplate,具體實現代碼如下:
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@EnableDiscoveryClientpublic class BusinessApplication { @LoadBalanced @Bean public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(BusinessApplication.class, args); }}
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); }}
此步驟又分為以下 5 步:
具體實現如下。
<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>
spring: application: name: nacos-discovery-business cloud: nacos: discovery: server-addr: localhost:8848 username: nacos password: nacos register-enabled: false
在 Spring Boot 啟動類上添加 @EnableFeignClients 注解。
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 為生產者的服務名public interface UserService { @RequestMapping("/user/getinfo") // 調用生產者的接口 String getInfo(@RequestParam String name);}
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;@RestControllerpublic class OrderController { @Autowired private UserService userService; @RequestMapping("/order") public String getOrder(@RequestParam String name){ return userService.getInfo(name); }}
因平臺不能上傳附件,所以想要獲取本文完整源碼,請聯系我:gg_stone,備注:Nacos 源碼,不然不予通過。
本文案例基于以下版本:
注冊中心作為微服務中不可或缺的重要組件,在微服務中充當著中介和協調者的作用。而 Nacos 作為近幾年來,國內最熱門的注冊中心,其 Restf 接口調用有兩種方式:RestTemplate + LoadBalancer 和 OpenFeign + LoadBalancer,開發者可以根據自己的實際需求,選擇相應的調用方式。
本文鏈接:http://www.tebozhan.com/showinfo-26-15888-0.htmlNacos注冊中心有幾種調用方式?
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com
上一篇: Python 制作微博抓取 GUI 程序