spring 远程接口访问及集群方案(二)

针对spring远程接口访问,本文给出一个实际的例子供参考,如果有不足之处,尚请原谅

 

RMI调用的过程,我的理解大致如下:

1   客户端和服务器端约定好要用于远程调用的方法的接口(包括方法名和输入输出参数)

2      服务器端将远程服务绑定到某个端口,进行TCP/IP监听(例如监听地址为rmi://127.0.0.1:1099)

3      客户端与服务器建立TCP/IP连接(当然事先要知道服务器端的监听地址),按照接口声明的方法发送数据(可序列化的Java对象)以及要调用的服务名、方法名等。

4      服务器端将接收到的数据(字节流)反序列化,得到要调用的方法和参数信息;然后服务器端进行本地计算(方法调用)

6      服务器端将计算的结果(可序列化的Java对象)发送给客户端。

7      客户端从接收到的返回数据(字节流)中进行反序列化,得到运算结果。

8      客户端断开与服务器端的TCP/IP连接,远程调用结束。

 

服务端开发

 

1.声明要发布为rmi服务的接口

 package demo.rmi.server;

 public interface IHelloWorld{

public void print(String message);

 }

2.实现要发布为RMI服务的接口

package demo.rmi.server;

 

public class HelloWorldImpl implements IHelloWorld {

     public void print (String message) {

                  Sysetm.out.println(message)

       }

    }

}

 首先在Spring配置文件中(这里我命名为server.xml),配置所需要的Bean。

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>

    <bean id="helloWorldService" class="demo.rmi.server.HelloWorldImpl" />

     <!-- define a RMI service listening at port 1009 -->

    <bean id="serviceExportert" class="org.springframework.remoting.rmi.RmiServiceExporter">

        <property name="service"><ref bean=" helloWorldService " /></property>

        <property name="serviceName"><value> helloWorldService </value></property>

        <property name="serviceInterface"><value>demo.rmi.server. IHelloWorld </value></property>

        <!-- defaults to 1099 -->  <property name="registryPort" value="1001" />

    </bean>

</beans>

 

然后启动我们的RMI服务,进行绑定和监听

  package demo.rmi.server;

 

import java.rmi.RemoteException;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.remoting.rmi.RmiServiceExporter;

  

public class RMIServer {

 

    public void startByAPI() {

       RmiServiceExporter exporter = new RmiServiceExporter();

       try {

           exporter.setServiceInterface(IHelloWorld.class);

           exporter.setServiceName("serviceByAPI");

            exporter.setService(new HelloWorldImpl());

           exporter.setRegistryPort(1009);

            exporter.afterPropertiesSet();

       } catch (RemoteException e) {

           System.out.println("error when export service", e);

       }

    } 

  

当我们初始化Spring的ApplicationContext后,Spring会自动完成RMI服务的绑定和监听。

 

客户端开发

首先在Spring配置文件中(这里我命名为client.xml),配置所需要的Bean

<?xml version="1.0" encoding="UTF-8"?>

<beans>

    <!-- define a RMI client service which send to port 1001 -->

<bean id="helloWorldServiceProxy" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">

        <property name="serviceUrl">

            <value>rmi://127.0.0.1:1001/helloWorldService</value>

        </property>

        <property name="serviceInterface">

            <value>demo.rmi.server.IPresence</value>

        </property>

    </bean>

</beans>

然后我们可以编写客户端进行RMI调用的代码。

package demo.rmi.client;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

import demo.rmi.server.IPresence;

 

public class RMIClient {

         public static void main(String[] args) {

                   ApplicationContext context = new ClassPathXmlApplicationContext(

                                     "client.xml");

                            System.out.println("RMI client initialized successfully!");

 

                   // test get a RMI client service via Spring API at runtime

                   // RmiProxyFactoryBean factory = new RmiProxyFactoryBean();

                   // factory.setServiceInterface(IHelloWorld.class);

                   // factory.setServiceUrl("rmi://127.0.0.1:1009/helloWorldService");

                   // factory.afterPropertiesSet();

                   //

                   // IPresence service2 = (IHelloWorld) factory.getObject();

        

                   IPresence service = (IPresence) context

                                     .getBean("helloWorldServiceProxy ");

                   StringBuilder presentitiesInfo = new StringBuilder();

                   Service.print(“hello world!”);

         }

 

}

如果在服务端显示"hello world"提示,则表示spring rmi远程接口调用成功

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值