开篇之前先说说dubbo 和 dubbox 区别
阿里的GIT源码 https://github.com/alibaba/dubbo
maven库 https://mvnrepository.com/artifact/com.alibaba/dubbo 最高版本 2.5.3
当当网开源的 :https://github.com/dangdangdotcom/dubbox 最高 版本号 2.8.4

我在学习的时候,打算是先学习dubbo 然后再学习dubbox,可是网上只说了 两者的区别只是在于dubbox支持rest方式调用,其他的说的不明确。 我根据我自己的理解就是 阿里的dubbo 版本最高是 2.5.3 后面就不再更新了。而当当网接下来之后,就是再阿里的版本上更新了。最高版本是2.8.4。JAR包同名只是版本号不一样而已。其他没什么了。

随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进。
dubbo/dubbox 和SpringMVC 使用-LMLPHP
单一应用架构
当网站流量很小时,只需一个应用,将所有功能都部署在一起,以减少部署节点和成本。
此时,用于简化增删改查工作量的 数据访问框架(ORM) 是关键。
垂直应用架构
当访问量逐渐增大,单一应用增加机器带来的加速度越来越小,将应用拆成互不相干的几个应用,以提升效率。
此时,用于加速前端页面开发的 Web框架(MVC) 是关键。
分布式服务架构
当垂直应用越来越多,应用之间交互不可避免,将核心业务抽取出来,作为独立的服务,逐渐形成稳定的服务中心,使前端应用能更快速的响应多变的市场需求。
此时,用于提高业务复用及整合的 分布式服务框架(RPC) 是关键。
流动计算架构
当服务越来越多,容量的评估,小服务资源的浪费等问题逐渐显现,此时需增加一个调度中心基于访问压力实时管理集群容量,提高集群利用率。
此时,用于提高机器利用率的 资源调度和治理中心(SOA) 是关键。

一直在埋头码代码。好久之前听说dubbo这个东西好用。又听说dubbox也很好用。现在终于有点时间来弄。
先说dubbo。网上找了几篇 dubbo的使用。目前dubbo 都是和 zookeeper一起配合使用的。所以这里我也使用了zookeeper。
其中 zookeeper是可以做成集群模式的
1.什么是zookeeper

  ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、名字服务、分布式同步、组服务等。
ZooKeeper的目标就是封装好复杂易出错的关键服务,将简单易用的接口和性能高效、功能稳定的系统提供给用户。
ZooKeeper包含一个简单的原语集,[1] 提供Java和C的接口。
ZooKeeper代码版本中,提供了分布式独享锁、选举、队列的接口,代码在zookeeper-3.4.3\src\recipes。其中分布锁和队列有Java和C两个版本,选举只有Java版本。
2.zookeeper的原理
  ZooKeeper是以Fast Paxos算法为基础的,paxos算法存在活锁的 问题,即当有多个proposer交错提交时,有可能互相排斥导致没有一个proposer能提交成功,而Fast Paxos作了一些优化,通过选举产生一个leader,只有leader才能提交propose,具体算法可见Fast Paxos。因此,要想弄懂ZooKeeper首先得对Fast Paxos有所了解。[3]

ZooKeeper的基本运转流程:
  1、选举Leader。
  2、同步数据。
  3、选举Leader过程中算法有很多,但要达到的选举标准是一致的。
  4、Leader要具有最高的zxid。
  5、集群中大多数的机器得到响应并follow选出的Leader
3.zookeeper的特点
   在Zookeeper中,znode是一个跟Unix文件系统路径相似的节点,可以往这个节点存储或获取数据。如果在创建znode时Flag设置为 EPHEMERAL,那么当创建这个znode的节点和Zookeeper失去连接后,这个znode将不再存在在Zookeeper 里,Zookeeper使用Watcher察觉事件信息。当客户端接收到事件信息,比如连接超时、节点数据改变、子节点改变,可以调用相应的行为来处理数 据。Zookeeper的Wiki页面展示了如何使用Zookeeper来处理事件通知,队列,优先队列,锁,共享锁,可撤销的共享锁,两阶段提交。
那么Zookeeper能作什么事情呢,简单的例子:假设我们有20个搜索引擎的服务器(每 个负责总索引中的一部分的搜索任务)和一个总服务器(负责向这20个搜索引擎的服务器发出搜索请求并合并结果集),一个备用的总服务器(负责当总服务器宕 机时替换总服务器),一个web的cgi(向总服务器发出搜索请求)。搜索引擎的服务器中的15个服务器提供搜索服务,5个服务器正在生成索引。这20个 搜索引擎的服务器经常要让正在提供搜索服务的服务器停止提供服务开始生成索引,或生成索引的服务器已经把索引生成完成可以搜索提供服务了。使用 Zookeeper可以保证总服务器自动感知有多少提供搜索引擎的服务器并向这些服务器发出搜索请求,当总服务器宕机时自动启用备用的总服务器。

1.zookeeper

我官网下载的是zookeeper是 3.4.9 版本,据说比较稳定
下载的是 zookeeper-3.4.9.tar.gz
解压 tar -zxvf zookeeper-3.4.9.tar.gz
进入conf 目录里面有一个 zoo_sample.cfg 文件。 复制一份 重命名为zoo.cfg 必须这么做。

[root@localhost conf]# cat  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=/home/zk/data
## 日志文件夹,原文中可能没有,自定义 文件夹一定要存在
dataLogDir=/home/zk/log
# the port at which the clients will connect
## 开启端口号
clientPort=2181

