08.06 springmvc+dubbo+mybatis 接入大眾點評 CAT 監控平臺

springmvc+dubbo+mybatis 接入大眾點評 CAT 監控平臺

CAT(Central Application Tracking)是基於Java開發的實時應用監控平臺,包括實時應用監控,業務監控。關於CAT的具體介紹可移步到CAT官網進行查閱。

CAT平臺的搭建可移步到「搭建大眾點評CAT監控平臺」。

1. 開發環境

Windows Java 8 Maven 3.5 MySQL 5.7 CAT 2.0.0 Dubbo 2.6 Spring 4.3

2. 客戶端配置

客戶端應用程序接入CAT需要在系統的特定路徑中部署client.xml配置文件。Windows系統和Linux系統的部署路徑不一樣,但其內容是一樣的。

2.1 Windows 客戶端配置

如果你的客戶端程序是運行在Windows系統中,例如你的應用程序項目所在的目錄路徑是D:\\application\\workspace\\idea\\springmvc-dubbo-mybatis-with-cat-sample。那麼,你需要在此項目所在的盤符(即這裡的D盤)創建data\\appdatas\\cat目錄,並將client.xml配置文件存放在這個路徑中。如作者的客戶端配置文件D:\\data\\appdatas\\cat\\client.xml:

<config>

<servers>

<server>

<server>

<server>

2.1 Linux 客戶端配置

如果你的客戶端程序是運行在Linux系統中,那麼你需要創建/data/appdatas/cat目錄,並確保運行程序的用戶對此目錄有讀寫權限。然後將client.xml配置文件存放在這個路徑中。配置文件的內容與上同。

3. 配置監控的項目名

在需要接入CAT監控平臺的項目中新建屬性配置文件src/main/resources/META-INF/app.properties。其內容如下:

################## CAT會自動加載此文件 ###################

應用的名稱(可以根據此名稱在CAT的管理控制檯查找對應的信息)

app.name=service-article

4. URL 監控埋點

客戶端程序接入CAT需要依賴cat-client包。由於cat-client沒有加入maven遠程中央倉庫,因此需要指定CAT專用的遠程倉庫。在需要接入CAT監控平臺的web項目的pom.xml中加入如下配置:

<repositories>

<repository>

unidal-nexus-repo

http://unidal.org/nexus/content/repositories/releases

<dependencies>

<dependency>

<groupid>com.dianping.cat/<groupid>

<artifactid>cat-client/<artifactid>

<version>${cat-client.version}/<version>

然後在web項目的web.xml配置文件中加入如下配置即可:

<filter>

<filter-name>cat-filter/<filter-name>

<filter-class>com.dianping.cat.servlet.CatFilter/<filter-class>

<filter-mapping>

<filter-name>cat-filter/<filter-name>

<url-pattern>/*/<url-pattern>

<dispatcher>REQUEST/<dispatcher>

<dispatcher>FORWARD/<dispatcher>

接入後,在Transaction中會生成URL信息。效果圖(縮略圖,可右鍵在新標籤頁打開圖片查看):

springmvc+dubbo+mybatis 接入大眾點評 CAT 監控平臺

5. mybatis 接入

項目地址:https://github.com/fanlychie/cat-client-mybatis

你可以檢出項目手工執行安裝到本地的maven倉庫。或者使用博主託管在github的maven倉庫:

<repositories>

<repository>

fanlychie-maven-repo

https://raw.github.com/fanlychie/maven-repo/releases

<dependencies>

<dependency>

<groupid>com.dianping.cat/<groupid>

<artifactid>cat-client-mybatis/<artifactid>

<version>2.0.0/<version>

接入方式(這裡僅給出 spring 的 xml 配置參考方式):

<bean>

<property>

<property>

<property>

<property>

<property>

<array>

<bean>

接入後,在Transaction中會生成SQL信息。效果圖(縮略圖,可右鍵在新標籤頁打開圖片查看):

springmvc+dubbo+mybatis 接入大眾點評 CAT 監控平臺

6. dubbo 接入 (生產者端)

項目地址:https://github.com/fanlychie/cat-dubbo-monitor

你可以檢出項目手工執行安裝到本地的maven倉庫。或者使用博主託管在github的maven倉庫:

<repositories>

<repository>

fanlychie-maven-repo

https://raw.github.com/fanlychie/maven-repo/releases

<dependencies>

<dependency>

<groupid>net.dubboclub/<groupid>

<artifactid>cat-dubbo-monitor/<artifactid>

<version>0.0.6/<version>

接入方式:只需要聲明依賴包,不需要做任何配置。接入後,在cat中會出現cross報表,dependency,服務端的matrix以及調用鏈路的trace信息。

效果圖(縮略圖,可右鍵在新標籤頁打開圖片查看):

springmvc+dubbo+mybatis 接入大眾點評 CAT 監控平臺

7. dubbo 接入 (web消費者端)

項目地址:https://github.com/fanlychie/cat-client-dubbo

你可以檢出項目手工執行安裝到本地的maven倉庫。或者使用博主託管在github的maven倉庫:

<repositories>

<repository>

fanlychie-maven-repo

https://raw.github.com/fanlychie/maven-repo/releases

<dependencies>

<dependency>

<groupid>org.fanlychie/<groupid>

<artifactid>cat-client-dubbo/<artifactid>

<version>1.0.0/<version>

接入方式為,在web項目的消費者端dubbo配置文件中加入如下配置:

<consumer>

接入後,在Transaction的URL中會生成dubbo調用鏈路的trace信息。

接入前的效果圖(縮略圖,可右鍵在新標籤頁打開圖片查看):

springmvc+dubbo+mybatis 接入大眾點評 CAT 監控平臺

接入後的效果圖(縮略圖,可右鍵在新標籤頁打開圖片查看):

springmvc+dubbo+mybatis 接入大眾點評 CAT 監控平臺

8. log4j 接入

異常日誌信息接入將異常日誌上報到CAT服務器,方便查看異常日誌。

log4j.rootCategory = INFO, ...xxx... , CAT

# 異常日誌上報到CAT

log4j.appender.CAT = com.dianping.cat.log4j.CatAppender

log4j.appender.CAT.Threshold = ERROR

9. 項目啟動報錯問題

當啟動兩個或以上依賴cat-client包的項目的時候,會報出如下錯誤,致使服務無法正常提供服務:

2018-08-05 23:14:01:326 [main] ERROR [Server:102] - [DUBBO] qos-server can not bind localhost:22222, dubbo version: 2.6.0, current host: 127.0.0.1

java.net.BindException: Address already in use: bind

這是dubbo的qos服務端口衝突引起的,其默認使用22222端口。可以在項目的dubbo.properties屬性配置文件中修改此端口:

# 避免端口衝突, 默認端口22222

dubbo.qos.port=20221


分享到:


相關文章: