【Java Web开发学习】Spring发布RMI服务

【Java 远程服务】Spring发布RMI服务

转载:https://www.cnblogs.com/yangchongxing/p/9084066.html

RmiServiceExporter可以把任意Spring管理的Bean发布为RMI服务。

1、Service端口实现,我自己是在Spring MVC种集成这

复制代码
package cn.ycx.web.service;

public interface DateRemote {
    public String now();
}



package cn.ycx.web.service;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.stereotype.Service;
@Service
public class DateRemoteImpl implements DateRemote {
    @Override
    public String now() {
        SimpleDateFormat format = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
        return format.format(new Date());
    }
}
复制代码

配置@Bean

复制代码
@Bean
public RmiServiceExporter rmiExporter(DateRemote dateRemote) {
    RmiServiceExporter rmiExporter = new RmiServiceExporter();
    rmiExporter.setService(dateRemote);
    rmiExporter.setServiceName("DateRemoteService");
    rmiExporter.setServiceInterface(DateRemote.class);
    rmiExporter.setRegistryPort(1199);//不设置默认为端口1099
    return rmiExporter;
}
复制代码

2、客户端

服务接口

package cn.ycx.service;

public interface DateRemote {
    public String now();
}
复制代码
package cn.ycx.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.rmi.RmiProxyFactoryBean;

import cn.ycx.service.DateRemote;

@Configuration
public class TestConfig {
    @Bean
    public RmiProxyFactoryBean dateRemote() {
        RmiProxyFactoryBean rmiProxy = new RmiProxyFactoryBean();
        rmiProxy.setServiceUrl("rmi://localhost:1199/DateRemoteService");// localhost也可更换为本机IP
        rmiProxy.setServiceInterface(DateRemote.class);
        return rmiProxy;
    }
}
复制代码
复制代码
package cn.ycx.test;

import static org.junit.Assert.assertNotNull;

import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.ycx.config.TestConfig;
import cn.ycx.service.DateRemote;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=TestConfig.class)
public class Test {
    @Autowired
    DateRemote dateRemote;
    
    @Before
    public void setUp() throws Exception {
    }

    @org.junit.Test
    public void test() {
        System.out.println(dateRemote.now());
        assertNotNull(dateRemote);
    }
}
复制代码

发布服务是指定Host待续...

 

posted @   翠微  阅读(384)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示