21
21
import com .java110 .service .smo .IQueryServiceSMO ;
22
22
import org .apache .commons .lang3 .math .NumberUtils ;
23
23
import org .springframework .beans .factory .annotation .Autowired ;
24
+ import org .springframework .http .HttpEntity ;
25
+ import org .springframework .http .HttpHeaders ;
24
26
import org .springframework .stereotype .Service ;
25
27
import org .springframework .transaction .annotation .Transactional ;
26
28
import org .springframework .web .client .RestTemplate ;
@@ -42,6 +44,9 @@ public class CenterServiceSMOImpl extends LoggerEngine implements ICenterService
42
44
@ Autowired
43
45
private RestTemplate restTemplate ;
44
46
47
+ @ Autowired
48
+ private RestTemplate restTemplateNoLoadBalanced ;
49
+
45
50
@ Autowired
46
51
private IQueryServiceSMO queryServiceSMOImpl ;
47
52
@@ -135,6 +140,93 @@ public String service(String reqJson, Map<String, String> headers) throws SMOExc
135
140
136
141
}
137
142
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
+
138
230
/**
139
231
* 解密
140
232
* @param reqJson
@@ -377,6 +469,40 @@ private void invokeBusinessSystem(DataFlow dataFlow) throws BusinessException {
377
469
}
378
470
379
471
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
+
380
506
381
507
/**
382
508
* 7.0 作废订单和业务项 插入撤单记录 等待撤单
@@ -925,6 +1051,24 @@ private JSONObject doRequestBusinessSystem(DataFlow dataFlow, AppService service
925
1051
return responseJson ;
926
1052
}
927
1053
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
+
928
1072
/**
929
1073
* 数据保存到BusinessTable 中
930
1074
* @param dataFlow
@@ -1009,6 +1153,24 @@ private void saveLogMessage(JSONObject requestJson,JSONObject responseJson){
1009
1153
}
1010
1154
}
1011
1155
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
+
1012
1174
/**
1013
1175
* 保存耗时信息
1014
1176
* @param dataFlow
@@ -1071,4 +1233,12 @@ public IQueryServiceSMO getQueryServiceSMOImpl() {
1071
1233
public void setQueryServiceSMOImpl (IQueryServiceSMO queryServiceSMOImpl ) {
1072
1234
this .queryServiceSMOImpl = queryServiceSMOImpl ;
1073
1235
}
1236
+
1237
+ public RestTemplate getRestTemplateNoLoadBalanced () {
1238
+ return restTemplateNoLoadBalanced ;
1239
+ }
1240
+
1241
+ public void setRestTemplateNoLoadBalanced (RestTemplate restTemplateNoLoadBalanced ) {
1242
+ this .restTemplateNoLoadBalanced = restTemplateNoLoadBalanced ;
1243
+ }
1074
1244
}
0 commit comments