Chinaunix首页 | 论坛 | 博客
  • 博客访问: 130018
  • 博文数量: 46
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 335
  • 用 户 组: 普通用户
  • 注册时间: 2014-07-19 19:13
文章分类

全部博文(46)

文章存档

2015年(1)

2014年(45)

我的朋友

分类: 网络与安全

2014-07-23 16:16:42

在成功的将hessian server端也和spring结合后,从整个分布式应用的架构可以看出,无论是服务端与客户端共有的业务接口,还是服务端业务实现类,以及客户端访问远 程服务的应用代码里,已经没有一行关于远程操作的代码了。也就是spring让我们的分布式业务开发完全无关于远程访问协议了,这样我们就可以埋头开发服 务端和客户端的业务接口、实现、应用等等,二不必关心远程调用究竟要怎么实现。很明显,这也是AOP(面向方面的编程)的一个比较完美的应用,它使远程访 问接口和业务逻辑完全无耦合的分开了。设想一下,如果某天我们有需要,远程访问的接口要从rmi、jax-rpc、hessian、 httpinvoker之间做切换,也只要改下配置文件就ok了,一行代码都不用改,真的非常exciting。

接着前面那篇日志,先把spring和hessian在服务端的结合帖上来:

接口,还是那个接口~;实现,还是那个实现~~;配置却不是了那个配置呀~~~

首先,servlet变成了:
Java代码  收藏代码
  1.   
  2.   
  3.    remote  
  4.   
  5.    class>org.springframework.web.servlet.DispatcherServletclass>  
  6.   
  7.    1  
  8.   
  9.   
  10.   
  11.   
  12.   
  13.    remote  
  14.   
  15.    /remote/*  
  16.   
  17.   

其次,applicationContext.xml没有必要了,但是要有个remote-servlet.xml,内容如下:

Java代码  收藏代码
  1. "1.0" encoding="UTF-8"?>  
  2.   
  3.   
  4.   
  5. "-//SPRING//DTD BEAN//EN" "">  
  6.   
  7.   
  8.   
  9.     server properties  
  10.   
  11.     "_myService_server" class="whao.test.hessian.server.impl.MyServiceImpl">  
  12.   
  13.       
  14.   
  15.     "/myService_server" class="org.springframework.remoting.caucho.HessianServiceExporter">  
  16.   
  17.        "service">  
  18.   
  19.            "_myService_server"/>  
  20.   
  21.          
  22.   
  23.        "serviceInterface">  
  24.   
  25.            whao.test.hessian.server.MyService  
  26.   
  27.          
  28.   
  29.           
  30.   
  31.   
  32.   
  33.    


这样一来这个hessian服务的发布地址就变成了

客户端的配置改成:
Java代码  收藏代码
  1. "myServiceClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">  
  2.   
  3.    "serviceUrl">  
  4.   
  5.        http://localhost:8080/test_web/remote/myService_server  
  6.   
  7.      
  8.   
  9.    "serviceInterface">  
  10.   
  11.        whao.test.hessian.server.MyService  
  12.   
  13.      
  14.   
  15.   



客户端的代码依然是:
Java代码  收藏代码
  1. MyService service = (MyService);SpringBeanFactory.getBean("myServiceClient");;  
  2.   
  3. System.out.println(service.doSomething("mmmmmmmmm"););;  


运行一下,输出:

HAHAHA: mmmmmmmmm

ok!



嗯,真是不错,下面尝试着不改一行代码,我们把远程协议由hessian改成rmi。

由于rmi server不用servlet,也就不用web server,所以可以由自己的进程之接启动,我们就把remote-servlet.xml改成beans.xml内容如下:

Java代码  收藏代码
  1. "1.0" encoding="UTF-8"?>  
  2.   
  3. "-//SPRING//DTD BEAN//EN" "">  
  4.   
  5.   
  6.   
  7.     "_myService_server" class="whao.test.hessian.server.impl.MyServiceImpl">  
  8.   
  9.       
  10.   
  11.     "myService_server" class="org.springframework.remoting.rmi.RmiServiceExporter">  
  12.   
  13.        "serviceName">myService_server  
  14.   
  15.        "service">"_myService_server"/>  
  16.   
  17.        "serviceInterface">whao.test.hessian.server.MyService  
  18.   
  19.       
  20.   
  21.   
  22.   
  23.    


然后随便写个main程序启动服务,就不用启动web server了:

Java代码  收藏代码
  1. /* 
  2.  
  3.  * Created on 2005-7-31 
  4.  
  5.  * 
  6.  
  7.  */  
  8.   
  9. package whao.test.rmi;  
  10.   
  11.    
  12.   
  13. import org.springframework.context.ApplicationContext;  
  14.   
  15. import org.springframework.context.support.FileSystemXmlApplicationContext;  
  16.   
  17.    
  18.   
  19. /** 
  20.  
  21.  * @author Hao Wei 
  22.  
  23.  * 
  24.  
  25.  */  
  26.   
  27. public class Server {  
  28.   
  29.          public static void main(String[] args); throws Exception{  
  30.   
  31.              ApplicationContext ctx = new FileSystemXmlApplicationContext("D:\\whao-work\\src\\test_web\\src_server\\beans.xml");;  
  32.   
  33.              ctx.getBean("myService_server");;  
  34.   
  35.                    System.out.println("Server started");;  
  36.   
  37.          }  
  38.   
  39. }  



这样,我们的服务接口和实现类的代码一行不用动,而变成rmi协议的服务已经以rmi://localhost/myService_server 的url启动了。

然后在把客户端的配置略微调整一下:
Java代码  收藏代码
  1. "myServiceClient" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">  
  2.   
  3.    "serviceUrl">  
  4.   
  5.        rmi://localhost/myService_server  
  6.   
  7.      
  8.   
  9.    "serviceInterface">  
  10.   
  11.        whao.test.hessian.server.MyService  
  12.   
  13.      
  14.   
  15.   

客户端的代码也一行不动,在执行下,输出依然是:

HAHAHA: mmmmmmmmm

ok!

然而,我们的remoting其实已经由基于http的hessian改成了rmi。果然不错啊,除了hessian,rmi之外,现在 spring支持的remoting协议还包括jax-rpc,burlap,httpinvoker,也就是说我们的remoting操作协议在这些中 间切换时,利用spring remoting框架,都可以不用改一行代码,已经非常强大了,只是现在还没有corba的支持。
阅读(814) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~