浏览代码

新增日志拦截切面

chenweida 8 年之前
父节点
当前提交
e3b439b480

+ 0 - 11
common/common-rest-model/src/main/java/com/yihu/jw/restmodel/common/Envelop.java

@ -21,8 +21,6 @@ public class Envelop implements Serializable {
    private static final long serialVersionUID = 2076324875575488461L;
    private boolean successFlg=true;
    private int pageSize = 10;
    private int currPage;
@ -57,14 +55,6 @@ public class Envelop implements Serializable {
        this.totalCount = totalCount;
    }
    public boolean isSuccessFlg() {
        return successFlg;
    }
    public void setSuccessFlg(boolean successFlg) {
        this.successFlg = successFlg;
    }
    public List getDetailModelList() {
        return detailModelList;
    }
@ -150,7 +140,6 @@ public class Envelop implements Serializable {
    public static Envelop getError(String message,int errorCode) {
        Envelop envelop = new Envelop();
        envelop.setSuccessFlg(false);
        envelop.setErrorMsg(message);
        envelop.setErrorCode(errorCode);
        return envelop;

+ 0 - 1
common/common-rest-model/src/main/java/com/yihu/jw/restmodel/common/EnvelopRestController.java

@ -54,7 +54,6 @@ public class EnvelopRestController {
     */
    protected Envelop getResult(List modelList, int totalCount) {
        Envelop envelop = new Envelop();
        envelop.setSuccessFlg(true);
        envelop.setDetailModelList(modelList);
        envelop.setTotalCount(totalCount);

+ 13 - 0
common/common-rest-model/src/main/java/com/yihu/jw/restmodel/gateway/GatewayContanrts.java

@ -0,0 +1,13 @@
package com.yihu.jw.restmodel.gateway;
/**
 * Created by chenweida on 2017/6/19.
 */
public class GatewayContanrts {
    public static final class ZipkinElasticKey {
        static public String gateway_input_params = "gateway_input_params";
        static public String gateway_out_params = "gateway_out_params";
    }
}

+ 49 - 0
web-gateway/src/main/java/com/yihu/jw/aspect/LogAspect.java

@ -0,0 +1,49 @@
package com.yihu.jw.aspect;
import com.yihu.jw.restmodel.gateway.GatewayContanrts;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
/**
 * Created by chenweida on 2017/6/19.
 */
@Aspect
@Component
public class LogAspect {
    @Autowired
    private Tracer tracer;
    //Controller层切点
    @Pointcut("within(@org.springframework.web.bind.annotation.RestController *)")
    public void controllerAspect() {
    }
    @Around("controllerAspect()")
    public Object checkToken(ProceedingJoinPoint point) throws Throwable {
        ;
        Object o = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        try {
            //访问前日志
            tracer.getCurrentSpan().tag(GatewayContanrts.ZipkinElasticKey.gateway_input_params,new JSONObject(request.getParameterMap()).toString());
            o = point.proceed();
            //访问后日志
            tracer.getCurrentSpan().tag(GatewayContanrts.ZipkinElasticKey.gateway_out_params,new JSONObject(o).toString());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return o;
    }
}

+ 9 - 8
web-gateway/src/main/java/com/yihu/jw/controller/PatientController.java

@ -3,6 +3,7 @@ package com.yihu.jw.controller;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.yihu.jw.fegin.PatientFegin;
import com.yihu.jw.restmodel.common.Envelop;
import com.yihu.jw.restmodel.exception.SystemException;
import com.yihu.jw.restmodel.exception.SecurityException;
import com.yihu.jw.restmodel.exception.business.ManageException;
@ -16,6 +17,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;
@ -33,23 +35,22 @@ public class PatientController {
    private Logger logger = LoggerFactory.getLogger(PatientController.class);
    @Autowired
    private PatientFegin patientFegin;
    @Autowired
    private Tracer tracer;
    @Value("${test.aaa}")
    private String aaaa;
    @GetMapping("/hello")
    @ApiVersion(1)
    @ResponseBody
    public String hello1(@RequestParam(name = "id") Integer id,
                         @RequestParam(name = "name") String name,
                         HttpServletRequest request
    public Envelop hello1(@RequestParam(name = "id") Integer id,
                          @RequestParam(name = "name") String name,
                          HttpServletRequest request
    ) throws Exception {
        tracer.getCurrentSpan().logEvent("logEvent");
        tracer.getCurrentSpan().tag("test","tag");
        tracer.getCurrentSpan().setBaggageItem("test","BaggageItem");
        tracer.getCurrentSpan().logEvent(new JSONObject(request.getParameterMap()).toString());
        switch (id) {
            case 1: {
                throw new ManageException("后台管理系统异常");
@ -62,7 +63,7 @@ public class PatientController {
            }
        }
        return "hello1";
        return Envelop.getSuccess("请求成功");
    }
    @GetMapping("/hello")

+ 0 - 1
web-gateway/src/main/java/com/yihu/jw/controller/wlyy/patient/AdvertisementControlelr.java

@ -30,7 +30,6 @@ public class AdvertisementControlelr extends EnvelopRestController {
    @Autowired
    private AdvertisementFegin advertisementFegin;
    @Autowired
    private Tracer tracer;

+ 4 - 0
web-gateway/src/main/java/com/yihu/jw/version/ApiVesrsionCondition.java

@ -1,5 +1,8 @@
package com.yihu.jw.version;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.condition.RequestCondition;
import javax.servlet.http.HttpServletRequest;
@ -11,6 +14,7 @@ import java.util.regex.Pattern;
 * Created by chenweida on 2017/6/15.
 */
public class ApiVesrsionCondition implements RequestCondition<ApiVesrsionCondition> {
    // 路径中版本的前缀, 这里用 /v[1-9]/的形式
    private final static Pattern VERSION_PREFIX_PATTERN = Pattern.compile("v(\\d+)/");

+ 2 - 0
web-gateway/src/main/java/com/yihu/jw/version/JWRequestMappingHandlerMapping.java

@ -1,6 +1,7 @@
package com.yihu.jw.version;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.condition.RequestCondition;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
@ -10,6 +11,7 @@ import java.lang.reflect.Method;
 * Created by chenweida on 2017/6/15.
 * 扩展spring的RequestMappingHandlerMapping
 */
public class JWRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
    @Override
    protected RequestCondition<ApiVesrsionCondition> getCustomTypeCondition(Class<?> handlerType) {