Browse Source

fix 修改rest調用方式

daiyihua 1 year ago
parent
commit
0ea75e5e69

+ 0 - 18
ywt-platform-outpatient-taihe-rpc/src/main/java/com/ywt/outpatient/OutpatientTaiheRpcServiceApplication.java

@@ -1,33 +1,15 @@
 package com.ywt.outpatient;
 
-import com.ywt.outpatient.taihe.rpc.config.RibbonConfiguration;
 import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.boot.web.client.RestTemplateBuilder;
-import org.springframework.cloud.client.loadbalancer.LoadBalanced;
-import org.springframework.cloud.netflix.ribbon.RibbonClient;
-import org.springframework.cloud.netflix.ribbon.RibbonClients;
-import org.springframework.context.annotation.Bean;
-import org.springframework.web.client.RestTemplate;
 
 @SpringBootApplication
 @EnableDubbo
-@RibbonClients(
-        value = {
-                @RibbonClient(name = "com.ywt.AlipayMpRestService", configuration = RibbonConfiguration.class)
-        }
-)
 public class OutpatientTaiheRpcServiceApplication {
 
     public static void main(String[] args) {
         SpringApplication.run(OutpatientTaiheRpcServiceApplication.class, args);
     }
 
-
-    @Bean
-    @LoadBalanced
-    public RestTemplate getRestTemplate(RestTemplateBuilder builder){
-        return builder.build();
-    }
 }

+ 113 - 0
ywt-platform-outpatient-taihe-rpc/src/main/java/com/ywt/outpatient/taihe/rpc/handler/RestCallerApiProvider.java

@@ -0,0 +1,113 @@
+package com.ywt.outpatient.taihe.rpc.handler;
+
+import com.ywt.biz.common.util.Checker;
+import com.ywt.biz.common.util.HttpUtil;
+import com.ywt.biz.common.util.serializers.JsonSerializer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author daiyihua
+ * @create 2018-0904 上午09:27
+ * @desc 调用 nutrimeal api抽出来的公共方法
+ **/
+@Service
+public class RestCallerApiProvider {
+
+    @Value("${com.ywt.restServiceMap}")
+    private String restServiceMap;
+    private final Logger logger = LoggerFactory.getLogger(RestCallerApiProvider.class);
+
+    @Autowired
+    private RestTemplateHandler restTemplateHandler;
+    private static final String alipayMpRestServiceName = "com.ywt.AlipayMpRestService";
+
+    public HttpUtil.Resp postAlipayMpRestApi(String path, Map<String, Object> map) {
+        return postCommon(alipayMpRestServiceName, path, map);
+    }
+    public HttpUtil.Resp getAlipayMpRestApi(String path, Map<String, String> map) {
+        return getCommon(alipayMpRestServiceName, path, map);
+    }
+
+    /**
+     * 通用 post 方法
+     *
+     * @param serverName 服务名
+     * @param path       路径
+     * @param map        参数
+     * @return {@link HttpUtil.Resp}
+     */
+    private HttpUtil.Resp postCommon(String serverName, String path, Map<String, Object> map) {
+
+        Map<String, String> headers = new HashMap<>(16);
+        headers.put("Content-Type", "application/json;charset=UTF-8");
+
+        String ipPort = getRestIpPort(serverName);
+        if (!Checker.isNone(ipPort)){
+            serverName = ipPort;
+        }
+        logger.info("RestCallerApiProvider>>postCommon(serverName:{}, \tpath:{}, \tmap:{}, \theaders:{})", serverName, path, JsonSerializer.toJson(map), JsonSerializer.toJson(headers));
+
+        RestTemplateHandler.Resp resp = restTemplateHandler.httpPost(serverName, path, map, headers);
+        HttpUtil.Resp r = new HttpUtil.Resp();
+        r.setCode(resp.getCode());
+        r.setContent(resp.getContent());
+        return r;
+    }
+
+    /**
+     * 通用 get 方法
+     *
+     * @param serverName 服务名
+     * @param path       路径
+     * @param map        参数
+     * @return {@link HttpUtil.Resp}
+     */
+    private HttpUtil.Resp getCommon(String serverName, String path, Map<String, String> map) {
+
+        String ipPort = getRestIpPort(serverName);
+        if (!Checker.isNone(ipPort)){
+            serverName = ipPort;
+        }
+        logger.info("RestCallerApiProvider>>getCommon(serverName:{}, \tpath:{}, \tmap:{})", serverName, path, JsonSerializer.toJson(map));
+        RestTemplateHandler.Resp resp = restTemplateHandler.httpGet(serverName, path, map, null);
+        HttpUtil.Resp r = new HttpUtil.Resp();
+        r.setCode(resp.getCode());
+        r.setContent(resp.getContent());
+        return r;
+    }
+
+
+    public String getRestServiceMap() {
+        return restServiceMap;
+    }
+    public String getRestIpPort(String restName) {
+        String restIpPort = "";
+        String restServiceMapParam = getRestServiceMap();
+        try {
+            if (!Checker.isNone(restServiceMapParam)) {
+                List list = JsonSerializer.from(restServiceMapParam, List.class);
+                if (!Checker.isNone(list)) {
+                    for (Object o : list) {
+                        Map<String, Object> map = (Map<String, Object>) o;
+                        String restNameValue = (String) map.getOrDefault("restName", "");
+                        String restIpPortValue = (String) map.getOrDefault("restIpPort", "");
+                        if (!Checker.isNone(restNameValue) && restName.equals(restNameValue)) {
+                            return restIpPortValue;
+                        }
+                    }
+                }
+            }
+        } catch (Exception e) {
+            logger.error("RestCallerApiProvider#getRestServiceMap(restServiceMapParam{}参数有误,{})", restServiceMapParam, e.getMessage(), e);
+        }
+        return restIpPort;
+    }
+}

