2018年2月15日,阿里巴巴的dubbo进入了Apache孵化器,社区的加入,希望dubbo能变得更好…

最近在学习一个分布式项目,使用到了dubbo,之前没有使用过,体验一下,分布式项目地址:点击这里

使用dubbo官网的一张图来介绍下dubbo(本人才开始学习,如有错误,欢迎指正):

  • Registry:注册中心,相当于房产中介,服务提供者和使用者都需要在这里注册/使用服务,我使用zookeeper实现。

  • Monitor:监控中心,相当于房产局,它可以统计服务提供者和服务使用者的一些信息,及他们之间的关系,我使用dubbo admin实现。

  • Provider:服务提供者,相当于房东,提供服务。

  • Consumer:服务消费者,想当于租户,使用服务。

下面我通俗的解释下dubbo的整个流程,我将服务比喻成房子:

start:dubbo一启动,房东想好自己准备要租出去的房子

register:房东将房子拿到房产中介那边进行登记,并留下自己的联系方式

subscribe:租户告诉房产中介自己想租一个什么样的房子

notify:房产中介回复给租户符合条件的房子的房东的联系方式

invoke:租户拿着联系方式去找房东租房子

count:房产局全程监控着房东和租户之间的交易

其中:

  • start、register、subscribe在dubbo服务一启动就完成了

  • notify、count是异步执行的

  • invoke是同步执行的

一、搭建java和tomcat环境

这一步比较简单,直接跳过,不会的可以看下这篇文章:Linux搭建JavaWeb开发环境(Java、Tomcat、MySQL)


二、搭建zookeeper

我使用的是zookeeper-3.5.2-alpha,点我下载。

下载后将其解压:

wxs@ubuntu:~$ sudo tar zxf zookeeper-3.5.2-alpha.tar.gz
wxs@ubuntu:~$ sudo mv zookeeper-3.5.2-alpha /usr
wxs@ubuntu:~$ cd /usr/zookeeper-3.5.2-alpha
wxs@ubuntu:/usr/zookeeper-3.5.2-alpha$ ls
bin ivysettings.xml recipes
build.xml ivy.xml src
CHANGES.txt lib zookeeper-3.5.2-alpha.jar
conf LICENSE.txt zookeeper-3.5.2-alpha.jar.asc
contrib NOTICE.txt zookeeper-3.5.2-alpha.jar.md5
dist-maven README_packaging.txt zookeeper-3.5.2-alpha.jar.sha1
docs

  

在zookper文件夹下建立logs文件夹和data文件夹用于存放日志和数据:

wxs@ubuntu:/usr/zookeeper-3.5.2-alpha$ sudo mkdir data
wxs@ubuntu:/usr/zookeeper-3.5.2-alpha$ sudo mkdir logs

  

进入conf目录,复制一份zoo_sample.cfg为zoo.cfg,对其进行修改:

wxs@ubuntu:/usr/zookeeper-3.5.2-alpha$ cd conf/
wxs@ubuntu:/usr/zookeeper-3.5.2-alpha/conf$ cp zoo_sample.cfg zoo.cfg
wxs@ubuntu:/usr/zookeeper-3.5.2-alpha/conf$ vim zoo.cfg

  

配置下dataDir和dataLogDir的路径,为之前创建的两个文件夹的路径,clientPort使用默认的2181端口即可:

我使用的时单机模式,没有配集群,这样就可以了。

进入到bin目录,启动服务即可:

wxs@ubuntu:/usr/zookeeper-3.5.2-alpha/bin$ ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/zookeeper-3.5.2-alpha/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
wxs@ubuntu:/usr/zookeeper-3.5.2-alpha/bin$ ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/zookeeper-3.5.2-alpha/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: standalone

  

小心踩坑:

执行./zkServer.sh start时不要加sudo,如果root用户配置文件没有配JAVA_HOME会出现找不到JAVA_HOME!

