Spring Cloud根據(jù)服務(wù)名獲取服務(wù)的ip端口問題
本篇示例我就以Nacos注冊中心為例了,下面是我注冊的兩個服務(wù)。其中nacos-payment-provider服務(wù)是集群,有兩個實(shí)例。

方式一:通過loadBalancerClient來獲取
如果使用的Nacos為注冊中心的時候會發(fā)現(xiàn)一個問題,當(dāng)引入的依賴版本比較高的時候,RestTemplate+@LoadBalanced 通過服務(wù)名稱調(diào)用的時候會報(bào)錯,使用其他注冊中心默認(rèn)都會引用ribbon依賴,因此我們只需要在注入RestTemplate的時候加上@LoadBalanced就可以實(shí)現(xiàn)根據(jù)名稱負(fù)載均衡調(diào)用。
而nacos高版本依賴包沒有引用ribbon依賴。ribbon早就已經(jīng)徹底停更了,spring又自己出了一個
loadbalancer負(fù)載均衡框架,來配合RestTemplate使用。但是他并沒有自動引用loadbalancer依賴所以我們需要自己引用才可以使用。
@Configuration
public class ApplicationContextBean {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
使用如下獲取ip端口的前提:引用了loadbalancer來作為負(fù)載均衡
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
loadBalancerClient.choose()這個方法就是負(fù)載均衡的核心方法。假如服務(wù)名稱為nacos-payment-provider有兩個實(shí)例,一個9001一個9002,通過如下方法調(diào)用會發(fā)現(xiàn)每次都是在輪詢。
@Autowired
private LoadBalancerClient loadBalancerClient;
@GetMapping("/getServiceInstance")
public ServiceInstance getServiceInstance() {
ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-payment-provider");
System.out.println(serviceInstance.getHost()); // ip
System.out.println(serviceInstance.getPort()); // 端口
System.out.println(serviceInstance.getInstanceId()); // 實(shí)例id
System.out.println(serviceInstance.getServiceId()); // 服務(wù)id
System.out.println(serviceInstance.getMetadata()); // 與服務(wù)實(shí)例關(guān)聯(lián)的元數(shù)據(jù)
System.out.println(serviceInstance.getScheme()); // 返回服務(wù)實(shí)例的方案
System.out.println(serviceInstance.getUri().toString()); // 返回服務(wù)的uri地址
return serviceInstance;
}


loadbalancer源碼:

方式二:通過discoveryClient來獲取
這種方式其他注冊中心也可以使用!
@Resource
private DiscoveryClient discoveryClient;
@GetMapping("/getServiceInstanceList")
public List<ServiceInstance> getServiceInstanceList() {
// 根據(jù)服務(wù)名稱查找所有的實(shí)例
return discoveryClient.getInstances("nacos-payment-provider");
}

DiscoveryClient這個其實(shí)是個接口存放于cloud-commons包當(dāng)中。

既然是接口為什么他能獲取到呢?我們可以看他的實(shí)現(xiàn)類,是有如下實(shí)現(xiàn)類的,也就是在nacos的服務(wù)發(fā)現(xiàn)依賴當(dāng)中會存在他的實(shí)現(xiàn)類,并注入到容器當(dāng)中了。其他注冊中心也是同樣如此??梢哉f這個接口是共用的,其他的注冊中心都可以來實(shí)現(xiàn)。

方式三:通過NacosServiceManager來獲取
這個是nacos獨(dú)有的!
@Autowired
private NacosServiceManager nacosServiceManager;
@Autowired
private NacosDiscoveryProperties nacosDiscoveryProperties;
@GetMapping("/nacos")
public List<Instance> getGatewayAddress() {
String res = null;
try {
NamingService namingService = nacosServiceManager.getNamingService(nacosDiscoveryProperties.getNacosProperties());
List<Instance> allInstances = namingService.getAllInstances("nacos-payment-provider");
return allInstances;
} catch (NacosException e) {
e.printStackTrace();
return null;
}
}

到此這篇關(guān)于Spring Cloud根據(jù)服務(wù)名獲取服務(wù)的ip端口的文章就介紹到這了,更多相關(guān)Spring Cloud ip端口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot 中使用 Mybatis Plus的操作方法
本文介紹了如何在 Spring Boot 項(xiàng)目中集成 Mybatis Plus,Spring Boot 與 MyBatis Plus 的集成非常簡單,通過自動配置和簡潔的 API,可以大大減少開發(fā)中常見的數(shù)據(jù)庫操作代碼,需要的朋友參考下吧2024-12-12
java poi設(shè)置生成的word的圖片為上下型環(huán)繞以及其位置的實(shí)現(xiàn)
這篇文章主要介紹了java poi設(shè)置生成的word的圖片為上下型環(huán)繞以及其位置的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
SpringBoot feign動態(tài)設(shè)置數(shù)據(jù)源(https請求)
這篇文章主要介紹了SpringBoot如何在運(yùn)行時feign動態(tài)添加數(shù)據(jù)源,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-08-08
MyBatis之自查詢使用遞歸實(shí)現(xiàn) N級聯(lián)動效果(兩種實(shí)現(xiàn)方式)
這篇文章主要介紹了MyBatis之自查詢使用遞歸實(shí)現(xiàn) N級聯(lián)動效果,本文給大家分享兩種實(shí)現(xiàn)方式,需要的的朋友參考下吧2017-07-07

