• Dubbo分布式服务框架入门实战(附源码)


    Dubbo分布式服务框架入门实战

    首先,有必要清楚Dubbo是什么。官方文档的定义如下:

    DUBBO是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。

    其实,总结起来就是:

    Dubbo是一个解决大规模服务治理的高性能分布式服务框架

    dubbo的架构如下:

    dubbo架构

    节点角色说明:

    • Provider: 暴露服务的服务提供方。
    • Consumer: 调用远程服务的服务消费方。
    • Registry: 服务注册与发现的注册中心。
    • Monitor: 统计服务的调用次调和调用时间的监控中心。
    • Container: 服务运行容器。

    调用关系说明:

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

    Dubbo入门实战

    要完成一个dubbo的demo,其实只需要上面的Provider、Consumer和Registry。其中Registry使用Zookeeper来承担,当提供者提供服务后,需要向Zookeeper(注册中心)暴露其发布的服务,消费者通过Zookeeper订阅其需要消费的服务,然后就可以像调用本地服务一样调用远程服务了。

    服务提供者:

    接口定义:

    package com.rhwayfun.service;
    
    /**
     * 
     * @ClassName: HelloService 
     * @Description: TODO
     * @author ZhongCB
     * @date 2016年8月1日 下午4:40:09 
     *
     */
    public interface HelloService {
    
        String getName();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    接口实现类:

    package com.rhwayfun.service.impl;
    
    import com.rhwayfun.service.HelloService;
    
    /**
     * 
     * @ClassName: HelloServiceImpl 
     * @Description: TODO
     * @author ZhongCB
     * @date 2016年8月5日 下午5:12:00 
     *
     */
    public class HelloServiceImpl implements HelloService {
    
        public String getName() {
            return "rhwayfun";
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    Spring配置文件:

      
       
    
          
          
          
            
          
             
          
          
          
          
     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    pom.xml文件:

    
        4.0.0
        com.rhwayfun
        provider-demo
        0.0.1-SNAPSHOT
        
            3.2.8.RELEASE
        
    
        
            
                com.alibaba
                dubbo
                2.5.3
                
                    
                        org.springframework
                        spring
                    
                
            
            
                com.github.sgroschupf
                zkclient
                0.1
            
            
            
                org.springframework
                spring-core
                ${spring.version}
            
            
                org.springframework
                spring-beans
                ${spring.version}
            
            
                org.springframework
                spring-context
                ${spring.version}
            
            
                org.springframework
                spring-jdbc
                ${spring.version}
            
            
                org.springframework
                spring-web
                ${spring.version}
            
            
                org.springframework
                spring-webmvc
                ${spring.version}
            
            
                org.springframework
                spring-aop
                ${spring.version}
            
            
                org.springframework
                spring-tx
                ${spring.version}
            
            
                org.springframework
                spring-orm
                ${spring.version}
            
            
                org.springframework
                spring-context-support
                ${spring.version}
            
            
                org.springframework
                spring-test
                ${spring.version}
            
            
                org.springframework
                spring-jms
                ${spring.version}
            
        
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90

    下面就需要编写测试类来验证服务提供者是否发布成功,由于使用了Zookeeper作为服务注册中心,所以在运行测试代码之前,需要首先启动Zookeeper。具体Zookeeper的使用与配置请参考Zookeeper入门实战。

    测试代码:

    package com.rhwayfun.test;
    
    import java.io.IOException;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * 
     * @ClassName: HelloServiceTest 
     * @Description: TODO
     * @author ZhongCB
     * @date 2016年8月5日 下午5:17:52 
     *
     */
    public class HelloServiceTest {
    
        public static void main(String[] args) throws IOException{
            ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"application.xml"});
            ctx.start();
            System.out.println("服务提供者已注册成功!");
            System.in.read();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    如果服务发布成功,则会在控制台打印出“服务提供者已注册成功!”的提示。

    服务消费者:

    pom.xml配置文件,在服务提供者的pom.xml基础添加如下如下依赖:

    
        com.rhwayfun
        provider-demo
        0.0.1-SNAPSHOT
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Spring配置文件:

    
    
    
        
        
    
          
           
    
        
        
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    使用服务提供者发布的服务:

    package com.rhwayfun.consumer.dubbo;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.rhwayfun.service.HelloService;
    
    /**
     * 
     * @ClassName: DubboConsumerDemo 
     * @Description: TODO
     * @author ZhongCB
     * @date 2016年8月5日 下午4:14:58 
     *
     */
    public class DubboConsumerDemo {
    
        public static void main(String[] args) {
            ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"classpath:application.xml"});
            ctx.start();
    
            // 获取远程服务代理
            HelloService helloservice = (HelloService)ctx.getBean("helloService");
            System.out.println(helloservice.getName());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    以上就是使用dubbo的入门实例,我们发现使用dubbo调用远程服务非常方便,感觉调用本地接口没什么很大不同。其实,这就是dubbo的高明之处了,有兴趣可以阅读以下dubbo的源码

    附:dubbo入门实战源码

  • 相关阅读:
    QSystemTrayIcon——实现系统托盘
    Android单元测试
    直播预告|OceanBase 社区版 4.0 全解析
    通达信交易系统接口实现自动交易策略的方法分享
    laravel5.1反序列化
    pyinstaller最简单教程
    SQL 中的 NULL 值:定义、测试和处理空数据,以及 SQL UPDATE 语句的使用
    DevExpress Office文件API打印 PDF 文档
    C#使用UA-.NETStandard开发OPC UA客户端
    乐鑫 ESP-Mesh-Lite:轻松覆盖更大范围,连接更多设备
  • 原文地址:https://blog.csdn.net/m0_67401153/article/details/126328001