wxs@ubuntu:/usr/zookeeper-3.5.-alpha/bin$ sudo ./zkServer.sh start
Error: JAVA_HOME is not set and java could not be found in PATH.

相关命令:

启动服务:start 停止服务: stop 重启服务; restart 查看状态:status


三、搭建dubbo监控中心

版本要求:

请使用dubbo-admin-2.5.6.war及以上版本,否则会不支持JDK1.8!

下载链接:点击这里

小心踩坑:

如果你的zookeeper和dubbo-admin在一台服务器上,dubbo-admin不用修改任何内容!

如果不在一台服务器上,将war包解压后,修改项目/WEF-INF/dubbo.properties文件,将zookeeper地址改为其所在服务器的地址(这里同时能修改root用户和guest用户的密码)。


四、配置项目

这里牵扯到项目代码,如果看不懂,可以下载文章开头的项目源码,或者直接使用官方提供的dubbo-demo,更为简单。

首先给服务提供方和服务使用方导入依赖包:

<properties>
<dubbo.version>2.6.1</dubbo.version>
<zookeeper.version>3.5.2-alpha</zookeeper.version>
<curator.version>4.0.1</curator.version>
</properties> <!-- dubbo包 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<!-- 排除dubbo自带的spring和netty,使用项目的,如果本身项目没有,无需排除 -->
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- zookeeper包 -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<type>pom</type>
</dependency>
<!-- curator(zookeeper的客户端)包 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
</dependency>

还需要在相关配置文件加上dubbo的bean的头部约束,将下面的添加到bean头部即可:

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd

4.1 服务提供方代码

对于服务提供方,如果我们想要TbItemService对外提供服务:

package jit.wxs.service.impl;

import jit.wxs.pojo.TbItem;
import jit.wxs.mapper.TbItemMapper;
import jit.wxs.service.TbItemService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service; /**
* <p>
* 商品表 服务实现类
* </p>
*
* @author jitwxs
* @since 2018-03-21
*/
@Service
public class TbItemServiceImpl extends ServiceImpl<TbItemMapper, TbItem> implements TbItemService { }

需要修改spring关于service的配置文件,加入dubbo的配置信息:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 扫描service层注解 -->
<context:component-scan base-package="jit.wxs.service"/> <!-- dubbo发布服务 -->
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="e3-manager" />
<!-- 配置zookeeper的地址,集群地址用逗号隔开 -->
<dubbo:registry protocol="zookeeper" address="192.168.30.145:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口
ref:为注入的对应接口的bean
timneout:超时时间,单位ms,开发模式可以设长一点方便debug
-->
<dubbo:service interface="jit.wxs.service.TbItemService" ref="tbItemServiceImpl" timeout="600000"/>
</beans>
  • dubbo:application:提供方的应用名

  • dubbo:registry:注册中心的类型和地址

  • dubbo:protocol:这个服务要暴露在哪个端口上(使用方根据这个端口使用服务)

  • dubbo:service:设置暴露的服务的接口,ref为该接口的bean,timeout为超时时间

4.2 服务使用方代码

服务使用方,我使用Spring MVC来实现,修改Spring MVC的配置文件,加入dubbo的配置:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 扫描组件 -->
<context:component-scan base-package="jit.wxs.web"/> <!-- 注解驱动 -->
<mvc:annotation-driven /> <!-- 全局异常类 -->
<!--<bean class="cn.edu.jit.exception.GlobalExceptionResolver"/>--> <!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean> <!-- 引用dubbo服务 -->
<!-- 使用方应用信息,用于计算依赖关系 -->
<dubbo:application name="e3-manager-web"/>
<!-- 指定zookeeper的地址,集群用逗号分隔 -->
<dubbo:registry protocol="zookeeper" address="192.168.30.145:2181"/>
<!-- 申明要访问的接口,并创建代理对象,注入bean,名为id的值 -->
<dubbo:reference interface="jit.wxs.service.TbItemService" id="tbItemService" />
</beans>
  • dubbo:application: 使用方的应用名

  • dubbo:registry:注册中心的类型和地址

  • dubbo:reference:要使用的服务的接口,并将返回的注入bean,名称为id设的值

