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="code.alibabatech/schema/dubbo"
5        xsi:schemaLocation="/schema/beans /schema/beans/spring-beans.xsd code.alibabatech/schema/dubbo code.alibabatech/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(spring framework是什么框架的
4new String[] {"META-INF/l"});
5        context.start();
6        ad(); // 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="code.alibabatech/schema/dubbo"
5        xsi:schemaLocation="/schema/beans /schema/beans/spring-beans.xsd code.alibabatech/schema/dubbo code.alibabatech/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/l"});
5        context.start();
6        DemoService demoService = (DemoService) Bean("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 = Registry("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 = ateRegistry(8088);
15// 创建⼀个服务
16            ServiceImpl server = new ServiceImpl();
17// 将服务绑定命名
18            bind("vince", server);
23        }
24    }
25 }
3.Hessian
基于HTTP的远程⽅法调⽤,在性能⽅⾯还不够完美,负载均衡和失效转移依赖于应⽤的负载均衡器,H
essian的使⽤则与RMI类似,区别在于淡化了Registry的⾓⾊,通过显⽰的地址调⽤,利⽤HessianProxyFactory根据配置的地址create⼀个代理对象,另外还要引⼊Hessian的Jar包。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。