使用RMI服务
创建RMI服务的基本步骤
- 编写一个服务实现类,类中的方法必须抛出java.rmi.RemoteException异常
- 创建一个继承于java.rmi.Remote的服务接口
- 运行RMI编译器(rmic),创建额客户端的stub类和服务端的skeleton类
- 启动一个RMI注册表,以便持有这些服务
- 在RMI注册表中注册服务
在Spring中配置RMI服务
服务端(com.yrls.*)
- IUserService.java
package com.yrls.service;
public interface IUserService {
public String getStr(String abc);
}
- UserServiceImpl.java
package com.yrls.service.impl;
import com.yrls.service.IUserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service("userService")
public class UserServiceImpl implements IUserService {
@Override
public String getStr(String abc){
return abc;
}
}
- spring-mvc
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/mvc"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 扫描web相关的bean -->
<context:component-scan base-package="com.yrls.controller"/>
<!-- 开启SpringMVC注解模式 -->
<mvc:annotation-driven/>
<!-- 静态资源默认servlet配置 -->
<mvc:default-servlet-handler/>
<!--开启AOP-->
<aop:aspectj-autoproxy/>
<bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<!--暴露的服务名称-用于客户端获取-->
<property name="serviceName" value="RpcUserService" />
<!--暴露的服务-userService是来自@Bean注解UserServiceImpl.java得的Bean-->
<property name="service" ref="userService" />
<!--暴露的服务接口-->
<property name="serviceInterface"
value="com.yrls.service.IUserService" />
<property name="registryPort" value="8080" />
<property name="servicePort" value="1099" />
</bean>
<!-- bean(userService)也可以在xml中配置 -->
<!--
<bean id="userService" class="com.yrls.service.impl.UserServiceImpl"/>
-->
<!--配置jsp 显示ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
客户端(com.rpc.*)
- IUserService
package com.rpc.service;
public interface IUserService {
public String getStr(String abc);
}
- Spring-mvc.xml
<bean id="mobileAccountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<!--8080端口对应服务端tomcat端口-->
<!--localhost对应服务端的ip-->
<!--RpcUserService暴露的服务名称-->
<property name="serviceUrl" value="rmi://localhost:8080/RpcUserService" />
<!-- 接口为client包里的接口,跟服务端的一致-->
<!-- 接口的实现调用服务端暴露的远程服务-->
<property name="serviceInterface"
value="com.rpc.service.IUserService" />
</bean>
- UserController.java
package com.rpc.controller;
import com.rpc.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Component
@Controller
@RequestMapping("/rpc")
public class UserController {
@Autowired
private IUserService service;
@ResponseBody
@RequestMapping("cc")
public String cc(){
String a = service.getStr2("Hello RMI!!!");
return a;
}
}