渠道服务

2020-11-17  本文已影响0人  追梦小蜗牛
pexels-szabolcs-toth-3205909.jpg

背景:

跨境支付行业下游需要对接很多个下发渠道,每个渠道的对接方式和接口、参数、逻辑都有所不同。各自需要一个独立的对接渠道的服务。

简单流程图:

流程图.png

知识点:

配置:

implementation 'org.springframework.boot:spring-boot-starter-webflux:2.3.0.RELEASE'

@Bean
    public WebClient getWebClient() {
        TcpClient tcpClient = TcpClient
                .create()
                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeOut)
                .doOnConnected(connection -> {
                    connection.addHandlerLast(new ReadTimeoutHandler(readTimeout, TimeUnit.MILLISECONDS));
                    connection.addHandlerLast(new WriteTimeoutHandler(writeTimeout, TimeUnit.MILLISECONDS));
                });
        WebClient webClient = WebClient.builder()
                .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                .clientConnector(new ReactorClientHttpConnector(HttpClient.from(tcpClient)))
                .build();

        return webClient;
    }

通用方法

public WebClient.ResponseSpec post(String url, String bodyData, String token) {

        WebClient.ResponseSpec responseSpec = webClient.method(HttpMethod.POST)
                .uri(url)
                .header("Authorization", "Bearer " + token)
                .header("partnerkey", eastWestBankPropertyConfig.getPartnerkey())
                .header("x-correlation-id", "ew-" + idWorker.nextId())
                .contentType(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(bodyData))
                .retrieve();

        return responseSpec;
    }

    public WebClient.ResponseSpec postToken(String url, MultiValueMap<String, String> formData) {

        return webClient.method(HttpMethod.POST)
                .uri(url)
                .contentType(MediaType.APPLICATION_FORM_URLENCODED).body(BodyInserters.fromFormData(formData))
                .retrieve();
    }

同步调用

    private SubAccountOpenResponse postOpenSubAccount(SubAccountOpenRequest subAccountOpenRequest, EastWestBankPropertyConfig eastWestBankPropertyConfig, String token) {
        return webClientUtil.post(eastWestBankPropertyConfig.getSubAccountOpenUrl(), commonService.buildCommonRequestParam(subAccountOpenRequest), token)
                .bodyToMono(SubAccountOpenResponse.class).onErrorResume(Mono::error).block();
    }
    private JSONObject parseJsonFromFile() {
        JSONObject jsonResult = new JSONObject();
        try {
            File jsonFile = ResourceUtils.getFile("classpath:MockData.json");
            String json = FileUtil.readAsString(jsonFile);
            jsonResult = JSON.parseObject(json);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return jsonResult;
    }

注入

    private final MockData mockData;

    @Autowired
    public TradeService(WebClientUtil webClientUtil, CommonService<T> commonService, MockData mockData) {
        this.webClientUtil = webClientUtil;
        this.commonService = commonService;
        this.mockData = mockData;
    }

总结:

未来可以整理一个通用的脚手架,简单的标准、模型。

上一篇 下一篇

猜你喜欢

热点阅读