目次
前言
一、概念
二、代码实现
1. 服务端实现
2. 客户端实现
前言
本篇接上一篇记Spring HTTP Invoker远程调用的利用(一)基于Url映射方式,DispatcherServlet统一处理实现-CSDN博客https://blog.csdn.net/u011529483/article/details/141678510?spm=1001.2014.3001.5501
之后,解说Spring HTTP Invoker设置servlet的实现方式。
一、概念
Spring HTTP Invoker是spring框架中的一个远程调用模型,基于HTTP协议的远程调用。利用java的序列化机制在网络上转达对象。由Spring提供服务端和客户端。
两种实现方式:
1.基于Url映射方式,客户端远程请求的方式同SpringMVC的controller类似,全部的请求通过在web.xml中的 org.springframework.web.servlet.DispatcherServlet统一处理,根据url映射查找跟请求的url匹配的bean设置(本文通过applicationContext-servers.xml文件管理服务端bean)
2.基于Servlet方式,由org.springframework.web.context.support.HttpRequestHandlerServlet去拦截url- pattern匹配的请求,查找对应的bean设置(本文通过applicationContext-servers.xml文件管理服务端bean)。
二、代码实现
1. 服务端实现
步调:
编写User.java实体对象类,
编写Q1001Service.java接口类和接口实现类
设置applicationContext-servers.xml文件和web.xml文件
关于服务端接口类的代码这里就不描述了,请参考上一篇文章。这里直接讲设置。
applicationContext-servers.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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
- <bean id="q1001ServiceImpl" class="com.wqbr.shoservice.q1001.service.impl.Q1001ServiceImpl"/>
- <bean name="q1001Service" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
- <property name="service" ref="q1001ServiceImpl" />
- <property name="serviceInterface" value="com.wqbr.shoservice.q1001.service.Q1001Service" />
- </bean>
- </beans>
复制代码 重点看name="q1001Service"的bean,实现org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter类,并指定接口类和注入接口实现类。
web.xml文件设置:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="3.0" 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">
- <!-- https://blog.csdn.net/qyb19970829/article/details/110100544 配置时注意关于spring容器加载顺序的问题,
- applicationContext.xml,spring-security.xml,springmvc.xml 即这几个配置文件加载顺序
- -->
- <!--字符编码过滤器一定要放在前面,在web.xml中配置Spring提供的过滤器类-->
- <filter>
- <filter-name>encodingFilter</filter-name> <!--encodingFilter 这个命名不要修改-->
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <async-supported>true</async-supported>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>encodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!--配置Spring的监听器,启动spring容器-->
- <!--配置加载类路径的配置文件,注意加载顺序-->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- classpath:spring/applicationContext.xml
- classpath:spring/applicationContext-servers.xml
- </param-value>
- </context-param>
- <display-name>Archetype Created Web Application</display-name>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!--远程接口配置-->
- <servlet>
- <servlet-name>q1001Service</servlet-name>
- <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>q1001Service</servlet-name>
- <url-pattern>/q1001Service</url-pattern>
- </servlet-mapping>
- </web-app>
复制代码 重点是加载了applicationContext-servers.xml文件到Spring,接受Spring对applicationContext-servers.xml文件中bean的管理。
并指定了远程接口设置的servlet,实现org.springframework.web.context.support.HttpRequestHandlerServlet类,指定url-pattern请求,HttpRequestHandlerServlet类负责进行servlet-name与applicationContext-servers.xml文件中的bean(q1001Service)进行匹配。从而调用到服务端提供的实现方法。
2. 客户端实现
步调:
编写控制器MyTestController.java来远程调用服务
设置applicationContext-clients.xml文件和web.xml文件
关于客户端控制器的代码这里就不描述了,请参考上一篇文章。这里直接讲设置。
applicationContext-clients.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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
- <bean id="httpInvokerRequestExecutor" class="org.springframework.remoting.httpinvoker.HttpComponentsHttpInvokerRequestExecutor">
- <property name="connectTimeout" value="3000" /> <!--连接超时时间,毫秒。-->
- <property name="readTimeout" value="9000" /> <!--从服务器读取响应的超时时间,毫秒。9000=9秒-->
- </bean>
- <!--除了以上配置,还可以配置多线程调用-->
- <bean id="q1001Service" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
- <!--<property name="serviceUrl" value="http://192.168.1.86:7001/shoservice-1.0.0/q1001Service" />-->
- <property name="serviceUrl" value="http://127.0.0.1:7001/shoservice-1.0.0/q1001Service" />
- <property name="serviceInterface" value="com.wqbr.shoservice.q1001.service.Q1001Service" />
- <!-- 使用Apache的HttpComponents客户端来设置httpInvokerRequestExecutor属性。设置超时时间,防止远程请求异常时拖累服务响应,导致应用卡住。默认HttpInvokerProxy使用的JDK的HTTP功能 -->
- <property name="httpInvokerRequestExecutor" ref="httpInvokerRequestExecutor" />
- </bean>
- </beans>
复制代码 指定了id="q1001Service"的bean,实现org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean代理工厂,指定serviceUrl请求服务端服务的路径 和 serviceInterface服务端提供服务的接口类,注入id="httpInvokerRequestExecutor"的bean,设置相干属性使应用的请求更加公道,遇到请求服务异常时及时释放避免影相应用的性能等。
web.xml文件设置:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="3.0" 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">
- <!-- https://blog.csdn.net/qyb19970829/article/details/110100544 配置时注意关于spring容器加载顺序的问题,
- applicationContext.xml,spring-security.xml,springmvc.xml 即这几个配置文件加载顺序
- -->
- <!--字符编码过滤器一定要放在前面,在web.xml中配置Spring提供的过滤器类-->
- <filter>
- <filter-name>encodingFilter</filter-name> <!--encodingFilter 这个命名不要修改-->
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <async-supported>true</async-supported>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>encodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!--配置Spring的监听器,启动spring容器-->
- <!--配置加载类路径的配置文件,注意加载顺序-->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- classpath:spring/applicationContext.xml
- classpath:spring/applicationContext-clients.xml
- </param-value>
- </context-param>
- <display-name>Archetype Created Web Application</display-name>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!--配置前端控制器,对浏览器发送的请求进行统一处理-->
- <servlet>
- <servlet-name>dispatcherServlet</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <!--加载springmvc.xml配置文件的位置和名称,配置的是Spring配置-->
- <init-param>
- <!--contextConfigLocation:上下文配置路径,固定值-->
- <param-name>contextConfigLocation</param-name>
- <!--classpath:类路径,指的是Java和resources文件夹-->
- <!--springmvc.xml:指的是配置文件的名称:需要配置springmvc.xml,在下面。
- spring默认配置文件为applicationContext.xml。当中配置spring创建容器时要扫描的包 已经整合到springmvc.xml中-->
- <param-value>
- classpath:spring/springmvc.xml
- </param-value>
- </init-param>
- <!--配置启动加载-->
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>dispatcherServlet</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- <!--开启项目时打开的页面-->
- <welcome-file-list>
- <welcome-file>/login.jsp</welcome-file>
- </welcome-file-list>
- <!--设置session超时,单位分钟 默认30分钟-->
- <!-- <session-config>-->
- <!-- <session-timeout>2</session-timeout>-->
- <!-- </session-config>-->
- </web-app>
复制代码 重点是加载applicationContext-clients.xml文件到Spring,如许在Controller中才能调用到applicationContext-clients.xml文件指定的bean。
到此本文报告完毕,运行方式同上一篇。运行结果为:成功调用到远程服务。
模拟两个错误场景,错误场景1. 设置客户端调用服务的路径错误,这时运行结果如下:
到达毗连超时时间<property name="connectTimeout" value="3000" />报connect timed out,释放毗连。
错误场景2.设置服务端实现方法线程等待,睡眠时间超过客户端的<property name="readTimeout" value="9000" />设置。
这时运行结果如下:
报Read timed out异常,客户端调用时到达 <property name="readTimeout" value="9000" />设置的时间后报异常,释放请求。
完结!谢谢关注。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |