dubbo(一):helloworld

1. dubbo简介

dubbo是通过zookeeper等来作为注册管理中,实现SOA的一个治理框架。提供集群的利用率,所有的单个服务可以通过dubbo实现分布式的远程RPC调用。可以简单理解为:dubbo就是RPC服务加上一个治理中心。

下面引入dubbo官网的介绍:

在这里插入图片描述
节点说明

节点角色说明
Provider暴露服务的服务提供方
Consumer调用远程服务的服务消费方
Registry服务注册与发现的注册中心
Monitor统计服务的调用次数和调用时间的监控中心
Container服务运行容器

调用关系

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

2.搭建环境

2.1 zookeeper安装

(1)下载:http://ftp.meisei-u.ac.jp/mirror/apache/dist/zookeeper/

(2)解压以后在zookeeper/conf目录下创建一个zoo.cfg的文件:

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=E:/zookeeper/zookeeper-3.4.14/conf/tmp # 配置文件的地址,需要手动修改
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

修改一个dataDir文件用于存放配置文件即可。

(3)将zookeeper的bin目录配置到path环境变量中。

(4)启动zookeeper

zkServer.cmd

2.2 IDEA创建整体项目

直接创建一个名为hello的项目:
在这里插入图片描述
一直next,创建完成以后删除src目录。

然后pom中加入:

 <dependencies>
        <!--junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!-- dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
        </dependency>
        <!--zookeeper客户端dubbo并没有集成,需要手动引入-->
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
    </dependencies>

2.3 创建dubbo-api

在hello项目下创建module
在这里插入图片描述
目录:
在这里插入图片描述
创建DubboService接口:
在这里插入图片描述

2.4 创建dubbo-provider

也是直接maven创建,不需要使用任何maven模板。

provider中的内容:
在这里插入图片描述
pom中引入api

<dependencies>
        <!--依赖api接口-->
        <dependency>
            <groupId>com.qianliu</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

DubboServiceImpl实现了DubboService
在这里插入图片描述
provider.java实现对provider这个module的初始化,并启动:
为了保证provider启动以后不会马上执行结束并关闭,System.in.read();表示按任意键退出,如果一直不输入数据,provider会一直在执行中。
在这里插入图片描述

provider的核心配置:将zookeeper和声明需要暴露给其他module的接口作出声明。这样就可以被其他的module使用。dubbo:application适用于 dubbo-admin 或 dubbo-monitor中管理使用的,本项目没有涉及。
在这里插入图片描述

2.5 创建consumer

创建项目方式和provider相同,consumer里面有调用provider的一些方法。
在这里插入图片描述
main方法中调用的dubboService就是在配置文件中配置好的。
在这里插入图片描述

pom.xml

<dependencies>
        <!--依赖api接口-->
        <dependency>
            <groupId>com.qianliu</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

2.6 测试

启动provider
在这里插入图片描述
启动consumer,调用其他module的方法成功。
在这里插入图片描述

源码:https://github.com/LUK-qianliu/dubbo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值