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 {

	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 {

	@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 accumulatorMap = new HashMap();

	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 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

 

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 配置文件:




	
	

    
        
            
                
            
        
        
            
                
            
        
     

	
	

	
		application/json
		application/jettison
	

	
		
		
		
		
		
		
		
		
	
	
		
			
		
		
			
		
		
			
			
		
	


 

 

你可能感兴趣的:(架构师-04-监督,专题-03-运行维护)