为什么要使用远程调用?
SpringBoot不仅继承了Spring框架原有的优秀特性,而且还通过简化配置来进一步简化了Spring应用的整个搭建和开发过程。在Spring-Boot项目开发中,存在着本模块的代码需要访问外面模块接口,或外部url链接的需求, 比如在apaas开发过程中需要封装接口在接口中调用apaas提供的接口(像发起流程接口submit等等)下面也是提供了三种方式(不使用dubbo的方式)供我们选择。
方式一:使用原始httpClient请求
public static Map<String, String> httpPostRequest(String url, Map<String, Object> params, int timeout) {
Map<String, String> resultMap = new HashMap<>();
CloseableHttpClient httpClient = HttpClients.createDefault();
String result = "";
try {
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json");
String s = Base64.getEncoder().encodeToString(getAuthorization().getBytes());
log.info("getAuthorization():{}", s);
httpPost.addHeader("authorization", "Basic " + s);
StringEntity se = new StringEntity(JSONObject.toJSONString(params), "utf-8");
se.setContentEncoding("UTF-8");
se.setContentType("application/json");
httpPost.setEntity(se);
HttpResponse response = httpClient.execute(httpPost);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout)
.setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).build();
httpPost.setConfig(requestConfig);
HttpEntity responseEntity = response.getEntity();
resultMap.put("scode", String.valueOf(response.getStatusLine().getStatusCode()