From e9942b22b66bdc1ecaaf18f6d529f562fa9e2db3 Mon Sep 17 00:00:00 2001 From: wangkang Date: Tue, 28 Nov 2017 08:29:21 +0800 Subject: [PATCH] =?UTF-8?q?rms=E7=9A=84call=E6=96=B9=E6=B3=95,=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E6=9C=89header=E7=9A=84=E9=87=8D=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/org/itkk/udf/rms/Rms.java | 81 ++++++++++++++----- 1 file changed, 63 insertions(+), 18 deletions(-) diff --git a/udf-starter-rms/src/main/java/org/itkk/udf/rms/Rms.java b/udf-starter-rms/src/main/java/org/itkk/udf/rms/Rms.java index c12931a..879e669 100644 --- a/udf-starter-rms/src/main/java/org/itkk/udf/rms/Rms.java +++ b/udf-starter-rms/src/main/java/org/itkk/udf/rms/Rms.java @@ -7,6 +7,7 @@ package org.itkk.udf.rms; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; import org.itkk.udf.core.exception.PermissionException; import org.itkk.udf.rms.meta.ApplicationMeta; @@ -65,6 +66,7 @@ public class Rms { * @param host 地址 * @param port 端口 * @param serviceCode 服务代码 + * @param headers headers * @param input 输入参数 * @param uriParam uri参数 * @param responseType 返回类型 @@ -73,7 +75,7 @@ public class Rms { * @param 输出类型 * @return 服务结果 */ - public ResponseEntity call(String host, int port, String serviceCode, I input, String uriParam, ParameterizedTypeReference responseType, Map uriVariables) { + public ResponseEntity call(String host, int port, String serviceCode, HttpHeaders headers, I input, String uriParam, ParameterizedTypeReference responseType, Map uriVariables) { //客户端权限验证 verification(serviceCode); //获取服务元数据 @@ -88,6 +90,10 @@ public class Rms { } //构建请求头 HttpHeaders httpHeaders = buildSystemTagHeaders(serviceCode); + //添加请求头 + if (MapUtils.isNotEmpty(headers)) { + httpHeaders.putAll(headers); + } //构建请求消息体 HttpEntity requestEntity = new HttpEntity<>(input, httpHeaders); //请求并且返回 @@ -95,6 +101,61 @@ public class Rms { return externalRestTemplate.exchange(path, HttpMethod.resolve(method), requestEntity, responseType, uriVariables != null ? uriVariables : new HashMap()); } + /** + * 远程服务调用 (不走ribbon) + * + * @param host 地址 + * @param port 端口 + * @param serviceCode 服务代码 + * @param input 输入参数 + * @param uriParam uri参数 + * @param responseType 返回类型 + * @param uriVariables rest参数 + * @param 输入类型 + * @param 输出类型 + * @return 服务结果 + */ + public ResponseEntity call(String host, int port, String serviceCode, I input, String uriParam, ParameterizedTypeReference responseType, Map uriVariables) { + return this.call(host, port, serviceCode, null, input, uriParam, responseType, uriVariables); + } + + /** + * 描述 : 远程服务调用 + * + * @param serviceCode 服务代码 + * @param headers headers + * @param input 输入参数 + * @param uriParam uri参数 + * @param responseType 返回类型 + * @param uriVariables rest参数 + * @param 输入类型 + * @param 输出类型 + * @return 服务结果 + */ + public ResponseEntity call(String serviceCode, HttpHeaders headers, I input, String uriParam, ParameterizedTypeReference responseType, Map uriVariables) { + //客户端权限验证 + verification(serviceCode); + //构建请求路径 + String path = getRmsUrl(serviceCode); + //获得请求方法 + String method = getRmsMethod(serviceCode); + //拼装路径参数 + if (StringUtils.isNotBlank(uriParam)) { + path += uriParam; + } + //构建请求头 + HttpHeaders httpHeaders = buildSystemTagHeaders(serviceCode); + //添加请求头 + if (MapUtils.isNotEmpty(headers)) { + httpHeaders.putAll(headers); + } + //构建请求消息体 + HttpEntity requestEntity = new HttpEntity<>(input, httpHeaders); + //请求并且返回 + log.debug("rms url : {} , method : {} ", path, method); + return restTemplate.exchange(path, HttpMethod.resolve(method), requestEntity, responseType, uriVariables != null ? uriVariables : new HashMap()); + } + /** * 描述 : 远程服务调用 * @@ -108,23 +169,7 @@ public class Rms { * @return 服务结果 */ public ResponseEntity call(String serviceCode, I input, String uriParam, ParameterizedTypeReference responseType, Map uriVariables) { - //客户端权限验证 - verification(serviceCode); - //构建请求路径 - String path = getRmsUrl(serviceCode); - //获得请求方法 - String method = getRmsMethod(serviceCode); - //拼装路径参数 - if (StringUtils.isNotBlank(uriParam)) { - path += uriParam; - } - //构建请求头 - HttpHeaders httpHeaders = buildSystemTagHeaders(serviceCode); - //构建请求消息体 - HttpEntity requestEntity = new HttpEntity<>(input, httpHeaders); - //请求并且返回 - log.debug("rms url : {} , method : {} ", path, method); - return restTemplate.exchange(path, HttpMethod.resolve(method), requestEntity, responseType, uriVariables != null ? uriVariables : new HashMap()); + return this.call(serviceCode, null, input, uriParam, responseType, uriVariables); } /**