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远程调用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 深入解析Apache Kafka实时流处理平台

    深入解析Apache Kafka实时流处理平台

    这篇文章主要为大家介绍了Apache Kafka实时流处理平台深入解析,从基本概念到实战操作详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2024-01-01
  • Java通过apache poi生成excel实例代码

    Java通过apache poi生成excel实例代码

    本篇文章主要介绍了Java通过apache poi生成excel实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • Java实现将html字符串插入到PPT幻灯片

    Java实现将html字符串插入到PPT幻灯片

    Java后端代码操作PPT幻灯片时,可直接在幻灯片中绘制形状,并在形状中添加文本字符串内容。本篇文章主要介绍通过java实现将html字符串添加到PPT幻灯片的的方法,可添加文字、图片、视频、音频等。以下是具体方法和步骤。
    2021-11-11
  • SpringCloud升级2020.0.x版之OpenFeign简介与使用实现思路

    SpringCloud升级2020.0.x版之OpenFeign简介与使用实现思路

    在微服务系统中,我们经常会进行 RPC 调用。在 Spring Cloud 体系中,RPC 调用一般就是 HTTP 协议的调用。对于每次调用,都要经过一系列详细步骤,接下来通过本文给大家介绍SpringCloud OpenFeign简介与使用,感兴趣的朋友一起看看吧
    2021-10-10
  • Java Map.getOrDefault方法详解

    Java Map.getOrDefault方法详解

    Map.getOrDefault(Object key, V defaultValue)是Java中Map接口的一个方法,用于获取指定键对应的值,如果键不存在,则返回一个默认值,这篇文章主要介绍了Java Map.getOrDefault方法详解,需要的朋友可以参考下
    2024-01-01
  • echarts图表导出excel示例

    echarts图表导出excel示例

    这篇文章主要介绍了echarts图表导出excel示例,需要的朋友可以参考下
    2014-04-04
  • java基于socket传输zip文件功能示例

    java基于socket传输zip文件功能示例

    这篇文章主要介绍了java基于socket传输zip文件功能,结合实例形式分析了java使用socket进行文件传输的具体操作步骤与服务器端、客户端相关实现技巧,需要的朋友可以参考下
    2017-07-07
  • apollo与springboot集成实现动态刷新配置的教程详解

    apollo与springboot集成实现动态刷新配置的教程详解

    这篇文章主要介绍了apollo与springboot集成实现动态刷新配置,本文分步骤给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-06-06
  • java虚拟机学习笔记进阶篇

    java虚拟机学习笔记进阶篇

    在本篇内容里小编给大家分享了关于java虚拟机学习笔记的进阶内容,需要的朋友们跟着学习下。
    2019-06-06
  • Java中的Cookie和Session详细解析

    Java中的Cookie和Session详细解析

    这篇文章主要介绍了Java中的Cookie和Session详细解析,客户端会话技术,服务端给客户端的数据,存储于客户端(浏览器),由于是保存在客户端上的,所以存在安全问题,需要的朋友可以参考下
    2024-01-01

最新评论