| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- package cn.com.yusys.consumer.util;
- import cn.com.yusys.consumer.model.Task;
- import cn.com.yusys.consumer.util.response.ExecuteResponse;
- import cn.com.yusys.consumer.util.response.InstanceStatusResponse;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.MediaType;
- import org.springframework.stereotype.Component;
- import org.springframework.web.reactive.function.client.WebClient;
- import org.springframework.web.reactive.function.client.WebClientResponseException;
- import reactor.core.publisher.Mono;
- /**
- * 解析服务实例实例接口调用工具
- */
- @Slf4j
- @Component
- public class ParseServiceClient {
- private final WebClient webClient;
- // 通过构造函数注入WebClient.Builder,利用Spring自动配置
- public ParseServiceClient(WebClient.Builder webClientBuilder) {
- this.webClient = webClientBuilder
- .codecs(config -> config.defaultCodecs().maxInMemorySize(1024 * 1024))
- .build();
- }
- /**
- * 调用/execute接口执行任务
- * @param taskData 任务数据
- * @return 执行结果响应
- */
- public ExecuteResponse executeTask(Task taskData) {
- String executeUrl = "http://127.0.0.1:8083//api/manager/parse";
- try {
- ExecuteResponse response = webClient.post()
- .uri(executeUrl)
- .contentType(MediaType.APPLICATION_JSON)
- .bodyValue(taskData)
- .retrieve()
- .onStatus(HttpStatus::isError, clientResponse ->
- Mono.error(new WebClientResponseException(
- "执行接口返回异常状态码",
- clientResponse.statusCode().value(),
- clientResponse.statusCode().getReasonPhrase(),
- null, null, null)))
- .bodyToMono(ExecuteResponse.class)
- .timeout(java.time.Duration.ofSeconds(30))
- .block();
- if (response != null && 200 == response.getCode()) {
- log.debug("任务执行成功,响应:{}", response);
- return response;
- } else {
- log.warn("任务执行返回失败,响应:{}", response);
- return createErrorResponse("接口返回非200响应");
- }
- } catch (WebClientResponseException e) {
- log.error("任务执行失败,HTTP状态码:{}", e.getRawStatusCode(), e);
- return createErrorResponse(String.format("HTTP请求失败,状态码:%d", e.getRawStatusCode()));
- } catch (Exception e) {
- log.error("任务执行异常", e);
- return createErrorResponse(e.getMessage());
- }
- }
- /**
- * 创建错误响应
- * @param errorMessage 错误消息
- * @return ExecuteResponse 错误响应
- */
- private ExecuteResponse createErrorResponse(String errorMessage) {
- ExecuteResponse response = new ExecuteResponse();
- response.setCode(500);
- response.setMessage(errorMessage);
- return response;
- }
- }
|