dubbo入门学习demo

发布时间:2022-09-11 13:30

一、工程结构

  1. 父工程:dubbo-parent
  2. 消费者:consumer
  3. 提供者:provider
  4. 通用模块:提供接口方法,消费者和提供者都需要引入。提供者需要实现该接口方法,消费者调用提供者暴露的实现方法时,需要提供一个和提供者一模一样接口(所以直接引用通用模块的接口)
    dubbo入门学习demo_第1张图片

二、pom依赖

  1. 父工程


    4.0.0

    org.example
    dubbo-parrent
    pom
    1.0-SNAPSHOT
    
        consumer
        provider
        common
    

    
        1.8
        1.18.20
        3.4.2
        4.2.0
        2.7.15
        2.5.6
    

    
    

        
        
            org.projectlombok
            lombok
            ${lombok.version}
        
        

        
        
            org.springframework.boot
            spring-boot-starter-log4j2
            ${springbootstartter.version}
        
        
            com.lmax
            disruptor
            ${disruptor.version}
        
        

        
            org.apache.dubbo
            dubbo-spring-boot-starter
            ${dubbo.version}
        
        
        
        
            org.apache.curator
            curator-framework
            ${zookeeper.version}
        
        
        
            org.apache.curator
            curator-recipes
            ${zookeeper.version}
        
        


        
            org.springframework.boot
            spring-boot-starter-web
            ${springbootstartter.version}
            
                
                    org.springframework.boot
                    spring-boot-starter-logging
                
            
        
    
    

    
        
            
            
                org.apache.maven.plugins
                maven-surefire-plugin
                2.12.4
                
                    true
                
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            repackage
                        
                    
                
            
            
                org.apache.maven.plugins
                maven-surefire-plugin
                
                    true
                
            
        
    


  1. common


    
        dubbo-parrent
        org.example
        1.0-SNAPSHOT
    
    4.0.0

    common

    
        8
        8
    

    
        
            org.projectlombok
            lombok
        
    


        

                
                    org.apache.maven.plugins
                    maven-surefire-plugin
                    2.12.4
                    
                        true
                    
                
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            repackage
                        
                    
                
            
            
                org.apache.maven.plugins
                maven-surefire-plugin
                
                    true
                
            
        
    

  1. provider


    
        dubbo-parrent
        org.example
        1.0-SNAPSHOT
    
    4.0.0

    provider

    
        8
        8
    

    
        
            org.projectlombok
            lombok
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.apache.dubbo
            dubbo-spring-boot-starter
        

        
        
            org.springframework.boot
            spring-boot-starter-log4j2
        
        
            com.lmax
            disruptor
        
        

        
        
        
            org.apache.curator
            curator-framework
        
        
        
            org.apache.curator
            curator-recipes
        
        

        
            org.springframework.boot
            spring-boot-starter-test
            2.5.6
        

        
            org.example
            common
            1.0-SNAPSHOT
        

    

        

                
                    org.apache.maven.plugins
                    maven-surefire-plugin
                    2.12.4
                    
                        true
                    
                
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            repackage
                        
                    
                
            
            
                org.apache.maven.plugins
                maven-surefire-plugin
                
                    true
                
            
        
    


  1. consumer


    
        dubbo-parrent
        org.example
        1.0-SNAPSHOT
    
    4.0.0

    consumer

    
        8
        8
    

    
        
            org.projectlombok
            lombok
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.apache.dubbo
            dubbo-spring-boot-starter
        

        
        
            org.springframework.boot
            spring-boot-starter-log4j2
        
        
            com.lmax
            disruptor
        
        

        
            org.springframework.boot
            spring-boot-starter-test
            2.5.6
        

        
            org.example
            common
            1.0-SNAPSHOT
        

        
            org.apache.curator
            curator-recipes
        


    


        

                
                    org.apache.maven.plugins
                    maven-surefire-plugin
                    2.12.4
                    
                        true
                    
                
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            repackage
                        
                    
                
            
            
                org.apache.maven.plugins
                maven-surefire-plugin
                
                    true
                
            
        
    

三、配置文件

  1. provider
rocketmq:
  name-server: 120.79.157.16:9876
  producer:
    group: dev-group
    topic: dev
    sync-tag: sync-tag
    async-tag: async-tag
    oneway-tag: oneway-tag

  consumer:
    topic: dev
    group: dev-group

server:
  port: 8082

zookeeper:
  connect_string: 120.79.157.16
  connection_timeout_ms: 15000
  session_timeout_ms: 60000

spring:
  application:
    name: providerApp

dubbo:
  scan:
    base-packages: com.provider.service.impl
  protocol:
    name: dubbo
    port: 9001
  registry:
    address: zookeeper://120.79.157.16:2181
  application:
    name: provider
  1. consumer
rocketmq:
  name-server: 120.79.157.16:9876
  producer:
    group: dev-group
    topic: dev
    sync-tag: sync-tag
    async-tag: async-tag
    oneway-tag: oneway-tag

  consumer:
    topic: dev
    group: dev-group

server:
  port: 8081


spring:
  application:
    name: consumerApp

dubbo:
  scan:
    base-packages: com.consumer.service
  protocol:
    name: dubbo
    port: 9002
  registry:
    address: zookeeper://120.79.157.16:2181

四、代码实现

代码结构不易展示,可以参考我的gitee链接,以下主要讲解一下实现过程
失效可以留下评论

  • common提供接口方法和实体类,供provider和consumer使用。provider实现接口方法,需要使用@DubboService将接口暴露出来,供消费者调用。
  • consumer使用@DubboReference引用provider暴露的接口。
  • 先启动provider,再启用consumer

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号