java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE, JavaME, JavaSE)的总称。本站提供基于Java框架struts,spring,hibernate等的桌面应用、web交互及移动终端的开发技巧与资料
保持永久学习的心态,将成就一个优秀的你,来 继续搞起java知识。
一: 服务端 暴露服务
</b> <br/>view plaincopyprint?<br/>package com.xx.service; <br/>/** <br/> * 定义远程服务接口 <br/> * 1.可以不继承java.rmi.Remote接口 <br/> * 2.方法可以不抛出java.rmi.RemoteException异常 <br/> * <br/> */ <br/>public interface ISayHelloService { <br/> public String doSayHello(String name); <br/>} <br/>1package com.xx.service;
/**
* 定义远程服务接口
* 1.可以不继承java.rmi.Remote接口
* 2.方法可以不抛出java.rmi.RemoteException异常
*
*/
public interface ISayHelloService {
public String doSayHello(String name);
}
</b> <br/>view plaincopyprint?<br/>package com.xx.service.impl; <br/>import com.xx.service.ISayHelloService; <br/>/** <br/> * 远程接口实现 <br/> */ <br/>public class ChinaSayHelloServiceImpl implements ISayHelloService { <br/> public String doSayHello(String name) { <br/> return "hello " + name; <br/> } <br/>} <br/>1package com.xx.service.impl;
import com.xx.service.ISayHelloService;
/**
* 远程接口实现
*/
public class ChinaSayHelloServiceImpl implements ISayHelloService {
public String doSayHello(String name) {
return "hello " + name;
}
}
</b> <br/>view plaincopyprint?<br/>package com.xx.service; <br/>import org.springframework.context.ApplicationContext; <br/>import org.springframework.context.support.ClassPathXmlApplicationContext; <br/>/** <br/> * 服务端 <br/> * 暴露远程服务 <br/> */ <br/>public class ServerMain { <br/> public static void main(String[] args) { <br/> ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:applicationContext-server.xml"}, true); <br/> System.out.println("==============服务启动成功 =============="); <br/> } <br/>} <br/>1package com.xx.service;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 服务端
* 暴露远程服务
*/
public class ServerMain {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:applicationContext-server.xml"}, true);
System.out.println("==============服务启动成功 ==============");
}
}
view plaincopyprint?
spring配置文件 applicationContext-server.xml
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
spring配置文件 applicationContext-server.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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="chinaSayHelloService" class="com.xx.service.impl.ChinaSayHelloServiceImpl" /> <bean id="chinaSayHelloServiceRmi" class="org.springframework.remoting.rmi.RmiServiceExporter" > <property name="serviceName" value="chinaSayHelloService" /> <property name="service" ref="chinaSayHelloService"/> <property name="serviceInterface" value="com.xx.service.ISayHelloService"/> <property name="registryPort" value="9999"/> </bean> </beans>
二:客户端 远程方法调用
</b> <br/>view plaincopyprint?<br/>package com.xx.service; <br/>import java.net.UnknownHostException; <br/>import org.springframework.context.ApplicationContext; <br/>import org.springframework.context.support.ClassPathXmlApplicationContext; <br/>/** <br/> * 客户端 <br/> */ <br/>public class ClientMain { <br/> public static void main(String[] args) throws UnknownHostException { <br/> ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext-client.xml"); <br/> ISayHelloService object = applicationContext.getBean("chinaSayHelloServiceRmi", ISayHelloService.class); <br/> System.out.println(object.doSayHello("张三")); <br/> } <br/>} <br/>1package com.xx.service;
import java.net.UnknownHostException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 客户端
*/
public class ClientMain {
public static void main(String[] args) throws UnknownHostException {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext-client.xml");
ISayHelloService object = applicationContext.getBean("chinaSayHelloServiceRmi", ISayHelloService.class);
System.out.println(object.doSayHello("张三"));
}
}
</b> <br/>view plaincopyprint?<br/>spring配置文件 applicationContext-client.xml <br/><?xml version="1.0" encoding="UTF-8"?> <br/><beans xmlns="http://www.springframework.org/schema/beans" <br/> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <br/> xmlns:context="http://www.springframework.org/schema/context" <br/> xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd <br/> http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <br/> <bean id="chinaSayHelloServiceRmi" class="org.springframework.remoting.rmi.RmiProxyFactoryBean" > <br/> <property name="serviceUrl" value="rmi://192.168.3.104:9999/chinaSayHelloService" /> <br/> <property name="serviceInterface" value="com.xx.service.ISayHelloService"/> <br/> </bean> <br/></beans> <br/>1spring配置文件 applicationContext-client.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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="chinaSayHelloServiceRmi" class="org.springframework.remoting.rmi.RmiProxyFactoryBean" > <property name="serviceUrl" value="rmi://192.168.3.104:9999/chinaSayHelloService" /> <property name="serviceInterface" value="com.xx.service.ISayHelloService"/> </bean> </beans>
rmi
因为水平有限,难免有疏忽或者不准确的地方,希望大家能够直接指出来,我会及时改正。一切为了知识的分享。
后续会有更多的精彩的内容分享给大家。
支付宝扫一扫
微信扫一扫
