springboot3整合遠程調(diào)用的過程解析
一、遠程調(diào)用

遠程過程調(diào)用:主要分為:服務提供者,服務消費者。通過連接對方服務器進行請求交互,來實現(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ā)送請求的行為
Mono<String> mono = webClient.get()
.uri("https://getweather.market.alicloudapi.com/lundear/weather7d?areaCn={areaCn}",map)
.accept(MediaType.APPLICATION_JSON)//定義響應的內(nè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)建客戶端
WebClient client = WebClient.builder()
.baseUrl("https://getweather.market.alicloudapi.com")
.codecs(clientCodecConfigurer -> {
clientCodecConfigurer
.defaultCodecs()
.maxInMemorySize(256*1024*1024);
//響應數(shù)據(jù)量太大有可能會超出BufferSize,所以這里設置的大一點
}).build();
//2、創(chuàng)建工廠
HttpServiceProxyFactory factory = HttpServiceProxyFactory
.builder(WebClientAdapter.forClient(client)).build();
//3、獲取代理對象
WeatherInterface weatherInterface = factory.createClient(WeatherInterface.class);
Mono<String> weather = weatherInterface.getWeather(city, "APPCODE 05ed0debacd9479c9788b1a44266eaef");
return weather;
}3.1、抽取方法
在配置文件中配置appcode

config配置類
@Configuration
public class RPCConfig {
@Value("${aliyu.appcode}")
private String appCode;
@Bean
HttpServiceProxyFactory factory(){
//1、創(chuàng)建客戶端
WebClient client = WebClient.builder()
.defaultHeader("Authorization","APPCODE "+ appCode)
.codecs(clientCodecConfigurer -> {
clientCodecConfigurer
.defaultCodecs()
.maxInMemorySize(256*1024*1024);
//響應數(shù)據(jù)量太大有可能會超出BufferSize,所以這里設置的大一點
}).build();
//2、創(chuàng)建工廠
HttpServiceProxyFactory factory = HttpServiceProxyFactory
.builder(WebClientAdapter.forClient(client)).build();
return factory;
}
@Bean
WeatherInterface weatherInterface(HttpServiceProxyFactory httpServiceProxyFactory){
//3、獲取代理對象
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整合遠程調(diào)用的文章就介紹到這了,更多相關(guān)springboot3遠程調(diào)用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot整合RabbitMQ實現(xiàn)RPC遠程調(diào)用功能
- SpringBoot + openFeign實現(xiàn)遠程接口調(diào)用的過程
- SpringBoot?Http遠程調(diào)用的方法
- springBoot使用openfeign來遠程調(diào)用的實現(xiàn)
- 在SpringBoot中,如何使用Netty實現(xiàn)遠程調(diào)用方法總結(jié)
- SpringBoot整合Dubbo框架,實現(xiàn)RPC服務遠程調(diào)用
- SpringBoot使用Netty實現(xiàn)遠程調(diào)用的示例
- SpringBoot如何使用feign實現(xiàn)遠程接口調(diào)用和錯誤熔斷
相關(guān)文章
關(guān)于訪問后端接口報404錯誤問題的解決方法(全網(wǎng)最細!)
404頁面的出現(xiàn)會降低用戶體驗,那么導致404頁面出現(xiàn)的原因是什么呢?這篇文章主要給大家介紹了關(guān)于訪問后端接口報404錯誤問題的解決方法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-04-04
詳解Java?ReentrantReadWriteLock讀寫鎖的原理與實現(xiàn)
ReentrantReadWriteLock讀寫鎖是使用AQS的集大成者,用了獨占模式和共享模式。本文和大家一起理解下ReentrantReadWriteLock讀寫鎖的實現(xiàn)原理,需要的可以了解一下2022-10-10
Java強制類型轉(zhuǎn)換的所有規(guī)則及說明
這篇文章主要介紹了Java強制類型轉(zhuǎn)換的所有規(guī)則及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06
簡單了解java函數(shù)式編碼結(jié)構(gòu)及優(yōu)勢
這篇文章主要介紹了簡單了解java函數(shù)式編碼結(jié)構(gòu)及優(yōu)勢,本文將探討三種下一代 JVM 語言:Groovy、Scala 和 Clojure,比較并對比新的功能和范例,讓 Java 開發(fā)人員對自己近期的未來發(fā)展有大體的認識。,需要的朋友可以參考下2019-06-06
Springboot詳細講解RocketMQ實現(xiàn)順序消息的發(fā)送與消費流程
RocketMQ作為一款純java、分布式、隊列模型的開源消息中間件,支持事務消息、順序消息、批量消息、定時消息、消息回溯等,本篇我們了解如何實現(xiàn)順序消息的發(fā)送與消費2022-06-06

