Dubbo学习步骤

Dubbo框架笔记

Dubbo介绍

互联网架构演变过程

单一应用架构

当网站流量很小时,只需一个应用,将所有功能都部署在一起,以减少部署节点和成本。此时,用于简化增删改查工作量的数据访问框架(ORM)是关键。

垂直应用架构

当访问量逐渐增大,单一应用增加机器带来的加速度越来越小,将应用拆成互不相干的几个应用,以提升效率。此时,用于加速前端页面开发的Web框架(MVC)是关键。

分布式服务架构

当垂直应用越来越多,应用之间交互不可避免,将核心业务抽取出来,作为独立的服务,逐渐形成稳定的服务中心,使前端应用能更快速的响应多变的市场需求。此时,用于提高业务复用及整合的分布式服务框架(RPC)是关键。

流动计算架构

当服务越来越多,容量的评估,小服务资源的浪费等问题逐渐显现,此时需增加一个调度中心基于访问压力实时管理集群容量,提高集群利用率。此时,用于提高机器利用率的资源调度和治理中心(SOA)是关键。

Dubbo解决了什么问题

在大规模服务化之前,应用可能只是通过 RMI 或 Hessian 等工具,简单的暴露和引用远程服务,通过配置服务的URL地址进行调用,通过 F5 等硬件进行负载均衡。

当服务越来越多时,服务 URL 配置管理变得非常困难,F5 硬件负载均衡器的单点压力也越来越大。 此时需要一个服务注册中心,动态地注册和发现服务,使服务的位置透明。并通过在消费方获取服务提供方地址列表,实现软负载均衡和 Failover,降低对 F5 硬件负载均衡器的依赖,也能减少部分成本。

当进一步发展,服务间依赖关系变得错踪复杂,甚至分不清哪个应用要在哪个应用之前启动,架构师都不能完整的描述应用的架构关系。 这时,需要自动画出应用间的依赖关系图,以帮助架构师理清理关系。

接着,服务的调用量越来越大,服务的容量问题就暴露出来,这个服务需要多少机器支撑?什么时候该加机器? 为了解决这些问题,第一步,要将服务现在每天的调用量,响应时间,都统计出来,作为容量规划的参考指标。其次,要可以动态调整权重,在线上,将某台机器的权重一直加大,并在加大的过程中记录响应时间的变化,直到响应时间到达阈值,记录此时的访问量,再以此访问量乘以机器数反推总容量。

除了服务注册中心之外, Dubbo还帮我们解决了服务远程调用的问题。 之前我们需要显示通过HttpClient、OkHttp、RestTemplate。。这些技术去通过Http协议调用远程服务,其间还要手动处理序列化、反序列化问题。而Dubbo将这些细节屏蔽了,对于我们开发者来说,调用远程的服务就像调用本地服务一样方便。

总的来说, Dubbo主要帮我们解决了分布式应用中的服务注册发现和RPC(Remote Procedure Call)远程调用这两个核心问题。

快速开始

因为服务提供者和服务消费者都需要引入共同的服务接口。所以在Dubbo的最佳实践上要求我们将服务接口抽出来作为单独的一个模块,供服务提供者、消费者引用。这样Dubbo应用就包含三部分:共同的api接口、服务提供者、服务消费者三部分(其中服务提供者和消费者都需要依赖API接口)