如果配置没有问题的话,现在使用方已经能够使用提供方提供的服务了,直接将tbItemService注入进来即可:

package jit.wxs.web;

import jit.wxs.pojo.TbItem;
import jit.wxs.service.TbItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* <p>
* 商品表 前端控制器
* </p>
*
* @author jitwxs
* @since 2018-03-21
*/
@RestController
@RequestMapping("/items")
public class TbItemController {
@Autowired
private TbItemService tbItemService; @GetMapping("/{id}")
public TbItem getItemById(@PathVariable Long id) {
TbItem item = null;
if(id != null) {
item = tbItemService.selectById(id);
} return item;
}
}

五、测试

服务器上分别启动zookper和tomcat:

wxs@ubuntu:/usr/zookeeper-3.5.-alpha/bin$ ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/zookeeper-3.5.-alpha/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
wxs@ubuntu:/usr/zookeeper-3.5.-alpha/bin$ ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/zookeeper-3.5.-alpha/bin/../conf/zoo.cfg
Client port found: . Client address: localhost.
Mode: standalone
wxs@ubuntu:/usr/apache-tomcat-8.5./bin$ ./startup.sh
Using CATALINA_BASE: /usr/apache-tomcat-8.5.
Using CATALINA_HOME: /usr/apache-tomcat-8.5.
Using CATALINA_TMPDIR: /usr/apache-tomcat-8.5./temp
Using JRE_HOME: /usr/jdk1..0_161/jre
Using CLASSPATH: /usr/apache-tomcat-8.5./bin/bootstrap.jar:/usr/apache-tomcat-8.5./bin/tomcat-juli.jar
Tomcat started.

使用下面命令可以持续显示tomcat的输出:

wxs@ubuntu:/usr/apache-tomcat-8.5.28/bin$ tail -f ../logs/catalina.out

分别启动服务提供方的项目和服务使用方的项目:

测试下/items/{id}这个API:

成功!下面再访问下监控中心,因为监控中心和zookeeper在一台服务器上,我的tomcat部署在8888端口,即访问192.168.30.145:8888/dubbo-admin即可,用户名密码默认为root:

查看所有注册的服务:

查看包括消费者和提供者的所有应用名:

消费者、提供者详细信息:

本文转自 作者:Jitwxs  来源:CSDN
原文:https://blog.csdn.net/yuanlaijike/article/details/79654183

