zoukankan      html  css  js  c++  java
  • Spring RMI Example

    一: 提供服务的远程一端

    1-1. applicationContext.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"
        xsi:schemaLocation="  
        http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
        <bean id="userRmiServiceImpl" class="com.goodfan.rmi.service.impl.UserRmiServiceImpl" />
        <bean id="userRmi" class="org.springframework.remoting.rmi.RmiServiceExporter">
            <property name="service" ref="userRmiServiceImpl" />
            <property name="serviceName" value="userRmi" />
            <property name="serviceInterface" value="com.goodfan.rmi.service.UserRmiService" />
            <property name="registryPort" value="9999" />
        </bean>
    </beans>  

    1-2. 接口

    package com.goodfan.rmi.service;
    
    public interface UserRmiService {
         public String sayHello(User user);
    }

    1-3. javabean

    package com.goodfan.rmi.service;
    
    import java.io.Serializable;  
    
    public class User implements Serializable{  
      
        private static final long serialVersionUID = 8550373205815267923L;  
        private String userName;  
      
        public String getUserName() {  
            return userName;  
        }  
      
        public void setUserName(String userName) {  
            this.userName = userName;  
        }  
      
    } 

    1-4. 实现类

    package com.goodfan.rmi.service.impl;
    
    import com.goodfan.rmi.service.User;
    import com.goodfan.rmi.service.UserRmiService;
    
    public class UserRmiServiceImpl implements UserRmiService {
    
        @Override
        public String sayHello(User user) {
             return "Hello, " + user.getUserName();
        }
    
    }

    1-5. ServerTest类

    package com.goodfan.rmi.service;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;  
    
    public class ServerTest {  
      
        public static void main(String[] args) {  
            System.setProperty("java.rmi.hostname", "10.7.3.12");   
            new ClassPathXmlApplicationContext("applicationContext.xml");  
            System.out.println("server start......");  
        }  
    }

    二: 本地调用一端

    2-1. applicationContext-client

    <?xml version="1.0" encoding="UTF-8"?>  
    <beans xmlns="http://www.springframework.org/schema/beans"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xsi:schemaLocation="  
    http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
      
        <bean id="rmiProxy" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">  
            <property name="serviceUrl" value="rmi://10.7.3.12:9999/userRmi"/>  
            <property name="ServiceInterface" value="com.goodfan.rmi.service.UserRmiService" />  
        </bean>  
    </beans> c

    2-2. ClientTest类

    package com.goodfan.rmi.service;
    
    import org.springframework.context.ApplicationContext;  
    import org.springframework.context.support.ClassPathXmlApplicationContext;  
      
    public class ClientTest {  
      
        public static void main(String[] args) {  
            ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-client.xml");  
            UserRmiService ums = (UserRmiService) ctx.getBean("rmiProxy");  
            User user = new User();  
            user.setUserName("RMI");  
            System.out.println(ums.sayHello(user));  
        }  
    } 
  • 相关阅读:
    读懂Netty的高性能架构之道
    大型网站架构演变和知识体系(转载)
    SAX,功能强大的 API
    防雪崩利器:熔断器 Hystrix 的原理与使用
    分布式系统设计系列 -- 基本原理及高可用策略
    分布式系统的事务处理
    分布式服务框架之服务化最佳实践
    深入理解 Java 虚拟机:JVM 高级特性与最佳实践
    内存屏障
    IntelliJ IDEA 2016 破解旗舰版
  • 原文地址:https://www.cnblogs.com/rocky-fang/p/5502903.html
Copyright © 2011-2022 走看看