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

遠程過程調(diào)用:主要分為:服務(wù)提供者,服務(wù)消費者。通過連接對方服務(wù)器進行請求交互,來實現(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)//定義響應(yīng)的內(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);
//響應(yīng)數(shù)據(jù)量太大有可能會超出BufferSize,所以這里設(shè)置的大一點
}).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);
//響應(yīng)數(shù)據(jù)量太大有可能會超出BufferSize,所以這里設(shè)置的大一點
}).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服務(wù)遠程調(diào)用
- SpringBoot使用Netty實現(xiàn)遠程調(diào)用的示例
- SpringBoot如何使用feign實現(xiàn)遠程接口調(diào)用和錯誤熔斷
相關(guān)文章
Java解析JSON數(shù)據(jù)時報錯問題解決方案
這篇文章主要介紹了Java解析JSON數(shù)據(jù)時報錯問題解決方案,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10
Java實現(xiàn)文件監(jiān)控器FileMonitor的實例代碼
這篇文章主要介紹了Java實現(xiàn)文件監(jiān)控器FileMonitor的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12
idea 2023.1字體設(shè)置及自動調(diào)整大小的圖文教程
這篇文章主要介紹了idea 2023.1字體設(shè)置及自動調(diào)整大小的教程,本文通過圖文并茂的形式給大家介紹的非常詳細,需要的朋友可以參考下2023-07-07
Java實現(xiàn)List與數(shù)組互轉(zhuǎn)(Arrays.asList與Collectors.toList)的兩種方法
在 Java 編程中,List 和數(shù)組(Array)是兩種常用的數(shù)據(jù)結(jié)構(gòu),本文將深入探討 List 與數(shù)組之間的相互轉(zhuǎn)換,重點介紹 Arrays.asList 和 Collectors.toList 這兩種常用且重要的方法,并分析它們的特點、適用場景及注意事項,需要的朋友可以參考下2026-01-01
Java實現(xiàn)JSON與XML相互轉(zhuǎn)換的簡明教程
Java實現(xiàn)復(fù)雜數(shù)據(jù)結(jié)構(gòu)(如嵌套對象、數(shù)組)在 JSON 與 XML 之間的相互轉(zhuǎn)換,可以使用 Jackson 和 Jackson XML 擴展庫來完成,Jackson 是一個流行的 JSON 處理庫,通過 Jackson 的 XML 擴展庫,可以實現(xiàn) JSON 和 XML 之間的轉(zhuǎn)換,需要的朋友可以參考下2024-08-08
IntelliJ IDEA下自動生成Hibernate映射文件以及實體類
這篇文章主要介紹了IntelliJ IDEA下自動生成Hibernate映射文件以及實體類,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
解決springboot遇到autowire注入為null的問題
這篇文章主要介紹了解決springboot遇到autowire注入為null的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
Spring @ConditionalOnMissingBean 注解的主要作用解析
@ConditionalOnMissingBean是Spring Boot中用于條件化配置的注解,確保只有在指定Bean不存在時才創(chuàng)建,它在自動配置中廣泛應(yīng)用,提供默認配置并允許用戶自定義Bean,本文給大家介紹Spring @ConditionalOnMissingBean注解的作用,感興趣的朋友一起看看吧2026-01-01

