dubbo+zookeeper+spring整合(注解式demo)

最近整理了一下dubbo+zookeeper+spring的主解式开发,做了一个小的demo,

配置式的dubbo工程请看:   http://blog.csdn.net/gjm15652957971/article/details/78394647

完整的代码下载链接为:       http://download.csdn.net/download/gjm15652957971/10105483

环境:

1)     本地zookeeper

2)     本地dubbo

3)     Jkd1.8

4)     Eclipse

5)    Tomcat8

一:单机模式安装zookeeper

1,下载zookeeper注册中心,下载地址:http://www.apache.org/dyn/closer.cgi/zookeeper/ 下载后解压至自己想放置的位置即可,

2, Zookeeper的配置文件在 conf 目录下,这个目录下有 zoo_sample.cfg 和 log4j.properties,你需要做的就是将zoo_sample.cfg 改名为 zoo.cfg,因为 Zookeeper在启动时会找这个文件作为默认配置文件.

    dubbo+zookeeper+spring整合(注解式demo)_第1张图片

   •tickTime:这个时间是作为Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。

   •dataDir:顾名思义就是Zookeeper保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。

   •dataLogDir:顾名思义就是Zookeeper 保存日志文件的目录

   •clientPort:这个端口就是客户端连接Zookeeper服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求

 

配置好后,zookeeper会监听本机的2181端口。

当这些配置项配置好后,你现在就可以启动 Zookeeper 了,启动后要检查 Zookeeper 是否已经在服务,可以通过 netstat – ano 命令查看是否有你配置的 clientPort 端口号在监听服务。

 

3, 进入E:\zookeeper-3.3.6\zookeeper-3.3.6\bin,双击zkServer.cmd启动注册中心服务。这样,zookeeper环境(注册中心)配置完毕,

二: 单机下安装dubbo-admin

1,下载dubbo-admin.war:http://download.csdn.net/download/gjm15652957971/10105423

2, 将下载下来的war包放入一个单独的tomcat的webapps里,确保项目默认保存文件夹为webapps,

   dubbo+zookeeper+spring整合(注解式demo)_第2张图片

未修改前为:

dubbo+zookeeper+spring整合(注解式demo)_第3张图片

启动tomcat后,然后打开浏览器,访问: http://localhost:8888/war的名字/ (如果用的war为我提供的,那访问地址为: http://localhost:8888/dubbo-admin-2.5.6/)然后点击回车, 输入用户名和密码,用户名和密码均为root,这时候就会看到dubbo控制台,如下图.

dubbo+zookeeper+spring整合(注解式demo)_第4张图片

三:创建两个工程,服务提供者和消费者:

服务提供者:

