• MDCFilter


    package com.common.filter;
    
    import lombok.extern.slf4j.Slf4j;
    import org.slf4j.MDC;
    import org.springframework.context.annotation.Configuration;
    
    import javax.servlet.*;
    import javax.servlet.http.HttpServletRequest;
    import java.io.IOException;
    import java.lang.management.ManagementFactory;
    import java.lang.management.RuntimeMXBean;
    import java.util.UUID;
    
    @Configuration
    @Slf4j
    public class MDCFilter implements Filter {
        private static final String SESSION_KEY = "sessionId";
        @Override
        public void init(FilterConfig filterConfig) {
            //
    
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
         
            HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
            mdc(httpServletRequest);
            filterChain.doFilter(httpServletRequest, servletResponse);
        }
    
        @Override
        public void destroy() {
            //
        }
    
        public void mdc(HttpServletRequest httpServletRequest){
            String url = httpServletRequest.getRequestURI();
            MDC.put("RequestIP",httpServletRequest.getRemoteAddr());
            MDC.put("Module",url);
            MDC.put("RemoteHost",httpServletRequest.getRemoteHost());
            MDC.put("ServerPort",httpServletRequest.getServerPort()+"");
            MDC.put("ServerIP",httpServletRequest.getLocalAddr());
            RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
            String name = runtime.getName();
            MDC.put("ProcessID ",name.substring(0, name.indexOf('@')));
            MDC.put("Thread", Thread.currentThread().getName());
    
            try{
                String sessionId = UUID.randomUUID().toString().replace("-", "");
                MDC.put(SESSION_KEY, sessionId);
            }catch (Exception e){
                log.warn("-------------set SESSION_KEY 异常---------------", e);
            }
        }
    }
  • 相关阅读:
    投产包错误的思考
    Oracle----用户操作
    3.27 学习记录
    3.26 学习记录
    3.25 学习记录
    3.24 学习记录
    3. 23构建之法读后感
    3.22 学习记录
    3. 21学习记录
    3.20 学习记录
  • 原文地址:https://www.cnblogs.com/tiancai/p/16489463.html
Copyright © 2020-2023  润新知