欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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多線程中斷機制三種方法及示例

    Java多線程中斷機制三種方法及示例

    這篇文章主要介紹了Java多線程中斷機制三種方法及示例,向大家分享了這三種方法的介紹幾代碼示例,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • 關于訪問后端接口報404錯誤問題的解決方法(全網最細!)

    關于訪問后端接口報404錯誤問題的解決方法(全網最細!)

    404頁面的出現(xiàn)會降低用戶體驗,那么導致404頁面出現(xiàn)的原因是什么呢?這篇文章主要給大家介紹了關于訪問后端接口報404錯誤問題的解決方法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-04-04
  • 超個性修改SpringBoot項目的啟動banner的方法

    超個性修改SpringBoot項目的啟動banner的方法

    這篇文章主要介紹了超個性修改SpringBoot項目的啟動banner的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-03-03
  • 詳解Java?ReentrantReadWriteLock讀寫鎖的原理與實現(xiàn)

    詳解Java?ReentrantReadWriteLock讀寫鎖的原理與實現(xiàn)

    ReentrantReadWriteLock讀寫鎖是使用AQS的集大成者,用了獨占模式和共享模式。本文和大家一起理解下ReentrantReadWriteLock讀寫鎖的實現(xiàn)原理,需要的可以了解一下
    2022-10-10
  • Java強制類型轉換的所有規(guī)則及說明

    Java強制類型轉換的所有規(guī)則及說明

    這篇文章主要介紹了Java強制類型轉換的所有規(guī)則及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Dubbo+zookeeper?最簡單的分布式搭建方案

    Dubbo+zookeeper?最簡單的分布式搭建方案

    這篇文章主要介紹了Dubbo+zookeeper?最簡單的分布式搭建,本例采用?dubbo+zookeeper?搭建分布式系統(tǒng),環(huán)境?jdk1.8,需要的朋友可以參考下
    2022-04-04
  • 簡單了解java函數(shù)式編碼結構及優(yōu)勢

    簡單了解java函數(shù)式編碼結構及優(yōu)勢

    這篇文章主要介紹了簡單了解java函數(shù)式編碼結構及優(yōu)勢,本文將探討三種下一代 JVM 語言:Groovy、Scala 和 Clojure,比較并對比新的功能和范例,讓 Java 開發(fā)人員對自己近期的未來發(fā)展有大體的認識。,需要的朋友可以參考下
    2019-06-06
  • Java 泛型全解析

    Java 泛型全解析

    這篇文章主要介紹了Java 泛型的相關資料,幫助大家更好的理解和學習Java,感興趣的朋友可以了解下
    2020-08-08
  • Java 8 lambda表達式引入詳解及實例

    Java 8 lambda表達式引入詳解及實例

    這篇文章主要介紹了Java 8 lambda表達式引入詳解及實例的相關資料,需要的朋友可以參考下
    2017-05-05
  • Springboot詳細講解RocketMQ實現(xiàn)順序消息的發(fā)送與消費流程

    Springboot詳細講解RocketMQ實現(xiàn)順序消息的發(fā)送與消費流程

    RocketMQ作為一款純java、分布式、隊列模型的開源消息中間件,支持事務消息、順序消息、批量消息、定時消息、消息回溯等,本篇我們了解如何實現(xiàn)順序消息的發(fā)送與消費
    2022-06-06

最新評論