springboot3整合远程调用的过程解析
更新时间:2023年06月16日 10:20:44 作者:鸭鸭老板
远程过程调用主要分为:服务提供者,服务消费者,通过连接对方服务器进行请求交互,来实现调用效果,这篇文章主要介绍了springboot3整合远程调用,需要的朋友可以参考下
一、远程调用
远程过程调用:主要分为:服务提供者,服务消费者。通过连接对方服务器进行请求交互,来实现调用效果。
二、使用WebClient
@Service public class WeatherServiceImpl { public Mono<String> weather(String city){ //创建Webclient WebClient webClient = WebClient.create(); HashMap<String, String> map = new HashMap<>(); map.put("areaCn",city); //定义发送请求的行为 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、创建客户端 WebClient client = WebClient.builder() .baseUrl("https://getweather.market.alicloudapi.com") .codecs(clientCodecConfigurer -> { clientCodecConfigurer .defaultCodecs() .maxInMemorySize(256*1024*1024); //响应数据量太大有可能会超出BufferSize,所以这里设置的大一点 }).build(); //2、创建工厂 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、创建客户端 WebClient client = WebClient.builder() .defaultHeader("Authorization","APPCODE "+ appCode) .codecs(clientCodecConfigurer -> { clientCodecConfigurer .defaultCodecs() .maxInMemorySize(256*1024*1024); //响应数据量太大有可能会超出BufferSize,所以这里设置的大一点 }).build(); //2、创建工厂 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远程调用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Spring中@Configuration注解的Full模式和Lite模式详解
这篇文章主要介绍了Spring中@Configuration注解的Full模式和Lite模式详解,准确来说,Full 模式和 Lite 模式其实 Spring 容器在处理 Bean 时的两种不同行为,这两种不同的模式在使用时候的表现完全不同,今天就来和各位小伙伴捋一捋这两种模式,需要的朋友可以参考下2023-09-09Spring中的NamespaceHandler加载过程源码详解
这篇文章主要介绍了Spring中的NamespaceHandler加载过程源码详解,Spring提供的NamespaceHandler的处理机制,简单来说就是命名空间处理器,Spring为了开放性提供了NamespaceHandler机制,这样我们就可以根据需求自己来处理我们设置的标签元素,需要的朋友可以参考下2024-02-02
最新评论