在大型企業應用開發中,一個項目經常需要依賴於多個項目集成,經常某個集成服務的升級或者不工作,會導致你所工作的服務也掛掉,甚至影響你的開發流程。你是否還在接到測試團隊或者運維團隊的某個Bug,而自己花費了大量時間終於查出來是某個集成服務升級或異常,在這裏浪費了大量時間,在筆者爲所在項目建立了一個第三方集成服務監控的Monitor,去實時監控項目所依賴的所有集成服務,數據庫。現在開源在github https://github.com/greengerong/green-monitor,在其sample目錄下有個使用demo。
maven dependency
<dependency>
<groupId>com.github.greengerong</groupId>
<artifactId>green.monitor</artifactId>
<version>1.2</version>
</dependency>
demo效果如下:
建立自己的monitor:
1:首先在你spring mvc web project 的 pom文件中引 入green-monitor的dependency。(spring 3.0以上)
2:在spring mvc的 ioc context config中啓用annotation dirver,如下xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<mvc:annotation-driven/><context:component-scan base-package="green.monitor"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix"><value>/WEB-INF/pages/</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean>
</beans>
3:在項目resources(mian/resources)下建立monitor-config.xml。如果需要 在不同環境配置不同信息,可以在運行機器上加入key爲appenv的環境變量,程序會根據不同agent加載monitor-config.[appenv].xml配置文件.或者利用mavn,gradle這類構建工具按環境輸出配置信息。
配置文件形如:
<?xml version="1.0" encoding="UTF-8"?>
<monitoring version="1.0" name="monitor-sample">
<monitors>
<monitor name="mock-monitor">green.monitor.demo.MockMonitorRunner</monitor>
</monitors>
<items>
<item monitor="http-connection" name="hello service">
<params>
<param name="url">http://localhost:8080/demo/hello</param>
<param name="method">GET</param><param name="response-code">200</param>
<param name="param">name=success</param>
</params>
<description>This is a monitor for hello service.should be success.</description>
</item>
<item monitor="http-connection" name="error service 2">
<params>
<param name="url">http://localhost:8080/demo/failed</param>
<param name="method">GET</param>
<param name="response-code">200</param>
<param name="param">name=must be failed</param>
</params>
<description>This is a monitor for error service.should be failed.</description>
</item>
<item monitor="mock-monitor" name="Random failed Service">
<description>This monitor will be random failed!</description>
</item>
</items>
</monitoring>
這樣你就可以運行monitor了,url爲host + “/monitor”
擴展
1:擴展runner 同時monitor爲你提供了自我特定需求擴展的機會,在xml config中你應該主意到了有個monitor的配置節,這裏就可以配置你自定義runner(其實現MonitorRunner接口),配置節name則作爲後邊item的引用name。
系統默認加入了web-service,dababase,http-connection3個常用runner。具體使用請看demo。
2:系統提供了service爲基於spring restfull api,所以你可以在其他地方展示該monitor狀況。(在下一個版本將提供jsonp的跨域處理)。
monitor project repo: https://github.com/greengerong/green-monitor