Skip to content

Commit 7afa326

Browse files
author
wuxw7
committed
日志服务实现
1 parent 37c6fc4 commit 7afa326

26 files changed

Lines changed: 598 additions & 138 deletions

File tree

CenterService/doc/request_protocol.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"value": "具体值"
1313
}]
1414
},
15-
"business": [{
15+
// 如果是批量方式 写成 "business":[{....}]
16+
"business": {
1617
"serviceCode": "queryCustInfo",
1718
"serviceName": "查询客户",
1819
"remark": "备注",
@@ -26,5 +27,5 @@
2627
"specCd": "配置的字段ID",
2728
"value": "具体值"
2829
}]
29-
}]
30+
}
3031
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.java110.core.factory.AuthenticationFactory;
1616
import com.java110.core.factory.DataFlowFactory;
1717
import com.java110.core.factory.DataTransactionFactory;
18+
import com.java110.core.factory.GenerateCodeFactory;
1819
import com.java110.entity.center.AppRoute;
1920
import com.java110.entity.center.AppService;
2021
import com.java110.entity.center.Business;
@@ -305,7 +306,7 @@ private void initConfigData(DataFlow dataFlow) {
305306
}
306307
//
307308
if("-1".equals(dataFlow.getDataFlowId()) || StringUtil.isNullOrNone(dataFlow.getDataFlowId())){
308-
dataFlow.setDataFlowId(SequenceUtil.getDataFlowId());
309+
dataFlow.setDataFlowId(GenerateCodeFactory.getDataFlowId());
309310
}
310311

311312
//添加耗时
Lines changed: 71 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package com.java110.code.api;
22

3+
import com.alibaba.fastjson.JSONObject;
34
import com.java110.code.smo.IPrimaryKeyServiceSMO;
45
import com.java110.common.constant.ResponseConstant;
6+
import com.java110.common.exception.ResponseErrorException;
57
import com.java110.common.util.Assert;
8+
import com.java110.common.util.DateUtil;
69
import com.java110.core.base.controller.BaseController;
710
import com.java110.core.context.CodeDataFlow;
811
import com.java110.core.factory.DataFlowFactory;
912
import com.java110.core.factory.DataTransactionFactory;
13+
import com.java110.feign.code.ICodeApi;
1014
import org.springframework.beans.factory.annotation.Autowired;
1115
import org.springframework.web.bind.annotation.RequestBody;
1216
import org.springframework.web.bind.annotation.RequestMapping;
@@ -16,64 +20,67 @@
1620
import javax.servlet.http.HttpServletRequest;
1721
import java.util.HashMap;
1822
import java.util.Map;
23+
import java.util.UUID;
1924

