Dubbo的配置使用及原理

1. Dubbo简介

Dubbo 是一款微服务开发框架,它提供了 RPC通信 与 微服务治理 两大关键能力。这意味着,使用 Dubbo 开发的微服务,将具备相互之间的远程发现与通信能力, 同时利用 Dubbo 提供的丰富服务治理能力,可以实现诸如服务发现、负载均衡、流量调度等服务治理诉求。同时 Dubbo 是高度可扩展的,用户几乎可以在任意功能点去定制自己的实现,以改变框架的默认行为来满足自己的业务需求。

Dubbo 提供了构建云原生微服务业务的一站式解决方案,可以使用 Dubbo 快速定义并发布微服务组件,同时基于 Dubbo 开箱即用的丰富特性及超强的扩展能力,构建运维整个微服务体系所需的各项服务治理能力,如 Tracing、Transaction 等,Dubbo 提供的基础能力包括:

  • 服务发现

  • 流式通信

  • 负载均衡

  • 流量治理

  • …..

Dubbo 提供了从服务定义、服务发现、服务通信到流量管控等几乎所有的服务治理能力,并且尝试从使用上对用户屏蔽底层细节,以提供更好的易用性。

 

2. Dubbo的架构

 

节点角色说明:

  • Provider:暴露服务的提供方。

  • Consumer:调用远程服务的服务消费方。

  • Registry:服务注册与发现的注册中心。

  • Monitor:统计服务的调用次数和调用时间的监控中心。

  • Contaniner:服务运行容器

 

中心化组件:

  • 注册中心。协调 Consumer 与 Provider 之间的地址注册与发现

  • 配置中心。

    • 存储 Dubbo 启动阶段的全局配置,保证配置的跨环境共享与全局一致性

    • 负责服务治理规则(路由规则、动态配置等)的存储与推送。

  • 元数据中心。

    • 接收 Provider 上报的服务接口元数据,为 Admin 等控制台提供运维能力(如服务测试、接口文档等)

    • 作为服务发现机制的补充,提供额外的接口/方法级别配置信息的同步能力,相当于注册中心的额外扩展。

 

3. Dubbo配置(Spring整合使用)

(1). 使用XML进行配置

provider.xml 示例

 
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
        xmlns="http://www.springframework.org/schema/beans"
        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:application name="demo-provider"/>
     <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
     <dubbo:protocol name="dubbo" port="20890"/>
     <bean id="demoService" class="org.apache.dubbo.samples.basic.impl.DemoServiceImpl"/>
     <dubbo:service interface="org.apache.dubbo.samples.basic.api.DemoService" ref="demoService"/>
 </beans>consumer.xml示例
 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
        xmlns="http://www.springframework.org/schema/beans"
        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:application name="demo-consumer"/>
     <dubbo:registry group="aaa" address="zookeeper://127.0.0.1:2181"/>
     <dubbo:reference id="demoService" check="false" interface="org.apache.dubbo.samples.basic.api.DemoService"/>
 </beans>

 

标签详解:

标签用途解释
标签 用途 解释
<dubbo:service/> 服务配置 用于暴露一个服务,定义服务的元信息,一个服务可以用多个协议暴露,一个服务也可以注册到多个注册中心
<dubbo:reference/> 2 引用配置 用于创建一个远程服务代理,一个引用可以指向多个注册中心
<dubbo:protocol/> 协议配置 用于配置提供服务的协议信息,协议由提供方指定,消费方被动接受
<dubbo:application/> 应用配置 用于配置当前应用信息,不管该应用是提供者还是消费者
<dubbo:module/> 模块配置 用于配置当前模块信息,可选
<dubbo:registry/> 注册中心配置 用于配置连接注册中心相关信息
<dubbo:monitor/> 监控中心配置 用于配置连接监控中心相关信息,可选
<dubbo:provider/> 提供方配置 当 ProtocolConfig 和 ServiceConfig 某属性没有配置时,采用此缺省值,可选
<dubbo:consumer/> 消费方配置 当 ReferenceConfig 某属性没有配置时,采用此缺省值,可选
<dubbo:method/> 方法配置 用于 ServiceConfig 和 ReferenceConfig 指定方法级的配置信息
<dubbo:argument/> 参数配置 用于指定方法参数配置

 

(2). 注解配置

服务提供方

Service注解暴露服务

@Service
public class AnnotationServiceImpl implements AnnotationService {
  @Override
  public String sayHello(String name) {
    return "annotation: hello, " + name;
  }
}

 

增加应用共享配置

dubbo.application.name=annotation-provider

dubbo.registry.address=zookeeper://127.0.0.1:2181

dubbo.protocol.name=dubbo

dubbo.protocol.port=20880

 

服务消费方

Reference注解引用服务

@Component("annotationAction")

public class AnnotationAction {

@Reference

private AnnotationService annotationService;

public String doSayHello(String name) {

return annotationService.sayHello(name);

}

 

增加应用共享配置

 dubbo.application.name=annotation-consumer
 dubbo.registry.address=zookeeper://127.0.0.1:2181
 dubbo.consumer.timeout=3000

 

 
posted @ 2022-01-13 21:27  minnersun  阅读(325)  评论(0编辑  收藏  举报