Spring boot2X Consul如何通過(guò)RestTemplate實(shí)現(xiàn)服務(wù)調(diào)用
這篇文章主要介紹了spring boot2X Consul如何通過(guò)RestTemplate實(shí)現(xiàn)服務(wù)調(diào)用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
Consul可以用于實(shí)現(xiàn)分布式系統(tǒng)的服務(wù)發(fā)現(xiàn)與配置
服務(wù)調(diào)用有兩種方式:
A.使用RestTemplate 進(jìn)行服務(wù)調(diào)用
負(fù)載均衡——通過(guò)Ribbon注解RestTemplate
B.使用Feign 進(jìn)行聲明式服務(wù)調(diào)用
負(fù)載均衡——默認(rèn)使用Ribbon實(shí)現(xiàn)
先使用RestTemplate來(lái)實(shí)現(xiàn)
1.服務(wù)注冊(cè)發(fā)現(xiàn)中心
啟動(dòng)Consul
consul agent -dev
2.服務(wù)端
在spring boot2X整合Consul 的基礎(chǔ)上
添加服務(wù)provider,provider1
provider測(cè)試方法
package com.xyz.provider.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class demoController {
@RequestMapping("/hello")
public String Hello(){
return "hello,provider";
}
}
provider1測(cè)試方法
package com.xyz.provider1.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class demoController {
@RequestMapping("/hello")
public String Hello(){
return "hello,another provider";
}
}
啟動(dòng)provider和provider1
瀏覽器訪(fǎng)問(wèn)http://localhost:8500

有兩個(gè)服務(wù)提供者節(jié)點(diǎn)實(shí)例