在这里插入图片描述

  1. 引入依赖

    依赖主要包含: Spring、Dubbo

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
        <spring.version>5.2.1.RELEASE</spring.version>
        <dubbo.version>2.7.3</dubbo.version>
    </properties>
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo</artifactId>
        <version>${dubbo.version}</version>
    </dependency>
    
  2. 编写核心API接口

    新建一个单独的maven module(如dubbo-service-api),在里面添加需要远程访问的接口

    public interface ICalcService {
    
        int plus(int num1, int num2);
    
        int minus(int num1, int num2);
    
    }
    
  3. 开发服务提供者

    ​ 新建单独的服务提供者模块 (如dubbo-service-provider)

    1. 开发API接口实现逻辑

      public class CalcServiceImpl implements ICalcService {
          public int plus(int num1, int num2) {
              int result = num1 + num2;
              System.out.println(num1 + " + " + num2 + " = " + result);
              return result;
          }
      
          public int minus(int num1, int num2) {
              int result = num1 - num2;
              System.out.println(num1 + " - " + num2 + " = " + result);
              return result;
          }
      }
      
    2. 通过Dubbo注册中心暴露服务(发布服务到注册中心)

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans.xsd
              http://dubbo.apache.org/schema/dubbo
              http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
      
          <!-- 给dubbo应用命名 -->
          <dubbo:application name="hello-dubbo">
              <dubbo:parameter key="qos.enable" value="false" />
          </dubbo:application>
      
          <!-- 配置Dubbo服务注册中心(这里使用的是基于multicast协议的注册中心) -->
          <dubbo:registry address="multicast://224.5.6.7:1234" />
          <dubbo:protocol name="dubbo" port="20880" />
          
          <!-- 对外暴露服务 -->
          <dubbo:service interface="com.lanou3g.dubbo.service.ICalcService" ref="calcService" />
      
          <bean id="calcService" class="com.lanou3g.dubbo.service.impl.CalcServiceImpl" />
      
      </beans>
      
    3. 启动服务提供者

      public class ProviderApplication {
          public static void main(String[] args) throws IOException {
      
              ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
              ctx.start();
              ctx.registerShutdownHook();
      
              System.out.println("Provider启动完成");
      
              // 阻塞当前应用,直到输入任意字符后才退出
              System.in.read();
          }
      }
      
  4. 开发服务消费者

    ​ 新建一个单独的服务消费者模块(如dubbo-service-consumer)

    1. 通过Dubbo注册中心引用远程服务

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:p="http://www.springframework.org/schema/p"
             xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans.xsd
              http://dubbo.apache.org/schema/dubbo
              http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
      
          <!-- 给dubbo应用命名 -->
          <dubbo:application name="hello-dubbo">
              <dubbo:parameter key="qos.enable" value="false" />
          </dubbo:application>
      
          <!-- 配置Dubbo服务注册中心(这里使用的是基于multicast协议的注册中心) -->
          <dubbo:registry address="multicast://224.5.6.7:1234?unicast=false" />
      
          <!-- 指定Dubbo底层在远程调用服务时通过什么协议,哪个端口调用 -->
          <dubbo:protocol name="dubbo" port="20880" />
      
          <dubbo:reference id="calcService" interface="com.lanou3g.dubbo.service.ICalcService" />
      
      </beans>
      
  5. 通过公共的API接口调用服务方法,完成远程接口调用

       public class ConsumerApplication {
           public static void main(String[] args) {
               ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
               ctx.registerShutdownHook();
               ctx.start();
       
               // 通过公共接口像调用本地方法一样调用远程服务
       		ICalcService calcService = ctx.getBean("calcService", ICalcService.class);
               int result = calcService.plus(11, 89);
               System.out.println("调用远程服务计算结果:" + result);
           }
       }
    

深入Dubbo知识点

Dubbo的架构图

  1. 服务容器负责启动,加载,运行服务提供者。
  2. 服务提供者在启动时,向注册中心注册自己提供的服务。
  3. 服务消费者在启动时,向注册中心订阅自己所需的服务。
  4. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
  5. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
  6. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

Dubbo中的角色

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

Dubbo支持的服务注册中心

基于Zookeeper的注册中心
  1. 需要启动zookeeper服务

    下载、启动zookeeper服务

  2. 在dubbo的应用中修改注册中心配置为zookeeper方式

    <!-- 服务提供方和消费方都添加如下配置 -->
    <!-- 配置基于zookeeper的服务注册中心  -->
    <!--<dubbo:registry address="zookeeper://127.0.0.1:2181" client="curator" />-->
    
  3. 在dubbo应用中需要添加zookeeper客户端依赖

    <!-- zookeeper客户端 —— curator依赖 -->
    <dependency>
        <groupId>com.netflix.curator</groupId>
        <artifactId>curator-framework</artifactId>
        <version>1.1.10</version>
    </dependency>
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-recipes</artifactId>
        <version>4.0.1</version>
    </dependency>
    
基于Redis的注册中心
  1. 启动redis服务

  2. 修改注册中心配置为redis

    <!-- 配置基于redis的服务注册中心 -->
    <dubbo:registry address="redis://localhost:6379" />
    

    注意: 如果说Redis需要口令验证,则不能作为Dubbo的注册中心

  3. 添加redis客户端依赖到工程中(服务提供者、消费者都需要)

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
        <version>2.7.0</version>
    </dependency>
    
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>3.1.0</version>
    </dependency>
    
