8个文件已删除
11个文件已修改
16 文件已重命名
16个文件已添加
| | |
| | | <artifactId>java110-config</artifactId> |
| | | <scope>provided</scope> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>com.java110</groupId> |
| | | <artifactId>java110-logAgent</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>junit</groupId> |
| | | <artifactId>junit</artifactId> |
| | |
| | | import com.java110.common.cache.MappingCache; |
| | | import com.java110.common.constant.*; |
| | | import com.java110.common.exception.*; |
| | | import com.java110.core.factory.AuthenticationFactory; |
| | | import com.java110.core.factory.DataFlowFactory; |
| | | import com.java110.core.factory.DataTransactionFactory; |
| | | import com.java110.common.kafka.KafkaFactory; |
| | | import com.java110.common.log.LoggerEngine; |
| | | import com.java110.common.util.*; |
| | | import com.java110.entity.center.Business; |
| | | import com.java110.core.context.DataFlow; |
| | | import com.java110.entity.center.*; |
| | | import com.java110.core.factory.AuthenticationFactory; |
| | | import com.java110.core.factory.DataFlowFactory; |
| | | import com.java110.core.factory.DataTransactionFactory; |
| | | import com.java110.entity.center.AppRoute; |
| | | import com.java110.entity.center.AppService; |
| | | import com.java110.entity.center.Business; |
| | | import com.java110.entity.center.DataFlowLinksCost; |
| | | import com.java110.event.center.DataFlowEventPublishing; |
| | | |
| | | import com.java110.service.smo.IQueryServiceSMO; |
| | | import org.apache.commons.lang3.math.NumberUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.http.HttpEntity; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | | import java.util.*; |
| | |
| | | reqJson = decrypt(reqJson,headers); |
| | | //1.0 创建数据流 |
| | | dataFlow = DataFlowFactory.newInstance(DataFlow.class).builder(reqJson, headers); |
| | | |
| | | /*LogAgent.sendLog(dataFlow.reBuilder(dataFlow.getTransactionId(),LogAgent.LOG_TYPE_S,LogAgent.LOG_STATUS_S, |
| | | dataFlow.getRequestURL(),dataFlow.getReqData(),dataFlow.getHeaders())); |
| | | */ |
| | | //2.0 加载配置信息 |
| | | initConfigData(dataFlow); |
| | | //3.0 校验 APPID是否有权限操作serviceCode |
| | |
| | | dataFlow.setEndDate(endDate); |
| | | dataFlow.setResJson(responseJson); |
| | | //添加耗时 |
| | | DataFlowFactory.addCostTime(dataFlow, "service", "业务处理总耗时", dataFlow.getStartDate(), dataFlow.getEndDate()); |
| | | //DataFlowFactory.addCostTime(dataFlow, "service", "业务处理总耗时", dataFlow.getStartDate(), dataFlow.getEndDate()); |
| | | |
| | | //这里保存耗时,以及日志 |
| | | saveLogMessage(dataFlow.getReqJson(), dataFlow.getResJson()); |
| | | //saveLogMessage(dataFlow.getReqJson(), dataFlow.getResJson()); |
| | | |
| | | //保存耗时 |
| | | saveCostTimeLogMessage(dataFlow); |
| | | //saveCostTimeLogMessage(dataFlow); |
| | | //处理返回报文鉴权 |
| | | AuthenticationFactory.putSign(dataFlow, responseJson); |
| | | } |
| | | resJson = encrypt(responseJson.toJSONString(),headers); |
| | | /*LogAgent.sendLog(dataFlow.reBuilder(dataFlow.getTransactionId(), |
| | | LogAgent.LOG_TYPE_C,DataTransactionFactory.isSuccessBusiness(responseJson)? |
| | | LogAgent.LOG_STATUS_S:LogAgent.LOG_STATUS_F, |
| | | dataFlow.getRequestURL(),responseJson.toJSONString(),headers));*/ |
| | | return resJson; |
| | | |
| | | } |
| File was renamed from OrderService/pom.xml |
| | |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>OrderService</artifactId> |
| | | <artifactId>LogService</artifactId> |
| | | <packaging>jar</packaging> |
| | | |
| | | <name>OrderService</name> |
| | | <name>LogService</name> |
| | | <url>http://maven.apache.org</url> |
| | | |
| | | <properties> |
| | |
| | | |
| | | <dependency> |
| | | <groupId>com.java110</groupId> |
| | | <artifactId>java110-event</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>com.java110</groupId> |
| | | <artifactId>java110-config</artifactId> |
| | | <scope>provided</scope> |
| | | </dependency> |
| | | </dependencies> |
| | | |
| | | <build> |
| | | <finalName>OrderService</finalName> |
| | | <finalName>LogService</finalName> |
| | | <plugins> |
| | | <plugin> |
| | | <groupId>org.apache.maven.plugins</groupId> |
| | |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-maven-plugin</artifactId> |
| | | <configuration> |
| | | <mainClass>com.java110.order.AppSpringBootApplication</mainClass> |
| | | <mainClass>com.java110.log.LogServiceApplicationStart</mainClass> |
| | | </configuration> |
| | | </plugin> |
| | | </plugins> |
| New file |
| | |
| | | package com.java110.log; |
| | | |
| | | /** |
| | | * Hello world! |
| | | */ |
| | | |
| | | import com.java110.service.init.ServiceStartInit; |
| | | import org.springframework.boot.SpringApplication; |
| | | import org.springframework.boot.autoconfigure.SpringBootApplication; |
| | | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; |
| | | import org.springframework.context.ApplicationContext; |
| | | |
| | | /** |
| | | * 日志服务初始化启动类 初始化启动类 |
| | | * |
| | | * @version v0.1 |
| | | * @auther com.java110.wuxw |
| | | * @mail 928255095@qq.com |
| | | * @date 2016年8月6日 |
| | | * @tag |
| | | */ |
| | | @SpringBootApplication(scanBasePackages = {"com.java110.service","com.java110.log","com.java110.core","com.java110.cache"}) |
| | | @EnableDiscoveryClient |
| | | public class LogServiceApplicationStart { |
| | | |
| | | |
| | | public static void main(String[] args) throws Exception { |
| | | |
| | | ApplicationContext context = SpringApplication.run(LogServiceApplicationStart.class, args); |
| | | |
| | | ServiceStartInit.initSystemConfig(context); |
| | | |
| | | } |
| | | } |
| New file |
| | |
| | | package com.java110.log.dao.impl; |
| | | |
| | | import com.java110.common.log.LoggerEngine; |
| | | import com.java110.core.base.dao.BaseServiceDao; |
| | | import com.java110.entity.order.BusiOrder; |
| | | import com.java110.entity.order.BusiOrderAttr; |
| | | import com.java110.entity.order.OrderList; |
| | | import com.java110.entity.order.OrderListAttr; |
| | | import com.java110.log.dao.logServiceDao; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 日志服务 与数据库交互 |
| | | * Created by wuxw on 2017/4/5. |
| | | */ |
| | | |
| | | @Service("logServiceDaoImpl") |
| | | @Transactional |
| | | public class LogServiceDaoImpl extends BaseServiceDao implements logServiceDao { |
| | | } |
| New file |
| | |
| | | package com.java110.log.dao; |
| | | |
| | | import com.java110.entity.order.BusiOrder; |
| | | import com.java110.entity.order.BusiOrderAttr; |
| | | import com.java110.entity.order.OrderList; |
| | | import com.java110.entity.order.OrderListAttr; |
| | | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 购物车,订单项信息管理 |
| | | * |
| | | * 增加,查询 |
| | | * 没有查询,理论上购物车信息,不允许删除和修改 |
| | | * |
| | | * Created by wuxw on 2016/12/27. |
| | | */ |
| | | public interface logServiceDao { |
| | | |
| | | } |
| New file |
| | |
| | | package com.java110.log.kafka; |
| | | |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | |
| | | /** |
| | | * Created by wuxw on 2018/4/15. |
| | | */ |
| | | @Configuration |
| | | public class LogServiceBean { |
| | | @Bean |
| | | public LogServiceKafka listener() { |
| | | return new LogServiceKafka(); |
| | | } |
| | | |
| | | } |
| New file |
| | |
| | | package com.java110.log.kafka; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.java110.common.constant.KafkaConstant; |
| | | import com.java110.common.constant.ResponseConstant; |
| | | import com.java110.common.constant.StatusConstant; |
| | | import com.java110.common.exception.InitConfigDataException; |
| | | import com.java110.common.exception.InitDataFlowContextException; |
| | | import com.java110.common.kafka.KafkaFactory; |
| | | import com.java110.core.base.controller.BaseController; |
| | | import com.java110.core.context.BusinessServiceDataFlow; |
| | | import com.java110.core.factory.DataTransactionFactory; |
| | | import com.java110.log.smo.ILogServiceSMO; |
| | | import org.apache.kafka.clients.consumer.ConsumerRecord; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.kafka.annotation.KafkaListener; |
| | | |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * kafka侦听 |
| | | * Created by wuxw on 2018/4/15. |
| | | */ |
| | | public class LogServiceKafka extends BaseController { |
| | | |
| | | @Autowired |
| | | private ILogServiceSMO logServiceSMOImpl; |
| | | |
| | | @KafkaListener(topics = {KafkaConstant.TOPIC_LOG_NAME}) |
| | | public void listen(ConsumerRecord<?, ?> record) { |
| | | logger.info("kafka的key: " + record.key()); |
| | | logger.info("kafka的value: " + record.value().toString()); |
| | | String orderInfo = record.value().toString(); |
| | | BusinessServiceDataFlow businessServiceDataFlow = null; |
| | | JSONObject responseJson = null; |
| | | try { |
| | | Map<String, String> headers = new HashMap<String, String>(); |
| | | //预校验 |
| | | preValiateOrderInfo(orderInfo); |
| | | businessServiceDataFlow = this.writeDataToDataFlowContext(orderInfo, headers); |
| | | //responseJson = logServiceSMOImpl.service(businessServiceDataFlow); |
| | | }catch (InitDataFlowContextException e){ |
| | | logger.error("请求报文错误,初始化 BusinessServiceDataFlow失败"+orderInfo,e); |
| | | responseJson = DataTransactionFactory.createNoBusinessTypeBusinessResponseJson(orderInfo,ResponseConstant.RESULT_PARAM_ERROR,e.getMessage(),null); |
| | | }catch (InitConfigDataException e){ |
| | | logger.error("请求报文错误,加载配置信息失败"+orderInfo,e); |
| | | responseJson = DataTransactionFactory.createNoBusinessTypeBusinessResponseJson(orderInfo,ResponseConstant.RESULT_PARAM_ERROR,e.getMessage(),null); |
| | | }catch (Exception e){ |
| | | logger.error("请求订单异常",e); |
| | | responseJson = DataTransactionFactory.createBusinessResponseJson(businessServiceDataFlow,ResponseConstant.RESULT_CODE_ERROR,e.getMessage()+e, |
| | | null); |
| | | }finally { |
| | | logger.debug("当前请求报文:" + orderInfo +", 当前返回报文:" +responseJson.toJSONString()); |
| | | //只有business 和 instance 过程才做通知消息 |
| | | if(!StatusConstant.REQUEST_BUSINESS_TYPE_BUSINESS.equals(responseJson.getString("businessType")) |
| | | && !StatusConstant.REQUEST_BUSINESS_TYPE_INSTANCE.equals(responseJson.getString("businessType"))){ |
| | | return ; |
| | | } |
| | | try { |
| | | KafkaFactory.sendKafkaMessage(KafkaConstant.TOPIC_NOTIFY_CENTER_SERVICE_NAME, "", responseJson.toJSONString()); |
| | | }catch (Exception e){ |
| | | logger.error("用户服务通知centerService失败"+responseJson,e); |
| | | //这里保存异常信息 |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 这里预校验,请求报文中不能有 dataFlowId |
| | | * @param orderInfo |
| | | */ |
| | | private void preValiateOrderInfo(String orderInfo) { |
| | | /* if(JSONObject.parseObject(orderInfo).getJSONObject("orders").containsKey("dataFlowId")){ |
| | | throw new BusinessException(ResponseConstant.RESULT_CODE_ERROR,"报文中不能存在dataFlowId节点"); |
| | | }*/ |
| | | } |
| | | |
| | | public ILogServiceSMO getLogServiceSMOImpl() { |
| | | return logServiceSMOImpl; |
| | | } |
| | | |
| | | public void setLogServiceSMOImpl(ILogServiceSMO logServiceSMOImpl) { |
| | | this.logServiceSMOImpl = logServiceSMOImpl; |
| | | } |
| | | } |
| New file |
| | |
| | | package com.java110.log.smo; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.java110.entity.order.OrderList; |
| | | |
| | | /** |
| | | * 订单服务业务处理接口 |
| | | * |
| | | * 订单受理 |
| | | * Created by wuxw on 2017/4/11. |
| | | */ |
| | | public interface ILogServiceSMO { |
| | | |
| | | |
| | | } |
| New file |
| | |
| | | package com.java110.log.smo.impl; |
| | | |
| | | import com.java110.core.base.smo.BaseServiceSMO; |
| | | import com.java110.log.smo.ILogServiceSMO; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | /** |
| | | * 订单服务业务逻辑处理实现类 |
| | | * Created by wuxw on 2017/4/11. |
| | | */ |
| | | @Service("orderServiceSMOImpl") |
| | | @Transactional |
| | | public class LogServiceSMOImpl extends BaseServiceSMO implements ILogServiceSMO { |
| | | } |
| File was renamed from OrderService/src/test/java/com/java110/order/AppTest.java |
| | |
| | | package com.java110.order; |
| | | package com.java110.log; |
| | | |
| | | import junit.framework.Test; |
| | | import junit.framework.TestCase; |
| File was renamed from OrderService/src/test/java/com/java110/order/common/PreBoCustAttr.java |
| | |
| | | package com.java110.order.common; |
| | | package com.java110.log.common; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.java110.entity.user.BoCustAttr; |
| | | import com.java110.order.AppTest; |
| | | import junit.framework.Test; |
| | | import junit.framework.TestCase; |
| | | import junit.framework.TestSuite; |
| File was renamed from OrderService/src/test/java/com/java110/order/executor/ExecutorTest.java |
| | |
| | | package com.java110.order.executor; |
| | | package com.java110.log.executor; |
| | | |
| | | import com.java110.order.AppTest; |
| | | import junit.framework.Test; |
| | | import junit.framework.TestCase; |
| | | import junit.framework.TestSuite; |
| File was renamed from OrderService/src/test/java/com/java110/order/executor/PrintInt.java |
| | |
| | | package com.java110.order.executor; |
| | | package com.java110.log.executor; |
| | | |
| | | import java.util.concurrent.Callable; |
| | | |
| File was renamed from OrderService/src/test/java/com/java110/order/jsonpath/CustIdTest.java |
| | |
| | | package com.java110.order.jsonpath; |
| | | package com.java110.log.jsonpath; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.alibaba.fastjson.JSONPath; |
| | | import com.java110.order.AppTest; |
| | | import junit.framework.Test; |
| | | import junit.framework.TestCase; |
| | | import junit.framework.TestSuite; |
| File was renamed from OrderService/src/test/java/com/java110/order/type/AppCustEvent.java |
| | |
| | | package com.java110.order.type; |
| | | package com.java110.log.type; |
| | | |
| | | import com.java110.core.context.AppContext; |
| | | |
| File was renamed from OrderService/src/test/java/com/java110/order/type/AppEvent.java |
| | |
| | | package com.java110.order.type; |
| | | package com.java110.log.type; |
| | | |
| | | import com.java110.core.context.AppContext; |
| | | |
| File was renamed from OrderService/src/test/java/com/java110/order/type/AppListener.java |
| | |
| | | package com.java110.order.type; |
| | | package com.java110.log.type; |
| | | |
| | | |
| | | |
| File was renamed from OrderService/src/test/java/com/java110/order/type/CommonTest.java |
| | |
| | | package com.java110.order.type; |
| | | package com.java110.log.type; |
| | | |
| | | import junit.framework.Test; |
| | | import junit.framework.TestCase; |
| File was renamed from OrderService/src/test/java/com/java110/order/type/CustDispatchListener.java |
| | |
| | | package com.java110.order.type; |
| | | package com.java110.log.type; |
| | | |
| | | |
| | | |
| File was renamed from OrderService/src/test/java/com/java110/order/type/Ordered.java |
| | |
| | | package com.java110.order.type; |
| | | package com.java110.log.type; |
| | | |
| | | /** |
| | | * 顺序 |
| File was renamed from OrderService/src/test/java/com/java110/order/type/TypeTest.java |
| | |
| | | package com.java110.order.type; |
| | | package com.java110.log.type; |
| | | |
| | | import com.java110.order.AppTest; |
| | | import junit.framework.Test; |
| | | import junit.framework.TestCase; |
| | | import junit.framework.TestSuite; |
| | | import org.apache.ibatis.plugin.Signature; |
| | | |
| | | import java.io.IOException; |
| | | import java.lang.reflect.ParameterizedType; |
| | | import java.lang.reflect.Type; |
| | | import java.lang.reflect.TypeVariable; |
| | | |
| | | /** |
| | | * Created by wuxw on 2017/4/14. |
| | |
| | | * @throws Exception |
| | | */ |
| | | public void testType() throws Exception{ |
| | | Object obj = Class.forName("com.java110.order.type.CustDispatchListener").newInstance(); |
| | | Object obj = Class.forName("com.java110.log.type.CustDispatchListener").newInstance(); |
| | | Type[] types = obj.getClass().getGenericInterfaces(); |
| | | System.out.println(((ParameterizedType)types[0]).getActualTypeArguments()[0].getTypeName()); |
| | | } |
| | |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.UUID; |
| | | import java.util.concurrent.locks.Lock; |
| | | import java.util.concurrent.locks.ReentrantLock; |
| | | |
| | |
| | | } |
| | | |
| | | public static String getDataFlowId() throws GenerateCodeException{ |
| | | if(!MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.KEY_NEED_INVOKE_GENERATE_ID))){ |
| | | /*if(!MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.KEY_NEED_INVOKE_GENERATE_ID))){ |
| | | return prefixMap.get("dataFlowId") + DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_H) + nextId("%08d"); |
| | | } |
| | | |
| | | return getCode(prefixMap.get("dataFlowId")); |
| | | return getCode(prefixMap.get("dataFlowId"));*/ |
| | | |
| | | return UUID.randomUUID().toString().replace("-","").toLowerCase(); |
| | | |
| | | } |
| | | |
| | |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | protected BusinessServiceDataFlow writeDataToDataFlowContext(String reqJson, Map<String,String> headers) throws InitDataFlowContextException { |
| | | protected BusinessServiceDataFlow writeDataToDataFlowContext(String reqJson, Map<String,String> headers) throws Exception { |
| | | BusinessServiceDataFlow businessServiceDataFlow = DataFlowFactory.newInstance(BusinessServiceDataFlow.class).builder(reqJson,headers); |
| | | return businessServiceDataFlow; |
| | | } |
| | |
| | | |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.java110.common.factory.ApplicationContextFactory; |
| | | import com.java110.common.log.LoggerEngine; |
| | | import com.java110.common.util.DateUtil; |
| | | import com.java110.entity.center.Business; |
| | | import com.java110.entity.center.DataFlowLinksCost; |
| | | import com.java110.entity.center.DataFlowLog; |
| | | |
| | | import java.net.InetAddress; |
| | | import java.net.UnknownHostException; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * 数据流上下文 |
| | | * Created by wuxw on 2018/5/18. |
| | | */ |
| | | public abstract class AbstractDataFlowContext implements DataFlowContext,Orders{ |
| | | public abstract class AbstractDataFlowContext extends AbstractTransactionLog implements DataFlowContext,Orders,TransactionLog{ |
| | | |
| | | private String dataFlowId; |
| | | |
| | |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public abstract DataFlowContext builder(String reqInfo, Map<String,String> headerAll) throws Exception; |
| | | public <T> T builder(String reqInfo, Map<String,String> headerAll) throws Exception{ |
| | | //预处理 |
| | | preBuilder(reqInfo, headerAll); |
| | | //调用builder |
| | | T dataFlowContext = (T)doBuilder(reqInfo, headerAll); |
| | | //后处理 |
| | | afterBuilder((DataFlowContext) dataFlowContext); |
| | | return dataFlowContext; |
| | | } |
| | | |
| | | /** |
| | | * 预处理 |
| | | * @param reqInfo |
| | | * @param headerAll |
| | | */ |
| | | protected void preBuilder(String reqInfo, Map<String,String> headerAll) { |
| | | super.preBuilder(reqInfo,headerAll); |
| | | } |
| | | |
| | | /** |
| | | * 构建对象 |
| | | * @param reqInfo |
| | | * @param headerAll |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public abstract DataFlowContext doBuilder(String reqInfo, Map<String,String> headerAll) throws Exception; |
| | | |
| | | protected void afterBuilder(DataFlowContext dataFlowContext){ |
| | | |
| | | } |
| | | |
| | | public void setTransactionId(String transactionId) { |
| | | this.transactionId = transactionId; |
| | |
| | | |
| | | public String getUserId(){return null;}; |
| | | |
| | | @Override |
| | | public void setAppId(String appId) { |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void setUserId(String userId) { |
| | | |
| | | } |
| | | |
| | | public String getRemark(){return null;}; |
| | | |
| | | public String getReqSign(){return null;}; |
| | |
| | | return null; |
| | | } |
| | | |
| | | public String getLogId(){ |
| | | return getbId(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 业务编码 当前需要处理的业务编码,可以写将要请求服务提供方的方法名 |
| | | * 主要用于 日志端展示 |
| | | * @return 当前服务编码 |
| | | */ |
| | | public String getServiceCode(){ |
| | | if(this.currentBusiness != null){ |
| | | return currentBusiness.getServiceCode(); |
| | | } |
| | | return ""; |
| | | } |
| | | |
| | | /** |
| | | * 业务名称 当前需要处理的业务名称,可以当前调用的业务名称 如 商品购买 等 |
| | | * 主要用于 日志端展示 |
| | | * @return 当前服务名称 |
| | | */ |
| | | public String getServiceName(){ |
| | | if(this.currentBusiness != null){ |
| | | return currentBusiness.getServiceName(); |
| | | } |
| | | return ""; |
| | | } |
| | | |
| | | |
| | | public abstract Orders getOrder(); |
| | | |
| | | protected AbstractDataFlowContext(Date startDate, String code){ |
| New file |
| | |
| | | package com.java110.core.context; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.alibaba.fastjson.serializer.SerializerFeature; |
| | | import com.java110.common.log.LoggerEngine; |
| | | import com.java110.common.util.DateUtil; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | |
| | | import java.net.InetAddress; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.UUID; |
| | | |
| | | /** |
| | | * 交互日志抽象类 |
| | | * Created by wuxw on 2018/6/9. |
| | | */ |
| | | public abstract class AbstractTransactionLog implements TransactionLog { |
| | | |
| | | private final static Logger logger = LoggerFactory.getLogger(AbstractTransactionLog.class); |
| | | |
| | | private String port; |
| | | |
| | | private String logStatus; |
| | | |
| | | private String requestMessage; |
| | | |
| | | private String responseMessage; |
| | | |
| | | public String getHostIp() { |
| | | String ip = null; |
| | | try { |
| | | ip = InetAddress.getLocalHost().getHostAddress(); |
| | | }catch (Exception e){ |
| | | logger.error("获取主机Ip失败",e); |
| | | ip = "-1"; |
| | | } |
| | | return ip; |
| | | } |
| | | |
| | | /** |
| | | * 时间戳 |
| | | * @return |
| | | */ |
| | | public String getTimestamp(){ |
| | | return DateUtil.getCurrentDate().getTime()+""; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public String getLogStatus() { |
| | | return logStatus; |
| | | } |
| | | |
| | | public String getRequestMessage() { |
| | | return requestMessage; |
| | | } |
| | | |
| | | public String getResponseMessage() { |
| | | return responseMessage; |
| | | } |
| | | |
| | | @Override |
| | | public String getPort() { |
| | | return port; |
| | | } |
| | | |
| | | /** |
| | | * 预构建 |
| | | * @param reqInfo |
| | | * @param headerAll |
| | | */ |
| | | protected void preBuilder(String reqInfo, Map<String,String> headerAll){ |
| | | |
| | | if(headerAll != null && headerAll.containsKey("port")){ |
| | | this.port = headerAll.get("port"); |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 重新构建 TransactionLog 对象 主要用于服务调用方 |
| | | * @return |
| | | */ |
| | | public TransactionLog reBuilder(String requestMessage,String responseMessage,String logStatus){ |
| | | |
| | | this.logStatus = logStatus; |
| | | this.requestMessage = requestMessage; |
| | | this.responseMessage = responseMessage; |
| | | |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 重新构建 TransactionLog 对象 主要用于服务提供方 |
| | | * @return |
| | | */ |
| | | public TransactionLog reBuilder(String appId,String userId,String requestMessage,String responseMessage,String logStatus){ |
| | | |
| | | this.logStatus = logStatus; |
| | | this.requestMessage = requestMessage; |
| | | this.responseMessage = responseMessage; |
| | | this.setAppId(appId); |
| | | this.setUserId(userId); |
| | | return this; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public String toString() { |
| | | //return JSONObject.toJSONString(this); |
| | | JSONObject logMessage = JSONObject.parseObject("{}"); |
| | | logMessage.put("transactionId",getTransactionId()); |
| | | logMessage.put("dataFlowId",getDataFlowId()); |
| | | logMessage.put("ip",getHostIp()); |
| | | logMessage.put("port", getPort()); |
| | | logMessage.put("appId",getAppId()); |
| | | logMessage.put("userId",getUserId()); |
| | | logMessage.put("serviceCode",getServiceCode()); |
| | | logMessage.put("serviceName",getServiceName()); |
| | | logMessage.put("timestamp",getTimestamp()); |
| | | logMessage.put("logStatus",getLogStatus()); |
| | | logMessage.put("requestMessage",getRequestMessage()); |
| | | logMessage.put("responseMessage",getResponseMessage()); |
| | | return logMessage.toJSONString(logMessage,SerializerFeature.WriteNullStringAsEmpty); |
| | | } |
| | | } |
| | |
| | | super(startDate, code); |
| | | } |
| | | |
| | | public BusinessServiceDataFlow builder(String reqInfo, Map<String, String> headerAll) throws InitDataFlowContextException { |
| | | public BusinessServiceDataFlow doBuilder(String reqInfo, Map<String, String> headerAll) throws InitDataFlowContextException { |
| | | try{ |
| | | Business business = null; |
| | | JSONObject reqInfoObj = JSONObject.parseObject(reqInfo); |
| | |
| | | */ |
| | | public class CodeDataFlow extends AbstractDataFlowContext { |
| | | |
| | | |
| | | |
| | | /** |
| | | * 前缀 |
| | | */ |
| | |
| | | return port; |
| | | } |
| | | |
| | | |
| | | |
| | | public void setPort(String port) { |
| | | this.port = port; |
| | | } |
| | |
| | | } |
| | | |
| | | @Override |
| | | public CodeDataFlow builder(String reqInfo, Map<String, String> headerAll) throws Exception { |
| | | public CodeDataFlow doBuilder(String reqInfo, Map<String, String> headerAll) throws Exception { |
| | | JSONObject reqInfoObj = JSONObject.parseObject(reqInfo); |
| | | this.setReqJson(reqInfoObj); |
| | | this.setReqData(reqInfo); |
| | |
| | | return this; |
| | | } |
| | | |
| | | public DataFlow builder(String reqInfo, Map<String,String> headerAll) throws Exception{ |
| | | public DataFlow doBuilder(String reqInfo, Map<String,String> headerAll) throws Exception{ |
| | | |
| | | try{ |
| | | Business business = null; |
| New file |
| | |
| | | package com.java110.core.context; |
| | | |
| | | import java.io.Serializable; |
| | | import java.net.UnknownHostException; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * 交互日志 |
| | | * |
| | | * id 业务ID,每次接口交互时的唯一编码 如订单ID 业务ID等 |
| | | * logContextId 上下文ID ,每次调用 每个系统只有一个contextId 每次不一样 生成规则为uuId |
| | | * ip 当前主机IP |
| | | * port 当前应用监听端口 |
| | | * url 交互地址 调用端和服务提供端 必须一致 |
| | | * serviceCode 服务编码 |
| | | * serviceName 服务名称 |
| | | * type S 调用端 C服务提供端 |
| | | * timestamp 时间戳 |
| | | * status 状态 S表示成功 F表示失败 |
| | | * messageHeaders 消息头部信息 目前只有调用端保存日志 |
| | | * message 消息 目前只有调用端保存 |
| | | * Created by wuxw on 2018/6/7. |
| | | */ |
| | | public interface TransactionLog extends Serializable { |
| | | |
| | | //业务ID |
| | | public String getTransactionId(); |
| | | |
| | | /** |
| | | * 日志上下文ID |
| | | * @return |
| | | */ |
| | | public String getDataFlowId(); |
| | | |
| | | //获取主机Ip |
| | | public String getHostIp(); |
| | | |
| | | //获取监听端口 |
| | | public String getPort(); |
| | | |
| | | public String getAppId(); |
| | | |
| | | public String getUserId(); |
| | | |
| | | public void setAppId(String appId); |
| | | |
| | | public void setUserId(String userId); |
| | | |
| | | //业务编码,如果是批量受理就取第一个 |
| | | public String getServiceCode(); |
| | | |
| | | //业务名称 如果是批量受理就取第一个 |
| | | public String getServiceName(); |
| | | |
| | | //时间 |
| | | public String getTimestamp(); |
| | | |
| | | //编码 S 表示成功 F其他表示失败 |
| | | |
| | | /** |
| | | * 当前状态 调用的时候和接受的时候统一写成S |
| | | * 返回时如果返回失败则写为F |
| | | * @return S 表示成功 F其他表示失败 |
| | | */ |
| | | public String getLogStatus(); |
| | | |
| | | /** |
| | | * 接口请求消息 |
| | | * @return |
| | | */ |
| | | public String getRequestMessage(); |
| | | |
| | | |
| | | public String getResponseMessage(); |
| | | |
| | | /** |
| | | * 重新构建 TransactionLog 对象 主要用于服务调用方 |
| | | * @return |
| | | */ |
| | | public TransactionLog reBuilder(String appId,String userId,String requestMessage,String responseMessage,String logStatus); |
| | | |
| | | /** |
| | | * 重新构建 TransactionLog 对象 主要用于服务提供方 |
| | | * @return |
| | | */ |
| | | public TransactionLog reBuilder(String requestMessage,String responseMessage,String logStatus); |
| | | |
| | | /** |
| | | * 转换成json模式 |
| | | * @return |
| | | */ |
| | | public String toString(); |
| | | |
| | | } |
| | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.alibaba.fastjson.JSONPath; |
| | | import com.java110.common.constant.MappingConstant; |
| | | import com.java110.common.constant.OrderTypeCdConstant; |
| | | import com.java110.common.constant.ResponseConstant; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 业务是否都成功了 |
| | | * @param response true 成功 false 失败 |
| | | * @return |
| | | */ |
| | | public static boolean isSuccessBusiness(JSONObject response){ |
| | | Object obj = JSONPath.eval(response,"$.orders.response.code"); |
| | | |
| | | if(obj != null && obj instanceof String && ResponseConstant.RESULT_CODE_SUCCESS.equals(obj.toString())){ |
| | | Object businessObj = JSONPath.eval(response,"$.business"); |
| | | if(businessObj == null){ |
| | | return true; |
| | | } |
| | | |
| | | if(businessObj instanceof JSONObject){ |
| | | JSONObject businessJson = (JSONObject) businessObj; |
| | | if(!businessJson.containsKey("response")){ //这里返回协议错误,我们认为是成功 |
| | | return true; |
| | | } |
| | | if(businessJson.getJSONObject("response").containsKey("code") |
| | | && ResponseConstant.RESULT_CODE_SUCCESS.equals(businessJson.getJSONObject("response").getString("code"))){ |
| | | return true; |
| | | } |
| | | } |
| | | |
| | | if(businessObj instanceof JSONArray){ |
| | | JSONArray businessJsons = (JSONArray) businessObj; |
| | | if(businessJsons == null || businessJsons.size() == 0){ |
| | | return true; |
| | | } |
| | | JSONObject businessJson = null; |
| | | for (int businessIndex = 0;businessIndex < businessJsons.size();businessIndex++) { |
| | | businessJson = businessJsons.getJSONObject(businessIndex); |
| | | if (!businessJson.containsKey("response")) { //这里返回协议错误,我们认为是成功 |
| | | continue; |
| | | } |
| | | if (businessJson.getJSONObject("response").containsKey("code") |
| | | && !ResponseConstant.RESULT_CODE_SUCCESS.equals(businessJson.getJSONObject("response").getString("code"))) { |
| | | return false; |
| | | } |
| | | } |
| | | return true; |
| | | } |
| | | } |
| | | |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 返回模板 只有Order信息 |
| | | * @param transactionId |
| | | * @param code |
| New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | |
| | | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <artifactId>MicroCommunity</artifactId> |
| | | <groupId>com.java110</groupId> |
| | | <version>1.0-SNAPSHOT</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>java110-logAgent</artifactId> |
| | | |
| | | <name>java110-logAgent</name> |
| | | <!-- FIXME change it to the project's website --> |
| | | <url>http://www.example.com</url> |
| | | |
| | | <properties> |
| | | <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
| | | <maven.compiler.source>1.7</maven.compiler.source> |
| | | <maven.compiler.target>1.7</maven.compiler.target> |
| | | </properties> |
| | | |
| | | <dependencies> |
| | | <dependency> |
| | | <groupId>com.java110</groupId> |
| | | <artifactId>java110-core</artifactId> |
| | | <scope>provided</scope> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>junit</groupId> |
| | | <artifactId>junit</artifactId> |
| | | <scope>test</scope> |
| | | </dependency> |
| | | </dependencies> |
| | | |
| | | |
| | | </project> |
| New file |
| | |
| | | package com.java110.log.agent; |
| | | |
| | | import com.java110.common.cache.MappingCache; |
| | | import com.java110.common.constant.KafkaConstant; |
| | | import com.java110.common.constant.MappingConstant; |
| | | import com.java110.common.factory.ApplicationContextFactory; |
| | | import com.java110.common.kafka.KafkaFactory; |
| | | import com.java110.common.log.LoggerEngine; |
| | | import com.java110.core.context.DataFlow; |
| | | import com.java110.core.context.TransactionLog; |
| | | |
| | | /** |
| | | * 日志代理 |
| | | * 收集日志 发送至 日志服务 |
| | | * Created by wuxw on 2018/6/9. |
| | | */ |
| | | public class LogAgent extends LoggerEngine{ |
| | | |
| | | public static final String LOG_STATUS_S = "S"; |
| | | public static final String LOG_STATUS_F = "F"; |
| | | public static final String LOG_TYPE_S = "S"; |
| | | public static final String LOG_TYPE_C = "C"; |
| | | |
| | | |
| | | /** |
| | | * 发送日志 |
| | | * @param transactionLog |
| | | * @return |
| | | */ |
| | | public static boolean sendLog(TransactionLog transactionLog){ |
| | | if(MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.KEY_LOG_ON_OFF))) { |
| | | try { |
| | | KafkaFactory.sendKafkaMessage(KafkaConstant.TOPIC_LOG_NAME, "", transactionLog.toString()); |
| | | }catch (Exception e){ |
| | | logger.error("保存日志失败:"+transactionLog.toString(),e); |
| | | return false; |
| | | } |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | |
| | | public static boolean sendLog(DataFlow dataFlow){ |
| | | return sendLog(dataFlow); |
| | | |
| | | } |
| | | |
| | | |
| | | } |
| New file |
| | |
| | | package com.java110; |
| | | |
| | | import static org.junit.Assert.assertTrue; |
| | | |
| | | import com.java110.core.context.DataFlow; |
| | | import com.java110.core.context.TransactionLog; |
| | | import org.junit.Test; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.util.Date; |
| | | |
| | | /** |
| | | * Unit test for simple App. |
| | | */ |
| | | public class AppTest |
| | | { |
| | | /** |
| | | * Rigorous Test :-) |
| | | */ |
| | | @Test |
| | | public void shouldAnswerWithTrue() |
| | | { |
| | | assertTrue( true ); |
| | | } |
| | | |
| | | @Test |
| | | public void testDataFlow(){ |
| | | TransactionLog transactionLog = new DataFlow(new Date(),"0000"); |
| | | transactionLog.reBuilder("234","234324","F"); |
| | | |
| | | System.out.println(transactionLog.toString()); |
| | | } |
| | | } |
| | |
| | | <groupId>org.mybatis.spring.boot</groupId> |
| | | <artifactId>mybatis-spring-boot-starter</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>com.alibaba</groupId> |
| | | <artifactId>druid</artifactId> |
| | |
| | | <module>java110-config</module> |
| | | <module>java110-common</module> |
| | | <module>UserService</module> |
| | | <module>OrderService</module> |
| | | <module>LogService</module> |
| | | <module>PayService</module> |
| | | <module>eureka</module> |
| | | <module>WechatService</module> |
| | |
| | | <module>CenterService</module> |
| | | <module>java110-cacheAgent</module> |
| | | <module>ConsoleService</module> |
| | | <module>java110-logAgent</module> |
| | | <module>zipkin</module> |
| | | </modules> |
| | | |
| | | <parent> |
| | |
| | | <type>pom</type> |
| | | <scope>import</scope> |
| | | </dependency> |
| | | |
| | | |
| | | <dependency> |
| | | <groupId>org.springframework</groupId> |
| | |
| | | <artifactId>java110-cacheAgent</artifactId> |
| | | <version>${microcommunity.version}</version> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>com.java110</groupId> |
| | | <artifactId>java110-logAgent</artifactId> |
| | | <version>${microcommunity.version}</version> |
| | | </dependency> |
| | | <!-- logback 日志组件支持 --> |
| | | |
| | | <dependency> |
| | |
| | | <artifactId>jedis</artifactId> |
| | | <version>2.8.2</version> |
| | | </dependency> |
| | | |
| | | |
| | | |
| | | |
| | | <dependency> |
| New file |
| | |
| | | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> |
| | | <parent> |
| | | <artifactId>MicroCommunity</artifactId> |
| | | <groupId>com.java110</groupId> |
| | | <version>1.0-SNAPSHOT</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | <artifactId>zipkin</artifactId> |
| | | <packaging>war</packaging> |
| | | <name>zipkin Maven Webapp</name> |
| | | <url>http://maven.apache.org</url> |
| | | <dependencies> |
| | | |
| | | <!--springboot依赖--> |
| | | <dependency> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-web</artifactId> |
| | | </dependency> |
| | | <!--加入zipkin依赖--> |
| | | <dependency> |
| | | <groupId>io.zipkin.java</groupId> |
| | | <artifactId>zipkin</artifactId> |
| | | <version>2.4.2</version> |
| | | </dependency> |
| | | |
| | | <!--引入zipkin的流绑定--> |
| | | <dependency> |
| | | <groupId>org.springframework.cloud</groupId> |
| | | <artifactId>spring-cloud-sleuth-zipkin-stream</artifactId> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>org.springframework.cloud</groupId> |
| | | <artifactId>spring-cloud-starter-stream-kafka</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>mysql</groupId> |
| | | <artifactId>mysql-connector-java</artifactId> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-jdbc</artifactId> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>junit</groupId> |
| | | <artifactId>junit</artifactId> |
| | | <version>3.8.1</version> |
| | | <scope>test</scope> |
| | | </dependency> |
| | | </dependencies> |
| | | <build> |
| | | <finalName>zipkin</finalName> |
| | | </build> |
| | | </project> |
| New file |
| | |
| | | package com.java110.zipkin; |
| | | |
| | | import org.springframework.boot.SpringApplication; |
| | | import org.springframework.boot.autoconfigure.SpringBootApplication; |
| | | import org.springframework.cloud.sleuth.zipkin.stream.EnableZipkinStreamServer; |
| | | |
| | | @SpringBootApplication |
| | | @EnableZipkinStreamServer//配置可以作为zipkinserver |
| | | public class ZipkinServerApplication { |
| | | |
| | | public static void main(String[] args) { |
| | | SpringApplication.run(ZipkinServerApplication.class,args); |
| | | } |
| | | } |
| New file |
| | |
| | | --- |
| | | spring: |
| | | profiles: peer1 # 指定profile=peer1 |
| | | server: |
| | | port: 8761 |
| | | eureka: |
| | | instance: |
| | | hostname: peer1 # 指定当profile=peer1时,主机名 |
| | | client: |
| | | serviceUrl: |
| | | defaultZone: http://peer2:8762/eureka/ # 将自己注册到peer2这个Eureka上面去 |
| New file |
| | |
| | | #配置kafka |
| | | spring: |
| | | sleuth: |
| | | enabled: false |
| | | sampler: |
| | | percentage: 1.0 |
| | | cloud: |
| | | stream: |
| | | kafka: |
| | | binder: |
| | | brokers: localhost:9092 |
| | | zkNodes: localhost:2181 |
| | | |
| | | logging: |
| | | file: logs/eureka.log |
| | | level: |
| | | org: |
| | | springframework: |
| | | web: DEBUG |
| | | |
| | | # 参考文档:http://projects.spring.io/spring-cloud/docs/1.0.3/spring-cloud.html#_standalone_mode |
| | | # 参考文档:http://my.oschina.net/buwei/blog/618756 |