码迷,mamicode.com
首页 > 编程语言 > 详细

Spring远程调用HTTP invoker

时间:2016-01-14 19:04:43      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

前言

在日常的工作中,会有客户端调用远程服务端接口的需求,这样的需求实现,在网上查到有像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

 

Spring远程调用HTTP invoker

标签:

原文地址:http://www.cnblogs.com/haaron/p/5131010.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!