创建一个maven工程,pom.xml为:


  4.0.0
  com.dubbo
  dubbo-provider-demo
  war
  0.0.1-SNAPSHOT
  dubbo-provider-demoMaven Webapp
  http://maven.apache.org
 
  
        
        4.1.5.RELEASE
        
        3.2.6
        
        1.6.6
        1.2.17
    
 
 
    
        
            junit
            junit
            4.12
            test
        
        
        
        
            org.springframework
            spring-core
            ${spring.version}
        
 
        
            org.springframework
            spring-web
            ${spring.version}
        
 
        
            org.springframework
            spring-oxm
            ${spring.version}
        
 
        
            org.springframework
            spring-tx
            ${spring.version}
        
 
        
            org.springframework
            spring-jdbc
            ${spring.version}
        
 
        
            org.springframework
            spring-webmvc
            ${spring.version}
        
 
        
            org.springframework
            spring-aop
            ${spring.version}
        
 
        
            org.springframework
            spring-context-support
            ${spring.version}
        
 
        
            org.springframework
            spring-aop
            ${spring.version}
        
 
        
            org.springframework
            spring-test
            ${spring.version}
        
        
            org.springframework
            spring-aspects
            ${spring.version}
        
 
        
            org.aspectj
            aspectjrt
            1.6.11
        
        
            org.aspectj
            aspectjweaver
            1.6.11
        
        
            cglib
            cglib
            2.1_3
        
        
 
        
        
            org.springframework
            spring-websocket
            ${spring.version}
        
 
        
        
            commons-fileupload
            commons-fileupload
            1.3.2
        
 
        
        
            org.mybatis
            mybatis
            ${mybatis.version}
        
        
        
            org.mybatis
            mybatis-spring
            1.2.2
        
 
        
        
            com.alibaba
            druid
            1.0.8
        
 
 
 
        
        
            org.codehaus.jackson
            jackson-mapper-asl
            1.9.13
        
        
            net.sf.json-lib
            json-lib
            2.4
            jdk15
        
 
        
        
            jdom
            jdom
            1.0
        
        
            dom4j
            dom4j
            1.6.1
        
 
 
 
        
        
        
            log4j
            log4j
            ${log4j.version}
        
        
            org.slf4j
            slf4j-api
            ${slf4j.version}
        
        
            org.slf4j
            slf4j-log4j12
            ${slf4j.version}
        
 
        
        
            javax.servlet
            javax.servlet-api
            3.0.1
        
 
        
        
            com.github.miemiedev
            mybatis-paginator
            1.2.17
        
 
        
        
            commons-codec
            commons-codec
            1.10
        
        
        
            net.sf.ezmorph
            ezmorph
            1.0.6
        
        
        
            javax.servlet.jsp
            jsp-api
            2.2.1-b03
        
        
            javax.servlet
            jstl
            1.2
        
        
            taglibs
            standard
            1.1.2
        
 
        
        
            com.whalin
            Memcached-Java-Client
            3.0.1
            jar
            compile
        
 
        
        
            io.springfox
            springfox-swagger2
            2.2.2
        
        
 
        
            com.fasterxml.jackson.core
            jackson-databind
            2.4.4
        
        
            net.sourceforge.jexcelapi
            jxl
            2.6.12
        
 
        
        
            commons-codec
            commons-codec
            1.6
        
        
            org.apache.poi
            poi
            3.12
        
 
        
            org.apache.poi
            poi-ooxml
            3.12
        
 
        
            org.apache.poi
            poi-ooxml-schemas
            3.12
        
 
        
            org.apache.poi
            poi-excelant
            3.12
        
 
        
            org.apache.poi
            poi-examples
            3.12
        
        
        
        
            cn.jpush.api
            jpush-client
            3.2.9
        
        
            com.github.fernandospr
            javapns-jdk16
            2.3
        
        
        
            org.glassfish.jersey.core
            jersey-client
            2.9
        
        
            org.glassfish.jersey.media
            jersey-media-json-jackson
            2.9
        
        
            org.glassfish.jersey.media
            jersey-media-multipart
            2.9
        
 
        
        
            com.alibaba
            dubbo
            2.5.3
            
                
                    spring
                    org.springframework
                
            
        
        
            org.apache.zookeeper
            zookeeper
            3.3.6
            
                
                    log4j
                    log4j
                
            
        
        
            com.101tec
            zkclient
            0.2
        
        
    