+ 35 - 0
ywt-platform-outpatient-taihe-rpc/src/main/java/com/ywt/outpatient/taihe/rpc/handler/RestTemplateConfig.java

@@ -0,0 +1,35 @@
+package com.ywt.outpatient.taihe.rpc.handler;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.http.client.ClientHttpRequestFactory;
+import org.springframework.http.client.SimpleClientHttpRequestFactory;
+import org.springframework.http.converter.StringHttpMessageConverter;
+import org.springframework.web.client.RestTemplate;
+
+import java.nio.charset.StandardCharsets;
+
+/**
+ * @author Walker
+ * Created on 2023/11/17
+ */
+@Configuration
+public class RestTemplateConfig {
+
+    @Bean
+    public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
+        RestTemplate restTemplate =  new RestTemplate(factory);
+        // 添加对utf的处理
+        restTemplate.getMessageConverters()
+                .add(0, new StringHttpMessageConverter(StandardCharsets.UTF_8));
+        return restTemplate;
+    }
+
+    @Bean
+    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
+        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
+        factory.setReadTimeout(5000);
+        factory.setConnectTimeout(15000);
+        return factory;
+    }
+}

+ 0 - 7
ywt-platform-outpatient-taihe-rpc/src/main/java/com/ywt/outpatient/taihe/rpc/handler/RestTemplateHandler.java

@@ -171,9 +171,6 @@ public class RestTemplateHandler {
         return resp;
     }
 
