利用spring RMI 调用远程服务


发布一个服务:

在spring配置文件中配置

<bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
  <property name="serviceName" value="ClientTaskService" />
  <property name="service" ref="clientTaskService" />
  <property name="serviceInterface" value="cn.com.harbor.rmiservice.task.service.GetClientTaskService" />
  <property name="registryPort" value="1198" />
     <property name="interceptors">
   <list>
    <ref bean="securityInterceptor" />
   </list>
  </property>
 </bean>
 
 <bean id="securityInterceptor" class="cn.com.harbor.rmiservice.interceptor.SecurityInterceptor">
  <!-- 这里配置允许访问RMI的客户端IP地址 -->
  <property name="allowed">
   <set>
    <value>127.0.0.1</value>
    <value>192.168.8.58</value>
   </set>
  </property>
 </bean>

 拦截器代码:

public class SecurityInterceptor implements MethodInterceptor {

	private Set allowed;

	public Object invoke(MethodInvocation methodInvocation) throws Throwable {
		String clientHost = RemoteServer.getClientHost();
		if (allowed != null && allowed.contains(clientHost)) {
			return methodInvocation.proceed();
		} else {
			throw new SecurityException("非法访问。");
		}
	}

	public void setAllowed(Set allowed) {
		this.allowed = allowed;
	}

}
客户端调用:
在spring配置文件中配置
 
 <bean id="approval" class="org.springframework.remoting.rmi.RmiProxyFactoryBean" lazy-init="true">
  <property name="serviceUrl" value="rmi://127.0.0.1:1198/ClientTaskService" />
  <property name="serviceInterface" value="cn.com.harbor.rmiservice.task.service.GetClientTaskService" />
 </bean>
客户端所有引用的类和接口必须和服务端的包路径一样!!!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值