`

CXF REST 访问统计

 
阅读更多

作为独立的 REST 应用服务器,需要统计每个接口被调用的次数,消耗时间,成功与失败结果等数据,用第三方的统计工具无法满足数据和性能,所以用 cxf 的拦截器实现了一个。

 

进入InInterceptor

import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.springframework.stereotype.Component;

/**
 * 性能分析拦截器
 * 
 * @author michael
 * 
 */
@Component
public class ProfilingInInterceptor extends AbstractPhaseInterceptor<Message> {

	public ProfilingInInterceptor() {
		super(Phase.RECEIVE);
	}

	public void handleMessage(Message message) throws Fault {
		message.getExchange().put("TRACKER_START_TIME", System.currentTimeMillis());
	}

}

 

输出:OutInterceptor

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.jaxrs.model.OperationResourceInfo;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.plusrun.rest.ResultBase;
import com.plusrun.profiling.service.impl.ProfilerManagerImpl;
/**
 * 性能分析拦截器
 * 
 * @author michael
 * 
 */
@Component
public class ProfilingOutInterceptor extends AbstractPhaseInterceptor<Message> {

	@Autowired
	@Qualifier("profilerManager")
	ProfilerManagerImpl profilerManager;

	public ProfilingOutInterceptor() {
		super(Phase.SEND);
	}

	public void handleMessage(Message message) throws Fault {
		Object sTime = message.getExchange().get("TRACKER_START_TIME");
		long startTime = 0;
		if (sTime != null && (Long) sTime >= 0) {
			startTime = (Long) sTime;
		}

		// String method
		OperationResourceInfo ori = message.getExchange().get(OperationResourceInfo.class);
		if (ori != null) {
			Method m = ori.getMethodToInvoke();
			String httpStatus = message.get("org.apache.cxf.message.Message.RESPONSE_CODE").toString();
			String serviceName = m.toString().substring(m.toString().lastIndexOf(" "));
			String methodName = m.getName();
			String httpMethod = ori.getHttpMethod();
			String path = ori.getClassResourceInfo().getPath().value();
			int returnValue = 0;
			Object clazz = m.getReturnType();
			if (clazz != null && clazz instanceof ResultBase) {
				returnValue = ((ResultBase) clazz).getRet();
			}
			for (Annotation an : m.getDeclaredAnnotations()) {
				String fullPath = an.toString();
				if (fullPath.startsWith("@javax.ws.rs.Path")) {
					path = path + fullPath.substring(fullPath.indexOf("=")+1, fullPath.length() - 1);
					break;
				}
			}
			profilerManager.access(path, serviceName, methodName, httpMethod, httpStatus, returnValue, System.currentTimeMillis() - startTime);
		}
	}

}

 

性能服务:ProfilerManagerImpl

import org.springframework.stereotype.Service;

@Service("profilerManager")
public class ProfilerManagerImpl {

	AccessMBean accessBean =new AccessMBean();
	
	public void access(String path, String service, String method, String httpMethod, String httpStatus, int logicReturn, long elapsedTime) {
		accessBean.addAccess(path, service, method, httpMethod, httpStatus, logicReturn, elapsedTime);
	}
	
	public AccessMBean getAccessStatus(){
		return accessBean;
	}
	
	public void cleanAccessStatus(){
		accessBean =new AccessMBean();
	}
}

 

访问MBean:AccessMBean

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.NONE)
@XmlType(namespace = "com.plusrun.profiling", name = "AccessMBean")
@XmlRootElement(namespace = "com.plusrun.profiling")
public class AccessMBean {

	private long elapsedTime = 0;
	private long accessCount = 0;
	private long httpErrorCount = 0;
	private long logicFaultCount = 0;
	private Date startDate = new Date();
	private Map<String, AccessAccumulator> accumulatorMap = new HashMap<String, AccessAccumulator>();

	public AccessMBean() {

	}

	public void addAccess(String path, String service, String method, String httpMethod, String httpStatus, int logicReturn, long elapsedTime) {
		boolean err = false;
		if (httpStatus.indexOf("200") < 0) {
			err = true;
		}
		AccessAccumulator aa = accumulatorMap.get(httpMethod + "-" + path);
		if (aa == null) {
			aa = new AccessAccumulator(path, httpMethod, service, method);
			accumulatorMap.put(httpMethod + "-" + path, aa);
		}
		aa.addOperation(elapsedTime, err, logicReturn);
		if (err) {
			this.httpErrorCount++;
		}
		if (logicReturn != 0) {
			this.logicFaultCount++;
		}
		this.accessCount++;
		this.elapsedTime += elapsedTime;

	}

	@XmlElement
	public float getTps() {
		float tps = 0;
		if (elapsedTime > 0 && accessCount > 0){
			tps = ((float) accessCount) / ((float) elapsedTime / 1000);
			tps = (float)(Math.round(tps*100))/100;
		}
		return tps;
	}

	@XmlElement
	public float getErrRat() {
		float rat = 0;
		if (accessCount > 0 && httpErrorCount > 0){
			rat = (float) httpErrorCount / (float) accessCount;
			rat = (float)(Math.round(rat*100))/100;
		}
		return rat;
	}

	@XmlElement
	public float getFaultRat() {
		float rat = 0;
		if (logicFaultCount > 0 && accessCount > 0){
			rat = (float) logicFaultCount / (float) accessCount;
			rat = (float)(Math.round(rat*100))/100;
		}
		return rat;
	}

	@XmlElement
	public long getElapsedTime() {
		return elapsedTime;
	}

	@XmlElement
	public long getAccessCount() {
		return accessCount;
	}

	@XmlElement
	public long getHttpFaultCount() {
		return httpErrorCount;
	}

	@XmlElement
	public long getLogicFaultCount() {
		return logicFaultCount;
	}

	@XmlElement
	public Date getStartTime() {
		return startDate;
	}

	@XmlElement
	public Date getSystemTime() {
		return new Date();
	}

	@XmlElement
	public long getTotalAccess() {
		return this.getAccessCount();
	}

	@XmlElement
	public long getTotalTimeMS() {
		return getSystemTime().getTime()-getStartTime().getTime();
	}

	@XmlElement
	public float getTotalTps() {
		float tps = 0;
		if (getTotalTimeMS() > 0 && this.getAccessCount() > 0){
			tps = ((float) this.getAccessCount()) / ((float) getTotalTimeMS() / 1000);
			tps = (float)(Math.round(tps*100))/100;
		}
		return tps;
	}


	@XmlElement
	public Collection<AccessAccumulator> getTracking() {
		return accumulatorMap.values();
	}

}

 

访问累加:AccessAccumulator

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.NONE)
@XmlType(namespace = "com.plusrun.profiling", name = "AccessAccumulator")
@XmlRootElement(namespace = "com.plusrun.profiling")
public class AccessAccumulator {

	@XmlElement
	public String getPath() {
		return path;
	}

	@XmlElement
	public String getHttpMethod() {
		return httpMethod;
	}

	@XmlElement
	public String getService() {
		return service;
	}

	@XmlElement
	public String getMethod() {
		return method;
	}

	@XmlElement
	public long getCount() {
		return count;
	}

	@XmlElement
	public long getTotal() {
		return total;
	}

	@XmlElement
	public long getMax() {
		return max;
	}

	@XmlElement
	public long getMin() {
		return min;
	}

	@XmlElement
	public float getAvg() {
		float avg = 0;
		if (total > 0 && count > 0){
			avg = (float) total / (float) count;
			avg = (float)(Math.round(avg*100))/100;
		}
		return avg;
	}

	@XmlElement
	public float getTps() {
		float tps = 0;
		if (total > 0 && count > 0){
			tps = ((float) count) / ((float) total / 1000);
			tps = (float)(Math.round(tps*100))/100;
		}
		return tps;
	}

	@XmlElement
	public long getErr() {
		return err;
	}

	@XmlElement
	public long getFault() {
		return fault;
	}

	@XmlElement
	public float getErrRat() {
		float rat = 0;
		if (count > 0 && err > 0){
			rat = (float) err / (float) count;
			rat = (float)(Math.round(rat*100))/100;
		}
		return rat;
	}

	@XmlElement
	public float getFaultRat() {
		float rat = 0;
		if (fault > 0 && count > 0){
			rat = (float) fault / (float) count;
			rat = (float)(Math.round(rat*100))/100;
		}
		return rat;
	}

	String path;
	String httpMethod;
	String service;
	String method;

	private long count = 0;
	private long total = 0;
	private long max = 0;
	private long min = -1;
	/**
	 * http error
	 */
	private long err = 0;
	/**
	 * logic error
	 */
	private long fault = 0;

	public AccessAccumulator() {

	}

	public AccessAccumulator(String path, String httpMethod, String service, String method) {
		this.path = path;
		this.httpMethod = httpMethod;
		this.service = service;
		this.method = method;
	}

	@XmlElement
	public String getAccessPath() {
		return httpMethod + "-" + path;
	}

	public void addOperation(long timeMS, boolean httpErr, int ret) {
		this.count++;
		this.total += timeMS;
		if (httpErr) {
			err++;
		}
		if (ret != 0) {
			fault++;
		}
		if (timeMS>max){
			max =timeMS;
		}
		if (timeMS<min ||min==-1){
			min =timeMS;
		}
	}

	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("{httpMethod").append(":").append(getHttpMethod()).append(",");
		sb.append("path").append(":").append(getPath()).append(",");
		sb.append("service").append(":").append(getService()).append(",");
		sb.append("method").append(":").append(getMethod()).append(",");
		sb.append("count").append(":").append(getCount()).append(",");
		sb.append("total").append(":").append(getTotal()).append(",");
		sb.append("max").append(":").append(getMax()).append(",");
		sb.append("min").append(":").append(getMin()).append(",");
		sb.append("avg").append(":").append(getAvg()).append(",");
		sb.append("tps").append(":").append(getTps()).append(",");
		sb.append("err").append(":").append(getErr()).append(",");
		sb.append("errRat").append(":").append(getErrRat()).append(",");
		sb.append("fault").append(":").append(getFault()).append(",");
		sb.append("faultRat").append(":").append(getFaultRat()).append("}");
		return sb.toString();
	}
}

 

Rest Service: ProfilingServiceResource

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author michael
 * 
 */
@Path("/profiling")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Service
public class ProfilingServiceResource {

	@Autowired
	ProfilerManagerImpl profilerManager;

	@GET
	@Path("/state")
	public AccessMBean getAccessStatus() {
		return profilerManager.getAccessStatus();
	}

	@POST
	@Path("/clean")
	public AccessMBean cleanAccessStatus() {
		profilerManager.cleanAccessStatus();
		return profilerManager.getAccessStatus();
	}
}

 

返回值Bean:

public class ResultBase {

	private Integer errcode;
	private Integer ret;
	private String msg;

	public Integer getErrcode() {
		return errcode;
	}

	public void setErrcode(Integer errcode) {
		this.errcode = errcode;
	}

	public Integer getRet() {
		return ret;
	}

	public void setRet(Integer ret) {
		this.ret = ret;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

}

 

 

CXF REST 配置文件:

<?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:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xmlns:jaxrs="http://cxf.apache.org/jaxrs"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:cxf="http://cxf.apache.org/core"
	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/tx
		http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		http://www.springframework.org/schema/util 
		http://www.springframework.org/schema/util/spring-util-2.5.xsd
		http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
		http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">

	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <bean id="cxf" class="org.apache.cxf.bus.spring.SpringBus">
        <property name="inInterceptors">
            <list>
                <ref bean="profilingInInterceptor"/>
            </list>
        </property>
        <property name="outInterceptors">
            <list>
                <ref bean="profilingOutInterceptor"/>
            </list>
        </property>
    </bean> 

	<util:list id="jsonKeys">
	</util:list>

	<util:list id="jsonTypes">
		<value>application/json</value>
		<value>application/jettison</value>
	</util:list>

	<bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.json.JSONProvider">
		<property name="serializeAsArray" value="true" />
		<property name="arrayKeys" ref="jsonKeys" />
		<property name="produceMediaTypes" ref="jsonTypes" />
		<property name="consumeMediaTypes" ref="jsonTypes" />
		<property name="ignoreNamespaces" value="true" />
		<property name="dropRootElement" value="true" />
		<property name="ignoreMixedContent" value="true" />
		<property name="attributesToElements" value="true" />
	</bean>
	<jaxrs:server id="restApiResource" address="/">
		<jaxrs:serviceBeans>
			<ref bean="profilingServiceResource" />
		</jaxrs:serviceBeans>
		<jaxrs:providers>
			<ref bean="jsonProvider" />
		</jaxrs:providers>
		<jaxrs:extensionMappings>
			<entry key="json" value="application/json" />
			<entry key="xml" value="application/xml" />
		</jaxrs:extensionMappings>
	</jaxrs:server>

</beans>

 

 

分享到:
评论

相关推荐

    spring cxf rest demo

    【Spring CXF REST Demo】项目是一个使用Maven构建的Java应用程序,它展示了如何将Spring框架与Apache CXF集成,以创建RESTful风格的服务。在这个项目中,开发者可以学习到如何利用CXF作为服务端的REST服务提供者,...

    cxf开发rest风格接口jar

    【标题】"CXF开发REST风格接口所需的最小JAR包" 在Java开发中,Apache CXF是一个广泛使用的开源服务框架,它支持Web服务(SOAP)和RESTful接口的创建。REST(Representational State Transfer)风格的接口已经成为...

    使用CXF发布Rest服务

    【标题】:使用CXF发布REST服务 在Java开发中,Apache CXF是一个广泛使用的开源框架,用于构建和部署Web服务。本教程将详细介绍如何利用CXF 2.3.3版本来发布RESTful服务,并在客户端进行调用。首先,我们需要理解...

    java cxf规范rest

    【标题】"java cxf规范rest"涉及到的是Java开发中使用Apache CXF框架实现RESTful服务的相关技术。CXF是一个开源的服务框架,它允许开发者创建和消费各种Web服务,包括基于JAX-WS和JAX-RS的标准。REST...

    cxf rest最简实例可直接运行

    CXF REST最简实例是一个快速入门的教程,它展示了如何使用Apache CXF框架创建RESTful Web服务并进行测试。Apache CXF是一个流行的开源框架,它允许开发者构建和消费Web服务,包括SOAP和REST风格的服务。REST...

    cxf REST文档

    一、CXF与REST基础 Apache CXF支持多种Web服务规范,包括SOAP和REST。REST服务通常以资源为中心,通过HTTP方法(GET、POST、PUT、DELETE等)操作这些资源。CXF提供了强大的工具和API,简化了REST服务的实现过程。 ...

    cxf rest webservice

    【标题】:“CXF REST Web服务” 在Java世界中,Apache CXF是一个广泛使用的开源框架,用于构建和消费Web服务,包括RESTful服务。REST(Representational State Transfer)是一种轻量级的架构风格,用于设计网络...

    Apache CXF + Spring3 + REST + JSON配置

    在"Apache CXF + Spring3 + REST + JSON配置"中,我们主要探讨如何利用Apache CXF和Spring 3框架来构建RESTful服务,并使用JSON作为数据交换格式。以下是一些关键知识点: 1. **Spring 3集成CXF**: - 首先,你...

    CXF整合spring实现REST接口开发

    当CXF与Spring结合时,可以创建高效且灵活的REST接口,便于开发分布式系统。本篇将详细介绍如何利用CXF和Spring进行REST接口的开发。 首先,我们需要在项目中引入CXF和Spring的相关依赖。在Maven工程中,可以在pom....

    Spring CXF Restful 实例

    Spring Security可以与Spring CXF很好地集成,提供基于角色的访问控制和OAuth2支持。 8. **监控和日志**:为了监控服务性能和错误,我们可以配置Spring CXF的日志和跟踪功能,同时可以使用Spring Boot Actuator等...

    CXF-REST搭建WebService服务端demo

    本示例将详细讲解如何使用Spring、Apache CXF和Maven来创建一个基于REST的WebService服务端Demo。 首先,让我们理解这些关键组件的作用: 1. **Spring**:这是一个全面的开发框架,用于构建Java应用程序,特别是...

    SPRING-MVC-MQ-CXF-REST_Demo

    "SPRING-MVC-MQ-CXF-REST_Demo"这个项目很可能是用来演示如何在同一个应用中整合Spring MVC、MQ、CXF和REST技术。项目可能包含了以下部分: 1. Spring MVC配置:展示了如何设置DispatcherServlet、视图解析器以及...

    使用CXF暴露您的REST服务

    ### 使用CXF暴露REST服务详解 #### 一、引言 在现代软件开发中,RESTful服务已经成为一种标准的Web服务交互方式。它基于HTTP协议,简单易用且具有良好的可扩展性。Apache CXF是一个强大的框架,用于构建和消费...

    cxf+spring发布webservice和restservice

    在IT行业中,Web服务是应用程序之间进行通信的一种标准方法,主要分为SOAP(简单对象访问协议)基于的Web Service和REST(Representational State Transfer)服务。本项目“cxf+spring发布webservice和restservice”...

    接受json格式的CXF+Spring整合的REST的服务完整接口实例

    发布CXF+Spring整合的REST的服务接口完整实例,其中包括数据库连接池,json数据格式传递数据,HttpURLConne的get和post方式调用接口,以及获取访问者ip地址工具类等众多的技术实例。

    2.CXF安全访问之Http Basic Auth(一)

    本文将深入探讨CXF安全访问的一个重要方面:HTTP基本认证(Http Basic Auth)。这是一种简单但有效的身份验证机制,适用于对Web服务进行安全控制。 HTTP基本认证是基于HTTP协议的,它在请求头中包含一个Base64编码...

    CXF发布的REST服务返回JSON格式数据

    CXF发布的REST服务返回JSON格式数据,只有服务端代码,没有客户端代码,可以通过http://localhost:9999/roomservice/room、http://localhost:9999/roomservice/room/001来访问测试数据。

    CXF框架访问远程webService服务之获取快递状态信息

    CXF框架是Java中用于构建和消费Web Service的流行工具,支持SOAP(简单对象访问协议)和WSDL(Web服务描述语言)。本教程将深入讲解如何利用CXF框架来访问远程Web Service服务,特别是获取快递状态信息。 首先,...

    CXF2.6 spring 提供rest服务输出xml或json等格式数据

    CXF作为一个开源的Web服务框架,它支持SOAP和REST两种服务模型,尤其在处理REST服务时表现出强大的功能。本文将详细探讨如何使用CXF 2.6版本与Spring框架结合,来提供REST服务并输出XML或JSON格式的数据。 首先,...

    Web Service 那点事儿(4)—— 使用 CXF 开发 REST 服务 - ImportNew1

    【CXF 开发 REST 服务】Apache CXF 是一个开源的 Java 框架,用于构建和开发服务导向架构(SOA)。它支持多种协议和服务风格,包括 SOAP 和 REST。在本篇中,我们将探讨如何使用 CXF 开发 RESTful Web 服务,以及...

Global site tag (gtag.js) - Google Analytics