Web.xml为;



 
  
        log4jConfigLocation
        classpath:log4j.xml
    
    
        log4jRefreshInterval
        60000
    
   
       
        webAppRootKey   
        webapp.root.common   
    
 
    
    
        springMvc
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            
                classpath:applicationContext.xml
            
        
        1
    
    
        springMvc
        /
    
 
 
    
    
        springCharacterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
        
            forceEncoding
            true
        
    
    
        springCharacterEncodingFilter
        /*
    
 
 
    
    
        org.springframework.web.util.IntrospectorCleanupListener
    
 
    
        30
    


Application.xml内容为:

dubbo+zookeeper+spring整合(注解式demo)_第5张图片

创建接口:

publicinterface TestService {

  String getName();

}

实现类:

@Service

publicclass TestServiceImplimplements TestService{

 

   public String getName() {

      return"test";

   }

 

}

消费者:

Pom.xml


   4.0.0
   com.dubbo
   dubbo-consumer-demo
   war
   0.0.1-SNAPSHOT
   dubbo-consumer-demoMaven Webapp
   http://maven.apache.org
 
    
        
        4.1.5.RELEASE
        
        3.2.6
        
        1.6.6
        1.2.17
    
 
 
    
        
            junit
            junit
            4.12
            test
        
        
        
        
            org.springframework
            spring-core
            ${spring.version}
        
 
        
            org.springframework
            spring-web
            ${spring.version}
        
 
        
            org.springframework
            spring-oxm
            ${spring.version}
        
 
        
            org.springframework
            spring-tx
            ${spring.version}
        
 
        
            org.springframework
            spring-jdbc
            ${spring.version}
        
 
        
            org.springframework
            spring-webmvc
            ${spring.version}
        
 
        
            org.springframework
            spring-aop
            ${spring.version}
        
 
        
            org.springframework
            spring-context-support
            ${spring.version}
        
 
        
            org.springframework
            spring-aop
            ${spring.version}
        
 
        
            org.springframework
            spring-test
            ${spring.version}
        
        
            org.springframework
            spring-aspects
            ${spring.version}
        
 
        
            org.aspectj
            aspectjrt
            1.6.11
        
        
            org.aspectj
            aspectjweaver
            1.6.11
        
        
            cglib
            cglib
            2.1_3
        
        
 
        
        
            org.springframework
            spring-websocket
            ${spring.version}
        
 
        
        
            commons-fileupload
            commons-fileupload
            1.3.2
        
 
        
        
            org.mybatis
            mybatis
            ${mybatis.version}
        
        
        
            org.mybatis
            mybatis-spring
            1.2.2
        
 
        
        
            com.alibaba
            druid
            1.0.8
        
 
 
 
        
        
            org.codehaus.jackson
            jackson-mapper-asl
            1.9.13
        
        
            net.sf.json-lib
            json-lib
            2.4
            jdk15
        
 
        
        
            jdom
            jdom
            1.0
        
        
            dom4j
            dom4j
            1.6.1
        
 
 
 
        
        
        
            log4j
            log4j
            ${log4j.version}
        
        
            org.slf4j
            slf4j-api
            ${slf4j.version}
        
        
            org.slf4j
            slf4j-log4j12
            ${slf4j.version}
        
 
        
        
            javax.servlet
            javax.servlet-api
            3.0.1
        
 
        
        
            com.github.miemiedev
            mybatis-paginator
            1.2.17
        
 
        
        
            commons-codec
            commons-codec
            1.10
        
        
        
            net.sf.ezmorph
            ezmorph
            1.0.6
        
        
        
            javax.servlet.jsp
            jsp-api
            2.2.1-b03
        
        
            javax.servlet
            jstl
            1.2
        
        
            taglibs
            standard
            1.1.2
        
 
        
        
            com.whalin
            Memcached-Java-Client
            3.0.1
            jar
            compile
        
        
        
            io.springfox
            springfox-swagger2
            2.2.2
        
        
 
        
            com.fasterxml.jackson.core
            jackson-databind
            2.4.4
        
        
            net.sourceforge.jexcelapi
            jxl
            2.6.12
        
 
        
        
            commons-codec
            commons-codec
            1.6
        
        
            org.apache.poi
            poi
            3.12
        
 
        
            org.apache.poi
            poi-ooxml
            3.12
        
 
        
            org.apache.poi
            poi-ooxml-schemas
            3.12
        
 
        
            org.apache.poi
            poi-excelant
            3.12
        
 
        
            org.apache.poi
            poi-examples
            3.12
        
        
        
        
            cn.jpush.api
            jpush-client
            3.2.9
        
        
            com.github.fernandospr
            javapns-jdk16
            2.3
        
        
        
            org.glassfish.jersey.core
            jersey-client
            2.9
        
        
            org.glassfish.jersey.media
            jersey-media-json-jackson
            2.9
        
        
            org.glassfish.jersey.media
            jersey-media-multipart
            2.9
        
 
        
        
            com.alibaba
            dubbo
            2.5.3
            
                
                    spring
                    org.springframework
                
            
        
        
            org.apache.zookeeper
            zookeeper
            3.3.6
            
                
                    log4j
                    log4j
                
            
        
        
            com.101tec
            zkclient
            0.2
        
        
    


Web.xml



  
   
       
        webAppRootKey   
        webapp.root.api   
    
    
    
        springMvc
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            
               classpath:applicationContext.xml
            
        
        1
    
    
        springMvc
        /
    
 
    
    
        springCharacterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
        
            forceEncoding
            true
        
    
    
        springCharacterEncodingFilter
        /*
    
     
     
        spring.profiles.active 
         
        development 
     
 


Application.xml :

dubbo+zookeeper+spring整合(注解式demo)_第6张图片

消费者中的方法类(用来调用服务提供者提供的接口的类)

@Controller

@RequestMapping(value ="/api/v1/driver")

publicclass ConsumerTest {

   @Reference

   TestService testService;

   @RequestMapping(value ="/login/login.yueyue")

   publicvoid name() {

      String name = testService.getName();

      System.out.println(name);

   }

}

dubbo+zookeeper+spring整合(注解式demo)_第7张图片

这就是断点执行时,把服务提供者中的实现类中的test返回了过来.

至此,整理完毕,完整的代码下载链接为: http://download.csdn.net/download/gjm15652957971/10105483

你可能感兴趣的:(dubbo+zookeeper+spring整合(注解式demo))