JAVA中几种常用的RPC框架介绍

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

JAVA中⼏种常⽤的RPC框架介绍
1. 浅谈服务治理与微服务
2. RPC框架可以从语⾔兼容和服务治理不同⾓度来划分:
从语⾔兼容上的rpc框架有 thrift zeroC-ICE protbuf
从服务治理⾓度的rpc架构有 dubbo RMI、Hessian spring Cloud
所谓服务治理,主要包括服务发现、负载均衡、容错、⽇志收集等功能
1.dubbo:
使⽤Hessian的序列化协议,传输则是TCP协议,使⽤了⾼性能的NIO框架Netty
服务接⼝
1public interface DemoService { 2 String sayHello(String name); 3 }
服务实现
1public class DemoServiceImpl implements DemoService {
2public String sayHello(String name) {
3return "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 if it’s preferred.
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="/schema/beans"
3 xmlns:xsi="/2001/XMLSchema-instance"
4 xmlns:dubbo="/schema/dubbo"
5 xsi:schemaLocation="/schema/beans /schema/beans/spring-beans.xsd /schema/dubbo /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
1public class Provider {
2public static void main(String[] args) throws Exception {
3 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
4new 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="/schema/beans"
3 xmlns:xsi="/2001/XMLSchema-instance"
4 xmlns:dubbo="/schema/dubbo"
5 xsi:schemaLocation="/schema/beans /schema/beans/spring-beans.xsd /schema/dubbo /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
1public class Consumer {
2public static void main(String[] args) throws Exception {
3 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
4new 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
实现结构图
对外接⼝:
1public interface IService extends Remote {
2
3public String queryName(String no) throws RemoteException;
4
5 }
接⼝实现
1public class ServiceImpl extends UnicastRemoteObject implements IService {
2
17 @Override
18public String queryName(String no) throws RemoteException {
19// ⽅法的具体实现
20 System.out.println("hello" + no);
21return String.valueOf(System.currentTimeMillis());
22 }
1// RMI客户端
2public class Client {
3
4public static void main(String[] args) {
5// 注册管理器
6 Registry registry = null;
7try {
8// 获取服务注册管理器
9 registry = LocateRegistry.getRegistry("127.0.0.1",8088);
10// 列出所有注册的服务
11 String[] list = registry.list();
12for(String s : list){
13 System.out.println(s);
14 }
15 } catch (RemoteException e) {
16
17 }
18try {
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服务端
2public class Server {
3
4public 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包。

相关文档
最新文档