Skip to content

Commit 6bfbbc6

Browse files
author
wuxw7
committed
加入 接口透传功能
1 parent ee9aff6 commit 6bfbbc6

File tree

12 files changed

+377
-19
lines changed

12 files changed

+377
-19
lines changed

CenterService/src/main/java/com/java110/center/CenterServiceApplicationStart.java

+11
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ public RestTemplate restTemplate() {
4343
return restTemplate;
4444
}
4545

46+
/**
47+
* 实例化RestTemplate
48+
* @return restTemplate
49+
*/
50+
@Bean
51+
public RestTemplate restTemplateNoLoadBalanced() {
52+
StringHttpMessageConverter m = new StringHttpMessageConverter(Charset.forName("UTF-8"));
53+
RestTemplate restTemplate = new RestTemplateBuilder().additionalMessageConverters(m).build();
54+
return restTemplate;
55+
}
56+
4657
public static void main(String[] args) throws Exception{
4758
ApplicationContext context = SpringApplication.run(CenterServiceApplicationStart.class, args);
4859

CenterService/src/main/java/com/java110/center/api/HttpApi.java

+48-4
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44
import com.java110.center.smo.ICenterServiceSMO;
55
import com.java110.common.constant.ResponseConstant;
66
import com.java110.common.exception.BusinessException;
7+
import com.java110.common.util.Assert;
78
import com.java110.core.factory.DataTransactionFactory;
89
import com.java110.core.base.controller.BaseController;
910
import org.springframework.beans.factory.annotation.Autowired;
10-
import org.springframework.web.bind.annotation.RequestBody;
11-
import org.springframework.web.bind.annotation.RequestMapping;
12-
import org.springframework.web.bind.annotation.RequestMethod;
13-
import org.springframework.web.bind.annotation.RestController;
11+
import org.springframework.web.bind.annotation.*;
1412

1513
import javax.servlet.http.HttpServletRequest;
14+
import javax.servlet.http.HttpServletResponse;
1615
import java.util.HashMap;
1716
import java.util.Map;
1817

1918
/**
2019
* 中心http服务 统一服务类
2120
* 1、只提供service方法
21+
* 2、提供 透传机制
2222
* Created by wuxw on 2018/4/13.
2323
*/
2424
@RestController
@@ -48,15 +48,59 @@ public String servicePost(@RequestBody String orderInfo, HttpServletRequest requ
4848
}
4949
}
5050

51+
/**
52+
* 对协议不遵循的 接口进行透传
53+
* @param orderInfo
54+
* @param request
55+
* @return
56+
*/
57+
@RequestMapping(path = "/httpApi/service/{serviceCode}",method= RequestMethod.POST)
58+
public String servicePostTransfer(@PathVariable String serviceCode, @RequestBody String orderInfo, HttpServletRequest request,
59+
HttpServletResponse response) {
60+
String resData = "";
61+
Map<String, String> headers = new HashMap<String, String>();
62+
try {
63+
headers.put("serviceCode",serviceCode);
64+
getRequestInfo(request, headers);
65+
//预校验
66+
preValiateOrderInfo(orderInfo,headers);
67+
resData = centerServiceSMOImpl.serviceTransfer(orderInfo, headers);
68+
}catch (Exception e){
69+
logger.error("请求订单异常",e);
70+
resData = DataTransactionFactory.createOrderResponseJson(ResponseConstant.NO_TRANSACTION_ID,
71+
ResponseConstant.RESULT_CODE_ERROR,e.getMessage()+e).toJSONString();
72+
}finally {
73+
for(String key : headers.keySet()) {
74+
response.addHeader(key,headers.get(key));
75+
}
76+
return resData;
77+
}
78+
}
79+
5180
/**
5281
* 这里预校验,请求报文中不能有 dataFlowId
5382
* @param orderInfo
5483
*/
5584
private void preValiateOrderInfo(String orderInfo) {
85+
5686
if(JSONObject.parseObject(orderInfo).getJSONObject("orders").containsKey("dataFlowId")){
5787
throw new BusinessException(ResponseConstant.RESULT_CODE_ERROR,"报文中不能存在dataFlowId节点");
5888
}
5989
}
90+
/**
91+
* 这里预校验,请求报文中不能有 dataFlowId
92+
* @param orderInfo
93+
*/
94+
private void preValiateOrderInfo(String orderInfo,Map<String, String> headers) {
95+
96+
Assert.hasKey(headers,"serviceCode","没有包含serviceCode");
97+
98+
Assert.hasLength(headers.get("serviceCode"),"serviceCode 不能为空");
99+
100+
Assert.hasKey(headers,"appId","没有包含appId");
101+
102+
Assert.hasLength(headers.get("appId"),"appId 不能为空");
103+
}
60104

61105
/**
62106
* 获取请求信息

CenterService/src/main/java/com/java110/center/smo/ICenterServiceSMO.java

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ public interface ICenterServiceSMO {
1818
*/
1919
public String service(String reqJson, Map<String,String> headers) throws SMOException;
2020

21+
/**
22+
* 透传业务
23+
* @param reqJson
24+
* @param headers
25+
* @return
26+
* @throws SMOException
27+
*/
28+
public String serviceTransfer(String reqJson, Map<String,String> headers) throws SMOException;
29+
2130
/**
2231
* 接受业务系统通知消息
2332
* @param receiveJson 接受报文

CenterService/src/main/java/com/java110/center/smo/impl/CenterServiceSMOImpl.java

+170
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import com.java110.service.smo.IQueryServiceSMO;
2222
import org.apache.commons.lang3.math.NumberUtils;
2323
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.http.HttpEntity;
25+
import org.springframework.http.HttpHeaders;
2426
import org.springframework.stereotype.Service;
2527
import org.springframework.transaction.annotation.Transactional;
2628
import org.springframework.web.client.RestTemplate;
@@ -42,6 +44,9 @@ public class CenterServiceSMOImpl extends LoggerEngine implements ICenterService
4244
@Autowired
4345
private RestTemplate restTemplate;
4446

47+
@Autowired
48+
private RestTemplate restTemplateNoLoadBalanced;
49+
4550
@Autowired
4651
private IQueryServiceSMO queryServiceSMOImpl;
4752

@@ -135,6 +140,93 @@ public String service(String reqJson, Map<String, String> headers) throws SMOExc
135140

136141
}
137142

143+
/**
144+
* 透传处理
145+
* @param reqJson
146+
* @param headers
147+
* @return
148+
* @throws SMOException
149+
*/
150+
@Override
151+
public String serviceTransfer(String reqJson, Map<String, String> headers) throws SMOException {
152+
DataFlow dataFlow = null;
153+
154+
String responseData = null;
155+
156+
String resJson = "";
157+
158+
try {
159+
reqJson = decrypt(reqJson,headers);
160+
//1.0 创建数据流
161+
dataFlow = DataFlowFactory.newInstance(DataFlow.class).builderTransfer(reqJson, headers);
162+
//2.0 加载配置信息
163+
initConfigData(dataFlow);
164+
//3.0 校验 APPID是否有权限操作serviceCode
165+
judgeAuthority(dataFlow);
166+
//4.0 调用规则校验
167+
ruleValidate(dataFlow);
168+
//5.0 保存订单和业务项 c_orders c_order_attrs c_business c_business_attrs
169+
//saveOrdersAndBusiness(dataFlow);
170+
//6.0 调用下游系统
171+
transferInvokeBusinessSystem(dataFlow);
172+
173+
responseData = DataTransactionFactory.createCommonResData(dataFlow);
174+
175+
} catch (DecryptException e){ //解密异常
176+
responseData = DataTransactionFactory.createOrderResponseJson(ResponseConstant.NO_TRANSACTION_ID,
177+
e.getResult().getCode(), e.getMessage()).toJSONString();
178+
} catch (RuleException e) {
179+
responseData = DataTransactionFactory.createOrderResponseJson(dataFlow.getTransactionId(),
180+
e.getResult().getCode(), e.getMessage()).toJSONString();
181+
} catch (NoAuthorityException e) {
182+
responseData = DataTransactionFactory.createOrderResponseJson(dataFlow.getTransactionId(),
183+
e.getResult().getCode(), e.getMessage()).toJSONString();
184+
} catch (InitConfigDataException e){
185+
responseData = DataTransactionFactory.createOrderResponseJson(dataFlow.getTransactionId(),
186+
e.getResult().getCode(), e.getMessage()).toJSONString();
187+
}catch (Exception e) {
188+
logger.error("内部异常了:",e);
189+
responseData = DataTransactionFactory.createOrderResponseJson(dataFlow == null
190+
? ResponseConstant.NO_TRANSACTION_ID
191+
: dataFlow.getTransactionId(),
192+
ResponseConstant.RESULT_CODE_INNER_ERROR, "内部异常了:" + e.getMessage() + e.getLocalizedMessage()).toJSONString();
193+
} finally {
194+
if(dataFlow != null) {
195+
//这里记录日志
196+
Date endDate = DateUtil.getCurrentDate();
197+
198+
dataFlow.setEndDate(endDate);
199+
dataFlow.setResData(responseData);
200+
//添加耗时
201+
DataFlowFactory.addCostTime(dataFlow, "service", "业务处理总耗时", dataFlow.getStartDate(), dataFlow.getEndDate());
202+
203+
//这里保存耗时,以及日志
204+
saveLogMessage(dataFlow.getReqJson(), dataFlow.getResJson());
205+
206+
//保存耗时
207+
saveCostTimeLogMessage(dataFlow);
208+
209+
//组装返回头信息
210+
putResponseHeader(dataFlow,headers);
211+
212+
//处理返回报文鉴权
213+
AuthenticationFactory.putSign(dataFlow, headers);
214+
}
215+
resJson = encrypt(responseData,headers);
216+
return resJson;
217+
218+
}
219+
}
220+
221+
/**
222+
* 抒写返回头信息
223+
* @param dataFlow
224+
*/
225+
private void putResponseHeader(DataFlow dataFlow,Map<String,String> headers) {
226+
headers.put("responseTime", DateUtil.getDefaultFormateTimeString(new Date()));
227+
headers.put("transactionId",dataFlow.getTransactionId());
228+
}
229+
138230
/**
139231
* 解密
140232
* @param reqJson
@@ -377,6 +469,40 @@ private void invokeBusinessSystem(DataFlow dataFlow) throws BusinessException {
377469
}
378470

379471

472+
/**
473+
* 6.0 调用下游系统
474+
*
475+
* @param dataFlow
476+
* @throws BusinessException
477+
*/
478+
private void transferInvokeBusinessSystem(DataFlow dataFlow) throws BusinessException {
479+
Date startDate = DateUtil.getCurrentDate();
480+
/* if(MappingCache.getValue(MappingConstant.KEY_NO_INVOKE_BUSINESS_SYSTEM) != null
481+
&&MappingCache.getValue(MappingConstant.KEY_NO_INVOKE_BUSINESS_SYSTEM).contains(dataFlow.getOrderTypeCd())){
482+
//不用调用 下游系统的配置(一般不存在这种情况,这里主要是在没有下游系统的情况下测试中心服务用)
483+
DataFlowFactory.addCostTime(dataFlow, "invokeBusinessSystem", "调用下游系统耗时", startDate);
484+
dataFlow.setResponseBusinessJson(DataTransactionFactory.createCommonResponseJson(dataFlow.getTransactionId(),
485+
ResponseConstant.RESULT_CODE_SUCCESS, "成功",null));
486+
return ;
487+
}*/
488+
489+
//6.1 先处理同步方式的服务,每一同步后发布事件广播
490+
AppService service = DataFlowFactory.getService(dataFlow,dataFlow.getCurrentBusiness().getServiceCode());
491+
492+
493+
String responseJson = doTransferRequestBusinessSystem(dataFlow, service, dataFlow.getCurrentBusiness().getTransferData());
494+
495+
dataFlow.setResData(responseJson);
496+
497+
DataFlowFactory.addCostTime(dataFlow,dataFlow.getCurrentBusiness().getServiceCode(), "调用"+dataFlow.getCurrentBusiness().getServiceCode()+"耗时", startDate);
498+
saveLogMessage(dataFlow.getCurrentBusiness().getTransferData(),dataFlow.getResData());
499+
500+
501+
DataFlowFactory.addCostTime(dataFlow, "invokeBusinessSystem", "调用下游系统耗时", startDate);
502+
}
503+
504+
505+
380506

