其他分享
首页 > 其他分享> > axis2添加拦截器

axis2添加拦截器

作者:互联网

项目背景: 2002年的某保险老项目,项目是部署了多个服务器,每个服务器有2到多个服务(每个服务的日志对应一个日志文件),外部对接是通过F5分发到随机服务器上来进行访问,正式出现问题或者看一些问题就需要一个服务器一个服务器去找日志(运气不好直接看遍所有的服务器日志文件),所以考虑做个接口调用时调用的IP和端口的记录,来方便查找问题日志

web.xml增加拦截器是最简单的方法,不过leader害怕影响了整个项目不让修改,所以只能改自己涉及的接口,

接口是通过axis2的aar包方式发布(WEB-INFO下servers文件夹下所有aar包),这里全局axis2.xml也没有直接加载的是jar包里面默认的配置,leader这里也不让加全局的文件,也是担心影响整个项目(吐槽下,太事),所以只能通过moudle.xml来配置,然后修改自己接口的aar包里面的services,xml配置文件来拦截获取了

百度了老半天,终于解决了(这里参考): https://blog.csdn.net/u013313550/article/details/53285011

先来程序代码

  

package com.axis2.module;

import org.apache.axis2.AxisFault;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.description.AxisDescription;
import org.apache.axis2.description.AxisModule;
import org.apache.axis2.modules.Module;
import org.apache.neethi.Assertion;
import org.apache.neethi.Policy;

/**该类虽然没有实现实际的功能,但是必须要存在的,因为模块的初始化与销毁都必须在该类中完成,*/ public class RMPhaseModule implements Module{ /**初始化模块类 */ public void init(ConfigurationContext paramConfigurationContext, AxisModule paramAxisModule) throws AxisFault { System.out.println("axis2[RMPhaseModule] 模块初始化完成!!!"); } public void engageNotify(AxisDescription paramAxisDescription) throws AxisFault { } public boolean canSupportAssertion(Assertion paramAssertion) { return false; } public void applyPolicy(Policy paramPolicy, AxisDescription paramAxisDescription) throws AxisFault { } public void shutdown(ConfigurationContext paramConfigurationContext) throws AxisFault { System.out.println("axis2[RMPhaseModule] 模块已停止执行。。。"); } }

  

package com.axis2.handler;

import javax.servlet.http.HttpServletRequest;

import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.engine.Handler;
import org.apache.axis2.handlers.AbstractHandler;
import org.apache.axis2.transport.http.HTTPConstants;

import com.axis2.ServerInfo;

/**该类拦截请求,将自己需要的信息保存到线程变量里面,该类的调用在modules.xml里面配置*/ public class ServerInfoHandler extends AbstractHandler implements Handler{ public InvocationResponse invoke(MessageContext paramMessageContext) throws AxisFault { HttpServletRequest request = (HttpServletRequest)paramMessageContext.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST); System.out.println("ServerInfoHandler invoke() 初始化服务的信息到ThreadLocal"); ServerInfo serverInfo = new ServerInfo(); serverInfo.setIp(ServerInfo.getIpAddress(request, true)); serverInfo.setPort(request.getServerPort()); ServerInfo.local_thread.set(serverInfo); return InvocationResponse.CONTINUE; } }

  

package com.axis2.handler;

import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.engine.Handler;
import org.apache.axis2.handlers.AbstractHandler;

import com.axis2.ServerInfo;

/** 程序处理完后,清楚线程变量的值 */ public class ServerInfoRevokeHandler extends AbstractHandler implements Handler{ public InvocationResponse invoke(MessageContext paramMessageContext) throws AxisFault { ServerInfo.local_thread.remove(); System.out.println("销毁ThreadLocal-ServerInfo"); return InvocationResponse.CONTINUE; } }

  

package com.axis2;

import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.servlet.http.HttpServletRequest;

/** 简单的pojo类,来保存信息 */
public class ServerInfo {
	
	public static final ThreadLocal<ServerInfo> local_thread = new ThreadLocal<ServerInfo>();
	
	private String ip;
	
