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

相关文章

  • Java解析JSON数据时报错问题解决方案

    Java解析JSON数据时报错问题解决方案

    这篇文章主要介绍了Java解析JSON数据时报错问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-10-10
  • Java实现文件监控器FileMonitor的实例代码

    Java实现文件监控器FileMonitor的实例代码

    这篇文章主要介绍了Java实现文件监控器FileMonitor的实例代码,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-12-12
  • idea 2023.1字体设置及自动调整大小的图文教程

    idea 2023.1字体设置及自动调整大小的图文教程

    这篇文章主要介绍了idea 2023.1字体设置及自动调整大小的教程,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友可以参考下
    2023-07-07
  • 使用Java操作TensorFlow的方法

    使用Java操作TensorFlow的方法

    TensorFlow是一个功能强大且广泛使用的框架,它不断得到改进,并最近被引入新语言包括Java和JavaScript,这篇文章主要介绍了如何使用Java操作TensorFlow,需要的朋友可以参考下
    2023-05-05
  • Java+JFrame实现贪吃蛇小游戏

    Java+JFrame实现贪吃蛇小游戏

    这篇文章主要为大家详细介绍了Java+JFrame实现贪吃蛇小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • Java实现List与数组互转(Arrays.asList与Collectors.toList)的两种方法

    Java实现List与数组互转(Arrays.asList与Collectors.toList)的两种方法

    在 Java 编程中,List 和数组(Array)是两种常用的数据结构,本文将深入探讨 List 与数组之间的相互转换,重点介绍 Arrays.asList 和 Collectors.toList 这两种常用且重要的方法,并分析它们的特点、适用场景及注意事项,需要的朋友可以参考下
    2026-01-01
  • Java实现JSON与XML相互转换的简明教程

    Java实现JSON与XML相互转换的简明教程

    Java实现复杂数据结构(如嵌套对象、数组)在 JSON 与 XML 之间的相互转换,可以使用 Jackson 和 Jackson XML 扩展库来完成,Jackson 是一个流行的 JSON 处理库,通过 Jackson 的 XML 扩展库,可以实现 JSON 和 XML 之间的转换,需要的朋友可以参考下
    2024-08-08
  • IntelliJ IDEA下自动生成Hibernate映射文件以及实体类

    IntelliJ IDEA下自动生成Hibernate映射文件以及实体类

    这篇文章主要介绍了IntelliJ IDEA下自动生成Hibernate映射文件以及实体类,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11
  • 解决springboot遇到autowire注入为null的问题

    解决springboot遇到autowire注入为null的问题

    这篇文章主要介绍了解决springboot遇到autowire注入为null的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03
  • Spring @ConditionalOnMissingBean 注解的主要作用解析

    Spring @ConditionalOnMissingBean 注解的主要作用解析

    @ConditionalOnMissingBean是Spring Boot中用于条件化配置的注解,确保只有在指定Bean不存在时才创建,它在自动配置中广泛应用,提供默认配置并允许用户自定义Bean,本文给大家介绍Spring @ConditionalOnMissingBean注解的作用,感兴趣的朋友一起看看吧
    2026-01-01

最新评论