Spring Cloud根據(jù)服務(wù)名獲取服務(wù)的ip端口問題
本篇示例我就以Nacos注冊中心為例了,下面是我注冊的兩個服務(wù)。其中nacos-payment-provider
服務(wù)是集群,有兩個實例。
方式一:通過loadBalancerClient來獲取
如果使用的Nacos為注冊中心的時候會發(fā)現(xiàn)一個問題,當引入的依賴版本比較高的時候,RestTemplate
+@LoadBalanced
通過服務(wù)名稱調(diào)用的時候會報錯,使用其他注冊中心默認都會引用ribbon依賴,因此我們只需要在注入RestTemplate
的時候加上@LoadBalanced
就可以實現(xiàn)根據(jù)名稱負載均衡調(diào)用。
而nacos高版本依賴包沒有引用ribbon依賴。ribbon早就已經(jīng)徹底停更了,spring又自己出了一個
loadbalancer
負載均衡框架,來配合RestTemplate
使用。但是他并沒有自動引用loadbalancer依賴所以我們需要自己引用才可以使用。
@Configuration public class ApplicationContextBean { @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); } }
使用如下獲取ip端口的前提:引用了loadbalancer來作為負載均衡
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency>
loadBalancerClient.choose()這個方法就是負載均衡的核心方法。假如服務(wù)名稱為nacos-payment-provider有兩個實例,一個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()); // 實例id System.out.println(serviceInstance.getServiceId()); // 服務(wù)id System.out.println(serviceInstance.getMetadata()); // 與服務(wù)實例關(guān)聯(lián)的元數(shù)據(jù) System.out.println(serviceInstance.getScheme()); // 返回服務(wù)實例的方案 System.out.println(serviceInstance.getUri().toString()); // 返回服務(wù)的uri地址 return serviceInstance; }
loadbalancer源碼:
方式二:通過discoveryClient來獲取
這種方式其他注冊中心也可以使用!
@Resource private DiscoveryClient discoveryClient; @GetMapping("/getServiceInstanceList") public List<ServiceInstance> getServiceInstanceList() { // 根據(jù)服務(wù)名稱查找所有的實例 return discoveryClient.getInstances("nacos-payment-provider"); }
DiscoveryClient這個其實是個接口存放于cloud-commons包當中。
既然是接口為什么他能獲取到呢?我們可以看他的實現(xiàn)類,是有如下實現(xiàn)類的,也就是在nacos的服務(wù)發(fā)現(xiàn)依賴當中會存在他的實現(xiàn)類,并注入到容器當中了。其他注冊中心也是同樣如此。可以說這個接口是共用的,其他的注冊中心都可以來實現(xiàn)。
方式三:通過NacosServiceManager來獲取
這個是nacos獨有的!
@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)文章
SpringBoot整合freemarker實現(xiàn)代碼生成器
這篇文章主要為大家詳細介紹了SpringBoot如何整合freemarker實現(xiàn)一個簡單的代碼生成器,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2023-03-03關(guān)于Java的Condition接口最佳理解方式
這篇文章主要介紹了關(guān)于Java的Condition接口最佳理解方式,Condition就是實現(xiàn)了管程里面的條件變量,Java?語言內(nèi)置的管程里只有一個條件變量,而Lock&Condition實現(xiàn)的管程支持多個條件變量,需要的朋友可以參考下2023-05-05在Ubuntu系統(tǒng)下安裝JDK和Tomcat的教程
這篇文章主要介紹了在Ubuntu系統(tǒng)下安裝JDK和Tomcat的教程,這樣便是在Linux系統(tǒng)下搭建完整的Java和JSP開發(fā)環(huán)境,需要的朋友可以參考下2015-08-08Java web Hibernate如何與數(shù)據(jù)庫鏈接
這篇文章主要介紹了Java web Hibernate如何與數(shù)據(jù)庫鏈接,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友可以參考下2020-06-06VsCode搭建Spring Boot項目并進行創(chuàng)建、運行、調(diào)試
這篇文章主要介紹了VsCode搭建Spring Boot項目并進行創(chuàng)建、運行、調(diào)試 ,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2020-05-05