-    public  RestTemplateHandler.Resp httpPostCenter(String path,Map<String, Object> params)  {
-        return httpPostCommon(mgCenterApiService,path,params);
-    }
 
     public  RestTemplateHandler.Resp httpPostCommon(String serviceName,String path,Map<String, Object> params)  {
         Map<String, String> headers = new HashMap<>(16);
@@ -183,10 +180,6 @@ public class RestTemplateHandler {
 
 
 
-    public  RestTemplateHandler.ByteArrayResp httpGetBarryCenter(String path,Map<String, String> params)  {
-        return this.httpGetBarry(mgCenterApiService,path,params);
-    }
-
     public  RestTemplateHandler.ByteArrayResp httpGetBarry(String serviceName,String path,Map<String, String> params)  {
         Map<String, String> headers = new HashMap<>(16);
         headers.put("Content-Type", "application/json;charset=UTF-8");

+ 4 - 7
ywt-platform-outpatient-taihe-rpc/src/main/java/com/ywt/outpatient/taihe/rpc/provider/TaiheRegisterProvider.java

@@ -5,6 +5,7 @@ import com.ywt.biz.common.constant.GlobalConstants;
 import com.ywt.biz.common.constant.YwtCommonRespCode;
 import com.ywt.biz.common.enums.PaymentChannelEnum;
 import com.ywt.biz.common.exception.YwtCommonException;
+import com.ywt.biz.common.util.HttpUtil;
 import com.ywt.gapi.Result;
 import com.ywt.gapi.ResultCode;
 import com.ywt.gapi.base.idCard.IdCardService;
@@ -28,7 +29,7 @@ import com.ywt.outpatient.taihe.rpc.domain.entity.center.*;
 import com.ywt.outpatient.taihe.rpc.domain.entity.center.User;
 import com.ywt.outpatient.taihe.rpc.domain.enums.*;
 import com.ywt.outpatient.taihe.rpc.domain.models.HisMedCardConfig;
-import com.ywt.outpatient.taihe.rpc.handler.RestTemplateHandler;
+import com.ywt.outpatient.taihe.rpc.handler.RestCallerApiProvider;
 import com.ywt.outpatient.taihe.rpc.service.Constants;
 import com.ywt.outpatient.taihe.rpc.service.IdGenerator;
 import com.ywt.outpatient.taihe.rpc.service.VerifyCreateRegisteredResult;
@@ -135,7 +136,7 @@ public class TaiheRegisterProvider extends DubboTaiheRegisterServiceTriple.Taihe
     RefundLogRepository refundLogRepository;
 
     @Autowired
-    private RestTemplateHandler restTemplateHandler ;
+    private RestCallerApiProvider restCallerApiProvider ;
 
     @Autowired
     private WeChatMsgProvider weChatMsgProvider;
@@ -917,7 +918,6 @@ public class TaiheRegisterProvider extends DubboTaiheRegisterServiceTriple.Taihe
                                        String tradeNo, String hospitalName, String deptName, String doctorName,
                                        String doctorId, String patientName, String regDate, String deptLoc,
                                        String alipayUid, int hospitalId, String merchantOrderStatus) {
-        String serverName = "com.ywt.AlipayMpRestService";
         String path = "/msg/sendRegMsg";
         Map<String, Object> params = new HashMap<>();
         params.put("orderId", orderId);
@@ -935,11 +935,8 @@ public class TaiheRegisterProvider extends DubboTaiheRegisterServiceTriple.Taihe
         params.put("alipayUid", alipayUid);
         params.put("hospitalId", hospitalId);
         params.put("merchantOrderStatus", merchantOrderStatus);
-        Map<String, String> headers = new HashMap<>();
-        headers.put("Content-Type", "application/json;charset=UTF-8");
         try {
-            // restCaller.post(serverName, path, JsonSerializer.toJson(params), headers);
-            RestTemplateHandler.Resp  resp = restTemplateHandler.httpPost(serverName, path, params,headers);
+            HttpUtil.Resp resp = restCallerApiProvider.postAlipayMpRestApi(path, params);
         } catch (Exception e) {
             logger.error("TaiheRegisterService#sendAlipayMpMsgForReg(orderId={} , orderNo={} , orderCreateTime={} , " +
                             "orderAmountStr={} , tradeNo={} , hospitalName={} , deptName={} , doctorName={} , doctorId={} , " +

+ 0 - 7
ywt-platform-outpatient-web/src/main/java/com/ywt/OutpatientFrontServiceApplication.java

@@ -1,15 +1,8 @@
 package com.ywt;
 
-import com.ywt.outpatient.configs.RibbonConfiguration;
 import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.boot.web.client.RestTemplateBuilder;
-import org.springframework.cloud.client.loadbalancer.LoadBalanced;
-import org.springframework.cloud.netflix.ribbon.RibbonClient;
-import org.springframework.cloud.netflix.ribbon.RibbonClients;
-import org.springframework.context.annotation.Bean;
-import org.springframework.web.client.RestTemplate;
 
 @SpringBootApplication
 @EnableDubbo

+ 4 - 2
ywt-platform-outpatient-web/src/main/resources/application.yml

@@ -67,5 +67,7 @@ biz-param:
     wxNfywtGenqrCodeFlag: @wx.nfywt.genqrcode.flag@
     ywtMsiteDomain: @ywt.msite.domain@
 
-
-
+# 自定义参数:rest地址配置
+com:
+  ywt:
+    restServiceMap: '[{"restName":"com.ywt.AlipayMpRestService","restIpPort":"120.77.174.122:8097"}]'