## if you have many server 多服务集群模式 后面的3888端口是 提供服务端口
#server.1=192.168.23.200:2888:3888
#server.2=192.168.23.211:2888:3888
#server.3=192.168.23.212:2888:3888
# 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
  • 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
  • 38

server.1=192.168.23.200:2888:3888
server.A=B:C:D:其中 A 是一个数字,表示这个是第几号服务器;B 是这个服务器的 ip 地址;C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口;D 表示的是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的 Leader,而这个端口就是用来执行选举时服务器相互通信的端口。如果是伪集群的配置方式,由于 B 都是一样,所以不同的 Zookeeper 实例通信端口号不能一样,所以要给它们分配不同的端口号。

配置好了之后就启动。
cd /bin ./zkServer.sh start 启动
然后查看状态

 ./zkServer.sh  status
 ZooKeeper JMX enabled by default
Using config: /home/zookeeper-3.4.9/bin/../conf/zoo.cfg
Mode: standalone
[root@localhost bin]# 
  • 1
  • 2
  • 3
  • 4
  • 5

可以看到mode standalone 表示是单机模式
如果是集群,查看状态可能是 follow (副机)
或者是 Leader(主机)
如果是集群方式配置

[root@leon127 bin]# ./zkServer.sh  status
JMX enabled by default
Using config: /usr/local/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: leader

[root@leon146 bin]# ./zkServer.sh  status
JMX enabled by default
Using config: /usr/local/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: follower

[root@leon67 bin]# ./zkServer.sh  status
JMX enabled by default
Using config: /usr/local/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: follower
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

如果是集群配置,还是需要在配置的数据文件中dataDir=/home/zk/data 创建myid文件
在dataDir目录下创建一个myid文件,然后分别在myid文件中按照zoo.cfg文件的server.A中A的数值,在不同机器上的该文件中填写相应的值。

2.springMVC配置

服务提供者 ,在spring配置文件中 集成一个新的spring-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:aop="http://www.springframework.org/schema/aop"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
     http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
     http://code.alibabatech.com/schema/dubbo
     http://code.alibabatech.com/schema/dubbo/dubbo.xsd"
    >
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="hehe_provider" />
    <!-- 使用zookeeper注册中心暴露服务地址   端口是zookeeper 中配置的2181-->
    <dubbo:registry address="zookeeper://192.168.23.212:2181"/>
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />

     <!-- 服务者与消费者的默认配置 -->
    <!-- 延迟到Spring初始化完成后,再暴露服务,服务调用超时设置为6秒,超时不重试   -->
    <dubbo:provider delay="-1" timeout="6000" retries="0"/>
    <dubbo:consumer timeout="6000" retries="0"/>

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.smk.alilife.service.AliTestService" ref="aliTestService"  timeout="3000" protocol="dubbo" />
     <!-- 要引用的服务 -->
  <!--  <dubbo:reference interface="cn.test.dubbo.registry.service.TestRegistryService" id="testRegistryService" /> -->
</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

服务消费者 ,在spring配置文件中 集成一个新的spring-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:aop="http://www.springframework.org/schema/aop"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
     http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
     http://code.alibabatech.com/schema/dubbo
     http://code.alibabatech.com/schema/dubbo/dubbo.xsd"
    >

    <dubbo:annotation package="com.smk.ac.action.ext"/>

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="hehe_provider" />
    <!-- 使用zookeeper注册中心暴露服务地址   端口是zookeeper 中配置的2181-->
    <dubbo:registry address="zookeeper://192.168.23.212:2181"/>
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />

     <!-- 服务者与消费者的默认配置 -->
    <!-- 延迟到Spring初始化完成后,再暴露服务,服务调用超时设置为6秒,超时不重试   -->
    <dubbo:provider delay="-1" timeout="6000" retries="0"/>
    <dubbo:consumer timeout="6000" retries="0"/>

    <!-- 声明需要暴露的服务接口
    <dubbo:service interface="com.smk.alilife.service.AliTestService" ref="aliTestService"  timeout="3000" protocol="dubbo" />-->
     <!-- 要引用的服务 -->
    <dubbo:reference interface="com.smk.alilife.service.AliTestService" id="aliTestService" />
</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
  • 36
  • 37

看消费者使用的代码

package com.smk.ac.action.ext.vircard;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.cat.common.bean.EMsg;
import com.cat.common.bean.FMsgResponse;
import com.smk.ac.action.ext.base.BaseAction;
import com.smk.alilife.bean.ActAppChannel;
import com.smk.alilife.service.AliTestService;

@Controller
@Scope("prototype")
public class VirtualCardAction extends BaseAction{

    //像引用本地service一样使用就可以
    @Autowired
    private AliTestService aliTestService;

   /**
    * 获取卡列表
    * @return
    */
   @RequestMapping(value ="dubbo/test")
   @ResponseBody
   public FMsgResponse getVirCardList(){
      try{
          ActAppChannel s =  aliTestService.getBean("123123");
         return new FMsgResponse(EMsg.Success.code(), EMsg.Success.value(),s);
      }catch(Exception e){
         e.printStackTrace();
         return new FMsgResponse(EMsg.Fail.code(), EMsg.Fail.value());
      }
   }


}
  • 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
  • 38
  • 39
  • 40
  • 41

由此可以看出 十分方便。但是有一个就是service JAR包 需要单独打包提供出去。消费者和服务都要引用用的。

服务提供者 提供service的实现类,所以需要该JAR包
消费者需要使用service 。所以也需要该JAR包

dubbox 的使用

10-06 18:09