Spring2.5远程调用Hessian的例子
来源:互联网 发布:简单的java小程序 编辑:程序博客网 时间:2024/05/21 05:44
我的apache_2.0.63+3个tomcat-6.0.18做好集成,并且配置好负载均衡。
现在我使用Spring2.5来实现远程调用给你举个例子。
1.服务端创建一个web工程。
服务端的类:
package cn.edu.zju.cmj.service.pojo;
import java.io.Serializable;
/**
* 普通实体类
* @author cmj
*
*/
public class Order implements Serializable{
private Integer id;
private String name;
public Order(){
}
public Order(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package cn.edu.zju.cmj.service;
import cn.edu.zju.cmj.service.pojo.Order;
/**
* 服务接口的定义
* @author cmj
*
*/
public interface IOrderService {
public Order getOrder(Integer id);
}
package cn.edu.zju.cmj.service.impl;
import cn.edu.zju.cmj.service.IOrderService;
import cn.edu.zju.cmj.service.pojo.Order;
/**
* 实现服务接口的类
* @author cmj
*
*/
public class OrderService implements IOrderService {
public Order getOrder(Integer id) {
//打印这一句是为了看到负载均衡的效果
System.out.println("Tomcat---->");
Order [] orders=new Order[5];
for(int i=0;i<5;i++){
orders[i]=new Order(i,"order"+i);
}
for(int i=0;i<5;i++){
if(id==orders[i].getId()){
return orders[i];
}
}
return null;
}
}
现在服务端Spring的配置文件
beans.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-2.5.xsd">
<bean id="orderService" class="cn.edu.zju.cmj.service.impl.OrderService">
</bean>
</beans>
需要在WEB工程的WEB-INF下面增加一个remoting-servlet.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<!--
- Dispatcher servlet for HTTP remoting via Hessian, Burlap, and Spring's
- HTTP invoker (see remoting-servlet.xml for the controllers).
-->
<beans>
<bean name="/OrderService-hessian"
class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="orderService"/>
<property name="serviceInterface"
value="cn.edu.zju.cmj.service.IOrderService"/>
</bean>
</beans>
最后web.xml的配置如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_2_5.xsd"
version="2.5">
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>Hession.root</param-value>
</context-param>
<context-param>
<description>Spring配置文件位置</description>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/beans.xml</param-value>
</context-param>
<context-param>
<description>Spring日志文件位置</description>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<listener>
<description>Spring配置加载器</description>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>remoting</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>remoting</servlet-name>
<url-pattern>/remoting/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
</web-app>
2.把这个web工程部署到三个tomcat上(我这里假定web应用名为order)。
3.在apache和tomcat的集成配置文件上增加映射
[uri:/order/remoting/*]
info= Map for jsp-examples context of tomcat
group=balanced
4.客户端的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-2.5.xsd">
<bean id="orderService"
class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl"
value="http://127.0.0.1:80/order/remoting/OrderService-hessian"/>
<property name="serviceInterface"
value="cn.edu.zju.cmj.service.IOrderService"/>
</bean>
</beans>
我创建了一个测试类
package cn.edu.zju.cmj;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.edu.zju.cmj.service.IOrderService;
import cn.edu.zju.cmj.service.pojo.Order;
public class TestOrder {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test
public void testOrder(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
IOrderService orderService=(IOrderService) ctx.getBean("orderService");
for(int i=0;i<5;i++){
Order order=orderService.getOrder(i);
System.out.println(order.getName());
}
}
}
你就可以看到打印结果,并且你可以看到3个tomcat的控制台都有打印结果
"Tomcat---->"
从而实现了服务接口调用的负载均衡。
- Spring2.5远程调用Hessian的例子
- hessian远程服务调用例子
- hessian远程调用的使用
- Spring的Hessian,Burlap,HttpInvoker远程调用
- Hessian实现远程调用的简单案例
- Hessian-远程服务调用
- Hessian实现远程调用
- spring hessian远程调用
- 浅尝Hessian远程调用
- Java远程调用之Hessian
- Java 远程调用 Hessian 简例
- hessian 实现远程接口调用
- hessian 远程接口调用原理
- JFinal+Hessian调用远程服务
- 解决hessian远程调用连接超时的问题
- 比WebService更简单的远程调用方案框架:Hessian
- 远程调用之RMI、Hessian、Burlap、Httpinvoker、WebService的比较
- 基于Dubbo的Hessian协议实现远程调用
- 简单工厂模式
- 使用T-SQL导入多个文件数据到SQL Server中
- 优化策略
- Mysql Master/Slaver 搭建
- 求助
- Spring2.5远程调用Hessian的例子
- 使用Proteges生成JADE本体
- 文件夹被隐藏,隐藏属性灰色解决方法
- 关于SVN导致电脑速度慢的问题,进程里TSVNCache.exe非常耗CPU
- Eclipse中cvs的使用介绍
- html2image安装和使用(转+增补)
- aliww开发文档
- 探究 Z-Order
- 网页页面随意拖动页面大小