Spring HTTP Invoker实战

欧渝
2023-12-01
Spring HTTP Invoker实现 service接口远程调用

首先你得添加spring httpinvoker所必须的jar包
服务端配置:
1.服务端web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.1"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <listener>
        <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
    </listener>
    <filter>
        <filter-name>log4jServletFilter</filter-name>
        <filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>log4jServletFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>

    <servlet>
        <servlet-name>remoting</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
<!--解析http接口调用地址-->
    <servlet-mapping>
        <servlet-name>remoting</servlet-name>
        <url-pattern>/remoting/*</url-pattern>
    </servlet-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring-core.xml</param-value>
    </context-param>
</web-app>
2.新建spring-mvc.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:c="http://www.springframework.org/schema/c"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
    <mvc:annotation-driven />
    <context:component-scan base-package="com.my.resources" />

    <!--接口-->
    <bean name="/msgValcodeService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
        <property name="service" ref="msgValcodeService"/>
        <property name="serviceInterface" value="com.my.resources.IMsgValcodeService"/>
    </bean>
</beans>
"msgValcodeService"是此service的名称
3.新建spring-core.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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">	
	<context:annotation-config />
</beans>


   
   
前端服务调用配置(客户端):
4.新建apache-http.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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">

    <!-- http client的请求重试操作 -->
    <bean id="defaultHttpRequestRetryHandler" class="org.apache.http.impl.client.StandardHttpRequestRetryHandler">
        <!-- 请求重试5次数,默认3次 -->
        <constructor-arg index="0" value="5" />
        <constructor-arg index="1" value="true" />
    </bean>

    <!-- 开启一个长连接 -->
    <bean id="keepAliveHeader" class="org.apache.http.message.BasicHeader">
        <constructor-arg index="0" value="Connection" />
        <constructor-arg index="1" value="keep-alive" />
    </bean>

    <!-- 使用keep-alive一定要设置Content-Length头 -->
    <bean id="keepAliveStrategy" class="org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy" />


    <!-- http客户端连接池管理器 -->
    <bean id="poolingHttpClientConnectionManager" class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager" destroy-method="shutdown">
        <!-- 连接池最大活动连接数,默认值为20 -->
        <property name="maxTotal" value="1000" />
        <!-- 每个主机的最大并行链接数,默认值为2 -->
        <property name="defaultMaxPerRoute" value="1000" />
    </bean>

    <!-- 创建http客户端工厂类 -->
    <bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder" factory-method="create">
        <!-- 注入http客户端连接池管理器 -->
        <property name="connectionManager" ref="poolingHttpClientConnectionManager" />
        <property name="retryHandler" ref="defaultHttpRequestRetryHandler" />
        <!-- 保持长连接配置,需要在头添加Keep-Alive -->
        <property name="keepAliveStrategy" ref="keepAliveStrategy" />
        <property name="defaultHeaders">
            <util:list>
                <ref bean="keepAliveHeader" />
            </util:list>
        </property>
    </bean>

    <!-- 利用工程创建http客户端 -->
    <bean id="httpClient" factory-bean="httpClientBuilder" factory-method="build" />

    <bean id="httpComponentsHttpInvokerRequestExecutor" class="org.springframework.remoting.httpinvoker.HttpComponentsHttpInvokerRequestExecutor">
        <!-- 依赖apache http客户端 -->
        <property name="httpClient" ref="httpClient" />
        <!-- 连接超时5秒,5000毫秒 -->
        <property name="connectTimeout" value="80000" />
        <!-- 读写超时5秒,5000毫秒 -->
        <property name="readTimeout" value="80000" />
        <!--<!– 连接不够用时的等待时间,不宜过长,必须设置。如果连接不够用时,时间过长将是灾难性的 –>-->
        <property name="connectionRequestTimeout" value="500" />
    </bean>
</beans>

5新建spring-http-service.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:aop="http://www.springframework.org/schema/aop"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">
    <import resource="apache-http.xml" />
    <!--主要是涉及业务办理的接口配置-->
    <bean id="msgValcodeServiceProxy" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
        <property name="httpInvokerRequestExecutor" ref="httpComponentsHttpInvokerRequestExecutor" />
        <property name="serviceUrl" value="http://localhost:8888/bushandler-app/remoting/msgValcodeService"/>
        <property name="serviceInterface" value="com.my.resources.IWifiPrecontractService"/>
    </bean>
</beans>

5web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
		  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <!-- log4j2 -->
    <listener>
        <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
    </listener>
    <filter>
        <filter-name>log4jServletFilter</filter-name>
        <filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>log4jServletFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>
    <!-- spring mvc编码处理 -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- Spring MVC -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- 上下文监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <session-config>
        <session-timeout>10</session-timeout>
    </session-config>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring-core.xml</param-value>
    </context-param>
    <!-- 404 页面不存在-->
    <error-page>
        <error-code>404</error-code>
        <location>/404.jsp</location>
    </error-page>
    <!-- 500 系统错误-->
    <error-page>
        <error-code>500</error-code>
        <location>/500.jsp</location>
    </error-page>
    <!--欢迎界面-->
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

6.spring-core.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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">	
	<context:annotation-config />
	<import resource="spring-http-service.xml"/>
</beans>

=完=

























































 类似资料: