OCP開源專案:日誌公共元件的實現(log-spring-boot-starter)

Coder程式設計發表於2020-01-13

OCP開源專案:日誌公共元件的實現(log-spring-boot-starter)

前言

企業微服務開放平臺 ,歷經多家公司生產考驗

  • 基於layui前後端分離的企業級微服務架構
  • 相容spring cloud netflix & spring cloud alibaba
  • 優化Spring Security內部實現,實現API呼叫的統一出口和許可權認證授權中心
  • 提供完善的企業微服務流量監控,日誌監控能力
  • 提供完善的壓力測試方案
  • 提供完善的微服務部署方案

專案演示地址

http://59.110.164.254:8066/login.html 使用者名稱/密碼:admin/admin

專案監控地址

http://47.98.236.203:3000 使用者名稱/密碼:admin/1q2w3e4r

入群學習:(備註:Coder程式設計) 群1:483725710(滿2000) 群2:897924507

本章主要是將ocp原來的log-core做一個程式碼優化,採用spring boot enable的方式重構程式碼。

log-spring-boot-starter

  • pom依賴

    image

  • LogServiceImpl 切換資料來源,記錄log表

    image

  • LogServiceImpl 切換資料來源,記錄log表

    image

  • AOP標準日誌格式

    image

  • springboot專案中是否使用過EnableCaching

    image

下面我們們按照springboot的方式 編寫EnableLogging

  • @Import(LogImportSelector.class)

    image

  • LogImportSelector

    image

  • 使用EnableLogging

    image

總結 1.@Import用來匯入一個或多個類(會被spring容器託管),或者配置類(配置類裡面的bean都會被spring容器託管) 2. ImportSelector 該介面的方法的返回值都會被納入到spring容器管理中

@Import

image

  • import可以採用importselecor的返回值加入到spring容器
  • import還可以自定義ImportBeanDefinitionRegistrar介面,通過BeanDefinitionRegistry納入到spring容器

鏈路跟蹤

spring-cloud-sleuth-core

image

閘道器傳遞traceid

image

方法攔截器設定traceid

image

aop處理traceid

image

traceid處理

image

feign處理traceid

image

RestTemplate處理traceid

image

api-gateway

image

auth-server

image

grep -rn

image

HP@owen /cygdrive/d/open-capacity-platform
$ grep -rn a26003208271fd94 *
logs/auth-gateway/auth-gateway-info.log:740:[auth-gateway:130.75.131.208:9200] [a26003208271fd94] [a26003208271fd94] 2019-09-06 09:38:33.898 INFO 6364 [XNIO-2 task-3] com.open.capacity.client.filter.ResponseFilter request url = http://127.0.0.1:9200/api-auth/validata/code/1263967C-FED7-4732-86D7-8CC6BDF752F5, traceId = a26003208271fd94
logs/auth-gateway/auth-gateway-info.log:741:[auth-gateway:130.75.131.208:9200] [a26003208271fd94] [a26003208271fd94] 2019-09-06 09:38:36.703 INFO 6364 [XNIO-2 task-3] com.open.capacity.client.filter.ResponseFilter response url http://127.0.0.1:9200/api-auth/validata/code/1263967C-FED7-4732-86D7-8CC6BDF752F5, traceId = a26003208271fd94
oauth-center/logs/auth-server/auth-server-info.log:722:[auth-server:130.75.131.208:8000] [a26003208271fd94] [b26ed6e6877dd47a] 2019-09-06 09:38:34.795 INFO 15168 [http-nio-8000-exec-1] com.open.capacity.log.aop.LogAnnotationAOP 開始請求,transid=a26003208271fd94,  url=com.open.capacity.uaa.server.controller.ValidateCodeController/createCode , httpMethod=null, reqData=["1263967C-FED7-4732-86D7-8CC6BDF752F5"]
oauth-center/logs/auth-server/auth-server-info.log:723:[auth-server:130.75.131.208:8000] [a26003208271fd94] [b26ed6e6877dd47a] 2019-09-06 09:38:36.671 INFO 15168 [http-nio-8000-exec-1] com.open.capacity.log.aop.LogAnnotationAOP 請求完成, transid=a26003208271fd94, 耗時=1898, resp=null:

複製程式碼

image

CompletableFuture.runAsync 與鏈路跟蹤

image

[test-log-center:130.75.131.208:8080] [,d1d8bf482db0af10] 2019-09-16 13:40:25.181 INFO 18088 [http-nio-8080-exec-10] com.open.capacity.log.test.controller.TestController ok
[test-log-center:130.75.131.208:8080] [,d1d8bf482db0af10] 2019-09-16 13:40:25.220 INFO 18088 [pool-1-thread-1] com.open.capacity.log.test.controller.TestController oook

複製程式碼
  • 非同步傳遞MDC

    image

  • CompletableFuture 繫結自定義TaskExecutor

    image

X-B3-TraceId生產的源頭

image

image

image

image

image

CompletableFuture.runAsync 與事物問題

image

需要下沉到另外一個類

image

文末

歡迎關注個人微信公眾號:Coder程式設計 歡迎關注Coder程式設計公眾號,主要分享資料結構與演算法、Java相關知識體系、框架知識及原理、Spring全家桶、微服務專案實戰、DevOps實踐之路、每日一篇網際網路大廠面試或筆試題以及PMP專案管理知識等。更多精彩內容正在路上~ 也分享一些雜文~

文章收錄至 Github: github.com/CoderMerlin… Gitee: gitee.com/573059382/c… 歡迎關注並star~

微信公眾號

相關文章