381507
/**
382508
* 7.0 作废订单和业务项 插入撤单记录 等待撤单
@@ -925,6 +1051,24 @@ private JSONObject doRequestBusinessSystem(DataFlow dataFlow, AppService service
9251051
return responseJson;
9261052
}
9271053

1054+
private String doTransferRequestBusinessSystem(DataFlow dataFlow, AppService service, String reqData) {
1055+
String responseMessage;
1056+
if(service.getMethod() == null || "".equals(service.getMethod())) {//post方式
1057+
//http://user-service/test/sayHello
1058+
HttpHeaders header = new HttpHeaders();
1059+
for(String key : dataFlow.getHeaders().keySet()){
1060+
header.add(key,dataFlow.getHeaders().get(key));
1061+
}
1062+
HttpEntity<String> httpEntity = new HttpEntity<String>(reqData, header);
1063+
responseMessage = restTemplateNoLoadBalanced.postForObject(service.getUrl(),httpEntity,String.class);
1064+
}else{//webservice方式
1065+
responseMessage = (String) WebServiceAxisClient.callWebService(service.getUrl(),service.getMethod(),
1066+
new Object[]{dataFlow.getRequestBusinessJson().toJSONString()},
1067+
service.getTimeOut());
1068+
}
1069+
return responseMessage;
1070+
}
1071+
9281072
/**
9291073
* 数据保存到BusinessTable 中
9301074
* @param dataFlow
@@ -1009,6 +1153,24 @@ private void saveLogMessage(JSONObject requestJson,JSONObject responseJson){
10091153
}
10101154
}
10111155

1156+
/**
1157+
* 保存日志信息
1158+
* @param requestJson
1159+
*/
1160+
private void saveLogMessage(String requestJson,String responseJson){
1161+
1162+
try{
1163+
if(MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.KEY_LOG_ON_OFF))){
1164+
JSONObject log = new JSONObject();
1165+
log.put("request",requestJson);
1166+
log.put("response",responseJson);
1167+
KafkaFactory.sendKafkaMessage(KafkaConstant.TOPIC_LOG_NAME,"",log.toJSONString());
1168+
}
1169+
}catch (Exception e){
1170+
logger.error("报错日志出错了,",e);
1171+
}
1172+
}
1173+
10121174
/**
10131175
* 保存耗时信息
10141176
* @param dataFlow
@@ -1071,4 +1233,12 @@ public IQueryServiceSMO getQueryServiceSMOImpl() {
10711233
public void setQueryServiceSMOImpl(IQueryServiceSMO queryServiceSMOImpl) {
10721234
this.queryServiceSMOImpl = queryServiceSMOImpl;
10731235
}
1236+
1237+
public RestTemplate getRestTemplateNoLoadBalanced() {
1238+
return restTemplateNoLoadBalanced;
1239+
}
1240+
1241+
public void setRestTemplateNoLoadBalanced(RestTemplate restTemplateNoLoadBalanced) {
1242+
this.restTemplateNoLoadBalanced = restTemplateNoLoadBalanced;
1243+
}
10741244
}

java110-bean/src/main/java/com/java110/entity/center/Business.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public class Business implements Comparable{
2222

2323
private JSONObject datas;
2424

25+
//透传
26+
private String transferData;
27+
2528
private JSONArray attrs;
2629
//返回 编码
2730
private String code;
@@ -111,6 +114,14 @@ public void setIsInstance(String isInstance) {
111114
this.isInstance = isInstance;
112115
}
113116

117+
public String getTransferData() {
118+
return transferData;
119+
}
120+
121+
public void setTransferData(String transferData) {
122+
this.transferData = transferData;
123+
}
124+
114125
/**
115126
* 构建成对象
116127
* @return
@@ -121,7 +132,7 @@ public Business builder(JSONObject businessObj) throws Exception{
121132
try{
122133
this.setbId(businessObj.getString("bId"));
123134
this.setServiceCode(businessObj.getString("serviceCode"));
124-
if(businessObj.containsKey("serviceCode")) {
135+
if(businessObj.containsKey("serviceName")) {
125136
this.setServiceName(businessObj.getString("serviceName"));
126137
}
127138
if(businessObj.containsKey("remark")) {
@@ -138,6 +149,11 @@ public Business builder(JSONObject businessObj) throws Exception{
138149
if(businessObj.containsKey("attrs")){
139150
this.setAttrs(businessObj.getJSONArray("attrs"));
140151
}
152+
153+
if(businessObj.containsKey("transferData")){
154+
this.setTransferData(businessObj.getString("transferData"));
155+
}
156+
141157
if(businessObj.containsKey("response")){
142158
this.setCode(businessObj.getJSONObject("response").getString("code"));
143159
this.setMessage(businessObj.getJSONObject("response").getString("message"));

0 commit comments

Comments
 (0)