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

springboot3整合遠(yuǎn)程調(diào)用的過程解析

 更新時(shí)間:2023年06月16日 10:20:44   作者:鴨鴨老板  
遠(yuǎn)程過程調(diào)用主要分為:服務(wù)提供者,服務(wù)消費(fèi)者,通過連接對方服務(wù)器進(jìn)行請求交互,來實(shí)現(xiàn)調(diào)用效果,這篇文章主要介紹了springboot3整合遠(yuǎn)程調(diào)用,需要的朋友可以參考下

一、遠(yuǎn)程調(diào)用

 遠(yuǎn)程過程調(diào)用:主要分為:服務(wù)提供者,服務(wù)消費(fèi)者。通過連接對方服務(wù)器進(jìn)行請求交互,來實(shí)現(xiàn)調(diào)用效果。

二、使用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)//定義響應(yīng)的內(nèi)容類型
                .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);
                    //響應(yīng)數(shù)據(jù)量太大有可能會超出BufferSize,所以這里設(shè)置的大一點(diǎn)
                }).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);
                    //響應(yīng)數(shù)據(jù)量太大有可能會超出BufferSize,所以這里設(shè)置的大一點(diǎn)
                }).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);
    }
}

到此這篇關(guān)于springboot3整合遠(yuǎn)程調(diào)用的文章就介紹到這了,更多相關(guān)springboot3遠(yuǎn)程調(diào)用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論