基于Multicast组播的注册中心

参见快速开始配置

Dubbo支持的配置方式

Dubbo支持以下几种方式的配置方式:

  • 基于SpringBean的XML配置
  • 基于注解的配置
  • 基于Properties属性文件的配置
  • 基于硬编码的配置
  • 支持加载存储在外部配置中心中的配置(如存储在zookeeper中的dubbo.properties配置)

Dubbo支持的RPC通讯协议

Dubbo支持以下几种服务远程调用的通讯协议:

  • dubbo(默认)
  • rest
  • http
  • redis
  • webservice

SpringBoot整合Dubbo

现在SpringBoot技术非常流行,可以极大程度上让我们从管理项目依赖和配置的工作中解放出来,更专注于核心业务实现(更傻瓜),Dubbo官方也提供了对SpringBoot的支持。

使用步骤如下:

第一步:在parent中定义所有依赖

在企业开发中的建议做法,便于统一管理整个工程中所有模块的依赖和版本。如果你不想这样做,这步可以省略,直接在各个module中添加需要的依赖也是可以滴。

  • 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>com.lanou3g</groupId>
    <artifactId>dubbo-springboot-parent</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- 引入SpringBoot -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.2.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- dubbo springboot依赖 -->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>2.7.3</version>
            </dependency>

            <!-- redis注册中心依赖 -->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-pool2</artifactId>
                <version>2.7.0</version>
            </dependency>
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>3.1.0</version>
            </dependency>

            <!-- zookeeper注册中心依赖 -->
            <dependency>
                <groupId>com.netflix.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>1.1.10</version>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-recipes</artifactId>
                <version>4.0.1</version>
            </dependency>
            <dependency>
                <groupId>com.lanou3g</groupId>
                <artifactId>dubbo-springboot-api</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

第二步:定义公共的服务API接口模块

该模块不需要添加什么依赖, 主要是定义各个服务的接口文件。以便让服务提供者和消费者依赖和复用。

  • 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>com.lanou3g</groupId>
    <artifactId>dubbo-springboot-api</artifactId>
    <version>1.0-SNAPSHOT</version>
</project>
  • 服务接口示例:

IHelloService.java

package com.lanou3g.dubbo.service;

public interface IHelloService {
    String hello();
    String hello(String name);
}

第三步:开发服务提供者模块

该模块提供了服务接口的具体实现逻辑,并且通过Dubbo的服务注册中心将服务暴露出去。

  • 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">
    <parent>
        <artifactId>dubbo-springboot-parent</artifactId>
        <groupId>com.lanou3g</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../dubbo-springboot-parent/pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-springboot-provider</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
        <!-- redis注册中心依赖 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>
        <!-- 如果注册中心是zookeeper换成此依赖 -->
        <!--<dependency>
            <groupId>com.netflix.curator</groupId>
            <artifactId>curator-framework</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
        </dependency>
		-->
        <dependency>
            <groupId>com.lanou3g</groupId>
            <artifactId>dubbo-springboot-api</artifactId>
        </dependency>
    </dependencies>
</project>
  • 服务实现示例代码

HelloService.java

package com.lanou3g.provider.service;

import com.lanou3g.dubbo.service.IHelloService;
import org.apache.dubbo.config.annotation.Service;

// 通过此注解将服务注册到Dubbo的服务注册中心(注意包名是dubbo而不是spring的)
// 该注解还支持添加服务的版本、所属组等参数,都是可选的
@Service
public class HelloService implements IHelloService {
    @Override
    public String hello() {
        String msg = "Hello Dubbo";
        System.out.println(msg);
        return msg;
    }

    @Override
    public String hello(String name) {
        String msg = "Hello, " + name;
        System.out.println(msg);
        return msg;
    }
}
  • 配置Dubbo的注册中心、应用名等参数

application.yml

dubbo:
  application:
    name: hello-dubbo-springboot
    qos-enable: false

  registry:
    address: redis://localhost:6379
  • 启动类

ProviderApplication.java

