java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > springboot3远程调用

springboot3整合远程调用的过程解析

作者:鸭鸭老板

远程过程调用主要分为:服务提供者,服务消费者,通过连接对方服务器进行请求交互,来实现调用效果,这篇文章主要介绍了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远程调用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文