您好,登錄后才能下訂單哦!
本篇內容介紹了“Spring Cloud Sleuth整合Zipkin的方法”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
Sleuth是一個Spring Cloud的分布式跟蹤解決方案。它由以下幾個方面組成
Span(跨度):Sleuth的基本工作單元,它由一個64位的id唯一標識。除ID外,span還包含其他數據,例如描述、時間戳事件、鍵值對的注解(標簽)、span ID,span父ID等。
trace(跟蹤):一組span組成的樹狀結構稱為trace.
Annotation(標注)
CS(Client Sent客戶端發送) 客戶端發起一個請求,該annotation描述了span的開始。
SR(Server Received服務端接收) 服務端獲得請求,并準備處理它。
SS(Server Sent服務器端發送) 該annotation表明完成請求處理,當響應發回客戶端時。
CR(Client Received客戶端接收) span結束的標識。客戶端成功接收到服務端到相應。
現在我們來具體看一下Sleuth的作用是什么,當我們沒有加Sleuth依賴的時候,假設我們要訪問這樣一個接口
@GetMapping(value = "/users-anon/internal", params = "username")public LoginAppUser findByUsername(String username) {return appUserService.findByUsername(username);}
在日志中,我們可以看到是這個樣子的
2019-11-01 00:17:05.402 DEBUG 805 --- [nio-8001-exec-1] c.c.u.d.U.findUserByUsername : ==> Preparing: select u.* from app_user u inner join user_credentials c on c.userId = u.id where c.username = ?
2019-11-01 00:17:05.426 DEBUG 805 --- [nio-8001-exec-1] c.c.u.d.U.findUserByUsername : ==> Parameters: admin(String)
2019-11-01 00:17:05.503 DEBUG 805 --- [nio-8001-exec-1] c.c.u.d.U.findUserByUsername : <== Total: 1
2019-11-01 00:17:05.522 INFO 805 --- [nio-8001-exec-1] c.c.u.service.impl.AppUserServiceImpl : {"createTime":1516179419000,"enabled":true,"headImgUrl":"","id":1,"nickname":"測試1","password":"$2a$10$3uOoX1ps14CxuotogUoDreW8zXJOZB9XeGdrC/xDV36hhaE8Rn9HO","phone":"","province":"廣東","receiveAddress":"廣州天河珠村東橫五路紅噗公寓367","sex":1,"type":"APP","updateTime":1516179421000,"userIdentity":"SERVICEOPERATOR","username":"admin"}
2019-11-01 00:17:05.528 DEBUG 805 --- [nio-8001-exec-1] c.c.u.dao.UserRoleDao.findRolesByUserId : ==> Preparing: select r.* from sys_role_user ru inner join sys_role r on r.id = ru.roleId where ru.userId = ?
2019-11-01 00:17:05.529 DEBUG 805 --- [nio-8001-exec-1] c.c.u.dao.UserRoleDao.findRolesByUserId : ==> Parameters: 1(Long)
2019-11-01 00:17:05.584 DEBUG 805 --- [nio-8001-exec-1] c.c.u.dao.UserRoleDao.findRolesByUserId : <== Total: 1
2019-11-01 00:17:05.605 DEBUG 805 --- [nio-8001-exec-1] c.c.u.d.R.findPermissionsByRoleIds : ==> Preparing: select p.* from sys_permission p inner join sys_role_permission rp on p.id = rp.permissionId WHERE rp.roleId in ( ? )
2019-11-01 00:17:05.606 DEBUG 805 --- [nio-8001-exec-1] c.c.u.d.R.findPermissionsByRoleIds : ==> Parameters: 1(Long)
2019-11-01 00:17:06.245 DEBUG 805 --- [nio-8001-exec-1] c.c.u.d.R.findPermissionsByRoleIds : <== Total: 31
除了mybatis的sql代碼外,就只能看到類似nio-8001-exec-1這樣的線程名了。現在我們加入依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId></dependency>
重啟服務,我們再來看一下日志
2019-11-01 00:23:01.469 DEBUG [user-center,d406e2f823a1c2a3,d406e2f823a1c2a3,false] 816 --- [nio-8001-exec-1] c.c.u.d.U.findUserByUsername : ==> Preparing: select u.* from app_user u inner join user_credentials c on c.userId = u.id where c.username = ?
2019-11-01 00:23:01.487 DEBUG [user-center,d406e2f823a1c2a3,d406e2f823a1c2a3,false] 816 --- [nio-8001-exec-1] c.c.u.d.U.findUserByUsername : ==> Parameters: admin(String)
2019-11-01 00:23:01.649 DEBUG [user-center,d406e2f823a1c2a3,d406e2f823a1c2a3,false] 816 --- [nio-8001-exec-1] c.c.u.d.U.findUserByUsername : <== Total: 1
2019-11-01 00:23:01.671 INFO [user-center,d406e2f823a1c2a3,d406e2f823a1c2a3,false] 816 --- [nio-8001-exec-1] c.c.u.service.impl.AppUserServiceImpl : {"createTime":1516179419000,"enabled":true,"headImgUrl":"","id":1,"nickname":"測試1","password":"$2a$10$3uOoX1ps14CxuotogUoDreW8zXJOZB9XeGdrC/xDV36hhaE8Rn9HO","phone":"","province":"廣東","receiveAddress":"廣州天河珠村東橫五路紅噗公寓367","sex":1,"type":"APP","updateTime":1516179421000,"userIdentity":"SERVICEOPERATOR","username":"admin"}
2019-11-01 00:23:01.679 DEBUG [user-center,d406e2f823a1c2a3,d406e2f823a1c2a3,false] 816 --- [nio-8001-exec-1] c.c.u.dao.UserRoleDao.findRolesByUserId : ==> Preparing: select r.* from sys_role_user ru inner join sys_role r on r.id = ru.roleId where ru.userId = ?
2019-11-01 00:23:01.679 DEBUG [user-center,d406e2f823a1c2a3,d406e2f823a1c2a3,false] 816 --- [nio-8001-exec-1] c.c.u.dao.UserRoleDao.findRolesByUserId : ==> Parameters: 1(Long)
2019-11-01 00:23:01.834 DEBUG [user-center,d406e2f823a1c2a3,d406e2f823a1c2a3,false] 816 --- [nio-8001-exec-1] c.c.u.dao.UserRoleDao.findRolesByUserId : <== Total: 1
2019-11-01 00:23:01.852 DEBUG [user-center,d406e2f823a1c2a3,d406e2f823a1c2a3,false] 816 --- [nio-8001-exec-1] c.c.u.d.R.findPermissionsByRoleIds : ==> Preparing: select p.* from sys_permission p inner join sys_role_permission rp on p.id = rp.permissionId WHERE rp.roleId in ( ? )
2019-11-01 00:23:01.852 DEBUG [user-center,d406e2f823a1c2a3,d406e2f823a1c2a3,false] 816 --- [nio-8001-exec-1] c.c.u.d.R.findPermissionsByRoleIds : ==> Parameters: 1(Long)
2019-11-01 00:23:01.980 DEBUG [user-center,d406e2f823a1c2a3,d406e2f823a1c2a3,false] 816 --- [nio-8001-exec-1] c.c.u.d.R.findPermissionsByRoleIds : <== Total: 31
現在我們可以看到類似于user-center,d406e2f823a1c2a3,d406e2f823a1c2a3,false的東西。
user-center很好理解,就是我們的項目名稱。
第一個d406e2f823a1c2a3為traceID
第二個d406e2f823a1c2a3為spanID
false表示是否將此數據上傳給zipkin
當然要打印這些日志,我們需要在配置文件中加入如下配置信息
logging: level: root: infocom.cloud: debug file: logs/${spring.application.name}.log
現在我們來看一下zipkin。
Zipkin是Twitter開源的分布式跟蹤系統,主要用來收集系統的時序數據,從而跟蹤系統的調用問題。
要使用zipkin我們需要先下載zipkin server,下載方式為
curl -sSL https://zipkin.io/quickstart.sh | bash -sx --
然后啟動它
java -jar zipkin.jar
當然我們也可以使用docker,建議在服務器上使用
docker pull openzipkin/zipkin
docker run -d --name zipkin -p 9411:9411 openzipkin/zipkin
啟動之后,我們可以訪問zipkin的UI界面
http://127.0.0.1:9411/zipkin/
在我們的項目中添加依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId></dependency>
這里zipkin的依賴已經包含了sleuth的依賴,所以我們可以去掉了之前sleuth的依賴。
添加配置
spring: zipkin: #。 在服務器上填入服務器的IP base-url: http://localhost:9411 enabled: truesender: type: web sleuth: sampler:# 抽樣率,默認是0.1(10%),現在取100%,只為測試方便,生產環境勿設100% probability: 1.0
現在我們重新啟動項目,進行一次請求訪問,日志為
2019-11-01 01:04:03.187 DEBUG [user-center,d7fa432636d9df52,d7fa432636d9df52,true] 956 --- [nio-8001-exec-1] c.c.u.d.U.findUserByUsername : ==> Preparing: select u.* from app_user u inner join user_credentials c on c.userId = u.id where c.username = ?
2019-11-01 01:04:03.203 DEBUG [user-center,d7fa432636d9df52,d7fa432636d9df52,true] 956 --- [nio-8001-exec-1] c.c.u.d.U.findUserByUsername : ==> Parameters: admin(String)
2019-11-01 01:04:03.276 DEBUG [user-center,d7fa432636d9df52,d7fa432636d9df52,true] 956 --- [nio-8001-exec-1] c.c.u.d.U.findUserByUsername : <== Total: 1
2019-11-01 01:04:03.290 INFO [user-center,d7fa432636d9df52,d7fa432636d9df52,true] 956 --- [nio-8001-exec-1] c.c.u.service.impl.AppUserServiceImpl : {"createTime":1516179419000,"enabled":true,"headImgUrl":"","id":1,"nickname":"測試1","password":"$2a$10$3uOoX1ps14CxuotogUoDreW8zXJOZB9XeGdrC/xDV36hhaE8Rn9HO","phone":"","province":"廣東","receiveAddress":"廣州天河珠村東橫五路紅噗公寓367","sex":1,"type":"APP","updateTime":1516179421000,"userIdentity":"SERVICEOPERATOR","username":"admin"}
2019-11-01 01:04:03.294 DEBUG [user-center,d7fa432636d9df52,d7fa432636d9df52,true] 956 --- [nio-8001-exec-1] c.c.u.dao.UserRoleDao.findRolesByUserId : ==> Preparing: select r.* from sys_role_user ru inner join sys_role r on r.id = ru.roleId where ru.userId = ?
2019-11-01 01:04:03.294 DEBUG [user-center,d7fa432636d9df52,d7fa432636d9df52,true] 956 --- [nio-8001-exec-1] c.c.u.dao.UserRoleDao.findRolesByUserId : ==> Parameters: 1(Long)
2019-11-01 01:04:03.347 DEBUG [user-center,d7fa432636d9df52,d7fa432636d9df52,true] 956 --- [nio-8001-exec-1] c.c.u.dao.UserRoleDao.findRolesByUserId : <== Total: 1
2019-11-01 01:04:03.367 DEBUG [user-center,d7fa432636d9df52,d7fa432636d9df52,true] 956 --- [nio-8001-exec-1] c.c.u.d.R.findPermissionsByRoleIds : ==> Preparing: select p.* from sys_permission p inner join sys_role_permission rp on p.id = rp.permissionId WHERE rp.roleId in ( ? )
2019-11-01 01:04:03.368 DEBUG [user-center,d7fa432636d9df52,d7fa432636d9df52,true] 956 --- [nio-8001-exec-1] c.c.u.d.R.findPermissionsByRoleIds : ==> Parameters: 1(Long)
2019-11-01 01:04:03.433 DEBUG [user-center,d7fa432636d9df52,d7fa432636d9df52,true] 956 --- [nio-8001-exec-1] c.c.u.d.R.findPermissionsByRoleIds : <== Total: 31
這里我們可以看到所有的user-center,d7fa432636d9df52,d7fa432636d9df52,true都已經為true了,表示全部提交給zipkin了。
我們多刷新訪問幾次,來看一下zipkin的結果記錄
在頁面中點查找,結果如下
由于我們這個接口在數據庫中進行了3次查詢,并且我這里連接的是云端數據庫,所以時間會比較長一點,但無論如何,這里都會記錄下每次調用的時間,它是按降序排序的,這樣我們就可以很輕松的插看到每次調用的情況,知道調用訪問哪里耗時。我們點擊某一個進去,可以看到
再點擊如上的service,可以看到更加詳細的信息
這里只有SR,SC的兩條數據,因為我們是使用瀏覽器訪問的,而瀏覽器并沒有集成zipkin,它不會上報自己的數據,所以這里沒有CS,CR的數據。如果我們在多個微服務之間都配置了zipkin,此處就會把所有的SR,SC,CS,CR的數據都上報上來。
zipkin數據持久化
一般我們會使用elasticsearch來做zipkin的數據持久化。
elasticsearch的docker安裝可以參考elasticsearch中文分詞器的安裝和體驗
安裝好elasticsearch后,我們可以用如下命令啟動zipkin服務端
STORAGE_TYPE=elasticsearch ES_HOSTS=192.168.1.219:9200 java -jar zipkin.jar
如果使用docker,啟動方式如下
docker run -d --name zipkin -p 9411:9411 -e STORAGE_TYPE=elasticsearch -e ES_HOSTS=192.168.1.219:9200 openzipkin/zipkin
“Spring Cloud Sleuth整合Zipkin的方法”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。