• 服务调用RMI远程方法调用示例【基于Spring配置文件】


    发一下牢骚和主题无关:

            上篇博客,分析了RMI远程方法调用示例,通过示例,可以总结到,其实,也就是被调用则宣布服务,称谓服务端,自动调用则是调用服务,称谓客户端。

             

            既然是远程调用,因此,服务器端要供给宣布的标签,让别人能够找到你这个服务对象,比如webservice,服务端要宣布成wsdl地址,客户端通过wsdl可以找到此服务对象。服务器一旦宣布服务标签,客户端只要根据此便可。

            所以无论何种方法实现分布式调用,或使用何种协议,基本步调或基本思想是想通的。

        

            上篇博客使用普通的java项目,来宣布或调用服务。我们也晓得,硬编码这种方法,一旦改动则需重新编译部署,却不及xml热部署来的快。

            这篇博客,则使用spring来管理rmi宣布或调用服务配置信息,比如宣布的地址或宣布的类。

        

            服务端:

            接口部份:【普通接口】

        

    package com.rmi.server;
    
    import com.entity.Person;
    
    public interface Server {
    
    	String helloWorld(String name);
    
    	Person getPerson(String name, int age) ;
    }

       实现部份:【普通实现】

        

    package com.rmi.server;
    
    import com.entity.Person;
    
    //继承该类, UnicastRemoteObject,暴露远程服务
    public class ServerImpl implements Server {
    
    
    	@Override
    	public String helloWorld(String name) {
    		// TODO Auto-generated method stub
    		return name + ",helloworld";
    	}
    
    	@Override
    	public Person getPerson(String name, int age){
    		// TODO Auto-generated method stub
    		return new Person(name, age);
    	}
    
    }

      若其中传递实体类,仍然需要实现序列化。

        

    package com.entity;
    
    import java.io.Serializable;
    
    public class Person implements Serializable {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public int getAge() {
    		return age;
    	}
    
    	public void setAge(int age) {
    		this.age = age;
    	}
    
    	private String name;
    	private int age;
    
    	// 无参数的构造器
    	public Person() {
    	}
    
    	// 初始化全体属性的构造器
    	public Person(String name, int age) {
    		this.name = name;
    		this.age = age;
    	}
    }

      服务端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" xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    
    <bean id="testSpringRMI" class="com.rmi.server.ServerImpl"/>
    
    <!-- 服务端  配置rmi服务宣布 -->
    <bean id="rmiServiceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
      <!-- 配置service -->
      <property name="service" ref="testSpringRMI"></property>
      <!-- 客户端使用的serviceName -->
      <property name="serviceName" value="testrmiSpring"/>
      <!-- 服务接口 -->
      <property name="serviceInterface" value="com.rmi.server.Server"/>
      
      <!-- 注册服务端口号,默许是1099 -->
      <property name="registryPort" value="1099"/>
    
    </bean>
    
    </beans>
        每日一道理
    “上下五千年,龙的看火不灭;古有愚公志,而今从头越…… ”站在新世纪的门槛上,我们的追求就是让祖国灿烂的喜悦飞扬在美好的明天……

      我们使用服务器启动,而不是手动加载配置文件,web.xml

        

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    	id="WebApp_ID" version="2.5">
    	<display-name>rmiTestServerSpring</display-name>
    
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>classpath:com/config/applicationContext-*.xml</param-value>
    	</context-param>
    	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    </web-app>

            ok,服务器启动后,则便可读取配置文件,然后宣布服务。

            则不用像第一篇博客中在main方法中注册端口以及宣布jndi名称以及响应的类。

            

            客户端一样使用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" xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    
    
    
    <!-- 客户端   调用服务端rmi服务配置-->
    <bean id="testrmiSpring" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
      <!--调用服务端url以及serviceName -->
      <property name="serviceUrl" value="rmi://:1099/testrmiSpring"/>
      <!-- 调用服务端的接口 -->
     <property name="serviceInterface" value="com.rmi.server.Server"/>
    
    </bean>
    
    </beans>

       我们客户端使用java项目,而不是web项目,则直接加载配置文件如下:

        

    package com.client;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.entity.Person;
    import com.rmi.server.Server;
    
    public class TestSpringClient {
      public static void main(String[] args) {
    	  ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-beans.xml");   
          Server testrmiSpring = (Server) context.getBean("testrmiSpring");  
          System.out.println(testrmiSpring.helloWorld("测试rmi"));
          Person person=testrmiSpring.getPerson("lhy", 22);
          System.out.println(person.getName());
    
    }
    }

           这样,就可以解决改动重新编译问题了,这就是xml的优点。其中rmi方法,jdk1.5以后,不需要使用rmi命令来生成stub和skeleton类,而是动态自动生成。

           倘若服务端,不愿使用服务器,则可以向客户端如此手动加载配置文件,然后便可宣布服务。

            

    文章结束给大家分享下程序员的一些笑话语录: 看新闻说中国输入法全球第一!领先了又如何?西方文字根本不需要输入法。一点可比性都没有。

  • 相关阅读:
    linux服务器安装nginx及使用
    服务器搭建
    Linux安装mysql5.7
    个人服务器的选择
    DECODE函数简介
    ORACLE数据库优化
    Mac下JD-GUI无法使用
    Qt 中QString 字符串操作:连接、组合、替换、去掉空白字符
    [Qt初级] 解决 中QMainWindow和QDockWidget添加布局失败问题
    我的JS 中级学习篇
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3074827.html
Copyright © 2020-2023  润新知