dubbo学习笔记

dubbo学习笔记

  1. dubbo是alibaba的开源项目,最近项目中涉及到了dubbo,我去学习,到官网去翻了,没有什么头绪(主要是我想直接用springboot去做,当时也没有找到dubbo整合springboot的连接,还不知道怎么看),于是直接上开始搭建dubbo项目,结果几天也没有成功。后来各种百度,博客园,csdn文章案例,都没有成功,jar依赖关系总是处理不好,到最后还是停下来了,还是去了官网,百度了dubbo官网学习步骤,终于找到了。
  2. dubbo整合spring boot的官网地址为:https://github.com/apache/dubbo-spring-boot-project ,dubbo
  3. 当我发现了这个地址后,我当即动手实践,新建了工程,迅速搭起了程序的框架。我首先做的是用xml的配置文件的方式进行配置(避免上次一做就是spring boot框架,在当时的环境下,jar的依赖关系复杂,以致最后以失败而告终),xml的方式虽然配置文件有些多,但没有那没多的jar依赖关系。万幸这次成功了,后来我又去改造,慢慢向spring boot方向靠拢。
  1. dubbo官网地址为:http://dubbo.apache.org/en-us/

  2. dubbo以zookeeper做为注册中心(在zookeeper启动的时候,需要把conf/zoo.sample.cfg文件复制一份,重新命名为zoo.cfg文件,不然会导致zookeeper启动的时候报错,我做的时候用的nacos作为注册中心):

    • zookeeper作为注册中心时pom.xml文件中需要加入的依赖如下
    <properties>
        <dubbo.version>2.7.8</dubbo.version>
    </properties>
        
    <dependencies>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>${dubbo.version}</version>
            <type>pom</type>
        </dependency>
    </dependencies>
    
    • nacos作为注册中心时pom.xml文件中需要加入的依赖如下:
    		<dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
            </dependency>
    
            <!-- Dubbo Nacos registry dependency -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo-registry-nacos</artifactId>
                <version>0.0.2</version>
            </dependency>
    
            <!-- Keep latest Nacos client version -->
            <dependency>
                <groupId>com.alibaba.nacos</groupId>
                <artifactId>nacos-client</artifactId>
                <version>0.9.0</version>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>2.7.3</version>
            </dependency>
    
  3. 各种标签如下:

    • 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="nacos://127.0.0.1:8848"/>
            <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>
        
    • consume.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="nacos://127.0.0.1:8848"/>
            <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/>参数配置用于指定方法参数配置
  1. 不同粒度之间的配置关系:

    • 以 timeout 为例,下图显示了配置的查找顺序,其它 retries, loadbalance, actives 等类似:

      • 方法级优先,接口级次之,全局配置再次之。

      • 如果级别一样,则消费方优先,提供方次之。(特别要注意前提条件,级别一样)

      • 服务提供方配置,通过 URL 经由注册中心传递给消费方

        优先级
        • 服务之间的调用,xml的方式是配置

          <dubbo:reference id="XX" interface="xxx.xxx.xxx" />
          

          注解中可以通过

          @Reference
          

          注解,xml文件的表示可以通过@Autowired注解注入到属性上面,就像在本地使用一样,注解表示引用远程的调用,效果都是一样的。

        • 服务的提供者,xml的方式是配置

          <dubbo:service interface="xxx.xxx.xxx" ref="XXXX"/>
          

          注解可以通过

          @Service(注意的包路径,这是dubbo的注解)
          

          注解。

  2. 这些搭建好了之后启动项目,测试,dubbo还有一套管理工具dubbo-admin,地址为:https://github.com/apache/dubbo-admin。注意要修改注册中心的地址信息。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值