zoukankan      html  css  js  c++  java
  • maven+springmvc+dubbo+zookeeper

     
     
    为什么要用dubbo?
     
    还是让官方来解释吧:
     
    一般 nginx+tomcat
             | ----> Controller1--------->service1
    请求----->nginx  |
             |----->Controller2--------->service2
    请求进了Controller 就只有一条路可以走了
     
    使用dubbo后
                  | ------->service1
    请求------>Controller---->   |
                  |---------->service2
    简单的说 也就是 一个Contoller 我可以部署多个 service   。
     
    一般的mvc项目 包含 Controller、Servicei、ServiceImpl、dao三层
    使用doubbo我们可以把项目拆分:
    Controller 作为 “消费着” 一个项目
    ServiceImpl +dao 作为 “提供者” 一个项目
    Servicei “接口” 可以作为一个项目
    我们可以部署多个“提供着”。。。。。。。。。。。。。。。。。。。
     
    Zookeeper作为Dubbo服务的注册中心,Dubbo原先基于数据库的注册中心,没采用Zookeeper,Zookeeper一个分布式的服务框架,是树型的目录服务的数据存储,能做到集群管理数据 ,这里能很好的作为Dubbo服务的注册中心,Dubbo能与Zookeeper做到集群部署,当提供者出现断电等异常停机时,Zookeeper注册中心能自动删除提供者信息,当提供者重启时,能自动恢复注册数据,以及订阅请求。我们先在linux上安装Zookeeper,我们安装最简单的单点
    Windons 安装Zookeeper
     
    1,Zookeeper 官网下载windons 版本,(不会下载百度)
    单机安装非常简单,只要获取到 Zookeeper 的压缩包并解压到某个目录如
     
    打开目录机构为:
     
     
    Zookeeper 的启动脚本在 bin 目录下,Windows 下的启动脚本是 zkServer.cmd。
     
    在你执行启动脚本之前,还有几个基本的配置项需要配置一下,Zookeeper 的配置文件在 conf 目录下,这个目录下有 zoo_sample.cfg 和 log4j.properties,你需要做的就是将 zoo_sample.cfg 改名为 zoo.cfg,因为 Zookeeper 在启动时会找这个文件作为默认配置文件。下面详细介绍一下,这个配置文件中各个配置项的意义。
     
    打开以后
     
    # 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.
    dataDir=/tmp/zookeeper
    # the port at which the clients will connect
    clientPort=2181
    

      

    • tickTime:这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。
    • dataDir:顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。
    • clientPort:这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
    二.dubbo-admin。 dubbo管控台的安装
    下载 dubbo-admin-2.5.3.war  
    解压之后:
    修改 META-INF/dubbo.properties文件
    dubbo.registry.address=zookeeper://127.0.0.1:2181
    dubbo.admin.root.password=root
    dubbo.admin.guest.password=guest
    

    address:zookeeper 的ip地址 后面是端口号  ,和zookeeper中配置的端口号一样

    修改完成后需要一个tomcat   打开tomcat     webappsROOT 目录 ,此目录放置的是tomcat的首页,删除所有的文件,将解压后修改好的所有的dubbo-admin 里的文件复制到 webappsROOT中,

    此时我们已经配置好了。现在可以启动tomcat了 (主意:在启动tomcat之前要先启动zookeeper ,启动zookeeper前面有介绍)。 启动tomcat之后 在浏览器输入 http://localhost:8080  (我的是8083) 进入dubbo管控台的主页

    (备注:使用jdk1.8 tomcat启动可能会出错  有关 URI。。。。 的错误。我最后换成1.7了)

    用户名和密码就是dubbo.properties 中配置的 默认的是 用户名 :root, 密码:root 

    输入用户名和密码之后 进入首页           

     当然现在还没有 消费者 和 提供者 
     
    现在我们来开发   消费者  和 提供者
     
     
    配置 好 zookeeper之后 搭建 maven+springmvc+dubbo环境
     
    新建3个maven项目
    1,test-dubbo-provider ,java项目,作为提供者,serviceImpl 和dao 层
    2,test-public-interface ,java项目,存放公共的接口 ,servicei        是service的接口  (备注:1要依赖2,  3也要依赖2)
    3,test-web-consumer,web项目,存放 Controller 和 页面 
     
    首先我们来开发 服务的提供者也就是 test-dubbo-provider ,目录结构为
    ApplicationContent-dubbo.xml        dubbo服务的配置文件(名字随意)     (待会再介绍)
    ApplicationContent.xml       spring的配置文件
    这两个配置文件 必须要放到 MATE-INF/spring/下面 (原因后面再说)
     
    UserServiceImpl.java  借口的实现类
    package com.cl.user.serviceImpl;
    
    import org.springframework.stereotype.Service;
    
    import com.cl.user.servicei.UserService;
    
    @Service("userService")
    public class UserServiceImpl implements UserService{
    
    	@Override
    	public String sayHello() {
    		
    		System.out.println("hello world----------------------------");
    		
    		return "hello world";
    		
    	}
    }
     
    有了 实现类 还需要接口  也就是项目test-public-interface 
     
     
    这个项目很简单 只需要 接口 就行了
    package com.cl.user.servicei;
    
    public interface UserService {
    	public String sayHello();
    
    }
    

    当然 项目 1 要 依赖 项目 2  

    所以在1 的 pom.xml 中 有

    1 <dependency>
    2             <groupId>test-web</groupId>
    3             <artifactId>test-pubilc-interface</artifactId>       加入项目2依赖
    4             <version>0.0.1-SNAPSHOT</version>
    5             <scope>test</scope>
    6 </dependency>

    那么 1 中的 UserServiceImpl 就可以实现 UserService接口

     当然我们用到 dubbo 就要有dubbo的核心jar包  所以在1 的 pom.xml 中 有
    1 <dependency>
    2     <groupId>com.alibaba</groupId> 
    3     <artifactId>dubbo</artifactId>
    4     <version>2.5.3</version>
    5 </dependency>

    还要有 zookeeper的  所以在1 的 pom.xml 中 有

    1 <dependency>
    2         <groupId>org.apache.zookeeper</groupId>
    3         <artifactId>zookeeper</artifactId>
    4         <version>3.3.3</version>
    5 </dependency>

    下面介绍一下项目1 中 的 ApplicationContent-dubbo.xml   和  ApplicationContent.xml 

     
    ApplicationContent.xml  主要是spring的配置 不多说。
    1 <!-- 启动组件扫描,排除@Controller组件,该组件由SpringMVC配置文件扫描 -->
    2     <context:component-scan base-package="com.cl.user.serviceImpl"/>

    ApplicationContent-dubbo.xml   dubbo服务的配置

     
     1     <!-- 提供方应用信息,用于计算依赖关系 -->
     2     <dubbo:application name="hehe_provider" />
     3     <!-- 使用zookeeper注册中心暴露服务地址   端口是zookeeper 中配置的2181-->
     4     <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
     5     <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
     6     <!-- 用dubbo协议在20880端口暴露服务 -->
     7     <dubbo:protocol name="dubbo" port="20880" />
     8     <!-- 具体的实现bean -->
     9     <bean id="userService" class="com.cl.user.serviceImpl.UserServiceImpl" />
    10     <!-- 声明需要暴露的服务接口 -->
    11     <dubbo:service interface="com.cl.user.servicei.UserService" ref="userService" />

     主意:有可能你的配置文件中不识别 <dubbo:>  去网上找解决的办法

    到这里我们的  “提供者” 即 服务   已经开发完了,那么如何启动服务呢?  在项目1中有个start.java 

     1 package com.test;
     2 
     3 import org.apache.log4j.PropertyConfigurator;
     4 
     5 public class start {
     6      static{  
     7             PropertyConfigurator.configure("src/main/resources/log4j.properties");  
     8         }  
     9     public static void main(String[] args) {
    10         
    11         com.alibaba.dubbo.container.Main.main(args);
    12     }
    13 }

     主意里面有main方法,可以直接运行启动了。别急!!! 之前还有个问题就是为什   ApplicationContent-dubbo.xml   和  ApplicationContent.xml    这两个配置文件 必须要放到 /MATE-INF/spring/下面

    因为  com.alibaba.dubbo.container.Main.main(args)  默认就会去加载 /MATE-INF/spring/ 下的配置文件

    我们来看一下源码

     看完源码 这下明白为什么了吧!!!!!!!!!!!!!!!!!
     
    启动服务后  我们在 dubbo-admin中就能看到我们刚才开发的的  “提供者”
     
     
     
    192.168.56.1 是本地局域网 地址
     
    这是 “提供者”  此时还没有“消费者”   接下来我们就要开发 “消费者”
     
    test-web-consumer   
     
     
    UserController.java
    @Controller
    public class UserController {
        
        @Resource(name="userService")
        private UserService userService;
        
        
        @RequestMapping("/hello/test/world")
        public void sayHello(){
            System.out.println(userService.sayHello()+"**************************");
        }
    }

    ApplicationContext-mvc.xml 都是springmvc的配置 如下不解释了

    <mvc:annotation-driven/>
        <mvc:default-servlet-handler/>
        <context:component-scan base-package="com" />
        
        
        <!-- 对静态资源文件的访问  restful-->     
        <mvc:resources mapping="/js/**" location="/js/" />
        <mvc:resources mapping="/lib/**" location="/lib/" />
        <mvc:resources mapping="/plugins/**" location="/plugins/" />
        <mvc:resources mapping="/uploadFiles/**" location="/uploadFiles/" /> 
        <mvc:resources mapping="/WEB-INF/html/**" location="/WEB-INF/html/" /> 
         
        <!-- 配置SpringMVC的视图解析器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/html"/>
            <property name="suffix" value=".jsp"/>
        </bean>
        
        <!-- 上传拦截,如最大上传值及最小上传值 -->
          <bean id="multipartResolver"   class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >   
              <property name="maxUploadSize">    
                  <value>104857600</value>    
               </property>   
                <property name="maxInMemorySize">    
                    <value>4096</value>    
                </property>   
                 <property name="defaultEncoding">    
                    <value>utf-8</value>    
                </property> 
        </bean>  

    ApplicationContext-dubbo.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-2.5.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    
        <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->  
        <dubbo:application name="hehe_consumer" />  
      
        <!-- 使用zookeeper注册中心暴露服务地址 -->  
       
         <dubbo:registry address="zookeeper://127.0.0.1:2181" /> 
        
        <!-- 组播注册 -->
       <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
      
        <!-- 生成远程服务代理,可以像使用本地bean一样使用userService -->  
        <dubbo:reference id="userService"    interface="com.cl.user.servicei.UserService" />
    </beans>

     开发完消费者后 ,启动消费者,可以在管控台看到

     完了!!      Dubbo更多详细的东西还去官网的用户手册寻找吧!!  http://dubbo.io/User+Guide-zh.htm   http://dubbo.io/    用户手册说的挺详细的!这里仅供参考!
     
    最后 ,在开发的时候为了方便,我们可以使用组播注册!
     <!-- 组播注册 -->
       <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
     源码:
     https://github.com/KellyLChen/Dubbo.git
     
     
     
    作者原创转载请说明:http://www.cnblogs.com/c9999/p/6019307.html
     

    域名购买.com 后缀好域名 

     https://mi.aliyun.com/shop/38040

     
     
     
     
     
     
     
     
     
  • 相关阅读:
    Adobe Flash Builder 4.5 Android Air 程序开发系列 之六 多点触控
    Adobe Flash Builder 4.5 Android Air 程序开发系列 之九 定位
    Adobe Flash Builder 4.5 Android Air 程序开发系列 之七 重力感应
    Adobe Flash Builder 4.5 Android Air 程序开发系列 之五 保存数据的几种方式
    Adobe Flash Builder 4.5 Android Air 程序开发系列 之八 照相机
    Adobe Flash Builder 4.5 Android Air 程序开发系列 之三 Application 配置详解
    Adobe Flash Builder 4.5 Android Air 程序开发系列 之四 打开与关闭应用程序是的保存数据
    ADOBE FLASH BUILDER 4.6 IOS 开发之部署与调试
    [译] 高性能JavaScript 1至5章总结
    页签及盒子的web标准实现
  • 原文地址:https://www.cnblogs.com/c9999/p/6019307.html
Copyright © 2011-2022 走看看