JAVA中幾種常用的RPC框架介紹


  1. 淺談服務治理與微服務 http://blog.csdn.net/suifeng3051/article/details/53992560

  2. 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包。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM