记Spring HTTP Invoker远程调用的利用(二)基于Servlet方式,设置servlet ...

打印 上一主题 下一主题

主题 564|帖子 564|积分 1692

目次
   前言
  一、概念
  二、代码实现
  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文件设置:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.     <bean id="q1001ServiceImpl" class="com.wqbr.shoservice.q1001.service.impl.Q1001ServiceImpl"/>
  6.     <bean name="q1001Service" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
  7.         <property name="service" ref="q1001ServiceImpl" />
  8.         <property name="serviceInterface" value="com.wqbr.shoservice.q1001.service.Q1001Service" />
  9.     </bean>
  10. </beans>
复制代码
重点看name="q1001Service"的bean,实现org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter类,并指定接口类和注入接口实现类。
web.xml文件设置:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  5.    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  6.   <!--  https://blog.csdn.net/qyb19970829/article/details/110100544 配置时注意关于spring容器加载顺序的问题,
  7.    applicationContext.xml,spring-security.xml,springmvc.xml 即这几个配置文件加载顺序
  8.    -->
  9.   <!--字符编码过滤器一定要放在前面,在web.xml中配置Spring提供的过滤器类-->
  10.   <filter>
  11.     <filter-name>encodingFilter</filter-name> <!--encodingFilter 这个命名不要修改-->
  12.     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  13.     <async-supported>true</async-supported>
  14.     <init-param>
  15.       <param-name>encoding</param-name>
  16.       <param-value>UTF-8</param-value>
  17.     </init-param>
  18.   </filter>
  19.   <filter-mapping>
  20.     <filter-name>encodingFilter</filter-name>
  21.     <url-pattern>/*</url-pattern>
  22.   </filter-mapping>
  23.   <!--配置Spring的监听器,启动spring容器-->
  24.   <!--配置加载类路径的配置文件,注意加载顺序-->
  25.   <context-param>
  26.     <param-name>contextConfigLocation</param-name>
  27.     <param-value>
  28.       classpath:spring/applicationContext.xml
  29.       classpath:spring/applicationContext-servers.xml
  30.     </param-value>
  31.   </context-param>
  32.   <display-name>Archetype Created Web Application</display-name>
  33.   <listener>
  34.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  35.   </listener>
  36.   <!--远程接口配置-->
  37.   <servlet>
  38.     <servlet-name>q1001Service</servlet-name>
  39.     <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
  40.   </servlet>
  41.   <servlet-mapping>
  42.     <servlet-name>q1001Service</servlet-name>
  43.     <url-pattern>/q1001Service</url-pattern>
  44.   </servlet-mapping>
  45. </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文件设置:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.         <bean id="httpInvokerRequestExecutor" class="org.springframework.remoting.httpinvoker.HttpComponentsHttpInvokerRequestExecutor">
  6.                 <property name="connectTimeout" value="3000" /> <!--连接超时时间,毫秒。-->
  7.                 <property name="readTimeout" value="9000" /> <!--从服务器读取响应的超时时间,毫秒。9000=9秒-->
  8.         </bean>
  9.         <!--除了以上配置,还可以配置多线程调用-->
  10.         <bean id="q1001Service" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
  11.                 <!--<property name="serviceUrl" value="http://192.168.1.86:7001/shoservice-1.0.0/q1001Service" />-->
  12.                 <property name="serviceUrl" value="http://127.0.0.1:7001/shoservice-1.0.0/q1001Service" />
  13.                 <property name="serviceInterface" value="com.wqbr.shoservice.q1001.service.Q1001Service" />
  14.                 <!-- 使用Apache的HttpComponents客户端来设置httpInvokerRequestExecutor属性。设置超时时间,防止远程请求异常时拖累服务响应,导致应用卡住。默认HttpInvokerProxy使用的JDK的HTTP功能  -->
  15.                 <property name="httpInvokerRequestExecutor" ref="httpInvokerRequestExecutor" />
  16.         </bean>
  17. </beans>
复制代码
指定了id="q1001Service"的bean,实现org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean代理工厂,指定serviceUrl请求服务端服务的路径 和 serviceInterface服务端提供服务的接口类,注入id="httpInvokerRequestExecutor"的bean,设置相干属性使应用的请求更加公道,遇到请求服务异常时及时释放避免影相应用的性能等。
web.xml文件设置:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  5.    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  6.   <!--  https://blog.csdn.net/qyb19970829/article/details/110100544 配置时注意关于spring容器加载顺序的问题,
  7.    applicationContext.xml,spring-security.xml,springmvc.xml 即这几个配置文件加载顺序
  8.    -->
  9.   <!--字符编码过滤器一定要放在前面,在web.xml中配置Spring提供的过滤器类-->
  10.   <filter>
  11.     <filter-name>encodingFilter</filter-name> <!--encodingFilter 这个命名不要修改-->
  12.     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  13.     <async-supported>true</async-supported>
  14.     <init-param>
  15.       <param-name>encoding</param-name>
  16.       <param-value>UTF-8</param-value>
  17.     </init-param>
  18.   </filter>
  19.   <filter-mapping>
  20.     <filter-name>encodingFilter</filter-name>
  21.     <url-pattern>/*</url-pattern>
  22.   </filter-mapping>
  23.   <!--配置Spring的监听器,启动spring容器-->
  24.   <!--配置加载类路径的配置文件,注意加载顺序-->
  25.   <context-param>
  26.     <param-name>contextConfigLocation</param-name>
  27.     <param-value>
  28.       classpath:spring/applicationContext.xml
  29.       classpath:spring/applicationContext-clients.xml
  30.     </param-value>
  31.   </context-param>
  32.   <display-name>Archetype Created Web Application</display-name>
  33.   <listener>
  34.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  35.   </listener>
  36.   <!--配置前端控制器,对浏览器发送的请求进行统一处理-->
  37.   <servlet>
  38.     <servlet-name>dispatcherServlet</servlet-name>
  39.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  40.     <!--加载springmvc.xml配置文件的位置和名称,配置的是Spring配置-->
  41.     <init-param>
  42.       <!--contextConfigLocation:上下文配置路径,固定值-->
  43.       <param-name>contextConfigLocation</param-name>
  44.       <!--classpath:类路径,指的是Java和resources文件夹-->
  45.       <!--springmvc.xml:指的是配置文件的名称:需要配置springmvc.xml,在下面。
  46.       spring默认配置文件为applicationContext.xml。当中配置spring创建容器时要扫描的包 已经整合到springmvc.xml中-->
  47.       <param-value>
  48.         classpath:spring/springmvc.xml
  49.       </param-value>
  50.     </init-param>
  51.     <!--配置启动加载-->
  52.     <load-on-startup>1</load-on-startup>
  53.   </servlet>
  54.   <servlet-mapping>
  55.     <servlet-name>dispatcherServlet</servlet-name>
  56.     <url-pattern>/</url-pattern>
  57.   </servlet-mapping>
  58.   <!--开启项目时打开的页面-->
  59.     <welcome-file-list>
  60.       <welcome-file>/login.jsp</welcome-file>
  61.     </welcome-file-list>
  62.   <!--设置session超时,单位分钟 默认30分钟-->
  63.   <!--  <session-config>-->
  64.   <!--    <session-timeout>2</session-timeout>-->
  65.   <!--  </session-config>-->
  66. </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企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

去皮卡多

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表