亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Feign的請求和響應日志方式是什么

發布時間:2022-06-16 13:42:47 來源:億速云 閱讀:247 作者:iii 欄目:開發技術

這篇文章主要介紹了Feign的請求和響應日志方式是什么的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Feign的請求和響應日志方式是什么文章都會有所收獲,下面我們一起來看看吧。

1、項目里定義FeignClient接口

package com.example.demo.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(name = "deom", url = "https://www.baidu.com")
public interface FeignDemo {
    @GetMapping("/")
    String test();
}

2、單個FeignClient接口開啟日志

在 application.yml 里指定Feign接口日志級別為DEBUG,類型為FULL:

注:com.example.demo.feign.FeignDemo就是上面定義的FeignClient接口

logging:
  level:
    com.example.demo.feign.FeignDemo: debug
# 下面的配置,也可以寫代碼代替
# @Bean
# public Logger.Level level() { return Logger.Level.FULL; }
feign:
  client:
    config:
      default:
        loggerLevel: full

OK了,重啟項目,調用 FeignDemo.test() 方法后,會輸出如下日志:

2020-10-13 11:46:24.161 DEBUG 20824 --- [nio-8080-exec-4] com.example.demo.feign.FeignDemo         : [FeignDemo#test] ---> GET https://www.baidu.com HTTP/1.1
2020-10-13 11:46:24.162 DEBUG 20824 --- [nio-8080-exec-4] com.example.demo.feign.FeignDemo         : [FeignDemo#test] ---> END HTTP (0-byte body)
2020-10-13 11:46:24.255 DEBUG 20824 --- [nio-8080-exec-4] com.example.demo.feign.FeignDemo         : [FeignDemo#test] <--- HTTP/1.1 200 OK (93ms)
2020-10-13 11:46:24.255 DEBUG 20824 --- [nio-8080-exec-4] com.example.demo.feign.FeignDemo         : [FeignDemo#test] content-length: 2443
2020-10-13 11:46:24.255 DEBUG 20824 --- [nio-8080-exec-4] com.example.demo.feign.FeignDemo         : [FeignDemo#test] content-type: text/html
2020-10-13 11:46:24.256 DEBUG 20824 --- [nio-8080-exec-4] com.example.demo.feign.FeignDemo         : [FeignDemo#test] date: Tue, 13 Oct 2020 03:46:24 GMT
2020-10-13 11:46:24.256 DEBUG 20824 --- [nio-8080-exec-4] com.example.demo.feign.FeignDemo         : [FeignDemo#test] server: bfe
2020-10-13 11:46:24.256 DEBUG 20824 --- [nio-8080-exec-4] com.example.demo.feign.FeignDemo         : [FeignDemo#test] 
2020-10-13 11:46:24.257 DEBUG 20824 --- [nio-8080-exec-4] com.example.demo.feign.FeignDemo         : [FeignDemo#test] <!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新聞</a> <a href=https://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地圖</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>視頻</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>貼吧</a> <noscript> <a href=https://cache.yisu.com/upload/information/20220616/112/36718.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登錄</a> </noscript> <script>document.write('<a href="https://cache.yisu.com/upload/information/20220616/112/36718.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登錄</a>');
                </script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多產品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>關于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必讀</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意見反饋</a>&nbsp;京ICP證030173號&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

3、所有FeignClient接口 開啟日志

上面的方法,只能開啟單個FeignClient接口,如果項目里有10個接口,那么要在yml里配置10項,而且以后添加新的FeignClient,還要記得去修改yml配置,太麻煩。

所以,下面是開啟所有FeignClient接口日志的配置:

3.1、修改FeignConfiguration

自定義feign.Logger,如下:

package com.example.demo.cacheDemo;
import feign.slf4j.Slf4jLogger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeignConfiguration {
    @Bean
    public feign.Logger logger() {
        return new Slf4jLogger();
    }
}

3.2、還是修改 application.yml 配置

logging:
  level:
    # 刪除具體的FeignClient接口配置,只保留這一個就好了
    feign.Logger: debug
# 也可以寫代碼代替
# @Bean
# public Logger.Level level() { return Logger.Level.FULL; }
feign:
  client:
    config:
      default:
        loggerLevel: full

2.3、OK了,此時項目里

不管新增多少個 FeignClient,都會輸出日志。

4、重寫FeignClient輸出日志

根據上面輸出的日志,可以看到是多條INFO日志,在并發時,很有可能會互相干擾,而且格式也無法調整。

我們知道,Feign默認情況下,是使用 feign.Client.Default 發起http請求;

我們可以重寫Client,并注入Bean來替換掉 feign.Client.Default,從而實現日志記錄,當然也可以做其它任意事情了,比如添加Header。下面是注入Bean的代碼:

// 默認不注入,如果yml配置里有 logging.level.beinet.cn.demostudy.MyClient 才注入
@Bean
@ConditionalOnProperty("logging.level.beinet.cn.demostudy.MyClient")
MyClient getClient() throws NoSuchAlgorithmException, KeyManagementException {
    // 忽略SSL校驗
    SSLContext ctx = SSLContext.getInstance("SSL");
    X509TrustManager tm = new X509TrustManager() {
        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) {
        }
        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) {
        }
        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    };
    ctx.init(null, new TrustManager[]{tm}, null);
    return new MyClient(ctx.getSocketFactory(), (hostname, sslSession) -> true);
}

下面是重寫的Client完整代碼:

package beinet.cn.demostudy;
import feign.Client;
import feign.Request;
import feign.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StreamUtils;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSocketFactory;
import java.io.*;
import java.util.Collection;
import java.util.Map;
@Slf4j
public class MyClient extends Client.Default {
    public MyClient(SSLSocketFactory socketFactory, HostnameVerifier hostnameVerifier) {
        super(socketFactory, hostnameVerifier);
    }
    @Override
    public Response execute(Request request, Request.Options options) throws IOException {
        StringBuilder sb = new StringBuilder("[log started]\r\n");
        sb.append(request.httpMethod()).append(" ").append(request.url()).append("\r\n");
        CombineHeaders(sb, request.headers()); // 請求Header
        CombineBody(sb, request.body());
        long costTime = -1;
        Exception exception = null;
        BufferingFeignClientResponse response = null;
        long begin = System.currentTimeMillis();
        try {
            response = new BufferingFeignClientResponse(super.execute(request, options));
            costTime = (System.currentTimeMillis() - begin);
        } catch (Exception exp) {
            costTime = (System.currentTimeMillis() - begin);
            exception = exp;
            throw exp;
        } finally {
            sb.append("\r\nResponse cost time(ms): ").append(String.valueOf(costTime));
            if (response != null)
                sb.append("  status: ").append(response.status());
            sb.append("\r\n");
            if (response != null) {
                CombineHeaders(sb, response.headers()); // 響應Header
                sb.append("Body:\r\n").append(response.body()).append("\r\n");
            }
            if (exception != null) {
                sb.append("Exception:\r\n  ").append(exception.getMessage()).append("\r\n");
            }
            sb.append("\r\n[log ended]");
            log.debug(sb.toString());
        }
        Response ret = response.getResponse().toBuilder()
                .body(response.getBody(),
                        response.getResponse().body().length()).build();
        response.close();
        return ret;
    }
    private static void CombineHeaders(StringBuilder sb, Map<String, Collection<String>> headers) {
        if (headers != null && !headers.isEmpty()) {
            sb.append("Headers:\r\n");
            for (Map.Entry<String, Collection<String>> ob : headers.entrySet()) {
                for (String val : ob.getValue()) {
                    sb.append("  ").append(ob.getKey()).append(": ").append(val).append("\r\n");
                }
            }
        }
    }
    private static void CombineBody(StringBuilder sb, byte[] body) {
        if (body == null || body.length <= 0)
            return;
        sb.append("Body:\r\n").append(new String(body)).append("\r\n");
    }
    static final class BufferingFeignClientResponse implements Closeable {
        private Response response;
        private byte[] body;
        private BufferingFeignClientResponse(Response response) {
            this.response = response;
        }
        private Response getResponse() {
            return this.response;
        }
        private int status() {
            return this.response.status();
        }
        private Map<String, Collection<String>> headers() {
            return this.response.headers();
        }
        private String body() throws IOException {
            StringBuilder sb = new StringBuilder();
            try (InputStreamReader reader = new InputStreamReader(getBody())) {
                char[] tmp = new char[1024];
                int len;
                while ((len = reader.read(tmp, 0, tmp.length)) != -1) {
                    sb.append(new String(tmp, 0, len));
                }
            }
            return sb.toString();
        }
        private InputStream getBody() throws IOException {
            if (this.body == null) {
                this.body = StreamUtils.copyToByteArray(this.response.body().asInputStream());
            }
            return new ByteArrayInputStream(this.body);
        }
        @Override
        public void close() {
            this.response.close();
        }
    }
}

輸出日志示例:

2020-10-15 16:48:26.081 DEBUG 15664 --- [           main] beinet.cn.demostudy.MyClient             : [log started]
POST https://www.baidu.com?flg=3
Headers:
  Content-Length: 14
  Content-Type: text/plain;charset=UTF-8
Body:
abcde我是ddd

Response cost time(ms): 207  status: 200
Headers:
  content-length: 2443
  content-type: text/html
  date: Thu, 15 Oct 2020 08:48:27 GMT
  server: bfe
Body:
<!DOCTYPE html>百度的html</html>

5、使用Aspect切面輸出日志

這個不推薦,因為它無法打印出具體的url、header等數據,大家可以參考:

不需要yml配置,直接在項目里添加如下代碼即可:

package com.example.demo.feign;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
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.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
@Aspect
@Component
@Slf4j
public class FeignAspect {
    // 這個也行 @Pointcut("@annotation(org.springframework.web.bind.annotation.PostMapping)")
    // 參考 https://github.com/spring-cloud/spring-cloud-openfeign/issues/322
    @Pointcut("@within(org.springframework.cloud.openfeign.FeignClient)")
    public void feignClientPointcut() {
    }
    @Around("feignClientPointcut()")
    public Object feignAround(ProceedingJoinPoint joinPoint) throws Throwable {
        return logAround(joinPoint);
    }
    private static ObjectMapper mapper = new ObjectMapper();
    private Object logAround(ProceedingJoinPoint point) throws Throwable {
        long beginTime = System.currentTimeMillis();
        Object result = null;
        Exception exception = null;
        try {
            result = point.proceed();
        } catch (Exception exp) {
            exception = exp;
        }
        long time = System.currentTimeMillis() - beginTime;
        saveLog(point, result, exception, time);
        if (exception != null) {
            throw exception;
        }
        return result;
    }
    private static void saveLog(ProceedingJoinPoint joinPoint, Object result, Exception exception, long time) {
        Dto dto = new Dto();
        dto.setCostTime(time);
        try {
            if (exception != null) {
                dto.setExp(exception.toString());
            }
            if (result != null) {
                dto.setResult(serial(result));
            }
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            //請求的 類名、方法名
            String className = joinPoint.getTarget().getClass().getName();
            String signName = signature.getDeclaringTypeName();
            if (!signName.equalsIgnoreCase(className))
                signName += "|" + className;
            dto.setClas(signName);
            String methodName = signature.getName();
            dto.setMethod(methodName);
            //請求的參數
            Object[] args = joinPoint.getArgs();
            if (args != null && args.length > 0) {
                dto.setPara(serial(args));
            }
        } catch (Exception e) {
            dto.setExp(e.toString());
        }
        if (exception != null) {
            log.warn(dto.toString());
        } else {
            log.info(dto.toString());
        }
    }
    private static String serial(Object obj) {
        try {
            return mapper.writeValueAsString(obj);
        } catch (Exception ex) {
            return obj.toString();
        }
    }
    @Data
    private static class Dto {
        /**
         * 調用類名
         */
        private String clas;
        /**
         * 調用方法名
         */
        private String method;
        /**
         * 調用的參數
         */
        private String para;
        /**
         * 方法返回結果
         */
        private String result;
        /**
         * 執行時長,毫秒
         */
        private long costTime;
        /**
         * 備注
         */
        private String remark;
        /**
         * 出現的異常
         */
        private String exp;
    }
}

OK,輸出的日志如下:

2020-10-13 14:24:48.321  INFO 21304 --- [nio-8080-exec-3] com.example.demo.feign.FeignAspect       : FeignAspect.Dto(clas=com.example.demo.feign.FeignDemo|com.sun.proxy.$Proxy72, method=test, para=null, result="<!DOCTYPE html>\r\n<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class=\"bg s_ipt_wr\"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class=\"bg s_btn_wr\"><input type=submit id=su value=百度一下 class=\"bg s_btn\" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新聞</a> <a href=https://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地圖</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>視頻</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>貼吧</a> <noscript> <a href=https://cache.yisu.com/upload/information/20220616/112/36718.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登錄</a> </noscript> <script>document.write('<a href=\"https://cache.yisu.com/upload/information/20220616/112/36718.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === \"\" ? \"?\" : \"&\")+ \"bdorz_come=1\")+ '\" name=\"tj_login\" class=\"lb\">登錄</a>');\r\n                </script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style=\"display: block;\">更多產品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>關于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必讀</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意見反饋</a>&nbsp;京ICP證030173號&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

關于“Feign的請求和響應日志方式是什么”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“Feign的請求和響應日志方式是什么”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

隆尧县| 巫山县| 绥化市| 会泽县| 化德县| 禹州市| 长宁县| 新干县| 怀安县| 城口县| 霍山县| 临汾市| 潞西市| 平和县| 独山县| 孟州市| 会宁县| 都匀市| 城市| 喜德县| 沁源县| 监利县| 游戏| 隆林| 鲜城| 岢岚县| 高阳县| 龙江县| 临漳县| 蒙自县| 白水县| 中方县| 奉化市| 承德县| 屯昌县| 栾川县| 锡林郭勒盟| 临夏市| 宾阳县| 平罗县| 汾阳市|