Spring?Cloud?Alibaba?Nacos服務(wù)治理平臺服務(wù)注冊、RestTemplate實(shí)現(xiàn)微服務(wù)之間訪問負(fù)載均衡訪問的問題
??Nacos簡介
Github:https://github.com/alibaba/nacos
官網(wǎng)文檔:https://nacos.io/zh-cn/docs/what-is-nacos.html
Nacos 提供了發(fā)現(xiàn)、配置和管理微服務(wù)能力,能快速實(shí)現(xiàn)動(dòng)態(tài)服務(wù)發(fā)現(xiàn)、服務(wù)配置、服務(wù)元數(shù)據(jù)及流量管理。使用 Nacos 可以更敏捷和容易地構(gòu)建、交付和管理微服務(wù)平臺。 Nacos 是構(gòu)建以“服務(wù)”為中心的現(xiàn)代應(yīng)用架構(gòu) (例如微服務(wù)范式、云原生范式) 的服務(wù)基礎(chǔ)設(shè)施。
1.服務(wù)發(fā)現(xiàn)和服務(wù)健康監(jiān)測
Nacos 支持基于 DNS 和基于 RPC 的服務(wù)發(fā)現(xiàn)。服務(wù)提供者使用 原生SDK、OpenAPI、或一個(gè)獨(dú)立的Agent TODO注冊 Service 后,服務(wù)消費(fèi)者可以使用DNS TODO 或HTTP&API查找和發(fā)現(xiàn)服務(wù)。
Nacos 提供對服務(wù)的實(shí)時(shí)的健康檢查,阻止向不健康的主機(jī)或服務(wù)實(shí)例發(fā)送請求。Nacos 支持傳輸層 (PING 或 TCP)和應(yīng)用層 (如 HTTP、MySQL、用戶自定義)的健康檢查。 對于復(fù)雜的云環(huán)境和網(wǎng)絡(luò)拓?fù)洵h(huán)境中(如 VPC、邊緣網(wǎng)絡(luò)等)服務(wù)的健康檢查,Nacos 提供了 agent 上報(bào)模式和服務(wù)端主動(dòng)檢測2種健康檢查模式。Nacos 還提供了統(tǒng)一的健康檢查儀表盤,幫助您根據(jù)健康狀態(tài)管理服務(wù)的可用性及流量。
2.動(dòng)態(tài)配置服務(wù)
動(dòng)態(tài)配置服務(wù)可以讓您以中心化、外部化和動(dòng)態(tài)化的方式管理所有環(huán)境的應(yīng)用配置和服務(wù)配置。
動(dòng)態(tài)配置消除了配置變更時(shí)重新部署應(yīng)用和服務(wù)的需要,讓配置管理變得更加高效和敏捷。
配置中心化管理讓實(shí)現(xiàn)無狀態(tài)服務(wù)變得更簡單,讓服務(wù)按需彈性擴(kuò)展變得更容易。
Nacos 提供了一個(gè)簡潔易用的UI (控制臺樣例 Demo) 幫助您管理所有的服務(wù)和應(yīng)用的配置。Nacos 還提供包括配置版本跟蹤、金絲雀發(fā)布、一鍵回滾配置以及客戶端配置更新狀態(tài)跟蹤在內(nèi)的一系列開箱即用的配置管理特性,幫助您更安全地在生產(chǎn)環(huán)境中管理配置變更和降低配置變更帶來的風(fēng)險(xiǎn)。
3.動(dòng)態(tài) DNS 服務(wù)
動(dòng)態(tài) DNS 服務(wù)支持權(quán)重路由,讓您更容易地實(shí)現(xiàn)中間層負(fù)載均衡、更靈活的路由策略、流量控制以及數(shù)據(jù)中心內(nèi)網(wǎng)的簡單DNS解析服務(wù)。動(dòng)態(tài)DNS服務(wù)還能讓您更容易地實(shí)現(xiàn)以 DNS 協(xié)議為基礎(chǔ)的服務(wù)發(fā)現(xiàn),以幫助您消除耦合到廠商私有服務(wù)發(fā)現(xiàn) API 上的風(fēng)險(xiǎn)。
Nacos 提供了一些簡單的 DNS APIs TODO 幫助您管理服務(wù)的關(guān)聯(lián)域名和可用的 IP:PORT 列表.
4.服務(wù)及其元數(shù)據(jù)管理
Nacos 能讓您從微服務(wù)平臺建設(shè)的視角管理數(shù)據(jù)中心的所有服務(wù)及元數(shù)據(jù),包括管理服務(wù)的描述、生命周期、服務(wù)的靜態(tài)依賴分析、服務(wù)的健康狀態(tài)、服務(wù)的流量管理、路由及安全策略、服務(wù)的 SLA 以及最首要的 metrics 統(tǒng)計(jì)數(shù)據(jù)。
?Spring Cloud 組件依賴版本
官網(wǎng)文檔:https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
本文參考使用組件依賴如下
?Nacos部署
下載:https://github.com/alibaba/nacos/releases
運(yùn)行bin
下的startup.cmd
/startup.sh
。
Nacos2.* 默認(rèn)改為集群運(yùn)行了,可以只用-m
指令指定啟動(dòng)方式。
Windows
rem 單機(jī)啟動(dòng) startup.cmd -m standalone rem 集群啟動(dòng) startup.cmd -m cluster
?訪問Nacos平臺
默認(rèn)賬密:nacos/nacos
??Nacos服務(wù)注冊、微服務(wù)訪問、負(fù)載均衡實(shí)現(xiàn)
創(chuàng)建微服務(wù)項(xiàng)目,項(xiàng)目結(jié)構(gòu)如下
root pom.xml
如下:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ber</groupId> <artifactId>SpringCloud</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.2</version> <relativePath/> </parent> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <java.version>1.8</java.version> <spring-boot.version>2.4.2</spring-boot.version> <spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version> <spring-cloud.version>2020.0.1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> <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> <modules> <module>ber-nacos</module> </modules> </project>
nacos-consumer微服務(wù)創(chuàng)建
nacos-comsumer pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>ber-nacos</artifactId> <groupId>com.ber</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>nacos-consumer</artifactId> <description>服務(wù)消費(fèi)</description> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- 負(fù)載均衡 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-loadbalancer</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> </project>
application.yml
spring.application.name=nacos-consumer spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 server.port=8891
啟動(dòng)類
記得在類上加上注解@EnableDiscoveryClient
,RestTemplate屬性注入增加 @LoadBalanced 注解(負(fù)載均衡)
package com.ber.nacos.consumer; 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; /** * @author: ber * @date: 2022/6/25 0025 9:30 * ------------------------------- * Github:https://github.com/berbai * Blog:https://blog.csdn.net/Ber_Bai */ @SpringBootApplication @EnableDiscoveryClient public class NacosConsumerApplication { @Bean @LoadBalanced RestTemplate initRestTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(NacosConsumerApplication.class, args); } }
創(chuàng)建controller訪問方法
package com.ber.nacos.consumer.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.client.RestTemplate; /** * @author: ber * @date: 2022/6/25 0025 10:58 * ------------------------------- * Github:https://github.com/berbai * Blog:https://blog.csdn.net/Ber_Bai */ @RestController public class MsgController { private final RestTemplate restTemplate; @Autowired public MsgController(RestTemplate restTemplate) { this.restTemplate = restTemplate; } /** * 獲取消息 * * @param msgStr 消息 * @return */ @GetMapping("/queryMsg/{msgStr}") public String getMsg(@PathVariable(value = "msgStr") String msgStr) { return restTemplate.getForObject("http://nacos-provider/getMsg/" + msgStr, String.class); } }
?nacos-provider微服務(wù)創(chuàng)建
nacos-provider pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>ber-nacos</artifactId> <groupId>com.ber</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <packaging>jar</packaging> <artifactId>nacos-provider</artifactId> <description>服務(wù)提供</description> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies> </project>
application.properties
server.port=8892 spring.application.name=nacos-provider spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
啟動(dòng)類
package com.ber.nacos.provider; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /** * @author: ber * @date: 2022/6/25 0025 14:03 * ------------------------------- * Github:https://github.com/berbai * Blog:https://blog.csdn.net/Ber_Bai */ @EnableDiscoveryClient @SpringBootApplication public class NacosProviderApplication { public static void main(String[] args) { SpringApplication.run(NacosProviderApplication.class, args); } }
注解@EnableDiscoveryClient
不要忘記加上。
創(chuàng)建controller方法,被consumer訪問的接口
package com.ber.nacos.provider.controller; import com.ber.nacos.provider.service.MsgService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; /** * @author: ber * @date: 2022/6/25 0025 14:06 * ------------------------------- * Github:https://github.com/berbai * Blog:https://blog.csdn.net/Ber_Bai */ @RestController public class MsgController { @Autowired MsgService msgService; @GetMapping("getMsg/{msgStr}") public String getMsg(@PathVariable(value = "msgStr") String msgStr) { String res = "C服務(wù)"; System.out.println(res + " 調(diào)用"+msgService.queryCount()+"次"); msgService.addCount(); return res + " 調(diào)用" + msgService.queryCount() + "次 Get " + msgStr; } }
另外創(chuàng)建一個(gè)實(shí)體類記錄consumer微服務(wù)訪問provider微服務(wù)的次數(shù)
package com.ber.nacos.provider.entity; import org.springframework.stereotype.Component; import java.util.concurrent.atomic.AtomicInteger; /** * @author: ber * @date: 2022/6/25 0025 14:09 * ------------------------------- * Github:https://github.com/berbai * Blog:https://blog.csdn.net/Ber_Bai */ @Component public class CountLog { private AtomicInteger atomicInteger; public CountLog(){ this.atomicInteger = new AtomicInteger(); } public AtomicInteger getAtomicInteger() { return atomicInteger; } public void setAtomicInteger(AtomicInteger atomicInteger) { this.atomicInteger = atomicInteger; } }
創(chuàng)建操作訪問次數(shù)的service方法
package com.ber.nacos.provider.service; import com.ber.nacos.provider.entity.CountLog; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * @author: ber * @date: 2022/6/25 0025 14:13 * ------------------------------- * Github:https://github.com/berbai * Blog:https://blog.csdn.net/Ber_Bai */ @Service public class MsgService { @Autowired CountLog countLog; public Integer queryCount() { return countLog.getAtomicInteger().get(); } public void addCount() { countLog.getAtomicInteger().addAndGet(1); } }
??測試
啟動(dòng)一個(gè)consumer和一個(gè)provider微服務(wù),訪問http://localhost:8848/nacos
,使用 nacos/nacos 登陸后,可以發(fā)現(xiàn)服務(wù)列表
中,兩個(gè)微服務(wù)已經(jīng)注冊,如下圖所示。
訪問http://localhost:8891/queryMsg/hello
,即consumer的接口,可以得到如下圖所示的界面
再額外啟動(dòng)兩個(gè)provider微服務(wù),測試nacos的負(fù)載均衡能力,這里我修改了provider端口和接口返回的數(shù)據(jù)以標(biāo)明不同的provider微服務(wù)。通過nacos控制平臺可以看到三個(gè)provider微服務(wù)已經(jīng)注冊上去,如下圖所示。
再次請求http://localhost:8891/queryMsg/hello
,可以發(fā)現(xiàn)每次請求接口,三個(gè)provider接口的內(nèi)容會(huì)順序出現(xiàn),如下圖所示。說明nacos默認(rèn)的負(fù)載均衡方式是輪詢
通過nacos修改其中一個(gè)provider的權(quán)重,修改參考下圖
經(jīng)過測試發(fā)現(xiàn),負(fù)載策略還是輪詢方式,本次修改無效。
通過翻閱spring cloud alibaba源碼,發(fā)現(xiàn)nacos實(shí)現(xiàn)了ribbon
修改consumer的啟動(dòng)類,如下所示
package com.ber.nacos.provider.service; import com.ber.nacos.provider.entity.CountLog; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * @author: ber * @date: 2022/6/25 0025 14:13 * ------------------------------- * Github:https://github.com/berbai * Blog:https://blog.csdn.net/Ber_Bai */ @Service public class MsgService { @Autowired CountLog countLog; public Integer queryCount() { return countLog.getAtomicInteger().get(); } public void addCount() { countLog.getAtomicInteger().addAndGet(1); } }
重啟consumer后,設(shè)置的權(quán)重已經(jīng)生效。
到此這篇關(guān)于Spring Cloud Alibaba:Nacos服務(wù)治理平臺,服務(wù)注冊、RestTemplate實(shí)現(xiàn)微服務(wù)之間訪問,負(fù)載均衡訪問的文章就介紹到這了,更多相關(guān)Spring Cloud Alibaba Nacos服務(wù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- restTemplate實(shí)現(xiàn)跨服務(wù)API調(diào)用方式
- Java服務(wù)調(diào)用RestTemplate與HttpClient的使用詳解
- SpringCloud基于RestTemplate微服務(wù)項(xiàng)目案例解析
- springcloud中Ribbon和RestTemplate實(shí)現(xiàn)服務(wù)調(diào)用與負(fù)載均衡
- 關(guān)于springboot 中使用httpclient或RestTemplate做MultipartFile文件跨服務(wù)傳輸?shù)膯栴}
- restTemplate未設(shè)置連接數(shù)導(dǎo)致服務(wù)雪崩問題以及解決
相關(guān)文章
SpringBoot中引入MyBatisPlus的常規(guī)操作
這篇文章主要介紹了SpringBoot中引入MyBatisPlus的常規(guī)操作,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Spring動(dòng)態(tài)加載bean后調(diào)用實(shí)現(xiàn)方法解析
這篇文章主要介紹了Spring動(dòng)態(tài)加載bean后調(diào)用實(shí)現(xiàn)方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08解讀CommandLineRunner和@PostConstruct區(qū)別與應(yīng)用場景
這篇文章主要介紹了解讀CommandLineRunner和@PostConstruct區(qū)別與應(yīng)用場景,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12Java訂單30分鐘未支付自動(dòng)取消該怎么實(shí)現(xiàn)
在開發(fā)中往往會(huì)遇到一些關(guān)于延時(shí)任務(wù)的需求,例如生成訂單30分鐘未支付,則自動(dòng)取消,下面這篇文章主要給大家介紹了關(guān)于Java訂單30分鐘未支付自動(dòng)取消該怎么實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2023-03-03java String.split 無法使用小數(shù)點(diǎn)分割的問題
這篇文章主要介紹了java String.split 無法使用小數(shù)點(diǎn)分割的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02