Dubbo + Zookeeper

最近在学习一个分布式项目,使用到了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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

在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
  • 1
  • 2

进入conf目录,复制一份zoo_sample.cfgzoo.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 
  • 1
  • 2
  • 3

配置下dataDirdataLogDir的路径,为之前创建的两个文件夹的路径,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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

小心踩坑:

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

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

相关命令:

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


三、搭建dubbo监控中心

版本要求:

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

下载链接:点击这里

小心踩坑:

如果你的zookeeperdubbo-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>
  • 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

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

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

http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  • 1
  • 2
  • 3
  • 4

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 {

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

需要修改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>
  • 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
  • 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>
  • 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
  • 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;
    }
}
  • 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

五、测试

服务器上分别启动zookpertomcat

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
wxs@ubuntu:/usr/apache-tomcat-8.5.28/bin$ ./startup.sh 
Using CATALINA_BASE:   /usr/apache-tomcat-8.5.28
Using CATALINA_HOME:   /usr/apache-tomcat-8.5.28
Using CATALINA_TMPDIR: /usr/apache-tomcat-8.5.28/temp
Using JRE_HOME:        /usr/jdk1.8.0_161/jre
Using CLASSPATH:       /usr/apache-tomcat-8.5.28/bin/bootstrap.jar:/usr/apache-tomcat-8.5.28/bin/tomcat-juli.jar
Tomcat started.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

使用下面命令可以持续显示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:

查看所有注册的服务: 

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

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

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值