spring RPC
Spring对RPC有丰富的支持
给出了以下几种方案
-
RMI
基于RMI协议,使用java的序列化机制,客户端服务端都必须时java,RMI协议不被防火墙支持,只能在内网使用
-
Hessian
基于HTTP协议,使用自身的序列化机制,客户端服务端可以是不同的语言,HTTP协议被防火墙支持,可被外网访问
-
Burlap
已过时
-
HttpInvoker
基于HTTP协议,使用java的序列化机制,客户端服务端都必须时java,必须使用spring,HTTP协议被防火墙支持,可被外网访问
-
web service
基于SOAP协议,使用自身的序列化机制,客户端服务端可以是不同的语言,HTTP+XML(WSDL语法)协议被防火墙支持,可被外网访问
上代码:
-
客户端
- 要连接的RPCService
import javax.jws.WebParam;
import com.zgg.group2.rpcserver.bean.RPCPojo;
public interface RpcService {
RPCPojo get();
RPCPojo post(@WebParam RPCPojo obj);
RPCPojo put(@WebParam RPCPojo obj);
RPCPojo delete(@WebParam Integer id);
}
- 绑定
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.caucho.HessianProxyFactoryBean;
import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;
import org.springframework.remoting.rmi.RmiProxyFactoryBean;
import com.zgg.group2.rpcserver.api.RpcService;
@Configuration
public class RpcBundler {
//RMI
@Bean
public RmiProxyFactoryBean rpcService() {
RmiProxyFactoryBean factory = new RmiProxyFactoryBean();
//url- rmi://ip:port/serviceName
factory.setServiceUrl("rmi://127.0.0.1:1099/rpcService");
factory.setServiceInterface(RpcService.class);
return factory;
}
//Hessian
// @Bean
public HessianProxyFactoryBean rpcHessianService() {
HessianProxyFactoryBean proxy = new HessianProxyFactoryBean();
//url- http://ip:port/finalName/hessianBeanName
proxy.setServiceUrl("http://127.0.0.1:8080/group2-level1-web-rest/hessian");
proxy.setServiceInterface(RpcService.class);
return proxy;
}
//Spring HTTP-invoker
// @Bean
public HttpInvokerProxyFactoryBean rpcInvokerService() {
HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
//url- http://ip:port/finalName/invokerBeanName
proxy.setServiceUrl("http://127.0.0.1:8080/group2-level1-web-rest/invoker");
proxy.setServiceInterface(RpcService.class);
return proxy;
}
}
-
服务端
- 要发布的RPCService
import javax.jws.WebParam;
import com.zgg.group2.rpcserver.bean.RPCPojo;
public interface RpcService {
RPCPojo get();
RPCPojo post(@WebParam RPCPojo obj);
RPCPojo put(@WebParam RPCPojo obj);
RPCPojo delete(@WebParam Integer id);
}
- 发布
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.caucho.HessianServiceExporter;
import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;
import org.springframework.remoting.rmi.RmiServiceExporter;
import com.zgg.group2.rpcserver.api.RpcService;
@Configuration
public class RpcPublisher {
//RMI
@Bean
public RmiServiceExporter exporter(RpcService rpcService) {
RmiServiceExporter exporter = new RmiServiceExporter();
exporter.setService(rpcService);
exporter.setServiceName("rpcService");
exporter.setServiceInterface(RpcService.class);
exporter.setRegistryPort(1099);
return exporter;
}
//Hessian
// @Bean(name="/hessian")
public HessianServiceExporter hessianExporter(RpcService rpcService) {
HessianServiceExporter exporter = new HessianServiceExporter();
exporter.setService(rpcService);
exporter.setServiceInterface(RpcService.class);
return exporter;
}
//Spring HTTP-invoker
// @Bean(name="/invoker")
public HttpInvokerServiceExporter invokerExporter(RpcService rpcService) {
HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
exporter.setService(rpcService);
exporter.setServiceInterface(RpcService.class);
return exporter;
}
//web service
//spring继承坑无数,不建议使用,读者可对齐自行封装
}