Dubbo的前身今世

首先是Dubbox,Dubbo开源过后,当当网根据自身的需求,为Dubbo实现了一些新的功能,并将其命名为Dubbox(即Dubbo eXtensions)。
更新的功能为
①支持REST风格远程调用(HTTP + JSON/XML)
②支持基于Kryo和FST的Java高效序列化实现
③支持基于嵌入式Tomcat的HTTP remoting体系
④升级Spring:将dubbo中Spring由2.x升级到目前最常用的3.x版本,减少项目中版本冲突带来的麻烦。
⑤升级ZooKeeper客户端:将dubbo中的zookeeper客户端升级到最新的版本,以修正老版本中包含的bug。
另外dubbox和dubbo 2.x是兼容的,没有改变dubbo的任何已有的功能和配置方式(除了升级了Spring之类的版本)。
Dubbo的核心架构
Dubbo框架设计一共划分了10个层:
服务接口层(Service)、配置层(Config)、服务代理层(Proxy)、服务注册层(Registry)、集群层(Cluster)、监控层(Monitor)、远程调用层(Protocol)、信息交换层(Exchange)、网络传输层(Transport)和数据序列化层(Serialize)。在这里插入图片描述Dubbo核心要点
① 服务定义
服务是围绕服务提供方和服务消费方的,服务提供方实现服务,而服务消费方调用服务。
② 服务注册
Dubbo提供的注册中心有如下几种类型可供选择:
Multicast(多播/组播)注册中心(开发测试用)、Zookeeper注册中心(生产环境用官方推荐)、Redis注册中心、Simple注册中心
③ 服务监控
无论是服务提供方,还是服务消费方,他们都需要对服务调用的实际状态进行有效的监控,从而改进服务质量。
④ 远程通信与信息交换
远程通信需要指定通信双方所约定的协议,在保证通信双方理解协议语义的基础上,还要保证高效、稳定的消息传输。Dubbo继承了当前主流的网络通信框架,主要包括如下几个:
Mina、Netty、Grizzly
⑤ 服务调用
在这里插入图片描述节点角色说明:
Provider: 暴露服务的服务提供方。
Consumer: 调用远程服务的服务消费方。
Registry: 服务注册与发现的注册中心。
Monitor: 统计服务的调用次调和调用时间的监控中心。
Container: 服务运行容器。
dubbo调用关系说明
0. 服务容器负责启动,加载,运行服务提供者。

  1. 服务提供者在启动时,向注册中心注册自己提供的服务。
  2. 服务消费者在启动时,向注册中心订阅自己所需的服务。
  3. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
  4. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
  5. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
    Dubbo的hello world
    官方Maven仓库
    阿里巴巴已将Dubbo已发布到Maven中央仓库中:
    http://central.maven.org/maven2/com/alibaba/dubbo。
<project>
    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.4</version>
        </dependency>
    </dependencies>
</project>

服务提供者: 提供服务接口的实现,发布服务地址,提供服务。
服务消费者:获取服务地址,使用服务接口调用服务,处理服务调用结果。
公共项目:包含公共配置、DO、VO、工具包等…
项目搭建(Maven项目管理方式):
① 创建公共项目工程:普通的Maven工程,dubbo-parent。
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>dubbo-parent</groupId>
    <artifactId>dubbo-parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>dubbo-user</module>
        <module>dubbo-order</module>
        <module>dubbo-api</module>
    </modules>
   <dependencies>
       <!-- dubbojar包 -->
       <dependency>
           <groupId>com.alibaba</groupId>
           <artifactId>dubbo</artifactId>
           <version>2.6.2</version>
       </dependency>

       <!-- junit测试包 -->
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.12</version>
           <!--scope标签表示作用域-->
           <scope>test</scope>
       </dependency>

       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-test</artifactId>
           <version>4.3.16.RELEASE</version>
       </dependency>
      <!--直接依赖zookeeper-->
       <dependency>
           <groupId>com.github.sgroschupf</groupId>
           <artifactId>zkclient</artifactId>
           <version>0.1</version>
       </dependency>
   </dependencies>
</project>

② 创建服务提供者项目:普通的Maven工程(依赖Dubbo)dubbo-parent的子项目,提供服务实现、服务启动功能。pom.xml

 <!-- dubbojar包 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.6.2</version>
    </dependency>

    <!-- junit测试包 -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <!--scope标签表示作用域-->
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.3.16.RELEASE</version>
    </dependency>
   <!--直接依赖zookeeper-->
    <dependency>
        <groupId>com.github.sgroschupf</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.1</version>
    </dependency>
</dependencies>

配置文件provider.xml

<!-- 提供方应用信息,用于计算依赖关系 一般取项目名-->
<dubbo:application name="dubbo-order"  />

<!-- 使用multicast广播注册中心暴露服务地址 -->
<!--<dubbo:registry address="multicast://224.5.6.7:1234" />-->
<dubbo:registry address="zookeeper://localhost:2181" client="zkclient"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />

<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="heng.DemoService" ref="demoService" />

<!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="heng.impl.DemoServiceImpl" />

测试启动服务监听

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath*:/consumer.xml"})
public class DubboServiceTest {
    @Reference
    private DemoService demoService;

    @Test
    public void testHello(){
        String sayHi = demoService.sayHello("老宋");
        System.out.println(sayHi);
    }
}

服务消费者实现
创建服务消费者项目:普通的Maven工程(依赖Dubbo),完成服务调用功能。
pom.xml无依赖,直接依赖父项目
consumer.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://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
             http://dubbo.apache.org/schema/dubbo
                http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="dubbo-user"  />

    <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    <!--<dubbo:registry address="multicast://224.5.6.7:1234" />-->
    <dubbo:registry address="zookeeper://localhost:2181" client="zkclient"/>

    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="demoService" interface="heng.DemoService" />
</beans>
JUnit4调用dubbo服务测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath*:/consumer.xml"})
public class DubboServiceTest {
    @Reference
    private DemoService demoService;

    @Test
    public void testHello(){
        String sayHi = demoService.sayHello("老宋");
        System.out.println(sayHi);
    }
}

Dubbo服务打包
服务的运行模式有三种
1、使用Servlet容器(不用)
利用Tomcat、Jetty等WEB容器启动Dubbo服务。
缺点:增加管理配置的复杂性,不必要地使用http端口,浪费内存资源
2、Java的Main方法(不建议,本地调试可以用)
基于Spring框架,写一个Java类并提供Main方法启动。
缺点:无法使用Dubbo的一些高级特性,服务的管理需要自己额外提供实现
3、Dubbo框架Main方法
Dubbo框架本身提供了服务运行支持方法,基于com.alibaba.dubbo.container.Main
简单高效地运行服务
很好地支持Dubbo服务的发布、关停(ShutdownHook)
maven打包
Idea工具打包
在这里插入图片描述Eclipse方法
选中项目,鼠标右键,点击maven
dubbo服务jar包运行
Cmd窗口:
① 定位到jar包所在目录
② 输入命令并回车执行:java -jar xxxxx.jar
Zookeeper注册中心
ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。
Dubbo监控中心
监控中心负责为服务的监控运维采集各维度的数据,统计各服务的调用次数、时间等,统计先在服务端和消费端内存中汇总,每隔一分钟发送到监控中心服务器,并以报表的形式展现。
监控中心是监控整个dubbo服务运行的健康情况,同时可为dubbo的服务的调用负载提供数据支撑。

猜你喜欢

转载自blog.csdn.net/h993438890/article/details/83868948