Spring2.5远程调用Hessian的例子
2009-03-12 13:29
267 查看
我的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来实现远程调用给你举个例子。
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---->"
从而实现了服务接口调用的负载均衡。
相关文章推荐
- hessian远程调用及spring中使用hessian
- Spring HttpInvoker远程调用的例子
- Spring使用Hessian实现远程调用
- 远程服务调用之RMI、Hessian、Burlap、Spring的HTTPinvoker
- 《Spring技术内幕》学习笔记18——Spring使用Hessian实现远程调用
- Spring使用Hessian实现远程调用
- spring技术内幕18-Spring使用Hessian实现远程调用
- Spring远程调用技术<2>-Hessian和Burlap
- spring hessian远程调用
- 《Spring技术内幕》学习笔记18——Spring使用Hessian实现远程调用
- 《Spring技术内幕》学习笔记18——Spring使用Hessian实现远程调用
- 《Spring技术内幕》学习笔记18——Spring使用Hessian实现远程调用
- Spring Boot + Dubbo 可运行的例子源码-实现服务注册和远程调用
- Spring 与Hessian 整合的简单远程调用实例
- Spring结合Hessian协议远程调用方法名不能重载
- 《Spring技术内幕》学习笔记18——Spring使用Hessian实现远程调用
- hessian远程服务调用例子
- Spring+Hessian搭建远程方法调用
- Spring + Hessian 实现轻量级分布式远程调用【包含rmi方式重构】
- Spring的Hessian,Burlap,HttpInvoker远程调用