RMI远程调用
Rmi_Spring (不用在接口中继承Remote,在这里关联起来就可以了) 3个地方设置就可以了
1. config.xml(关联RMI的设置)
<bean id="userService" class="example.chapter8.UserServiceImpl" /> 实体类关联
<bean id="rmiService" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName" value="UserService" /> 客户使用
<property name="service" ref="userService" /> Rmi与类关联
<property name="serviceInterface" value="example.chapter8.UserService" /> 接口关联
<property name="registryPort" value="1099" /> 客户使用
</bean>
2 .启动RMI
public class Main {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("config.xml");
}
}
3.客户端使用
package example.chapter8.client;
import org.springframework.remoting.rmi.RmiProxyFactoryBean;
import example.chapter8.UserService;
public class Client {
public static void main(String[] args) throws Exception {
RmiProxyFactoryBean factory = new RmiProxyFactoryBean();
factory.setServiceInterface(UserService.class);
factory.setServiceUrl("rmi://localhost:1099/UserService");
factory.afterPropertiesSet();
UserService userService = (UserService)factory.getObject();
userService.create("test", "password");
System.out.println(userService.login("test", "password"));
try {
userService.login("test", "bad-password");
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
RMI最大的缺点是使用特定的JRMP(Java Remote Method Protocol,Java远程
方法协议)二进制协议,很难穿透防火墙,公适合在企业内部网中使用。如果需要防火墙调用,则HTTP协议几乎唯一的选择.
Spring支持三种Http远程调用 :Hessian,Burlap,Http Invoker.
Hessian和Burlap这两种集成在Resin服务器内部,可以直接使用.
Hessian是一个二进制协议(效率高,但很难被读懂),而Burlap是一个基于XML的协议。其他语言只要能解析XML,就可以使用Bulap和Java程序通信.这两种协议是私有协议,并没有成为标准,因此仅适合Java系统内跨越防火墙进行调用.
Http协议的远程调用 (以Hessian为例,其他的使用方法基本类似)
服务端(包含TimeService接口,TimeServiceImpl类)
1关联
<bean id="timeService" class="example.chapter8.TimeServiceImpl" />
<bean name="/HessianService" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="timeService" />
<property name="serviceInterface" value="example.chapter8.TimeService" />
</bean>
web.xml(url样式定义成/remote/*)
<web-app>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/remote/*</url-pattern>
</servlet-mapping>
</web-app>
2.没有启动RMI这步骤了吗?(Http协议的远程调用没有这个步骤,这个又不是RMI远程调用。)
客户端
2 使用 config.xml
<bean id="hessianService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl" value="http://localhost:8080/remote/HessianService" /> //由服务器端的样式定义和Bean 的名字组成。
<property name="serviceInterface" value="example.chapter8.TimeService"/>
</bean>
TimeService service = (TimeService)context.getBean("hessianService");
service.getTime();
Web服务
一 就是一种能够实现跨平台远程调用技术标准,即应用程序通过向外界暴露一个可以通过Web来远程调用的API接口,客户端就可以调用这个服务接口来实现相应的功能。
可以把Web服务看成是基于因特网的远程调用,有了Web服务,Web网站和web网站之间就不再是简单地用链接连起来,而是可以互相调用的Web应用程序。
web服务是一种面向服务架构技术,由于XML是一咱平台无关的描述语言,因此,在Web服务中,XML被用来描述如何传递数据。这样,调用双方就可以是任意的异构平台。
二 Web服务大致可以应用在以下领域:
1.面向企业的Web服务,包括企业内部的各种ERP系统和企业间合作伙伴的系统对接。由于大企业内部或企业之间常常中异构平台,因此,采用Web服务来实现系统集成非常合适。
2.面向消费的Web服务,尤其是B2C站点。通过Web服务,第三方桌面应用程序就可以为用户提供更方便的服务,例如,个人理财软件可以通过Web服务获得最新的股票价格。
3.面向终端设备的Web服务。包括手机,PDA等各种移动终端,可以通过服务轻松实现天气预报,酒店预订等服务。
三 实现:
web服务调用者 <-------(获取WDSL文件) WSDL文件 <------- (发布WSDL文件) Web服务发布者
------------------------------------------------------------->(发送SOAP请求)
(返回SOAP请求) <---------------------------------------------------------
WSDL文件,即Web Services Description Language ,这个XML文件定义了调用Web服务的所有信息,包括所有的方法名称,参数类型,返回类型和数据类型的映射等。
Web服务的调用者只要该WSDL文件,就可以根据WSDL文件通过相应的工具生成客户端支持类。
四为了在Java中实现Web服务,还必须获得一个具体的实现。Axis和XFire都是实现了完整的Web服
务协议的库。可以用于访问和发布Web服务。
1.Axis(在有网络的情况下练习CallAmozonWs,看一下WSDL文件的构成)
调用Web服务,要知道WSDL文件的URL(包含调用Web服务的全部信息),调用前还要使用
WSDL2Java来根据WSDl自动生成客户端支持类.
总结:仅在企业内部网中使用,并且通信双方都是Java平台,可以考虑使用RMI,如查需要通过因特网调用,或者通信双方是异构系统,则Web服务几乎是唯一的选择。