zoukankan      html  css  js  c++  java
  • Spring远程调用HTTP invoker

    前言

    在日常的工作中,会有客户端调用远程服务端接口的需求,这样的需求实现,在网上查到有像RMI、hessian、Burlap等,下文给出HTTP Invoker实现远程调用,他不用使用者考虑对象序列化的问题,对使用者,非常的友好。

    下面介绍在Spring中配置HTTP Invoker实现远程调用。分为服务提供方和服务消费方两部分。

    案例

    服务提供方

    1.定义一个接口和接口实现类,服务提供方提供。

    WelcomeService.java

    package com.aaron.service;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @Author Aaron
     * @Date 创建时间:2016-1-14
     * @Version 1.0
     * 
     * @Project_Package_Description Service || com.aaron.service
     * @Function_Description 服务端接口
     * 
     */
    public interface WelcomeService {
        public void provider(String str);
    
        public List<String> customer(String str);
    }

    WelcomeServiceImpl.java

    package com.aaron.service;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @Author Aaron
     * @Date 创建时间:2016-1-14
     * @Version 1.0
     * 
     * @Project_Package_Description Service || com.aaron.service
     * @Function_Description 服务端服务接口实现类
     * 
     */
    public class WelcomeServiceImpl implements WelcomeService {
        /**
         * 本地测试打印
         */
        @Override
        public void provider(String str) {
            for (int i = 0; i < 5; i++) {
                System.out.println("服务提供方测试==" + str + i);
            }
        }
    
        /**
         * 供消费方使用
         */
        @Override
        public List<String> customer(String str) {
            List<String> list = new ArrayList<String>();
            for (int i = 0; i < 5; i++) {
                list.add("服务消费方测试==" + str  + i);
            }
            return list;
        }
    }

    2.新建spring-remote.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">
        
        <!-- 用Spring配置声明暴露服务 -->
        <!-- bean -->
        <bean id="ws" class="com.aaron.service.WelcomeServiceImpl"/>
        <!-- service 导出器,将pojo转成spring所需的controller对象 -->
        <bean id="/ws.service" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
            <property name="serviceInterface" value="com.aaron.service.WelcomeService"/>
            <property name="service" ref="ws"></property>
        </bean>
    </beans>

    3.在服务提供方的web.xml配置service

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <display-name>ProviderService</display-name>
      <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:spring-remote.xml</param-value>
            </init-param>
      </servlet>
      <servlet-mapping>
        <servlet-name>service</servlet-name>
        <url-pattern>*.service</url-pattern>
      </servlet-mapping>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>

    这里服务提供方配置完成。

    服务消费方

    1、在服务消费方一个与服务提供方一样的接口

     WelcomeService.java

    2、新建customer.xml文件,通过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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
        
        <!-- 通过Spring配置引用远程服务 -->
        <!-- 客户端代理 -->
        <bean id="wsClient" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
            <property name="serviceInterface" value="com.aaron.client.WelcomeService"/>
            <property name="serviceUrl" value="http://localhost:8080/ProviderService/ws.service"/>
        </bean>
    </beans>

    测试

    package com.aaron.client;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * @Author Aaron
     * @Date 创建时间:2016-1-14
     * @Version 1.0
     * 
     * @Project_Package_Description Client || com.aaron.client
     * @Function_Description
     * 
     */
    public class TestMain {
        public static void main(String[] args) {
            ApplicationContext ac = new ClassPathXmlApplicationContext("customer.xml");
            WelcomeService ws = (WelcomeService) ac.getBean("wsClient");
            ws.provider("provider");
            List<String> list = ws.customer("customer");
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i));
            }
        }
    }
    TestMain.java

    结果分别在服务提供方以及服务消费方后台打印测试记录。

    PS:在看到的 Dubbo 看到spring的HTTP Invoker远程调用 http://blog.csdn.net/ichsonx/article/details/39008519

  • 相关阅读:
    C++ 获取图片文件信息
    java中redis的分布式锁工具类
    java中的redis工具类
    mysql中的sql查询优化
    利用Linux中的crontab实现分布式项目定时任务
    MYSQL的REPLACE和ON DUPLICATE KEY UPDATE使用
    redis学习三,Redis主从复制和哨兵模式
    redis学习五,redis集群搭建及添加主从节点
    String 转化成java.sql.Date和java.sql.Time
    SpringMVC配置双数据源,一个java项目同时连接两个数据库
  • 原文地址:https://www.cnblogs.com/haaron/p/5131010.html
Copyright © 2011-2022 走看看