	private int port;

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}
	
    /**
     * 获取Ip地址
     * @param request
     * @return
     */
    public static String getIpAddress(HttpServletRequest request,boolean isLocal) {
        String ipAddress = null;
        try {
        	if(isLocal){
        		InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                return inet.getHostAddress();
        	}
            ipAddress = request.getHeader("x-forwarded-for");
            if (ipAddress == null || ipAddress.length() == 0
                    || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0
                    || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0
                    || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if (ipAddress.equals("127.0.0.1")) {
                    // 根据网卡取本机配置的IP
                    InetAddress inet = null;
                    try {
                        inet = InetAddress.getLocalHost();
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                    ipAddress = inet.getHostAddress();
                }
            }
            // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
            if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
                // = 15
                if (ipAddress.indexOf(",") > 0) {
                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                }
            }
        } catch (Exception e) {
            ipAddress = "";
        }
        // ipAddress = this.getRequest().getRemoteAddr();

        return ipAddress;
    }
    
   
	
}

  这里涉及的程序代码就没有了,当时让我头疼的是配置这块的问题,不是项目启不来,就是module类加载不了,各种报错

 

配置


  编写modules.xml, RMPhase是默认axis2.xml配置里面预留出来的,可以直接用,这样不用重新整axis2.xml(这里是leader最忌讳的,其实是瞎担心)

<?xml version="1.0" encoding="UTF-8"?>
<module name="serverInfo" class="com.axis2.module.RMPhaseModule">
	<InFlow>
		<handler name="InFlowLogHandler" class="com.axis2.handler.ServerInfoHandler">
			<order phase="RMPhase"/>
		</handler>
	</InFlow>
	<OutFlow>
		<handler name="OutFlowLogHandler" class="com.axis2.handler.ServerInfoRevokeHandler">
			<order phase="RMPhase"/>
		</handler>
	</OutFlow>
<!--	<OutFaultFlow>
		<handler name="FaultOutFlowLogHandler" class="com.test.module.handler.LoggingHandler">
			<order phase="RMPhase"/>
		</handler>
	</OutFaultFlow>
	<InFaultFlow>
		<handler name="FaultInFlowLogHandler" class="com.test.module.handler.LoggingHandler">
			<order phase="RMPhase"/>
		</handler>
	</InFaultFlow> -->
</module>

 自己对应接口的***.aar包里面的services.xml添加module

 

<service name="YTBWasForwardXmlProxy">
    <description>YTBWasForwardXmlProxy</description>
    <parameter name="ServiceClass">
        com.sinosoft.wasforwardxml.project.padcheck.services.YTBWasForwardXmlProxy
    </parameter>
    <!-- 引入这个module-->
    <module ref="serverInfo"/>  
    <messageReceivers>
        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
            class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
            class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
    </messageReceivers>    
</service>

 

aar包是之前已经发布的,这里只需要重新修改下xml文件就行,我是没解压的情况下直接winrar里面修改的

 

剩下就是发布mar包了(这里要感谢参考文章的大神了)

目录结构

test(随便一个文件名都行,路径也无所谓)

------com
------com/axis2
------com/axis2/ServerInfo.class
------com/axis2/module
------com/axis2/module/RMPhaseModule
------com/axis2/handler
------com/axis2/handler/ServerInfoHandler
------com/axis2/handler/ServerInfoRevokeHandler
------META-INF
------META-INF/modules.xml

  

 

黑窗口(DOS命令进入test文件夹),打包文件名为serverInfo,jar,然后修改.jar为.mar

 

 

 

 

 

 

将serverInfo.jar修改为serverInfo.mar.复制到项目目录WEB-INFO下的modules文件夹下即可(无此文件夹新建)

这里注意命令 ( jar cvf serverInfo.jar .) 最后有个.别漏了

 

 

 

程序里面使用相应信息的时候直接 获取即可

 

 

弊端是如果其他arr也需要修改相应的配置文件,这个项目里面有相当多的aar,这也是个繁琐的过程,

 

最后leader针对业务最后只获取了服务器的IP(程序直接可以获取,无需请求对象),没有要其他的信息

我也只更新了自己的接口,方便自己查找日志,只更新了一个aar

 

标签:拦截器,axis2,添加,org,apache,import,ipAddress,public
来源: https://www.cnblogs.com/zuxp/p/15802902.html