2018年2月15日,阿里巴巴的dubbo进入了Apache孵化器,社区的加入,但愿dubbo能变得更好…前端
最近在学习一个分布式项目,使用到了dubbo
,以前没有使用过,体验一下,分布式项目地址:点击这里java
使用dubbo官网的一张图来介绍下dubbo
(本人才开始学习,若有错误,欢迎指正):git
Registry:注册中心,至关于房产中介,服务提供者和使用者都须要在这里注册/使用服务,我使用zookeeper
实现。github
Monitor:监控中心,至关于房产局,它能够统计服务提供者和服务使用者的一些信息,及他们之间的关系,我使用dubbo admin
实现。web
Provider:服务提供者,至关于房东,提供服务。spring
Consumer:服务消费者,想当于租户,使用服务。apache
下面我通俗的解释下dubbo的整个流程,我将服务比喻成房子:bootstrap
start:dubbo一启动,房东想好本身准备要租出去的房子ubuntu
register:房东将房子拿到房产中介那边进行登记,并留下本身的联系方式vim
subscribe:租户告诉房产中介本身想租一个什么样的房子
notify:房产中介回复给租户符合条件的房子的房东的联系方式
invoke:租户拿着联系方式去找房东租房子
count:房产局全程监控着房东和租户之间的交易
其中:
start、register、subscribe在dubbo服务一启动就完成了
notify、count是异步执行的
invoke是同步执行的
这一步比较简单,直接跳过,不会的能够看下这篇文章:Linux搭建JavaWeb开发环境(Java、Tomcat、MySQL)
我使用的是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.2-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-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
对于服务提供方,若是咱们想要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为超时时间
服务使用方,我使用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.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
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.
使用下面命令能够持续显示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