springboot3整合遠(yuǎn)程調(diào)用的過(guò)程解析
一、遠(yuǎn)程調(diào)用

遠(yuǎn)程過(guò)程調(diào)用:主要分為:服務(wù)提供者,服務(wù)消費(fèi)者。通過(guò)連接對(duì)方服務(wù)器進(jìn)行請(qǐng)求交互,來(lái)實(shí)現(xiàn)調(diào)用效果。
二、使用WebClient
@Service
public class WeatherServiceImpl {
public Mono<String> weather(String city){
//創(chuàng)建Webclient
WebClient webClient = WebClient.create();
HashMap<String, String> map = new HashMap<>();
map.put("areaCn",city);
//定義發(fā)送請(qǐng)求的行為
Mono<String> mono = webClient.get()
.uri("https://getweather.market.alicloudapi.com/lundear/weather7d?areaCn={areaCn}",map)
.accept(MediaType.APPLICATION_JSON)//定義響應(yīng)的內(nèi)容類(lèi)型
.header("Authorization", "APPCODE 05ed0debacd9479c9788b1a44266eaef")
.retrieve()
.bodyToMono(String.class);
return mono;
}
}@RestController
public class WeatherController {
@Autowired
private WeatherServiceImpl weatherService;
@GetMapping("/weather")
public Mono<String> weather(@RequestParam("city") String city){
return weatherService.weather(city);
}
@GetMapping("/hello")
public String hello(){
return "你好";
}
}
三、使用HTTP Interface
public interface WeatherInterface {
@GetExchange(url = "/lundear/weather7d",accept = "application/json")
Mono<String> getWeather(@RequestParam("areaCn") String city,
@RequestHeader("Authorization") String auth);
} public Mono<String> weather1(String city){
//1、創(chuàng)建客戶(hù)端
WebClient client = WebClient.builder()
.baseUrl("https://getweather.market.alicloudapi.com")
.codecs(clientCodecConfigurer -> {
clientCodecConfigurer
.defaultCodecs()
.maxInMemorySize(256*1024*1024);
//響應(yīng)數(shù)據(jù)量太大有可能會(huì)超出BufferSize,所以這里設(shè)置的大一點(diǎn)
}).build();
//2、創(chuàng)建工廠(chǎng)
HttpServiceProxyFactory factory = HttpServiceProxyFactory
.builder(WebClientAdapter.forClient(client)).build();
//3、獲取代理對(duì)象
WeatherInterface weatherInterface = factory.createClient(WeatherInterface.class);
Mono<String> weather = weatherInterface.getWeather(city, "APPCODE 05ed0debacd9479c9788b1a44266eaef");
return weather;
}3.1、抽取方法
在配置文件中配置appcode

