首页 > 其他分享 >记Spring HTTP Invoker远程调用的使用(二)基于Servlet方式,配置servlet映射url-pattern实现

记Spring HTTP Invoker远程调用的使用(二)基于Servlet方式,配置servlet映射url-pattern实现

时间:2024-08-29 22:55:11浏览次数:5  
标签:xml web HTTP applicationContext url Spring springframework bean org

目录

前言

一、概念

二、代码实现

1. 服务端实现

2. 客户端实现


前言

本篇接上一篇记Spring HTTP Invoker远程调用的使用(一)基于Url映射方式,DispatcherServlet统一处理实现-CSDN博客icon-default.png?t=N7T8https://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" />设置的时间后报异常,释放请求。

完结!谢谢关注。

标签:xml,web,HTTP,applicationContext,url,Spring,springframework,bean,org
From: https://blog.csdn.net/u011529483/article/details/141685195

相关文章

  • 二. Spring Boot 中的 “依赖管理和自动配置” 详解透彻到底(附+详细代码流程)
    二.SpringBoot中的“依赖管理和自动配置”详解透彻到底(附+详细代码流程)@目录二.SpringBoot中的“依赖管理和自动配置”详解透彻到底(附+详细代码流程)1.如何理解“约定优于配置”2.SpringBoot依赖管理和自动配置2.1SpringBoot的依赖管理2.1.1什么是依赖管理......
  • 如何使用Spring IOC的注解进行开发
    目录1、如何使用注解标记和扫描2、如何使用注解配置作用域和周期方法3、如何使用注解进行引用类型自动装配4、如何使用注解对基本类型属性赋值Spring IoC(Inversion of Control,控制反转)容器通过注解提供了一种简洁且强大的方式来进行依赖注入和配置管理。注解开发使得......
  • 逆向工程、Spring框架IOC、AOP学习
    系列文章目录第一章基础知识、数据类型学习第二章万年历项目第三章代码逻辑训练习题第四章方法、数组学习第五章图书管理系统项目第六章面向对象编程:封装、继承、多态学习第七章封装继承多态习题第八章常用类、包装类、异常处理机制学习第九章集合学习第......
  • 全栈程序员 | 精通安卓、鸿蒙,小程序,Java、Vue.js、SpringBoot及更多技术
    我是一个全栈程序员,擅长多种开发技术,包括安卓开发、Java编程、Vue.js、SpringBoot以及小程序开发等。我在技术上有广泛的涉猎,并致力于将创新解决方案应用于实际项目中。无论是开发高性能的安卓应用,还是构建响应式网页、实现复杂的后端功能,我都能提供专业的技术支持和高质量的代......
  • form-data与x-www-form-urlcoded区别
    引言很多同学在使用postman工具时,经常搞不清楚form-data与x-www-form-urlcoded的区别,如下:multipart/form-data和application/x-www-form-urlencoded是两种常用的HTTP请求内容类型(Content-Type),它们用于在HTTP请求中发送数据,但是它们之间存在一些区别。区别数据格......
  • 基于Spring Boot的陶瓷文化网站的设计与实现
    毕业设计(论文)论文题目:基于SpringBoot的陶瓷文化网站的设计与实现博主可接毕设论文!!! 摘 要中国悠久的陶瓷艺术,作为民族文化遗产的重要载体,历经时代的洗礼,其文化价值日益凸显。在现代社会变迁中,探寻传统陶瓷文化的传播路径显得尤为重要。随着科技进步与消费模式的革......
  • SpringBoot把本地的对象封装成为Nacos的配置对象
    你需要有个NacosNacos建立你的配置文件--建议yml文件编写你的yml配置platform:transaction:properties:notifyHost:"http://10.130.1.18:${server.port.cztech-service-gateway}"smsTemplate:"TEM_0029"#订单默认过期时间--分钟defau......
  • 基于Springboot公寓电费管理系统的设计与实现(源码+LW+调试文档)
     目录:程序功能截图:程序部分代码参考:数据库sql:程序技术介绍:后端springboot介绍:mysql介绍:程序论文:​选择我的理由:程序获取:......
  • 基于SpringBoot大学生征兵入伍管理系统的设计与实现(源码+LW+调试文档)
     目录:程序功能截图:程序部分代码参考:数据库sql:程序技术介绍:后端springboot介绍:mysql介绍:程序论文:​选择我的理由:程序获取:......
  • java毕业设计-基于springboot+vue的高校自习室预约系统设计和实现,基于springboot+vue
    文章目录前言演示视频项目架构和内容获取(文末获取)项目相关文件系统功能部分实现截图架构设计MVC的设计模式基于B/S的架构技术栈具体功能模块设计系统需求分析可行性分析系统测试为什么我?关于我我自己的网站项目开发案例前言博主介绍:✌️码农一枚,专注于大学生项目......