前言

Apache Dubbo (incubating) |ˈdʌbəʊ| 是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。

1、特性

面向接口代理的高性能RPC调用

提供高性能的基于代理的远程调用能力,服务以接口为粒度,为开发者屏蔽远程调用底层细节。

智能负载均衡

内置多种负载均衡策略,智能感知下游节点健康状况,显著减少调用延迟,提高系统吞吐量。

服务自动注册与发现

支持多种注册中心服务,服务实例上下线实时感知。

高度可扩展能力

遵循微内核+插件的设计原则,所有核心能力如Protocol、Transport、Serialization被设计为扩展点,平等对待内置实现和第三方实现。

运行期流量调度

内置条件、脚本等路由策略,通过配置不同的路由规则,轻松实现灰度发布,同机房优先等功能。

可视化的服务治理与运维

提供丰富服务治理、运维工具:随时查询服务元数据、服务健康状态及调用统计,实时下发路由策略、调整配置参数。

2、节点角色

角色说明
Provider暴露服务的服务提供方
Consumer调用远程服务的服务消费方
Registry服务注册与发现的注册中心
Monitor统计服务的调用次数和调用时间的监控中心
Container服务运行容器

我们再来看一张官网提供的架构图:

关于更多Dubbo的架构、用法等,请访问官网: http://dubbo.apache.org/zh-cn/index.html

一、与Spring集成

Dubbo 采用全 Spring 配置方式,透明化接入应用,对应用没有任何 API 侵入,只需用 Spring 加载 Dubbo 的配置即可,Dubbo 基于Spring 的 Schema 扩展 进行加载。

所以,我们想使用Dubbo,只需引入Maven坐标。Dubbo里面已经包含了Spring、Zookeeper、Netty等。

  1. <dependency>

  2.    <groupId>com.alibaba</groupId>

  3.    <artifactId>dubbo</artifactId>

  4.    <version>2.6.2</version>

  5. </dependency>


  6. <dependency>

  7.    <groupId>com.101tec</groupId>

  8.    <artifactId>zkclient</artifactId>

  9.    <version>0.10</version>

  10. </dependency>

首先,我们要定义接口和实现类,在这里我们还是搞一个操作用户信息的接口。

1、接口

  1. package com.viewscenes.netsupervisor.service;

  2. import java.util.List;

  3. import com.viewscenes.netsupervisor.entity.InfoUser;


  4. public interface InfoUserService {


  5.    void insertInfoUser(InfoUser user);

  6.    InfoUser getUserById(String id);    

  7.    void deleteUserById(String id);

  8.    List<InfoUser> getAllUser();

  9. }

  1. package com.viewscenes.netsupervisor.service.impl;


  2. public class InfoUserServiceImpl implements InfoUserService{


  3.    private Logger logger = LoggerFactory.getLogger(this.getClass());


  4.    private Map<String,InfoUser> userMap = new HashMap<String, InfoUser>();


  5.    public void insertInfoUser(InfoUser user) {

  6.        logger.info("新增用户信息...{}",JSONObject.toJSONString(user));

  7.        userMap.put(user.getId(), user);

  8.    }

  9.    public List<InfoUser> getAllUser(){

  10.        List<InfoUser> infoUserList = new ArrayList<InfoUser>();

  11.        for(Map.Entry<String, InfoUser> entry:userMap.entrySet()) {

  12.            infoUserList.add(entry.getValue());

  13.        }

  14.        System.out.println("获取全部用户数据:"+infoUserList.size());

  15.        return infoUserList;

  16.    }

  17. }

2、Provider

我们先来看一下生产者端的配置文件。

  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://dubbo.apache.org/schema/dubbo"

  5.            xsi:schemaLocation="http://www.springframework.org/schema/beans

  6.            http://www.springframework.org/schema/beans/spring-beans-4.3.xsd

  7.            http://dubbo.apache.org/schema/dubbo

  8.            http://dubbo.apache.org/schema/dubbo/dubbo.xsd">  


  9.       <!-- 提供方应用信息,用于计算依赖关系 -->

  10.    <dubbo:application name="dubbo_producer1"/>  

  11.    <!-- 使用zookeeper注册中心暴露服务地址 -->

  12.    <dubbo:registry address="zookeeper://192.168.139.129:2181?client=zkclient"/>    

  13.    <!-- 用dubbo协议在20880端口暴露服务 -->

  14.    <dubbo:protocol name="dubbo" port="20880"/>    

  15.    <!-- Spring的Bean -->

  16.       <bean id="infoUserService" class="com.viewscenes.netsupervisor.service.impl.InfoUserServiceImpl" />    

  17.    <!-- 服务配置,暴露一个接口服务  -->

  18.       <dubbo:service interface="com.viewscenes.netsupervisor.service.InfoUserService" ref="infoUserService" />

  19. </beans>

3、Consumer

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  3.       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"

  4.       xmlns="http://www.springframework.org/schema/beans"

  5.       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd

  6.       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">


  7.    <!-- 消费者方应用信息,用于计算依赖关系 -->

  8.    <dubbo:application name="dubbo_consumer"/>

  9.    <!-- 用于配置连接注册中心相关信息 -->

  10.    <dubbo:registry protocol="zookeeper" address="192.168.139.129:2181" client="zkclient" />

  11.    <!-- 引用配置   用于创建一个远程接口服务代理 -->

  12.    <dubbo:reference id="infoUserService" check="false" interface="com.viewscenes.netsupervisor.service.InfoUserService"/>

  13. </beans>

4、启动

配置好之后,我们就可以在Spring项目中分别启动生产者端和消费者端。当然,记得先要把zookeeper服务器启动才行。

public class Provider1 {    public static void main(String[] args) throws IOException {        ClassPathXmlApplicationContext applicationContext = new            ClassPathXmlApplicationContext(new String[]{"classpath:dubbo_provider1.xml"});              applicationContext.start();        System.in.read();    }}

然后,我们通过消费者端来创建一个用户,并不断的请求用户查询方法。

  1. public class Consumer1 {    

  2.    @SuppressWarnings("resource")

  3.    public static void main(String[] args) {


  4.        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext

  5.                (new String[]{"classpath:dubbo_consumer1.xml"});

  6.        context.start();


  7.        InfoUserService userService = (InfoUserService) context.getBean("infoUserService");


  8.        InfoUser infoUser = new InfoUser();

  9.        infoUser.setId(UUID.randomUUID().toString());

  10.        infoUser.setName("Jack");

  11.        infoUser.setAddress("BeiJing");

  12.        userService.insertInfoUser(infoUser);


  13.        while(true) {

  14.            List<InfoUser> userList = userService.getAllUser();

  15.            System.out.println("查询用户信息返回数据:"+JSONObject.toJSONString(userList));

  16.            try {

  17.                TimeUnit.SECONDS.sleep(1);

  18.            } catch (InterruptedException e) {

  19.                e.printStackTrace();

  20.            }

  21.        }

  22.    }

  23. }

通过上面的代码,消费者端就可以源源不断的从生产者端获取数据。以上为Dubbo框架的最基础应用,怎么样?简单吧。快来试一试~