Spring Http Invoker

时间:2022-07-22
本文章向大家介绍Spring Http Invoker,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Spring的HttpInvoker,它基于HTTP之上提供RPC,同时又使用了Java的对象序列化机制。

执行http调用意味着可以通过防火墙,同时又可以传输序列化对象。

Spring Http Invoker

  • 创建服务端
  1. 创建实体类,实现Serializable接口
@Data
public class User implements Serializable {
    private static final long serialVersionUID = -6970967506712260305L;
    private String name;
    private int age;
    private String email;
}

2.创建一个接口

public interface UserService {
    public User getUserbyName(String name);
}

3.实现一个接口

public class UserServiceImpl implements UserService {
    public User getUserbyName(String name) {
        User u = new User();
        u.setName(name);
        u.setEmail("abc@abc.com");
        u.setAge(20);
        return u;
    }
}

4.创建service-sevlet.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-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
    <!-- 这个Bean映射了当URL是/userService时,处理器为userServiceInvoker -->
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/userService">userServiceInvoker</prop>
            </props>
        </property>
    </bean>
 
    <!-- Announce that this interface is a HTTP invoker service. -->
    <bean id="userServiceInvoker" 
        class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
        <property name="service" ref="userServiceImpl" />
        <property name="serviceInterface" value="com.abc.invoke.server.service.UserService" />
    </bean>
    <bean id="userServiceImpl" class="com.abc.invoke.server.service.impl.UserServiceImpl" />
</beans>

配值文件直接与spring整合或者配置到web.xml中路径

<prop key="/userService">userServiceInvoker</prop>中的/userService是请求的服务的URL中的一部分,就是说这样的URL会被userServiceInvoker处理

这里将com.abc.invoke.server.service.UserService映射给了com.abc.invoke.server.service.impl.UserServiceImpl类了。

5.在web.xml中添加路径

   <servlet>
        <servlet-name>service</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:service-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>service</servlet-name>
        <url-pattern>/service/*</url-pattern>
    </servlet-mapping>
  • 创建客户端
  1. 创建服务接口和相应的pojo,同时实现类,这和创建服务端是一致的。
  2. 在spring的配置文件中创建服务配置文件
<?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-3.0.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd">
     
    <!-- 客户端使用 HttpInvokerProxyFactoryBean 代理客户端向服务器端发送请求,请求接口为 UserService 的服务 -->
    <bean id="userService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean" >
        <property name="serviceUrl" value="http://localhost:8080/InvokeServer/service/userService"/>
        <property name="serviceInterface" value="com.abc.invoke.client.service.UserService" />
    </bean>
</beans>

这里使用了org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean这个类来作为一个service的代理类。注意到serviceUrl属性为,这个URL的地址以/service开始,因此会被Server端拦截下来,而URL中的 /userService则为service路径,该路径与在Server端中service-servlet.xml中声明的

3.test

public class Test {
   public static void main(String[] args) {
       ApplicationContext ac = new ClassPathXmlApplicationContext(
                       "classpath:application-context.xml");
       UserService service = (UserService)ac.getBean("userService");
       User u = service.getUserbyName("Alvis");
       System.out.println(u);
   }