package com.lanou3g;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
// 服务提供方一定要添加此注解,开启Dubbo的注解扫描(扫描我们配置的@Service注解)
@EnableDubbo(scanBasePackages = "com.lanou3g.provider.service")
public class ProvicerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProvicerApplication.class, args);
    }
}

第四步:开发服务消费者模块

该模块同样依赖公共的服务接口模块,通过接口类型,结合Dubbo的服务中心和RPC可以像调用本地Service方法一样调用远程的服务接口实现。

  • pom依赖

    和服务提供者一样,参见上面服务提供者pom文件

  • 配置Dubbo的注册中心、应用名等参数

    application.yml

    dubbo:
      application:
        name: hello-dubbo-springboot
        qos-enable: false
    
      registry:
        address: redis://localhost:6379
    
  • 在需要调用远程服务的地方通过Dubbo引入远程服务(这里直接在Application类中通过生命周期方法调用)

    ConsumerApplication.java

    package com.lanou3g;
    
    import com.lanou3g.dubbo.service.IHelloService;
    import org.apache.dubbo.config.annotation.Reference;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    import javax.annotation.PostConstruct;
    
    @SpringBootApplication
    public class ConsumerApplication {
        
        // 通过Dubbo提供的Reference注解引用远程服务
        // 如果服务提供者暴露服务时配置的版本、所属组等参数,这里引用时也需要加上,所有参数匹配才能调用
        @Reference
        private IHelloService helloService;
    
        public static void main(String[] args) {
            SpringApplication.run(ConsumerApplication.class, args);
        }
    
        @PostConstruct
        public void invokeService() {
            String result = helloService.hello();
            System.out.println("远程服务hello()调用结果: " + result);
    
            result = helloService.hello("John");
            System.out.println("远程服务hello(name)调用结果: " + result);
        }
    }
    

Dubbo监控工具DubboAdmin

  1. 下载安装

  2. 修改配置

    主要修改dubbo注册中心地址为你现在使用的注册中心地址:

    dubbo.registry.address=zookeeper://teacher.lanou.com:2181?client=curator
    # dubbo.registry.address=multicast://224.5.6.7:1234?unicast=false
    # dubbo.registry.address=redis://127.0.0.1:6379
    
  3. 重新打包

    mvn clean package
    
  4. 启动

    java -jar dubbo-admin-0.0.1-SNAPSHOT.jar
    
  5. 使用方法

    登录时,用户名和密码都是root

    界面效果:

    在这里插入图片描述

常见问题

问题一:找不到服务提供者

问题描述:

​ 使用multicast注册中心时,一直报 No provider available for the service xxxx Service1

问题原因:

​ dubbo的服务消费方在服务注册中心中无法找到匹配的服务提供者,导致服务无法调用

问题解决:

  1. 检查服务提供者、消费者配置是否和快速开始或者官方multicast示例中一致

  2. 如果服务提供者和消费者处于同一台服务器上,或者一个服务提供者有多个服务消费者,在消费者这方需要关闭multicast的单播模式,改为广播模式,否则消费者可能无法接收到服务提供者发出的服务注册消息。

    <dubbo:registry address="multicast://224.5.6.7:1234?unicast=false" />
    

    <dubbo:registry protocol="multicast" address="224.5.6.7:1234">
        <dubbo:parameter key="unicast" value="false" />
    </dubbo:registry>
    

    参见:官方说明

  3. 如果上面两步都排除掉了,依然无法解决。 使用终极大法,将你的网线拔掉,或者网络断掉,然后重启服务提供者、消费者,你会发现已经解决了。之后你再插上网线也不会再有问题,就是这么神奇~

问题二:启动消费者时,有qos server无法启动的警告

问题描述:

警告:  [DUBBO] Fail to start qos server: , dubbo version: 2.7.3, current host: 10.10.13.127
java.net.BindException: Address already in use: bind

问题原因:

通常这种情况发生在消费者和提供者运行在同一台服务器上,因为dubbo应用在启动时默认会启动一个心跳服务运行在22222端口,当一台服务器运行多个dubbo应用时,就会端口冲突。

问题解决:

​ 在开发过程中可以直接将qos心跳服务停掉

<dubbo:application name="hello-dubbo">
    <!-- 关闭qos心跳服务 -->
    <dubbo:parameter key="qos.enable" value="false" />
</dubbo:application>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值