(转)Dubbo + Zookeeper入门初探的更多相关文章

  1. Dubbo,Zookeeper入门

    Zookeeper 功能:分布式应用程序协调服务,集群管理者,监视集群各个节点状态-->提交反馈-->进行下一步合理操作: 机制:目录方式,当目录节点发生变化(数据改变,被删除,子节点增加 ...

  2. Dubbo+Zookeeper 入门Demo

    1.Zookeeper安装及启动 可参考这篇文章https://www.cnblogs.com/geekdc/p/5948326.html 从下载到启动都描述的很详细,按照文章一步一步走即可. 2.D ...

  3. spring Boot环境下dubbo+zookeeper的一个基础讲解与示例

    一,学习背景 1.   前言 对于我们不管工作还是生活中,需要或者想去学习一些东西的时候,大致都考虑几点: a)      我们为什么需要学习这个东西? b)     这个东西是什么? c)      ...

  4. 基于springboot构建dubbo的入门demo

    之前记录了构建dubbo入门demo所需的环境以及基于普通maven项目构建dubbo的入门案例,今天记录在这些的基础上基于springboot来构建dubbo的入门demo:众所周知,springb ...

  5. zookeeper 入门知识

    作为开启分布式架构的基石,除了必会还有的选么 自己的一些理解,有错误的话请一定要给予指正! 一.是什么? 分布式数据一致性的解决方案. 二.有什么用 数据的发布/订阅(配置中心)  . 负载均衡(du ...

  6. Dubbo从入门到实战:实战篇

    一.加入 zookeeper 作为注册中心 在前面的案例中,我们没有使用任何的注册中心,而是用一种直连的方式进行的.但是,实际上很多时候,我们都是使用 dubbo + zookeeper 的方式,使用 ...

  7. Dubbo从入门到实战:入门篇

    很多时候,其实我们使用这个技术的时候,可能都是因为项目需要,所以,我们就用了,但是,至于为什么我们需要用到这个技术,可能自身并不是很了解的,但是,其实了解技术的来由及背景知识,对于理解一项技术还是有帮 ...

  8. springboot整合dubbo+zookeeper最新详细

    引入 最近和小伙伴做一个比赛,处于开发阶段,因为涉及的服务比较多,且服务需要分开部署在不同的服务器上,讨论之后,打算采用分布式来做,之前学习springboot的时候,部分章节涉及到了springbo ...

  9. 搭载Dubbo+Zookeeper踩了这么多坑,我终于决定写下这篇!

    大家好,我是melo,一名大二上软件工程在读生,经历了一年的摸滚,现在已经在工作室里边准备开发后台项目啦. 这篇文章我们不谈数据结构了,来谈谈入门分布式踩过的坑.感觉到了分布式这一层,由于技术更新迭代 ...

随机推荐

  1. 放大镜如何用js

    例如: let imgs = { small: ["imgA_1.jpg", "imgB_1.jpg", "imgC_1.jpg"], mi ...

  2. for update的作用和用法

    一.for update定义 for update是一种行级锁,又叫排它锁,一旦用户对某个行施加了行级加锁,则该用户可以查询也可以更新被加锁的数据行,其它用户只能查询但不能更新被加锁的数据行.如果其它 ...

  3. Badboy + JMeter性能测试(转)

    1. 软件介绍   1.1 Badboy  Badboy是用来录制操作过程的,它录制的结果是被jmeter做并发测试的素材使用. 下载网址:http://www.badboy.com.au/ 1.2下 ...

  4. django 模型层(orm)05

    目录 配置测试脚本 django ORM基本操作 增删改查 Django 终端打印SQL语句 13条基本查询操作 双下滑线查询 表查询 建表 一对多字段数据的增删改查 多对多字段数据的增删改查 基于对 ...

  5. 【51nod 1847】奇怪的数学题

    题目描述 给出 N,K ,请计算下面这个式子: \(∑_{i=1}^N∑_{j=1}^Nsgcd(i,j)^k\) 其中,sgcd(i, j)表示(i, j)的所有公约数中第二大的,特殊地,如果gcd ...

  6. Linux Tomcat 文件上传异常

    如题: ERROR > The temporary upload location [/tmp/tomcat.7982919351026796141.9097/work/Tomcat/local ...

  7. Java进阶知识05 Hibernate联合主键之Annotation(注解)和XML实现方式

    1.Hibernate联合主键(Annotation实现) 1.1.单列主键 1.1.1.为什么要有主键? //唯一确定一条记录    1.1.2.一个表能否有多个主键? //不能    1.1.3. ...

  8. php写入文件来调试接口数据

    $fp = fopen('write.txt', 'a+b'); //a+读写方式打开,将文件指针指向文件末尾.b为强制使用二进制模式. 如果文件不存在则尝试创建之. fwrite($fp, prin ...

  9. 用Python操作excel文档

    使用Python第三方库 这一节我们学习如何使用Python去操作Excel文档.如果大家有人不知道Excel的话,那么建议先学一学office办公基础.这里想要操作Excel,必须安装一个Pytho ...

  10. CodeForces–833B--The Bakery(线段树&&DP)

    B. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...