#243 init
This commit is contained in:
@@ -209,16 +209,16 @@
|
||||
</dependency>
|
||||
|
||||
<!-- MONITOR begin -->
|
||||
<!-- servo -->
|
||||
<dependency>
|
||||
<groupId>com.netflix.servo</groupId>
|
||||
<artifactId>servo-core</artifactId>
|
||||
<!-- codahale metrics -->
|
||||
<dependency>
|
||||
<groupId>com.codahale.metrics</groupId>
|
||||
<artifactId>metrics-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.netflix.servo</groupId>
|
||||
<artifactId>servo-graphite</artifactId>
|
||||
<groupId>com.codahale.metrics</groupId>
|
||||
<artifactId>metrics-graphite</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- javasimon -->
|
||||
<dependency>
|
||||
<groupId>org.javasimon</groupId>
|
||||
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package org.springside.examples.showcase.demos.metrics;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import com.codahale.metrics.JmxReporter;
|
||||
import com.codahale.metrics.MetricRegistry;
|
||||
import com.codahale.metrics.graphite.Graphite;
|
||||
import com.codahale.metrics.graphite.GraphiteReporter;
|
||||
|
||||
public class MetricsReporterManager {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(MetricsReporterManager.class);
|
||||
|
||||
@Autowired
|
||||
private MetricRegistry metricRegistry;
|
||||
|
||||
private GraphiteReporter graphiteReporter;
|
||||
private JmxReporter jmxReporter;
|
||||
|
||||
@PostConstruct
|
||||
public void start() {
|
||||
Graphite graphite = new Graphite(new InetSocketAddress("localhost", 2003));
|
||||
graphiteReporter = GraphiteReporter.forRegistry(metricRegistry).prefixedWith("metrics").build(graphite);
|
||||
graphiteReporter.start(30, TimeUnit.SECONDS);
|
||||
|
||||
jmxReporter = JmxReporter.forRegistry(metricRegistry).build();
|
||||
jmxReporter.start();
|
||||
|
||||
logger.info("Started Metrics Reporter");
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void stop() {
|
||||
graphiteReporter.stop();
|
||||
jmxReporter.stop();
|
||||
|
||||
logger.info("Stopped Metrics Reporter");
|
||||
}
|
||||
}
|
||||
+30
-10
@@ -1,5 +1,7 @@
|
||||
package org.springside.examples.showcase.webservice.rest;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -13,6 +15,9 @@ import org.springside.examples.showcase.entity.User;
|
||||
import org.springside.examples.showcase.service.AccountEffectiveService;
|
||||
import org.springside.modules.mapper.BeanMapper;
|
||||
|
||||
import com.codahale.metrics.MetricRegistry;
|
||||
import com.codahale.metrics.Timer;
|
||||
|
||||
/**
|
||||
* Shiro的配置文件中对/api/secure/**进行拦截,要求authBasic认证.
|
||||
*
|
||||
@@ -26,6 +31,16 @@ public class UserRestController {
|
||||
@Autowired
|
||||
private AccountEffectiveService accountService;
|
||||
|
||||
@Autowired
|
||||
private MetricRegistry metricRegistry;
|
||||
|
||||
private Timer metrics;
|
||||
|
||||
@PostConstruct
|
||||
public void register() {
|
||||
metrics = metricRegistry.timer("Rest_GetUser");
|
||||
}
|
||||
|
||||
/**
|
||||
* 基于ContentNegotiationManager,根据URL的后缀渲染不同的格式
|
||||
* eg. /api/v1/user/1.xml 返回xml
|
||||
@@ -35,17 +50,22 @@ public class UserRestController {
|
||||
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public UserDTO getUser(@PathVariable("id") Long id) {
|
||||
User user = accountService.getUser(id);
|
||||
final Timer.Context metricsContext = metrics.time();
|
||||
try {
|
||||
User user = accountService.getUser(id);
|
||||
|
||||
if (user == null) {
|
||||
String message = "用户不存在(id:" + id + ")";
|
||||
logger.warn(message);
|
||||
throw new RestException(HttpStatus.NOT_FOUND, message);
|
||||
if (user == null) {
|
||||
String message = "用户不存在(id:" + id + ")";
|
||||
logger.warn(message);
|
||||
throw new RestException(HttpStatus.NOT_FOUND, message);
|
||||
}
|
||||
|
||||
// 使用Dozer转换DTO类,并补充Dozer不能自动绑定的属性
|
||||
UserDTO dto = BeanMapper.map(user, UserDTO.class);
|
||||
dto.setTeamId(user.getTeam().getId());
|
||||
return dto;
|
||||
} finally {
|
||||
metricsContext.stop();
|
||||
}
|
||||
|
||||
// 使用Dozer转换DTO类,并补充Dozer不能自动绑定的属性
|
||||
UserDTO dto = BeanMapper.map(user, UserDTO.class);
|
||||
dto.setTeamId(user.getTeam().getId());
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,17 +8,18 @@
|
||||
<import resource="security/applicationContext-shiro.xml" />
|
||||
|
||||
<!-- optional -->
|
||||
<import resource="cache/applicationContext-ehcache.xml" />
|
||||
<!-- <import resource="cache/applicationContext-ehcache.xml" />
|
||||
<import resource="cache/applicationContext-memcached.xml" />
|
||||
<import resource="email/applicationContext-email.xml" />
|
||||
<import resource="jms/applicationContext-jms-simple.xml" />
|
||||
<import resource="jms/applicationContext-jms-advanced.xml" />
|
||||
<import resource="jmx/applicationContext-jmx.xml" />
|
||||
<import resource="monitor/applicationContext-monitor.xml" />
|
||||
<import resource="jmx/applicationContext-jmx.xml" />-->
|
||||
<import resource="monitor/applicationContext-metrics.xml" />
|
||||
<!-- <import resource="monitor/applicationContext-profiler.xml" />
|
||||
<import resource="schedule/applicationContext-jdk-timer.xml" />
|
||||
<import resource="schedule/applicationContext-spring-scheduler.xml" />
|
||||
<import resource="schedule/applicationContext-quartz-cron-local.xml" />
|
||||
<import resource="schedule/applicationContext-quartz-timer-cluster.xml" />
|
||||
<import resource="webservice/applicationContext-soap-server.xml" />
|
||||
<import resource="webservice/applicationContext-soap-server.xml" /> -->
|
||||
<import resource="webservice/applicationContext-jaxrs-server.xml" />
|
||||
</beans>
|
||||
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
|
||||
|
||||
<description>Yammer Coda Hale's Metrics监控配置</description>
|
||||
|
||||
<bean id="metrics" class="com.codahale.metrics.MetricRegistry "/>
|
||||
|
||||
<bean id="graphiteReporterManager" class="org.springside.examples.showcase.demos.metrics.MetricsReporterManager"/>
|
||||
|
||||
</beans>
|
||||
+2
-2
@@ -10,7 +10,7 @@
|
||||
<!-- 调用StopWatch计时的Interceptor -->
|
||||
<bean id="monitoringInterceptor" class="org.javasimon.spring.MonitoringInterceptor" />
|
||||
|
||||
<!-- 监控定义了@Monitored的方法 -->
|
||||
<!-- way1.监控定义了@Monitored的方法 -->
|
||||
<bean id="monitoringAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
|
||||
<property name="advice" ref="monitoringInterceptor" />
|
||||
<property name="pointcut">
|
||||
@@ -18,7 +18,7 @@
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<!-- 额外不修改代码临时增加监听 -->
|
||||
<!-- way2.额外不修改代码临时增加监听 -->
|
||||
<aop:config>
|
||||
<aop:advisor advice-ref="monitoringInterceptor" pointcut="execution(public * org.springside..NotifyMessageProducer.*(..))
|
||||
|| execution(public * org.springside..BusinessLogger.*(..))" />
|
||||
+13
-13
@@ -27,6 +27,8 @@
|
||||
<hibernate-validator.version>4.3.1.Final</hibernate-validator.version>
|
||||
<jolokia.version>1.1.5</jolokia.version>
|
||||
<hystrix.version>1.3.8</hystrix.version>
|
||||
<metrics.version>3.0.1</metrics.version>
|
||||
<javasimon.version>3.4.0</javasimon.version>
|
||||
<jackson.version>2.3.0</jackson.version>
|
||||
<slf4j.version>1.7.5</slf4j.version>
|
||||
<logback.version>1.0.13</logback.version>
|
||||
@@ -37,8 +39,6 @@
|
||||
<dozer.version>5.4.0</dozer.version>
|
||||
<httpclient.version>4.3.1</httpclient.version>
|
||||
<freemarker.version>2.3.20</freemarker.version>
|
||||
<servo.version>0.4.46</servo.version>
|
||||
<javasimon.version>3.4.0</javasimon.version>
|
||||
<aspectj.version>1.7.4</aspectj.version>
|
||||
<junit.version>4.11</junit.version>
|
||||
<mockito.version>1.9.5</mockito.version>
|
||||
@@ -421,17 +421,17 @@
|
||||
<!-- hystrix end -->
|
||||
|
||||
<!-- Monitor begin -->
|
||||
<!-- servo -->
|
||||
<dependency>
|
||||
<groupId>com.netflix.servo</groupId>
|
||||
<artifactId>servo-core</artifactId>
|
||||
<version>${servo.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.netflix.servo</groupId>
|
||||
<artifactId>servo-graphite</artifactId>
|
||||
<version>${servo.version}</version>
|
||||
</dependency>
|
||||
<!-- codahale metrics -->
|
||||
<dependency>
|
||||
<groupId>com.codahale.metrics</groupId>
|
||||
<artifactId>metrics-core</artifactId>
|
||||
<version>${metrics.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.codahale.metrics</groupId>
|
||||
<artifactId>metrics-graphite</artifactId>
|
||||
<version>${metrics.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- javasimon -->
|
||||
<dependency>
|
||||
|
||||
Reference in New Issue
Block a user