微服务http调用其他服务的方法

发布时间:2024年01月12日
  1. 在对应需要调的服务配置文件加上路径
#===============审批方案微服务配置================
server.port: 9004
upload.path: /alldev/u01/
schedule.cron.countDown: 0 0 8-18 * * ?
statistics.syskey: ywsp
schedule.countDown.isExecute: true
post.url.updateStatus: http://10.3.2.222:8888/ecological-retrieval/ZbStZtgl/updateState(对应调用的方法路径)
  1. 依赖
  <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5.10</version>
 </dependency>
  1. 获取服务器配置文件调用方法路径
 @Value("${post.url.updateStatus}") 
 private String updateStatusUrl;
  1. 调用方法
  public SendResponse sendHttpPublish(ShzbDoSendRequest doSendRequest) {
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost request = new HttpPost(updateStatusUrl);
            request.addHeader("Token-Auth", authUtil.getToken());
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            ContentType contentType = ContentType.create("text/plain", StandardCharsets.UTF_8);
            builder.addTextBody("ids", doSendRequest.getInstId(), contentType);
            builder.addTextBody("state", doSendRequest.getState().toString(), contentType);
            builder.addTextBody("mkmc", doSendRequest.getMkmc(), contentType);
            HttpEntity build = builder.build();
            request.setEntity(build);
            CloseableHttpResponse response = httpClient.execute(request);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != 200) {
                throw new RuntimeException("请求ecological-retrieval服务统一状态更新接口失败, 请联系管理员!");
            }
        } catch (Exception e) {
            throw new RuntimeException("更新项目状态失败!" + e.getMessage());
        }
        return caseService.doSend(doSendRequest);
    }
文章来源:https://blog.csdn.net/weixin_44165280/article/details/135558050
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。