dubbo教程(一):dubbo入门Demo

什么是dubbo?

  • 高性能和透明化的RPC远程服务调用方案
  • SOA 服务治理方案

dubbo能做什么?

  • 远程化的远程方法调用d
    • 就像调用本地方法一样调用远程方法(动态代理)
    • 只需要简单的配置,没有任何API侵入
  • 软负载均衡及容错机制
    • 可在内网代替F5等硬件负载均衡器
  • 服务自动注册与发现
    • 不需要写死服务提供方地址,注册中心基于接口名查询服务提供者的IP地址,并且能够平滑添加或者删除服务提供者

dubbo原理

dubbo原理

示例

  • 项目demo已经存放在github上面

    1
    git clone https://github.com/hadesvip/dubbo-demo.git
  • 依赖

    • gradle
    • java8
  • 项目描述

    • dubbo-demo 父模块,主要维护了子模块的构建信息,构件信息
    • dubbo-demo-domain dubbo-demo的子模块,里面定义了数据类和接口
    • dubbo-demo-provider dubbo-demo的子模块,dubbo的服务提供者
    • dubbo-demo-consumer dubbo-demo的子模块,dubbo服务消费者
  • 项目运行

    1. 预先安装zookeeper,并启动zk,启动一个zk就行了,生产环境上最好搭建一个zk集群(3的倍数)

    2. 构建

      1
      2
      cd dubbo-demo
      gradle clean build
    3. 运行dubbo-demo-provider项目com.provider.Console方法

      • 从控制台上面打印的日记可以看出连接到了zk上面,并把dubbo服务注册到了zk上

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        //连接本地zk
        org.apache.zookeeper.ClientCnxn - Socket  connection established to  localh ost/127.0.0.1:2181, initiating session

        //zk状态为连接状态
        org.apache.zookeeper.ClientCnxn - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x100003c49000000, negotiated timeout = 30000
        org.I0Itec.zkclient.ZkClient - zookeeper state changed (SyncConnected)

        //在zk上面注册服务
        [DUBBO] Register: dubbo://172.17.0.1:20880/com.service.UserService?anyhost=true&application=dubbo-demo-provider-app&dubbo=2.6.0&generic=false&interface=com.service.UserService&logger=log4j&methods=getUserById&pid=14225&side=provider&timestamp=1516428681983, dubbo version: 2.6.0, current host: 172.17.0.1

         //订阅configurators配置
        [DUBBO] Subscribe: provider://172.17.0.1:20880/com.service.UserService?anyhost=true&application=dubbo-demo-provider-app&category=configurators&check=false&dubbo=2.6.0&generic=false&interface=com.service.UserService&logger=log4j&methods=getUserById&pid=14225&side=provider&timestamp=1516428681983, dubbo version: 2.6.0, current host: 172.17.0.1

        //通知订阅这些服务的提供者
        [DUBBO] Notify urls for subscribe url provider://172.17.0.1:20880/com.service.UserService?anyhost=true&application=dubbo-demo-provider-app&category=configurators&check=false&dubbo=2.6.0&generic=false&interface=com.service.UserService&logger=log4j&methods=getUserById&pid=14225&side=provider&timestamp=1516428681983, urls: [empty://172.17.0.1:20880/com.service.UserService?anyhost=true&application=dubbo-demo-provider-app&category=configurators&check=false&dubbo=2.6.0&generic=false&interface=com.service.UserService&logger=log4j&methods=getUserById&pid=14225&side=provider&timestamp=1516428681983], dubbo version: 2.6.0, current host: 172.17.0.1
    4. 运行dubbo-demo-consumer项目 com.consumer.Console

      • 控制台输出下面语句,说明服务调用成功

        1
        User{userId='001', userName='admin', email='admin@126.com'}
      • 从控制台日记也可以知道一些细节

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

        //连接zk
        INFO org.apache.zookeeper.ClientCnxn - Socket connection established to localhost/127.0.0.1:2181, initiating session

        //ZK状态改变为连接状态
        INFO org.I0Itec.zkclient.ZkClient - zookeeper state changed (SyncConnected)

        //注册消费者
        [DUBBO] Register: consumer://172.17.0.1/com.service.UserService?application=dubbo-demo-consumer-app&category=consumers&check=false&dubbo=2.6.0&interface=com.service.UserService&logger=log4j&methods=getUserById&pid=15803&side=consumer&timestamp=1516429428346, dubbo version: 2.6.0, current host: 172.17.0.1

        //订阅providers,configurators,routers
        [DUBBO] Subscribe: consumer://172.17.0.1/com.service.UserService?application=dubbo-demo-consumer-app&category=providers,configurators,routers&dubbo=2.6.0&interface=com.service.UserService&logger=log4j&methods=getUserById&pid=15803&side=consumer&timestamp=1516429428346, dubbo version: 2.6.0, current host: 172.17.0.1

        // 通知订阅了这个地址的服务消费者
        [DUBBO] Notify urls for subscribe url consumer://172.17.0.1/com.service.UserService?application=dubbo-demo-consumer-app&category=providers,configurators,routers&dubbo=2.6.0&interface=com.service.UserService&logger=log4j&methods=getUserById&pid=15803&side=consumer&timestamp=1516429428346, urls: [dubbo://172.17.0.1:20880/com.service.UserService?anyhost=true&application=dubbo-demo-provider-app&dubbo=2.6.0&generic=false&interface=com.service.UserService&logger=log4j&methods=getUserById&pid=14225&side=provider&timestamp=1516428681983, empty://172.17.0.1/com.service.UserService?application=dubbo-demo-consumer-app&category=configurators&dubbo=2.6.0&interface=com.service.UserService&logger=log4j&methods=getUserById&pid=15803&side=consumer&timestamp=1516429428346, empty://172.17.0.1/com.service.UserService?application=dubbo-demo-consumer-app&category=routers&dubbo=2.6.0&interface=com.service.UserService&logger=log4j&methods=getUserById&pid=15803&side=consumer&timestamp=1516429428346], dubbo version: 2.6.0, current host: 172.17.0.1

        //成功连接到这个服务地址
        Successed connect to server /172.17.0.1:20880 from NettyClient 172.17.0.1 using dubbo version 2.6.0, channel is NettyChannel [channel=[id: 0x7f2cfe3f, /172.17.0.1:49624 => /172.17.0.1:20880]], dubbo version: 2.6.0, current host: 172.17.0.1
        Refer dubbo service com.service.UserService from url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=dubbo-demo-consumer-app&check=false&dubbo=2.6.0&generic=false&interface=com.service.UserService&logger=log4j&methods=getUserById&pid=15803&register.ip=172.17.0.1&remote.timestamp=1516428681983&side=consumer&timestamp=1516429428346, dubbo version: 2.6.0, current host: 172.17.0.1
      • 在dubbo-demo-provider 控制台也会有下面日记输出

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        //消费者请求
        4:22:45.913 [New I/O server worker #1-1] WARN c.a.d.r.transport.AbstractServer - [DUBBO] All clients has discontected from /172.17.0.1:20880. You can graceful shutdown now., dubbo version: 2.6.0, current host: 172.17.0.1
        14:22:45.913 [DubboServerHandler-172.17.0.1:20880-thread-3] INFO c.a.d.r.protocol.dubbo.DubboProtocol -

        [DUBBO] disconected from /192.168.31.44:49580,url:dubbo://172.17.0.1:20880/com.service.UserService?anyhost=true&application=dubbo-demo-provider-app&bind.ip=172.17.0.1&bind.port=20880&channel.readonly.sent=true&codec=dubbo&dubbo=2.6.0&generic=false&heartbeat=60000&interface=com.service.UserService&logger=log4j&methods=getUserById&pid=14225&side=provider&timestamp=1516428681983, dubbo version: 2.6.0, current host: 172.17.0.1

        14:23:49.175 [New I/O server worker #1-2] WARN c.a.d.r.transport.AbstractServer - [DUBBO] All clients has discontected from /172.17.0.1:20880. You can graceful shutdown now., dubbo version: 2.6.0, current host: 172.17.0.1

        14:23:49.175 [DubboServerHandler-172.17.0.1:20880-thread-6] INFO c.a.d.r.protocol.dubbo.DubboProtocol - [DUBBO] disconected from

        /192.168.31.44:49624,url:dubbo://172.17.0.1:20880/com.service.UserService?anyhost=true&application=dubbo-demo-provider-app&bind.ip=172.17.0.1&bind.port=20880&channel.readonly.sent=true&codec=dubbo&dubbo=2.6.0&generic=false&heartbeat=60000&interface=com.service.UserService&logger=log4j&methods=getUserById&pid=14225&side=provider&timestamp=1516428681983, dubbo version: 2.6.0, current host: 172.17.0.1

更多资料 https://dubbo.io