springboot3整合遠程調用的過程解析
更新時間:2023年06月16日 10:20:44 作者:鴨鴨老板
遠程過程調用主要分為:服務提供者,服務消費者,通過連接對方服務器進行請求交互,來實現(xiàn)調用效果,這篇文章主要介紹了springboot3整合遠程調用,需要的朋友可以參考下
一、遠程調用

遠程過程調用:主要分為:服務提供者,服務消費者。通過連接對方服務器進行請求交互,來實現(xiàn)調用效果。
二、使用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)//定義響應的內容類型
.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);
}
}到此這篇關于springboot3整合遠程調用的文章就介紹到這了,更多相關springboot3遠程調用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解Java?ReentrantReadWriteLock讀寫鎖的原理與實現(xiàn)
ReentrantReadWriteLock讀寫鎖是使用AQS的集大成者,用了獨占模式和共享模式。本文和大家一起理解下ReentrantReadWriteLock讀寫鎖的實現(xiàn)原理,需要的可以了解一下2022-10-10
Springboot詳細講解RocketMQ實現(xiàn)順序消息的發(fā)送與消費流程
RocketMQ作為一款純java、分布式、隊列模型的開源消息中間件,支持事務消息、順序消息、批量消息、定時消息、消息回溯等,本篇我們了解如何實現(xiàn)順序消息的發(fā)送與消費2022-06-06