2025
/**
2126
* ID 生成
2227
* Created by wuxw on 2018/6/3.
2328
*/
2429
@RestController
25-
public class CodeApi extends BaseController {
30+
public class CodeApi extends BaseController implements ICodeApi {
2631

2732
@Autowired
2833
IPrimaryKeyServiceSMO primaryKeyServiceSMOImpl;
2934

30-
@RequestMapping(path = "/codeApi/generate",method= RequestMethod.GET)
35+
@RequestMapping(path = "/codeApi/generate", method = RequestMethod.GET)
3136
public String generateGet(HttpServletRequest request) {
32-
return DataTransactionFactory.createCodeResponseJson(ResponseConstant.NO_TRANSACTION_ID,"-1",
33-
ResponseConstant.RESULT_CODE_ERROR,"不支持Get方法请求").toJSONString();
37+
return DataTransactionFactory.createCodeResponseJson(ResponseConstant.NO_TRANSACTION_ID, "-1",
38+
ResponseConstant.RESULT_CODE_ERROR, "不支持Get方法请求").toJSONString();
3439
}
3540

36-
@RequestMapping(path = "/codeApi/generate",method= RequestMethod.POST)
37-
public String generatePost(@RequestBody String orderInfo,HttpServletRequest request) {
41+
@RequestMapping(path = "/codeApi/generate", method = RequestMethod.POST)
42+
public String generatePost(@RequestBody String orderInfo, HttpServletRequest request) {
3843
Map<String, String> headers = new HashMap<String, String>();
3944
try {
4045
getRequestInfo(request, headers);
4146
//预校验
42-
preValiateOrderInfo(orderInfo,headers);
43-
CodeDataFlow dataFlow = DataFlowFactory.newInstance(CodeDataFlow.class).builder(orderInfo,null);
47+
preValiateOrderInfo(orderInfo, headers);
48+
CodeDataFlow dataFlow = DataFlowFactory.newInstance(CodeDataFlow.class).builder(orderInfo, null);
4449
primaryKeyServiceSMOImpl.generateCode(dataFlow);
4550
return dataFlow.getResJson().toJSONString();
46-
}catch (Exception e){
47-
logger.error("请求订单异常",e);
48-
return DataTransactionFactory.createBusinessResponseJson(ResponseConstant.RESULT_CODE_ERROR,e.getMessage()+e).toJSONString();
51+
} catch (Exception e) {
52+
logger.error("请求订单异常", e);
53+
return DataTransactionFactory.createBusinessResponseJson(ResponseConstant.RESULT_CODE_ERROR, e.getMessage() + e).toJSONString();
4954
}
5055
}
5156

5257
/**
5358
* 获取请求信息
59+
*
5460
* @param request
5561
* @param headers
5662
* @throws RuntimeException
5763
*/
58-
private void getRequestInfo(HttpServletRequest request,Map headers) throws Exception{
59-
try{
60-
super.initHeadParam(request,headers);
61-
super.initUrlParam(request,headers);
62-
}catch (Exception e){
63-
logger.error("加载头信息失败",e);
64+
private void getRequestInfo(HttpServletRequest request, Map headers) throws Exception {
65+
try {
66+
super.initHeadParam(request, headers);
67+
super.initUrlParam(request, headers);
68+
} catch (Exception e) {
69+
logger.error("加载头信息失败", e);
6470
throw e;
6571
}
6672
}
6773

6874
/**
6975
* 这里预校验,请求报文中不能有 dataFlowId
76+
*
7077
* @param orderInfo
7178
*/
72-
private void preValiateOrderInfo(String orderInfo,Map<String, String> headers) {
79+
private void preValiateOrderInfo(String orderInfo, Map<String, String> headers) {
7380

74-
Assert.jsonObjectHaveKey(orderInfo,"prefix","没有包含prefix");
81+
Assert.jsonObjectHaveKey(orderInfo, "prefix", "没有包含prefix");
7582

76-
Assert.jsonObjectHaveKey(orderInfo,"transactionId","没有包含transactionId");
83+
Assert.jsonObjectHaveKey(orderInfo, "transactionId", "没有包含transactionId");
7784

7885
}
7986

@@ -85,4 +92,48 @@ public IPrimaryKeyServiceSMO getPrimaryKeyServiceSMOImpl() {
8592
public void setPrimaryKeyServiceSMOImpl(IPrimaryKeyServiceSMO primaryKeyServiceSMOImpl) {
8693
this.primaryKeyServiceSMOImpl = primaryKeyServiceSMOImpl;
8794
}
95+
96+
/**
97+
* 生成 编码
98+
*
99+
* @param prefix 前缀
100+
* @return
101+
*/
102+
@Override
103+
public String generateCode(String prefix) {
104+
105+
try {
106+
JSONObject requestInfo = new JSONObject();
107+
108+
//封装符合构建CodeDataFlow对象的JSON对象参数
109+
builderRequestInfo(prefix, requestInfo);
110+
111+
CodeDataFlow dataFlow = DataFlowFactory.newInstance(CodeDataFlow.class).builder(requestInfo.toJSONString(), null);
112+
113+
//生成编码
114+
primaryKeyServiceSMOImpl.generateCode(dataFlow);
115+
116+
if (!ResponseConstant.RESULT_CODE_SUCCESS.equals(dataFlow.getResJson().getString("code"))) {
117+
throw new ResponseErrorException(ResponseConstant.RESULT_CODE_ERROR, "生成oId编码失败 "
118+
+ dataFlow.getResJson().getString("message"));
119+
}
120+
121+
return dataFlow.getResJson().getString("id");
122+
} catch (Exception e) {
123+
logger.error("请求订单异常", e);
124+
return ResponseConstant.RESULT_CODE_ERROR;
125+
}
126+
}
127+
128+
/**
129+
* 封装符合构建CodeDataFlow对象的JSON对象参数
130+
*
131+
* @param prefix 前缀
132+
* @param requestInfo 构建的请求JSON对象
133+
*/
134+
private void builderRequestInfo(String prefix, JSONObject requestInfo) {
135+
requestInfo.put("transactionId", UUID.randomUUID().toString().replace("-", ""));
136+
requestInfo.put("prefix", prefix);
137+
requestInfo.put("requestTime", DateUtil.getNowDefault());
138+
}
88139
}

LogService/doc/create_table.db

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
-- 日志表
2+
3+
create table l_transaction_log(
4+
log_id varchar(30) not null keY unique COMMENT 'id',
5+
transaction_id VARCHAR(30) NOT NULL COMMENT '外部交易流水',
6+
contract_id varchar(64) not null comment '上下文ID',
7+
ip varchar(20) not null comment '日志产生主机IP',
8+
port varchar(10) not null comment '日志产生端口',
9+
src_ip varchar(20) comment '调用方IP',
10+
src_port varchar(10) comment '调用方端口',
11+
app_id varchar(30) not null comment '调用方应用ID',
12+
user_id varchar(30) comment '用户ID',
13+
service_code varchar(50) comment '服务编码',
14+
service_name varchar(50) comment '服务名称',
15+
timestamp TIMESTAMP NOT NULL comment '日志交互时间,时间戳',
16+
status_cd varchar(2) not null comment '交互状态 S 成功 F 失败',
17+
month INT NOT NULL default month(CURRENT_DATE) comment '月份',
18+
create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
19+
PRIMARY KEY (log_id)
20+
)
21+
partition BY RANGE (month) (
22+
partition transaction_log_1 VALUES LESS THAN (2),
23+
partition transaction_log_2 VALUES LESS THAN (3),
24+
partition transaction_log_3 VALUES LESS THAN (4),
25+
partition transaction_log_4 VALUES LESS THAN (5),
26+
partition transaction_log_5 VALUES LESS THAN (6),
27+
partition transaction_log_6 VALUES LESS THAN (7),
28+
partition transaction_log_7 VALUES LESS THAN (8),
29+
partition transaction_log_8 VALUES LESS THAN (9),
30+
partition transaction_log_9 VALUES LESS THAN (10),
31+
partition transaction_log_10 VALUES LESS THAN (11),
32+
partition transaction_log_11 VALUES LESS THAN (12),
33+
partition transaction_log_12 VALUES LESS THAN (13)
34+
);
35+
36+
37+
38+
-- 日志原始内容表
39+
40+
create table l_transaction_log_message(
41+
log_id varchar(30) not null unique FOREIGN KEY REFERENCES l_transaction_log(log_id) COMMENT 'id',
42+
request_header LONGTEXT COMMENT '请求头信息',
43+
response_header LONGTEXT COMMENT '返回头信息',
44+
request_message LONGTEXT comment '请求报文',
45+
response_message LONGTEXT comment '返回报文',
46+
remark varchar(200) comment '备注',
47+
month INT NOT NULL default month(CURRENT_DATE) comment '月份',
48+
create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间'
49+
)
50+
partition BY RANGE (month) (
51+
partition transaction_log_message_1 VALUES LESS THAN (2),
52+
partition transaction_log_message_2 VALUES LESS THAN (3),
53+
partition transaction_log_message_3 VALUES LESS THAN (4),
54+
partition transaction_log_message_4 VALUES LESS THAN (5),
55+
partition transaction_log_message_5 VALUES LESS THAN (6),
56+
partition transaction_log_message_6 VALUES LESS THAN (7),
57+
partition transaction_log_message_7 VALUES LESS THAN (8),
58+
partition transaction_log_message_8 VALUES LESS THAN (9),
59+
partition transaction_log_message_9 VALUES LESS THAN (10),
60+
partition transaction_log_message_10 VALUES LESS THAN (11),
61+
partition transaction_log_message_11 VALUES LESS THAN (12),
62+
partition transaction_log_message_12 VALUES LESS THAN (13)
63+
);
64+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.java110.log.dao;
2+
3+
import com.java110.common.exception.DAOException;
4+
import com.java110.entity.order.BusiOrder;
5+
import com.java110.entity.order.BusiOrderAttr;
6+
import com.java110.entity.order.OrderList;
7+
import com.java110.entity.order.OrderListAttr;
8+
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
/**
13+
* 购物车,订单项信息管理
14+
*
15+
* 增加,查询
16+
* 没有查询,理论上购物车信息,不允许删除和修改
17+
*
18+
* Created by wuxw on 2016/12/27.
19+
*/
20+
public interface LogServiceDao {
21+
22+
/**
23+
* 保存日志
24+
* 相关表 l_transaction_log
25+
* @param logMessageParams 日志参数信息
26+
*/
27+
public void saveTransactionLog(Map logMessageParams) throws DAOException;
28+
29+
/**
30+
* 保存日志(交互报文)
31+
* 相关表 l_transaction_log_message
32+
* @param logMessageParams 日志参数信息
33+
*/
34+
public void saveTransactionLogMessage(Map logMessageParams) throws DAOException;
35+
}
Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,62 @@
11
package com.java110.log.dao.impl;
22

3-
import com.java110.common.log.LoggerEngine;
3+
import com.java110.common.constant.ResponseConstant;
4+
import com.java110.common.exception.DAOException;
45
import com.java110.core.base.dao.BaseServiceDao;
5-
import com.java110.entity.order.BusiOrder;
6-
import com.java110.entity.order.BusiOrderAttr;
7-
import com.java110.entity.order.OrderList;
8-
import com.java110.entity.order.OrderListAttr;
9-
import com.java110.log.dao.logServiceDao;
6+
import com.java110.log.dao.LogServiceDao;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
109
import org.springframework.stereotype.Service;
1110
import org.springframework.transaction.annotation.Transactional;
1211

13-
import java.util.List;
12+
import java.util.Map;
1413

1514
/**
1615
* 日志服务 与数据库交互
1716
* Created by wuxw on 2017/4/5.
1817
*/
1918

2019
@Service("logServiceDaoImpl")
21-
@Transactional
22-
public class LogServiceDaoImpl extends BaseServiceDao implements logServiceDao {
20+
//@Transactional
21+
public class LogServiceDaoImpl extends BaseServiceDao implements LogServiceDao {
22+
23+
private final static Logger logger = LoggerFactory.getLogger(LogServiceDaoImpl.class);
24+
25+
/**
26+
* 保存日志
27+
* @param logMessageParams 日志参数信息
28+
*/
29+
@Override
30+
public void saveTransactionLog(Map logMessageParams) throws DAOException {
31+
logger.debug("save log params :{}",logMessageParams);
32+
try{
33+
int row = sqlSessionTemplate.insert("logServiceDaoImpl.saveTransactionLog",logMessageParams);
34+
if(row < 1){
35+
throw new DAOException(ResponseConstant.RESULT_CODE_INNER_ERROR,"save log error, nothing to save");
36+
}
37+
}catch (Exception e){
38+
logger.error("save log error:",e);
39+
throw new DAOException(ResponseConstant.RESULT_CODE_INNER_ERROR,"save log error"+e);
40+
}
41+
42+
}
43+
44+
/**
45+
* 保存日志(交互报文)
46+
* @param logMessageParams 日志参数信息
47+
*/
48+
@Override
49+
public void saveTransactionLogMessage(Map logMessageParams) throws DAOException{
50+
logger.debug("save log message params :{}",logMessageParams);
51+
try{
52+
int row = sqlSessionTemplate.insert("logServiceDaoImpl.saveTransactionLogMessage",logMessageParams);
53+
if(row < 1){
54+
throw new DAOException(ResponseConstant.RESULT_CODE_INNER_ERROR,"save log error, nothing to save");
55+
}
56+
}catch (Exception e){
57+
logger.error("save log error:",e);
58+
throw new DAOException(ResponseConstant.RESULT_CODE_INNER_ERROR,"save log error"+e);
59+
}
60+
61+
}
2362
}

LogService/src/main/java/com/java110/log/dao/logServiceDao.java

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)