1. 程式人生 > >spring整理(四)Spring RPC

spring整理(四)Spring RPC

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繼承坑無數,不建議使用,讀者可對齊自行封裝
}