第一个dubbo+zookeeper项目例子

公司项目要用这两个东西,于是打算学习它。

首先我的理解dubbo是什么?zookeeper是什么?为什要这么搞。

项目分层:

  传统的,mvc -->垂直架构(将模块抽取成单独项目,项目互相调用)-->分布式服务架构(类似maven中央仓库类型,zookeeper提供注册器,类似容器,dubbo是一种手段类似,在垂直架构基础上,将服务provider(功能)发布,使用的项目consumer使用dubbo获取接口开发)

对传统工程改造
传统工程:MVC
控制层 Controller
业务逻辑层 service
模型层 dal,entity

一般改造过程:
将业务逻辑层、dao层以下的东西做成dubbo服务,控制层调服务

改造成Dubbo服务调用方式后的工程结构:
edu_common_parent(Maven父配置)
edu_facade_user(用户服务接口)
edu_service_user(用户服务实现)
edu_web_boss(服务消费者)

部署环境规划
edu-web-boss(consumer)
zookeeper-3.4.6(注册中心)
edu-service-user(provider)
MySql5.6(数据库)

第一步:

  mvc->垂直架构 (业务层,web层分离,facade服务层放些公共数据)

第二步:

  web使用facade接口代替service,服务层实现facade接口,使用装饰者模式。

第三步:

  服务接口配置prividexml:

<?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"
       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">
    <dubbo:application name="edu_service_user"/>
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
    <dubbo:protocol name="dubbo" port="20880"/>
    <dubbo:service interface="wusc.edu.facade.user.service.PmsUserFacade" ref="pmsUserFacade"/>
</beans>

  使用的消费项目配置consumer的xml:

  

<?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"
       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">
    <dubbo:application name="demo-consumer"/>
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
    <dubbo:reference id="pmsUserFacade" interface="wusc.edu.facade.user.service.PmsUserFacade"/>
</beans>

然后项目部分完成。启动本地zookeeper,再启动privider项目注册,然后启动consumer项目测试是否能正常消费了。

zoopeeper wndows安装:

把下载的zookeeper的文件解压到指定目录

D:machinezookeeper-3.3.6>



修改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 anacknowledgement

syncLimit=5

# the directory where the snapshot isstored.  //镜像数据位置

dataDir=D:\data\zookeeper

#日志位置

dataLogDir=D:\logs\zookeeper

# the port at which the clients willconnect  客户端连接的端口

clientPort=2181

注:如果启动有报错提示cfg文件有错误,可以用zoo_sample.cfg内内容替代也是可以的

 

进入到bin目录,并且启动zkServer.cmd,脚本启动

遇到的其他问题:

  1、项目报<context:annotation-config />需要jdk1.5或者以上才支持。前面看我的maven里面的json-lib是jdk15版本的,所以怀疑是这里的问题,于是想去下载jdk16的,发现没有。发现jsonlib下载必须指定jdk版本否则下载不来。

  http://blog.csdn.net/rambo_china/article/details/7691409

  2、发现不一定是json-lib的问题,好像是说spring-core使用注解里面对于jdk8的版本的判定原因导致的前面的报错,于是将spring-core里面的JDKVersion进行替换,可惜没有产生什么效果。

  http://bbs.csdn.net/topics/390838736

  发现在jar包里面加东西可以直接打开jar包然后拖动进去就好了

  3、最后前面的问题是将jdk替换成1.6解决的。因为我的侧重点在于dubbo和zookeeper所以忽略掉了它。

  4、出现问题项目启动,web项目访问总是404.查询日志,没发现什么原因,恶心得很。最后找同事帮忙,发现maven依赖报红。万恶的错误不主动提示log,出问题了也不提供log。

  5、启动项目使用的是tomcat容器或者测试类,测试类和dubbo配置文件参照写法:

    http://dubbo.io/

   测试提供方的写法:

 public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] {"classpath:spring/spring-context.xml"});
        context.start();
        synchronized (ProvideStartTest.class){
            while(true){
                ProvideStartTest.class.wait();
            }
        }
    }

  后面不加wait的话服务方得不到。

  6、中途出现的其他问题:

    mapper.xml的namespace没有写

    dubbo-privide-user.xml没有import

    

  dubbo的那几个jar:

  

 <!-- dubbo need Begin-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.netty</groupId>
            <artifactId>netty</artifactId>
            <version>3.2.5.Final</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.3</version>
        </dependency>
        <!-- dubbo need end -->

  继续进行项目提取分层:

项目增加工程:

edu-common(公共工程)
edu-common-config(公共配置工程)
edu-common-core(公共core工程)
edu-common-web(公共web工程)

这里注意一个要点:

  假如你的本地项目引用另一个本地项目,另一个本地项目引用其他的本地项目,你需要tomcat手动将这些项目全部引入依赖,不要指望自动引入,除非你安装到了maven,执行了install命令。

  

原文地址:https://www.cnblogs.com/aigeileshei/p/7910193.html