Spring HttpInvoker远程调用的例子

HttpInvoker的实现原理和java的RMI实现原理相同,都是基于Http协议,将java对象序列化以后以流的方式在客户端和服务器端进行传输。


例子的项目结构图:

 

Spring HttpInvoker远程调用的例子_第1张图片

执行流程图如下:

 

类图如下:

涉及到的类及接口:

客户端:

接口
package com.httpinvoker.test.client;

import com.httpinvoker.test.bean.User;

/**
 * 远程调用的客户端
 */
public interface ClientService {
	
	/**
	 * 添加一个账户
	 * @param account
	 */
	void addAccount(User account);
	
	/**
	 * 查询一个账户
	 * @return
	 */
	User findAccount();

}

测试类
package com.httpinvoker.test.client;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.httpinvoker.test.bean.User;

public class HttpInvokerTest {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
                new String[] {"spring-register-service.xml" });
    
		ClientService clientService = (ClientService)ctx.getBean("accountService");
       
		User account = new User("张三", "zhangsan", "男");
		
		clientService.addAccount(account);
		
		User account2 = clientService.findAccount();
		System.out.println("客户端查询到的用户的名字为 : " + account2.getName());
	}
}


 

服务器端:

接口
package com.httpinvoker.test.server;

import com.httpinvoker.test.bean.User;

/**
 * 远程调用的客户端
 */
public interface ServerService {
	
	/**
	 * 添加一个账户
	 * @param account
	 */
	void addAccount(User account);
	
	/**
	 * 查询一个账户
	 * @return
	 */
	User findAccount();

}

实现
package com.httpinvoker.test.server;

import com.httpinvoker.test.bean.User;

public class ServerServiceImpl implements ServerService {

	public void addAccount(User account) {
		System.out.println("用户 : " + account.getName() + " 添加成功!");
	}

	public User findAccount() {
		User account = new User("张三", "zhangsan", "男");
		System.out.println("用户 : " + account.getName() + " 查找成功!");
		return account;
	}
}

公共的User对象

package com.httpinvoker.test.bean;

import java.io.Serializable;

public class User implements Serializable{
	private static final long serialVersionUID = 1L;
	
	private String name;
	private String passwd;
	private String sex;
	
	public User() {
	}

	public User(String name, String passwd, String sex) {
		this.name = name;
		this.passwd = passwd;
		this.sex = sex;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}
}

客户端spring的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	<bean id="accountService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
    	<property name="serviceUrl">
			<value>http://localhost:8080/MavenTest/userHttpInvokerService</value>
		</property>
		<property name="serviceInterface">
			<value>com.httpinvoker.test.client.ClientService</value>
		</property>
    </bean>

</beans>


服务器端 spring的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	
    <bean id="accountService" class="com.httpinvoker.test.server.ServerServiceImpl">
    </bean>
    
    <bean name="userHttpInvokerService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
        <property name="service" ref="accountService"/>
        <property name="serviceInterface" value="com.httpinvoker.test.server.ServerService"/>
    </bean>

</beans>


web.xml的配置:

<servlet>
     <servlet-name>userHttpInvokerService</servlet-name>
     <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
 </servlet>
 
 <servlet-mapping>
     <servlet-name>userHttpInvokerService</servlet-name>
     <url-pattern>/userHttpInvokerService</url-pattern>
 </servlet-mapping>

注意:http://localhost:8080/MavenTest/userHttpInvokerService中的/userHttpInvokerService是服务器端的servlet所匹配的servlet-name所对应的bean的name属性值。

 

 

 

 

 


 

你可能感兴趣的:(Spring HttpInvoker远程调用的例子)