JAVA中几种常用的RPC框架介绍
-
浅谈服务治理与微服务 http://blog.csdn.net/suifeng3051/article/details/53992560
-
RPC框架可以从语言兼容和服务治理不同角度来划分:
从语言兼容上的rpc框架有 thrift zeroC-ICE protbuf
从服务治理角度的rpc架构有 dubbo RMI、Hessian spring Cloud
所谓服务治理,主要包括服务发现、负载均衡、容错、日志收集等功能
1.dubbo:
使用Hessian的序列化协议,传输则是TCP协议,使用了高性能的NIO框架Netty

服务接口
1 public interface DemoService { 2 String sayHello(String name); 3 }
服务实现
1 public class DemoServiceImpl implements DemoService {
2 public String sayHello(String name) {
3 return "Hello " + name;
4 }
5 }
Configure service provider
The code snippet below shows how a dubbo service provider is configured with spring framework, which is recommended, however you could also use API configuration if it’s preferred.
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 6 <dubbo:application name="demo-provider"/> 7 <dubbo:registry address="multicast://224.5.6.7:1234"/> 8 <dubbo:protocol name="dubbo" port="20880"/> 9 <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/> 10 <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/> 11 </beans>
Start service provider
1 public class Provider {
2 public static void main(String[] args) throws Exception {
3 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
4 new String[] {"META-INF/spring/dubbo-demo-provider.xml"});
5 context.start();
6 System.in.read(); // press any key to exit
7 }
8 }
Configure service consumer
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 6 <dubbo:application name="demo-consumer"/> 7 <dubbo:registry address="multicast://224.5.6.7:1234"/> 8 <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService"/> 9 </beans>
Run service consumer
1 public class Consumer {
2 public static void main(String[] args) throws Exception {
3 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
4 new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});
5 context.start();
6 DemoService demoService = (DemoService) context.getBean("demoService"); // obtain proxy object for remote invocation
7 String hello = demoService.sayHello("world"); // execute remote invocation
8 System.out.println(hello); // show the result
9 }
10 }
2.RMI
- 实现结构图
对外接口:
1 public interface IService extends Remote {
2
3 public String queryName(String no) throws RemoteException;
4
5 }
接口实现
1 public class ServiceImpl extends UnicastRemoteObject implements IService {
2
17 @Override
18 public String queryName(String no) throws RemoteException {
19 // 方法的具体实现
20 System.out.println("hello" + no);
21 return String.valueOf(System.currentTimeMillis());
22 }
1 // RMI客户端
2 public class Client {
3
4 public static void main(String[] args) {
5 // 注册管理器
6 Registry registry = null;
7 try {
8 // 获取服务注册管理器
9 registry = LocateRegistry.getRegistry("127.0.0.1",8088);
10 // 列出所有注册的服务
11 String[] list = registry.list();
12 for(String s : list){
13 System.out.println(s);
14 }
15 } catch (RemoteException e) {
16
17 }
18 try {
19 // 根据命名获取服务
20 IService server = (IService) registry.lookup("vince");
21 // 调用远程方法
22 String result = server.queryName("ha ha ha ha");
23 // 输出调用结果
24 System.out.println("result from remote : " + result);
25 }32 }
33 }
1 // RMI服务端
2 public class Server {
3
4 public static void main(String[] args) {
5 // 注册管理器
6 Registry registry = null;
8 // 创建一个服务注册管理器
9 registry = LocateRegistry.createRegistry(8088);
15 // 创建一个服务
16 ServiceImpl server = new ServiceImpl();
17 // 将服务绑定命名
18 registry.rebind("vince", server);
23 }
24 }
25 }
3.Hessian
基于HTTP的远程方法调用,在性能方面还不够完美,负载均衡和失效转移依赖于应用的负载均衡器,Hessian的使用则与RMI类似,区别在于淡化了Registry的角色,通过显示的地址调用,利用HessianProxyFactory根据配置的地址create一个代理对象,另外还要引入Hessian的Jar包。


浙公网安备 33010602011771号