config配置類(lèi)
@Configuration
public class RPCConfig {
@Value("${aliyu.appcode}")
private String appCode;
@Bean
HttpServiceProxyFactory factory(){
//1、創(chuàng)建客戶(hù)端
WebClient client = WebClient.builder()
.defaultHeader("Authorization","APPCODE "+ appCode)
.codecs(clientCodecConfigurer -> {
clientCodecConfigurer
.defaultCodecs()
.maxInMemorySize(256*1024*1024);
//響應(yīng)數(shù)據(jù)量太大有可能會(huì)超出BufferSize,所以這里設(shè)置的大一點(diǎn)
}).build();
//2、創(chuàng)建工廠(chǎng)
HttpServiceProxyFactory factory = HttpServiceProxyFactory
.builder(WebClientAdapter.forClient(client)).build();
return factory;
}
@Bean
WeatherInterface weatherInterface(HttpServiceProxyFactory httpServiceProxyFactory){
//3、獲取代理對(duì)象
WeatherInterface weatherInterface = httpServiceProxyFactory.createClient(WeatherInterface.class);
return weatherInterface;
}
@Bean
AlicloudAPIService alicloudAPIService(HttpServiceProxyFactory httpServiceProxyFactory){
AlicloudAPIService alicloudAPIService = httpServiceProxyFactory.createClient(AlicloudAPIService.class);
return alicloudAPIService;
}
}public interface AlicloudAPIService {
@GetExchange(url = "https://wuliu.market.alicloudapi.com/kdi",accept = "application/json")
Mono<String> getWeather(@RequestParam("no") String no);
}public interface WeatherInterface {
@GetExchange(url = "https://getweather.market.alicloudapi.com/lundear/weather7d",accept = "application/json")
Mono<String> getWeather(@RequestParam("areaCn") String city);
} @Autowired
private WeatherInterface weatherInterface;
@Autowired
private AlicloudAPIService alicloudAPIService;
public Mono<String> weather1(String city){
Mono<String> weather = weatherInterface.getWeather(city);
return weather;
}
public Mono<String> alicloudAPI(String no){
Mono<String> weather = alicloudAPIService.getWeather(no);
return weather;
}@RestController
public class WeatherController {
@Autowired
private WeatherServiceImpl weatherService;
@GetMapping("/weather")
public Mono<String> weather(@RequestParam("city") String city){
//return weatherService.weather(city);
return weatherService.weather1(city);
}
@GetMapping("/alicloudAPI")
public Mono<String> alicloudAPI(@RequestParam("no") String no){
return weatherService.alicloudAPI(no);
}
}到此這篇關(guān)于springboot3整合遠(yuǎn)程調(diào)用的文章就介紹到這了,更多相關(guān)springboot3遠(yuǎn)程調(diào)用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot整合RabbitMQ實(shí)現(xiàn)RPC遠(yuǎn)程調(diào)用功能
- SpringBoot + openFeign實(shí)現(xiàn)遠(yuǎn)程接口調(diào)用的過(guò)程
- SpringBoot?Http遠(yuǎn)程調(diào)用的方法
- springBoot使用openfeign來(lái)遠(yuǎn)程調(diào)用的實(shí)現(xiàn)
- 在SpringBoot中,如何使用Netty實(shí)現(xiàn)遠(yuǎn)程調(diào)用方法總結(jié)
- SpringBoot整合Dubbo框架,實(shí)現(xiàn)RPC服務(wù)遠(yuǎn)程調(diào)用
- SpringBoot使用Netty實(shí)現(xiàn)遠(yuǎn)程調(diào)用的示例
- SpringBoot如何使用feign實(shí)現(xiàn)遠(yuǎn)程接口調(diào)用和錯(cuò)誤熔斷
相關(guān)文章
關(guān)于訪(fǎng)問(wèn)后端接口報(bào)404錯(cuò)誤問(wèn)題的解決方法(全網(wǎng)最細(xì)!)
404頁(yè)面的出現(xiàn)會(huì)降低用戶(hù)體驗(yàn),那么導(dǎo)致404頁(yè)面出現(xiàn)的原因是什么呢?這篇文章主要給大家介紹了關(guān)于訪(fǎng)問(wèn)后端接口報(bào)404錯(cuò)誤問(wèn)題的解決方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
超個(gè)性修改SpringBoot項(xiàng)目的啟動(dòng)banner的方法
這篇文章主要介紹了超個(gè)性修改SpringBoot項(xiàng)目的啟動(dòng)banner的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
詳解Java?ReentrantReadWriteLock讀寫(xiě)鎖的原理與實(shí)現(xiàn)
ReentrantReadWriteLock讀寫(xiě)鎖是使用AQS的集大成者,用了獨(dú)占模式和共享模式。本文和大家一起理解下ReentrantReadWriteLock讀寫(xiě)鎖的實(shí)現(xiàn)原理,需要的可以了解一下2022-10-10
Java強(qiáng)制類(lèi)型轉(zhuǎn)換的所有規(guī)則及說(shuō)明
這篇文章主要介紹了Java強(qiáng)制類(lèi)型轉(zhuǎn)換的所有規(guī)則及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
Dubbo+zookeeper?最簡(jiǎn)單的分布式搭建方案
這篇文章主要介紹了Dubbo+zookeeper?最簡(jiǎn)單的分布式搭建,本例采用?dubbo+zookeeper?搭建分布式系統(tǒng),環(huán)境?jdk1.8,需要的朋友可以參考下2022-04-04
簡(jiǎn)單了解java函數(shù)式編碼結(jié)構(gòu)及優(yōu)勢(shì)
這篇文章主要介紹了簡(jiǎn)單了解java函數(shù)式編碼結(jié)構(gòu)及優(yōu)勢(shì),本文將探討三種下一代 JVM 語(yǔ)言:Groovy、Scala 和 Clojure,比較并對(duì)比新的功能和范例,讓 Java 開(kāi)發(fā)人員對(duì)自己近期的未來(lái)發(fā)展有大體的認(rèn)識(shí)。,需要的朋友可以參考下2019-06-06
Java 8 lambda表達(dá)式引入詳解及實(shí)例
這篇文章主要介紹了Java 8 lambda表達(dá)式引入詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-05-05
Springboot詳細(xì)講解RocketMQ實(shí)現(xiàn)順序消息的發(fā)送與消費(fèi)流程
RocketMQ作為一款純java、分布式、隊(duì)列模型的開(kāi)源消息中間件,支持事務(wù)消息、順序消息、批量消息、定時(shí)消息、消息回溯等,本篇我們了解如何實(shí)現(xiàn)順序消息的發(fā)送與消費(fèi)2022-06-06