3.客戶(hù)端
(1)添加依賴(lài)
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR4</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<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>
(2)添加配置
server.port=8015 spring.application.name=xyz-comsumer spring.cloud.consul.host=localhost spring.cloud.consul.port=8500 spring.cloud.consul.discovery.register=false spring.cloud.consul.discovery.health-check-url=/actuator/health spring.cloud.consul.discovery.heartbeat.enabled=true management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always
(3)測(cè)試方法
獲取所有注冊(cè)的服務(wù),從注冊(cè)的服務(wù)中選取一個(gè),服務(wù)調(diào)用
package com.xyz.comsumer.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class HelloController {
@Autowired
private LoadBalancerClient loadBalancer;
@Autowired
private DiscoveryClient discoveryClient;
private String serviceName = "service-provider";
@RequestMapping("/services")
public Object services() {
return discoveryClient.getInstances(serviceName);
}
@RequestMapping("/discover")
public Object discover() {
return loadBalancer.choose(serviceName).getUri().toString();
}
@RequestMapping("/hello")
public String hello() {
ServiceInstance serviceInstance = loadBalancer.choose(serviceName);
String callServiceResult = new RestTemplate().getForObject(serviceInstance.getUri().toString() + "/hello", String.class);
return callServiceResult;
}
}
注:
客戶(hù)端調(diào)用的服務(wù)名,是在服務(wù)端指定的,在服務(wù)端配置里使用 spring.cloud.consul.discovery.service-name指注冊(cè)到 Consul 的服務(wù)名稱(chēng)
測(cè)試
啟動(dòng)Consul
啟動(dòng)provider和provider1
啟動(dòng)comsumer
測(cè)試地址 http://localhost:8015/services
返回結(jié)果
[
{
"instanceId": "provider-8010",
"serviceId": "service-provider",
"host": "hkgi-PC",
"port": 8010,
"secure": false,
"metadata": {
"secure": "false"
},
"uri": "http://hkgi-PC:8010",
"scheme": null
},
{
"instanceId": "provider-8011",
"serviceId": "service-provider",
"host": "hkgi-PC",
"port": 8011,
"secure": false,
"metadata": {
"secure": "false"
},
"uri": "http://hkgi-PC:8011",
"scheme": null
}
]
測(cè)試地址 http://localhost:8015/discover
返回結(jié)果
- http://hkgi-PC:8011 或 http://hkgi-PC:8011
- 測(cè)試地址 http://localhost:8015/hello
- 返回結(jié)果
- hello,provider 或 hello,another provider
注:
結(jié)果交替出現(xiàn)的,這是因?yàn)樨?fù)載均衡器是采用的是輪詢(xún)的方式
說(shuō)明:
調(diào)用的過(guò)程:
A.通過(guò)LoadBalancerClient查詢(xún)服務(wù)
B.通過(guò)RestTemplate調(diào)用遠(yuǎn)程服務(wù)
Ribbon負(fù)載均衡策略
- BestAvailableRule
- AvailabilityFilteringRule
- WeightedResponseTimeRule
- RetryRule
- RoundRobinRule
- RandomRule
- ZoneAvoidanceRule
自定義Ribbon負(fù)載均衡——使用隨機(jī)訪(fǎng)問(wèn)策略
修改啟動(dòng)類(lèi)
package com.xyz.comsumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class ComsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ComsumerApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
服務(wù)調(diào)用
package com.xyz.comsumer.controller;
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
public class RibbonHelloController {
@Autowired
private RestTemplate restTemplate;
private String serviceName = "service-provider";
@RequestMapping("/ribbon/hello")
public String hello() {
String callServiceResult = restTemplate.getForObject("http://"+serviceName+"/hello", String.class);
return callServiceResult;
}
}
配置添加
service-provider.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
重新啟動(dòng) comsumer
測(cè)試地址 http://localhost:8015/ribbon/hello
輸出的結(jié)果不再是交替出現(xiàn),改為隨機(jī)的了
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
學(xué)習(xí)Java之自定義異常與NullPointerException的處理
有時(shí)候Java自身提供的異常類(lèi)并不能很好地表達(dá)我們的需求,所以這時(shí)候我們就可以自定義異常,也就是說(shuō),我們可以制造出一個(gè)自己的異常類(lèi),這樣就可以?huà)伋龌虿东@自己的異常了,本文就給大家詳細(xì)講講Java自定義異常與NullPointerException的處理2023-08-08
Springboot配置文件相關(guān)說(shuō)明解析
這篇文章主要介紹了Springboot配置文件相關(guān)說(shuō)明解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
SpringBoot?MP簡(jiǎn)單的分頁(yè)查詢(xún)測(cè)試實(shí)現(xiàn)步驟分解
好久沒(méi)水后端的東西了,最近在做vue項(xiàng)目寫(xiě)前端的代碼,所以cloud也停進(jìn)度了,吃完飯突然記得我沒(méi)有在博客里寫(xiě)分頁(yè)的東西,雖然項(xiàng)目中用到了,但是沒(méi)有拎出來(lái),這里就拎出來(lái)看看2023-04-04
Java利用StampedLock實(shí)現(xiàn)讀寫(xiě)鎖的方法詳解
在jdk8以后,java提供了一個(gè)性能更優(yōu)越的讀寫(xiě)鎖并發(fā)類(lèi)StampedLock,該類(lèi)的設(shè)計(jì)初衷是作為一個(gè)內(nèi)部工具類(lèi),用于輔助開(kāi)發(fā)其它線(xiàn)程安全組件。本文就來(lái)和大家一起學(xué)習(xí)下StampedLock的功能和使用2022-10-10
java實(shí)現(xiàn)簡(jiǎn)單音樂(lè)播放器
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單音樂(lè)播放器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
Java實(shí)現(xiàn)經(jīng)典俄羅斯方塊游戲
俄羅斯方塊是一個(gè)最初由阿列克謝帕吉特諾夫在蘇聯(lián)設(shè)計(jì)和編程的益智類(lèi)視頻游戲。本文將利用Java實(shí)現(xiàn)這一經(jīng)典的小游戲,需要的可以參考一下2022-01-01
Spring框架JavaMailSender發(fā)送郵件工具類(lèi)詳解
這篇文章主要為大家詳細(xì)介紹了Spring框架JavaMailSender發(fā)送郵件工具類(lèi),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04
mybatis.type-aliases-package的作用及用法說(shuō)明
這篇文章主要介紹了mybatis.type-aliases-package的作用及用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01

