chenweida 6 年 前
コミット
9ea68a1bb1

+ 30 - 96
admin/admin-client-starter/src/main/java/com/yihu/admin/client/advice/ZuulPreFilter.java

@ -4,50 +4,37 @@ import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.yihu.admin.client.properties.AdminClientProperties;
import com.yihu.admin.client.util.NetworkUtil;
import com.yihu.admin.client.websocket.buffer.EventBuffer;
import com.yihu.admin.client.websocket.event.HttpEvent;
import com.yihu.admin.client.websocket.sender.SimpleEventSender;
import net.sf.json.JSONObject;
import okhttp3.*;
import okhttp3.internal.http.HttpMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.StreamUtils;
import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
/**
 * Created by chenweida on 2018/5/31 0031.
 */
@Component
@ConditionalOnProperty(name = "yihu.admin.client.advice.zuul.pre", havingValue = "true")
public class ZuulPreFilter extends ZuulFilter {
    private Logger logger= LoggerFactory.getLogger(ZuulPreFilter.class);
    @Autowired
    private Tracer tracer;
    @Autowired
    private EventBuffer eventBuffer;
    private SimpleEventSender simpleEventSender;
    @Autowired
    private AdminClientProperties adminClientProperties;
    @Value("${spring.application.name:unknow}")
    @Value("${spring.application.name}")
    private String spanrName;
    @Autowired
    private ProxyRequestHelper helper;
    @Override
    public String filterType() {
        //filterType:返回过滤器的类型。有pre、route、post、error等几种取值
        return "route";
        return "pre";
    }
    @Override
@ -64,96 +51,43 @@ public class ZuulPreFilter extends ZuulFilter {
    @Override
    public Object run() {
        try {
            RequestContext ctx = RequestContext.getCurrentContext();
            HttpServletRequest request = ctx.getRequest();
            OkHttpClient httpClient = new OkHttpClient.Builder()
                    // customize
                    .build();
            String method = request.getMethod();
            String uri = this.helper.buildZuulRequestURI(request);
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
            Headers.Builder headers = new Headers.Builder();
            Enumeration<String> headerNames = request.getHeaderNames();
            while (headerNames.hasMoreElements()) {
                String name = headerNames.nextElement();
                Enumeration<String> values = request.getHeaders(name);
                while (values.hasMoreElements()) {
                    String value = values.nextElement();
                    headers.add(name, value);
                }
            }
            InputStream inputStream = request.getInputStream();
            RequestBody requestBody = null;
            if (inputStream != null && HttpMethod.permitsRequestBody(method)) {
                MediaType mediaType = null;
                if (headers.get("Content-Type") != null) {
                    mediaType = MediaType.parse(headers.get("Content-Type"));
                }
                requestBody = RequestBody.create(mediaType, StreamUtils.copyToByteArray(inputStream));
            }
            Request.Builder builder = new Request.Builder()
                    .headers(headers.build())
                    .url(uri)
                    .method(method, requestBody);
            //日志
        try {
            HttpEvent event = new HttpEvent();
            Long startTime = System.currentTimeMillis();
            String ip = NetworkUtil.getIpAddress(request);
            JSONObject headerObj = new JSONObject();
            Enumeration<String> headersed = request.getHeaderNames();
            while (headersed.hasMoreElements()) {
                String value = headersed.nextElement();
            Enumeration<String> headers = request.getHeaderNames();
            while (headers.hasMoreElements()) {
                String value = headers.nextElement();
                headerObj.put(value, request.getHeader(value));
            }
            Response response = httpClient.newCall(builder.build()).execute();
            try{
                Long endTime = System.currentTimeMillis();
                event.setHeader(headerObj.toString());
                event.setEventStartTime(startTime);
                event.setEventEndTime(endTime);
                event.setExcuteTime(endTime-startTime);
                event.setHeader(headerObj.toString());
                event.setEventName("system_zuul_tracer");
                event.setResponseParams( response.body().string());
                event.setSpanName(spanrName);
                event.setSpanId(tracer.getCurrentSpan().getSpanId() + "");
                event.setTraceId(tracer.getCurrentSpan().traceIdString());
                event.setRequestParams(JSONObject.fromObject(request.getParameterMap()).toString());
                event.setSuccess(1);
                event.setUri(HttpAdvice.matchURI(request));
                event.setIp(ip);
                event.setMethod(request.getMethod());
                eventBuffer.addEvent(event);
            }catch (Exception e){
            }
            LinkedMultiValueMap<String, String> responseHeaders = new LinkedMultiValueMap<>();
            for (Map.Entry<String, List<String>> entry : response.headers().toMultimap().entrySet()) {
                responseHeaders.put(entry.getKey(), entry.getValue());
            }
            this.helper.setResponse(response.code(), response.body().byteStream(),responseHeaders);
            ctx.setRouteHost(null); // prevent SimpleHostRoutingFilter from running
            event.setHeader(headerObj.toString());
            event.setEventStartTime(startTime);
            event.setEventEndTime(startTime);
            event.setExcuteTime(0L);
            event.setHeader(headerObj.toString());
            event.setEventName("system_zuul_tracer");
            event.setSpanName(spanrName);
            event.setSpanId(tracer.getCurrentSpan().getSpanId() + "");
            event.setTraceId(tracer.getCurrentSpan().traceIdString());
            event.setRequestParams(JSONObject.fromObject(request.getParameterMap()).toString());
            event.setSuccess(1);
            event.setUri(HttpAdvice.matchURI(request));
            event.setIp(ip);
            event.setMethod(request.getMethod());
            simpleEventSender.send(event);
        } catch (Exception e) {
        }
        HttpServletResponse response = ctx.getResponse();
        return null;
        return response;
    }
}

+ 3 - 1
admin/admin-client-starter/src/main/java/com/yihu/admin/client/websocket/sender/SimpleEventSender.java

@ -31,6 +31,8 @@ public class SimpleEventSender implements EventSender {
    @Autowired
    private EventBuffer eventBuffer;
    @Autowired
    private SocketClient socketClient;
    public Boolean send(IEvent event) {
        try {
@ -67,7 +69,7 @@ public class SimpleEventSender implements EventSender {
                    }
                }
            }
            eventBuffer.addEvent(event);
            socketClient.getCurrentClient().send(JSONObject.fromObject(event).toString());
            return true;
        } catch (Exception e) {
            logger.error(e.getMessage());

+ 29 - 29
admin/admin-server-starter/src/main/java/com/yihu/admin/server/log/LogEndPoint.java

@ -34,15 +34,15 @@ public class LogEndPoint {
    ) {
        List<LogModel> logs = new ArrayList<>();
        StringBuffer sql = new StringBuffer("SELECT traceid, max(eventendtime) eventendtime ,min(eventstarttime) eventstarttime, max(excutetime) excutetime FROM " + adminServerProperties.getRealDefaultIndexName() + " where eventname='system_http_tracer'  ");
        StringBuffer sql = new StringBuffer("SELECT traceId, max(eventEndTime) eventEndTime ,min(eventStartTime) eventStartTime, max(excuteTime) excuteTime FROM " + adminServerProperties.getRealDefaultIndexName() + " where eventName='system_http_tracer'  ");
        StringBuffer whereSQL = new StringBuffer();
        if (!StringUtils.isEmpty(traceId)) {
            whereSQL.append(" and traceid='" + traceId + "'");
            whereSQL.append(" and traceId='" + traceId + "'");
        }
        if (time != null && time > 0) {
            whereSQL.append(" and  excutetime >" + time + "");
            whereSQL.append(" and  excuteTime >" + time + "");
        }
        StringBuffer groupby = new StringBuffer(" group by traceid  order by eventstarttime desc limit " + (page * pageSize) + "," + pageSize);
        StringBuffer groupby = new StringBuffer(" group by traceId  order by eventStartTime desc limit " + (page * pageSize) + "," + pageSize);
        String finalSql = sql.toString() + whereSQL.toString() + groupby.toString();
        List<Map<String, Object>> traceidList = elastricSearchHelper.excuceSQL(finalSql);
        for (int i = 0; i < traceidList.size(); i++) {
@ -51,11 +51,11 @@ public class LogEndPoint {
            if (value.size() == 0) {
                continue;
            }
            logTemp.setTraceid(value.get("traceid").toString());
            Long startTime = ((Double) value.get("eventstarttime")).longValue();
            logTemp.setTraceid(value.get("traceId").toString());
            Long startTime = ((Double) value.get("eventStartTime")).longValue();
            logTemp.setEventstarttime(startTime);
            logTemp.setEventstarttimestr(DateUtil.dateToStr(new Date(startTime), DateUtil.YYYY_MM_DD_HH_MM_SS_SSS));
            Long endtime = ((Double) value.get("eventendtime")).longValue();
            Long endtime = ((Double) value.get("eventEndTime")).longValue();
            logTemp.setEventendtime(endtime);
            logTemp.setEventendtimestr(DateUtil.dateToStr(new Date(endtime), DateUtil.YYYY_MM_DD_HH_MM_SS_SSS));
            logs.add(logTemp);
@ -80,7 +80,7 @@ public class LogEndPoint {
        StringBuffer sql = new StringBuffer("SELECT * FROM " + adminServerProperties.getRealDefaultIndexName());
        StringBuffer whereSQL = initWhere(traceId, spanname, eventname, time, uri, method);
        StringBuffer groupBY = new StringBuffer("order by eventstarttime "+sort);
        StringBuffer groupBY = new StringBuffer("order by eventStartTime "+sort);
        String finalSQL = sql.append(whereSQL).append(groupBY).toString();
@ -90,17 +90,17 @@ public class LogEndPoint {
            Map<String, Object> value = traceidList.get(i);
            logTemp.setTraceid(value.get("traceid").toString());
            logTemp.setSpanid(value.get("spanid").toString());
            logTemp.setSpanname(value.get("spanname").toString());
            logTemp.setEventname(value.get("eventname").toString());
            if (value.containsKey("eventstarttime") && value.get("eventstarttime") != null) {
                Long startTime = (Long) value.get("eventstarttime");
            logTemp.setTraceid(value.get("traceId").toString());
            logTemp.setSpanid(value.get("spanId").toString());
            logTemp.setSpanname(value.get("spanName").toString());
            logTemp.setEventname(value.get("eventName").toString());
            if (value.containsKey("eventStartTime") && value.get("eventStartTime") != null) {
                Long startTime = (Long) value.get("eventStartTime");
                logTemp.setEventstarttime(startTime);
                logTemp.setEventstarttimestr(DateUtil.dateToStr(new Date(startTime), DateUtil.YYYY_MM_DD_HH_MM_SS_SSS));
            }
            if (value.containsKey("eventendtime") && value.get("eventendtime") != null) {
                Long endtime = (Long) value.get("eventendtime");
            if (value.containsKey("eventEndTime") && value.get("eventEndTime") != null) {
                Long endtime = (Long) value.get("eventEndTime");
                logTemp.setEventendtime(endtime);
                logTemp.setEventendtimestr(DateUtil.dateToStr(new Date(endtime), DateUtil.YYYY_MM_DD_HH_MM_SS_SSS));
            }
@ -116,8 +116,8 @@ public class LogEndPoint {
            if (value.containsKey("uri") && value.get("uri") != null) {
                logTemp.setUri(value.get("uri").toString());
            }
            if (value.containsKey("excutetime") && value.get("excutetime") != null) {
                logTemp.setExcutetime((Integer) value.get("excutetime"));
            if (value.containsKey("excuteTime") && value.get("excuteTime") != null) {
                logTemp.setExcutetime((Integer) value.get("excuteTime"));
            }
            //设置header
@ -145,13 +145,13 @@ public class LogEndPoint {
        StringBuffer whereSQL = new StringBuffer();
        Map<String, Object> params = new HashMap<>();
        if (!StringUtils.isEmpty(traceId)) {
            params.put("traceid", traceId);
            params.put("traceId", traceId);
        }
        if (!StringUtils.isEmpty(spanname)) {
            params.put("spanname", spanname);
            params.put("spanName", spanname);
        }
        if (!StringUtils.isEmpty(eventname)) {
            params.put("eventname", eventname);
            params.put("eventName", eventname);
        }
        if (!StringUtils.isEmpty(uri)) {
            params.put("uri", uri);
@ -165,7 +165,7 @@ public class LogEndPoint {
        String key = " where ";
        for (Map.Entry<String, Object> one : params.entrySet()) {
            if ("time".equals(one.getKey())) {
                whereSQL.append(key).append("excutetime > '" + one.getValue() + "'");
                whereSQL.append(key).append("excuteTime > '" + one.getValue() + "'");
            } else if ("uri".equals(one.getKey())) {
                whereSQL.append(key).append("uri like '%" + one.getValue() + "%'");
            } else {
@ -183,8 +183,8 @@ public class LogEndPoint {
            @RequestParam(value = "spanname", required = true) String spanname) {
        spanname = spanname.toLowerCase();
        List<QuotaModel> logs = new ArrayList<>();
        StringBuffer sql = new StringBuffer("SELECT uri,sum(success) success,sum(fail) fail,avg(excutetime) avgtime ,max(excutetime) maxtime ,count(*) allcount FROM  " + adminServerProperties.getRealDefaultIndexName() + " " +
                " where spanname ='" + spanname + "' and eventname='system_http_tracer'  " +
        StringBuffer sql = new StringBuffer("SELECT uri,sum(success) success,sum(fail) fail,avg(excuteTime) avgtime ,max(excuteTime) maxtime ,count(*) allcount FROM  " + adminServerProperties.getRealDefaultIndexName() + " " +
                " where spanName ='" + spanname + "' and eventName='system_http_tracer'  " +
                " group by  uri ");
        List<Map<String, Object>> traceidList = elastricSearchHelper.excuceSQL(sql.toString());
        if (traceidList.size() > 0) {
@ -211,17 +211,17 @@ public class LogEndPoint {
            @RequestParam(value = "spanname", required = true) String spanname) {
        spanname = spanname.toLowerCase();
        List<QuotaSQLModel> logs = new ArrayList<>();
        StringBuffer sql = new StringBuffer("SELECT sql,excutetime,eventstarttime FROM  " + adminServerProperties.getRealDefaultIndexName() + " " +
                " where spanname ='" + spanname + "' and eventname='system_sql_springJDBC_tracer' and eventtime>1500 ");
        StringBuffer sql = new StringBuffer("SELECT sql,excuteTime,eventStartTime FROM  " + adminServerProperties.getRealDefaultIndexName() + " " +
                " where spanName ='" + spanname + "' and eventName='system_sql_springJDBC_tracer' and eventTime>1500 ");
        List<Map<String, Object>> traceidList = elastricSearchHelper.excuceSQL(sql.toString());
        if (traceidList.size() > 0) {
            for (Map<String, Object> one : traceidList) {
                if (one.size() > 0) {
                    QuotaSQLModel quotaSQLModel = new QuotaSQLModel();
                    quotaSQLModel.setSql(one.get("sql").toString());
                    quotaSQLModel.setExcutetime(Double.valueOf(one.get("excutetime").toString()).intValue());
                    if (one.containsKey("eventstarttime") && one.get("eventstarttime") != null) {
                        Long startTime = (Long) one.get("eventstarttime");
                    quotaSQLModel.setExcutetime(Double.valueOf(one.get("excuteTime").toString()).intValue());
                    if (one.containsKey("eventStartTime") && one.get("eventStartTime") != null) {
                        Long startTime = (Long) one.get("eventStartTime");
                        quotaSQLModel.setEventstarttime(startTime);
                        quotaSQLModel.setEventstarttimestr(DateUtil.dateToStr(new Date(startTime), DateUtil.YYYY_MM_DD_HH_MM_SS_SSS));
                    }

+ 2 - 2
common-data-fastdfs-starter/pom.xml

@ -35,8 +35,8 @@
        </dependency>
        <dependency>
            <groupId>org.csource</groupId>
            <artifactId>fastdfs-client-java</artifactId>
            <version>1.0</version>
            <artifactId>fastdfs_lib</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>

+ 8 - 0
demo/demo-admin-client/src/main/java/com/yihu/client/controller/DemoController.java

@ -1,5 +1,7 @@
package com.yihu.client.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
@ -12,10 +14,12 @@ import javax.websocket.server.PathParam;
 */
@RestController
@RequestMapping("demo")
@Api(description = "demo例子")
public class DemoController {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @ApiOperation(value = "ceshisql")
    @RequestMapping(value = "/testSQL", method = RequestMethod.GET)
    public String loginfoGET(
            String sql
@ -26,6 +30,7 @@ public class DemoController {
        return "成功";
    }
    @ApiOperation(value = "ceshisql")
    @RequestMapping(value = "/testSQL", method = RequestMethod.DELETE)
    public String loginfoDELETE(
            String sql
@ -36,6 +41,7 @@ public class DemoController {
        return "成功";
    }
    @ApiOperation(value = "ceshisql")
    @RequestMapping(value = "/testSQL", method = RequestMethod.PUT)
    public String loginfoPUT(
            String sql
@ -46,6 +52,7 @@ public class DemoController {
        return "成功";
    }
    @ApiOperation(value = "ceshisql")
    @RequestMapping(value = "/testSQL", method = RequestMethod.POST)
    public String loginfoPOST(
            String sql
@ -56,6 +63,7 @@ public class DemoController {
        return "成功";
    }
    @ApiOperation(value = "ceshisql")
    @RequestMapping(value = "/testSQL/{value}", method = RequestMethod.GET)
    public String loginfoValue(
            @PathVariable(value = "value") String value

+ 2 - 3
demo/demo-admin-client/src/main/resources/application.yml

@ -22,9 +22,8 @@ yihu:
        zuul:
          pre: true #默认false
          post: true #默认false
          route:true #默认false
      server:
        ip: 192.168.131.102
        ip: 192.168.131.250
        port: 8082
@ -55,6 +54,6 @@ endpoints:
logging:
  file: ${logging.path}demo-socket-client.log
  path: G:\idea_workspace\jkzl_git\jkzl-start\logs\
  path: E:\idea_workspace\jkzl_git\jkzl-starter\jkzl-starter\demo\demo-admin-client\logs\
  level:
    root: INFO

+ 2 - 2
demo/demo-admin-server/src/main/java/com/demo/DemoEurekaApplication.java

@ -5,9 +5,9 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
/**
 * Created by chenweida on 2018/2/24.
 * Created by chenweida on 2018/2/24
 * http://localhost:8082/admin/index/index.html.
 */
@SpringBootApplication
public class DemoEurekaApplication {
    public static ApplicationContext ctx = null;

+ 2 - 2
demo/demo-admin-server/src/main/resources/application.yml

@ -1,5 +1,5 @@
server:
  port: 8761
  port: 8082
spring:
  application:
@ -52,7 +52,7 @@ endpoints:
logging:
  file: ${logging.path}demo-eureka.log
  path: G:\idea_workspace\jkzl_git\jkzl-start\logs\
  path: E:\idea_workspace\jkzl_git\jkzl-starter\jkzl-starter\demo\demo-admin-server\logs\
  level:
    root: INFO