rpc-dubbo简单入门

本文介绍Dubbo框架,一个用于提供高性能RPC远程服务调用及SOA服务治理的分布式服务框架。涵盖Dubbo的核心功能,包括远程通讯、集群容错与自动发现特性。同时对比RPC概念,给出Dubbo的具体使用示例。

什么是dubbo?


Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。


简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式的时候,才有dubbo这样的分布式服务框架的需求,并且本质上是个服务调用的东东,说白了就是个远程服务调用的分布式框架(告别Web Service模式中的WSdl,以服务者与消费者的方式在dubbo上注册)


其核心部分包含


  1. 远程通讯: 提供对多种基于长连接的NIO框架抽象封装,包括多种线程模型,序列化,以及“请求-响应”模式的信息交换方式。

  2. 集群容错: 提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持。

  3. 自动发现: 基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器。


什么是rpc?


RPC(Remote Procedure Call)—远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。RPC协议假定某些传输协议的存在,如TCP或UDP,为通信程序之间携带信息数据。在OSI网络通信模型中,RPC跨越了传输层和应用层。RPC使得开发包括网络分布式多程序在内的应用程序更加容易。


官方架构图



一个官方简单的例子


加入pom依赖


<dependency>    
   <groupId>com.alibaba</groupId>  
   <artifactId>dubbo</artifactId>    
   <version>${dubbo.version}</version>
</dependency>


提供者接口


package com.alibaba.dubbo.demo;

public interface DemoService {    
   String sayHello(String name);
}


提供者实现类


package com.alibaba.dubbo.demo.provider;
import com.alibaba.dubbo.demo.DemoService;

public class DemoServiceImpl implements DemoService {
   public String sayHello(String name) {
       return "Hello " + name;    
   }
}


xml配置


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"      
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      
   xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"      
   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"
>


   <dubbo:application name="demo-provider"/>  
   <dubbo:registry address="multicast://224.5.6.7:1234"/>    
   <dubbo:protocol name="dubbo" port="20880"/>    
   <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>    
   <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>
</beans>


测试:注意224.5.6.7:1234为注册中心,需要自行安装zookepeer


import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {
   public static void main(String[] args) throws Exception {
       ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"META-INF/spring/dubbo-demo-provider.xml"});
       context.start();        // 按任意键退出        
       System.in.read();    
   }
}


消费者xml配置224.5.6.7为zookepeer


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"      
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      
   xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"      
   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"
>
   
   <dubbo:application name="demo-consumer"/>    
   <dubbo:registry address="multicast://224.5.6.7:1234"/>    
   <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService"/>
</beans>


测试消费者


import com.alibaba.dubbo.demo.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Consumer {    
   public static void main(String[] args) throws Exception {        
       ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});        
       context.start();        // 获取远程调用的代理对象      
       DemoService demoService = (DemoService) context.getBean("demoService"); // 执行远程调用        
       String hello = demoService.sayHello("world");        // 显示结果        
       System.out.println(hello);    
   }
}



dubbo调用服务的负载均衡


Random LoadBalance 随机

按权重设置随机概率。 在一个截面上碰撞的概率高,但调用量越大分布越均匀,而且按概率使用权重后也比较均匀,有利于动态调整提供者权重。 

RoundRobin LoadBalance 轮循

按公约后的权重设置轮循比率。 存在慢的提供者累积请求问题,比如:第二台机器很慢,但没挂,当请求调到第二台时就卡在那,久而久之,所有请求都卡在调到第二台上。 

LeastActive LoadBalance 

最少活跃调用数,相同活跃数的随机,活跃数指调用前后计数差。 使慢的提供者收到更少请求,因为越慢的提供者的调用前后计数差会越大。 

ConsistentHash LoadBalance 

一致性Hash,相同参数的请求总是发到同一提供者,比较适合短时间内大量参数一样的请求


当某一台提供者挂时,原本发往该提供者的请求,基于虚拟节点,平摊到其它提供者,不会引起剧烈变动。 


算法参见:http://en.wikipedia.org/wiki/Consistent_hashing。 

缺省只对第一个参数Hash,如果要修改,请配置


<dubbo:parameter key="hash.arguments" value="0,1" /> 


缺省用160份虚拟节点,如果要修改,请配置


<dubbo:parameter key="hash.nodes" value="320" />  


配置如:


<dubbo:service interface="..." loadbalance="roundrobin" />  


或:


<dubbo:reference interface="..." loadbalance="roundrobin" /> 


或:


<dubbo:service interface="..."> 
   <dubbo:method name="..." loadbalance="roundrobin"/>
</dubbo:service>


或:


<dubbo:reference interface="..."> 
   <dubbo:method name="..." loadbalance="roundrobin"/>
</dubbo:reference>


以上 一个简单的rpc就可以了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值