}

    MethodSignature signature = (MethodSignature) joinPoint.getSignature();

    SysLog sysLog = new SysLog();

    // 请求的方法名

    String className = joinPoint.getTarget().getClass().getName();

    String methodName = signature.getName();

    sysLog.setMethod(className + "." + methodName + "()");

    // 请求的参数

    Object[] args = joinPoint.getArgs();

    try{

        String params = JSONObject.toJSONString(args[0]);

        sysLog.setParams(params);

    } catch (Exception e){

    }

    // 获取request

    HttpServletRequest request = HttpUtils.getHttpServletRequest();

    // 设置IP地址

    sysLog.setIp(IPUtils.getIpAddr(request));

    // 用户名

    sysLog.setUserName(userName);

    // 执行时长(毫秒)

    sysLog.setTime(time);

    // 保存系统日志

    sysLogService.save(sysLog);

}

}

复制代码


**2.声明ip工具类**

代码如下:

public class IPUtils {

private static Logger logger = LoggerFactory.getLogger(IPUtils.class);

/**

 * 获取IP地址

 * 使用Nginx等反向代理软件, 则不能通过request.getRemoteAddr()获取IP地址

 * 如果使用了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP地址,X-Forwarded-For中第一个非unknown的有效IP字符串,则为真实IP地址

 */

public static String getIpAddr(HttpServletRequest request) {

    String ip = null;

    try {

        ip = request.getHeader("x-forwarded-for");

        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {

            ip = request.getHeader("Proxy-Client-IP");

        }

        if (StringUtils.isEmpty(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

            ip = request.getHeader("WL-Proxy-Client-IP");

        }

        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {

            ip = request.getHeader("HTTP_CLIENT_IP");

        }

        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {

            ip = request.getHeader("HTTP_X_FORWARDED_FOR");

        }

        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {

            ip = request.getRemoteAddr();

        }

    } catch (Exception e) {

        logger.error("IPUtils ERROR ", e);

    }

    return ip;

}

}

复制代码


**3.声明HttpUtils工具类**

代码如下:

public class HttpUtils {

private static final Log log = LogFactory.getLog(HttpUtils.class);

/**

 * 获取HttpServletRequest对象

 * @return

 */

public static HttpServletRequest getHttpServletRequest() {

    return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

}

/**

 * 输出信息到浏览器

 * @param response

 * @throws IOException

 */

public static void print(HttpServletResponse response, int code, String msg) throws IOException {

    response.setContentType("application/json; charset=utf-8");

    HttpResult result = HttpResult.error(code, msg);

    String json = JSONObject.toJSONString(result);

    response.getWriter().print(json);

    response.getWriter().flush();

    response.getWriter().close();

}

public static String sendpost(String serverUrl, String data) {

    StringBuilder responseBuilder = null;

    BufferedReader reader = null;

    OutputStreamWriter wr = null;

    String re=null;

    try {

        URL url = new URL(serverUrl);

        URLConnection e = url.openConnection();

        e.setDoOutput(true);

        e.setConnectTimeout(5000);

        wr = new OutputStreamWriter(e.getOutputStream());

最后

现在其实从大厂招聘需求可见,在招聘要求上有高并发经验优先,包括很多朋友之前都是做传统行业或者外包项目,一直在小公司,技术搞的比较简单,没有怎么搞过分布式系统,但是现在互联网公司一般都是做分布式系统。

所以说,如果你想进大厂,想脱离传统行业,这些技术知识都是你必备的,下面自己手打了一份Java并发体系思维导图,希望对你有所帮助。

资料获取方式:戳这里免费下载

利用Spring的aop原理实现系统级日志管理,送给正在迷茫的你