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

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

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

1. 開發環境github

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

2. 客戶端配置spring

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

2.1 Windows 客戶端配置服務器

若是你的客戶端程序是運行在Windows系統中,例如你的應用程序項目所在的目錄路徑是D:applicationworkspaceideaspringmvc-dubbo-mybatis-with-cat-sample。那麼,你須要在此項目所在的盤符(即這裏的D盤)建立dataappdatascat目錄,並將client.xml配置文件存放在這個路徑中。如做者的客戶端配置文件D:dataappdatascatclient.xml:mybatis

<?xml version="1.0" encoding="utf-8"?>mvc

<config mode="client" xmlns:xsi="http://www.w3.org/2001/XMLSchema" xsi:noNamespaceSchemaLocation="config.xsd">app

<servers>

<server ip="10.10.10.121" port="2280" http-port="8080" />

<server ip="10.10.10.122" port="2280" http-port="8080" />

<server ip="10.10.10.123" port="2280" http-port="8080" />

</servers>

</config>

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>

<!-- CAT client 倉庫 -->

<repository>

<id>unidal-nexus-repo</id>

<url>http://unidal.org/nexus/content/repositories/releases</url>

</repository>

</repositories>

<dependencies>

<!-- 客戶端接入CAT的依賴 -->

<dependency>

<groupId>com.dianping.cat</groupId>

<artifactId>cat-client</artifactId>

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

</dependency>

</dependencies>

而後在web項目的web.xml配置文件中加入以下配置便可:

<filter>

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

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

</filter>

<filter-mapping>

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

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

<dispatcher>REQUEST</dispatcher>

<dispatcher>FORWARD</dispatcher>

</filter-mapping>

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

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


5. mybatis 接入

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

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

<!-- CAT mybatis和dubbo 倉庫 -->

<repositories>

<repository>

<id>fanlychie-maven-repo</id>

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

</repository>

</repositories>

<dependencies>

<!-- mybatis接入CAT的依賴 -->

<dependency>

<groupId>com.dianping.cat</groupId>

<artifactId>cat-client-mybatis</artifactId>

<version>2.0.0</version>

</dependency>

</dependencies>

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

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource"/>

<property name="typeAliasesPackage" value="org.fanlychie.entity"/>

<property name="configLocation" value="classpath:mybatis-config.xml"/>

<property name="mapperLocations" value="classpath*:mapper/*.xml"/>

<!-- MyBatis 接入 CAT -->

<property name="plugins">

<array>

<bean class="com.wanda.cat.sample.plugins.CatMybatisPlugin"></bean>

</array>

</property>

</bean>

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

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


6. dubbo 接入 (生產者端)

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

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

<!-- CAT mybatis和dubbo 倉庫 -->

<repositories>

<repository>

<id>fanlychie-maven-repo</id>

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

</repository>

</repositories>

<dependencies>

<!-- dubbo接入CAT的依賴 -->

<dependency>

<groupId>net.dubboclub</groupId>

<artifactId>cat-dubbo-monitor</artifactId>

<version>0.0.6</version>

</dependency>

</dependencies>

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

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

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


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

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

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

<!-- CAT mybatis和dubbo 倉庫 -->

<repositories>

<repository>

<id>fanlychie-maven-repo</id>

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

</repository>

</repositories>

<dependencies>

<!-- 客戶端dubbo接入CAT -->

<dependency>

<groupId>org.fanlychie</groupId>

<artifactId>cat-client-dubbo</artifactId>

<version>1.0.0</version>

</dependency>

</dependencies>

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

<dubbo:consumer filter="CatClientFilter"/>

接入後,在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

相關文章
相關標籤/搜索