zoukankan      html  css  js  c++  java
  • Dubbox 环境搭建

    第一章:Dubbox简介

     

    Dubbox是一个开源的RPC(Remote ProcedureCall Protocol)远程调用框架,是由dangdang对阿里的Dubbo的升级,可以被视为Dubbo的增强版,基本架构没有变动,升级spring2.x到spring3.x,支持restful风格的调用调试方式,丰富了序列化的方式,提高了序列化的性能。

     

    Dubbox的Github官网:

    https://github.com/dangdangdotcom/dubbox

     

    第二章 环境搭建(基于注册中心是Zookeeper的搭建)

      

    2.1 dubbo-admin控制台的安装

     

    2.1.1打开dubbox的官网https://github.com/dangdangdotcom/dubbox

    点击Clone or download 我们选择最原始的Download ZIP,当前的stable版本是2.8.4

    2.1.2下载到某个文件夹下,解压文件夹:

    2.1.3 解压dubbox-master.zip

    2.1.4按住键盘shift 鼠标右击,选择命令窗口,进入maven编译 :

    mvn install -Dmaven.test.skip=true

    编译安装大概需要4~5分钟,编译安装成功之后显示如下:

    好了,到此为止,dubbox2.8.4就算是编译成功了,接下来我们要先搭建dubbox的控制台,因为可视化是我们最喜欢的模式了

     

    2.1.5进入刚才的文件夹D:dubboxstudydubbox-masterdubbo-admin arget

    找到dubbo-admin-2.8.4.war文件,因为这是一个war文件,所以我们就使用tomcat启动,下载apache-tomcat-7.0.40-windows-x64.zip到我们dubbox study目录

    解压apache-tomcat.zip,然后将刚才的dubbo-admin-2.8.4.war复制到D:dubboxstudyapache-tomcat-7.0.40webapps文件夹下

    因为dubbo的注册中心和管理控制台是依赖zookeeper,所以我们在测试环境下,需要启动一个zookeeper的实例,关于zookeeperwindow环境下的搭建就不赘述了,详细参考:

    http://blog.csdn.net/morning99/article/details/40426133

    2.1.6启动dubbo-admin控制台之前,先启动一个zookeeper实例(因为只是测试,所以就启动zookeeper集群了):

    2.1.7启动tomcat,进入D:dubbox studyapache-tomcat-7.0.40in,双击startup.bat

    上图表示启动成功,启动成功之后,我们会发现D:dubbox studyapache-tomcat-7.0.40webapps下多了一个文件夹dubbo-admin-2.8.4,进入

    D:dubboxstudyapache-tomcat-7.0.40webappsdubbo-admin-2.8.4WEB-INF,打开dubbo.properties:

    可以看到dubbo默认的注册机制是zookeeper,地址也是本地地址:127.0.0.1:2181,假如你此时zookeeper的实例的地址不是127.0.0.1:2181,或者注册机制是Redis的话,需要修改dubbo.properties的配置,此处就不做修改了

     

     

    2.1.8打开浏览器。输入http://localhost:8080/dubbo-admin-2.8.4/,账户密码是root/root.就可以看到页面了

    2.2 dubbo的Provider/Consumer 消费者和提供者的Demo代码编写

     

    2.2.1环境准备JDK1.7 +Eclipse(STS) + Maven3.x

     

     

    2.2.2新建WorkingSet

     

    点击finish:

     

    2.2.3新建maven项目

     

    建好之后的目录结构:

     

    2.2.4在bazinga-provider和bazinga-consumer的pom.xml中引入dubbox的依赖(暂时使用2.8.3的依赖,相对简单一点):

    1.  
      <properties>
    2.  
      <dubbox.version>2.8.3</dubbox.version>
    3.  
      <slf4j.version>1.7.5</slf4j.version>
    4.  
      <zookeeper.version>3.4.6</zookeeper.version>
    5.  
      </properties>
    6.  
      <dependencies>
    7.  
      <dependency>
    8.  
      <groupId>com.alibaba</groupId>
    9.  
      <artifactId>dubbo</artifactId>
    10.  
      <version>${dubbox.version}</version>
    11.  
      </dependency>
    12.  
      <dependency>
    13.  
      <groupId>org.slf4j</groupId>
    14.  
      <artifactId>slf4j-api</artifactId>
    15.  
      <version>${slf4j.version}</version>
    16.  
      </dependency>
    17.  
      <dependency>
    18.  
      <groupId>org.apache.zookeeper</groupId>
    19.  
      <artifactId>zookeeper</artifactId>
    20.  
      <version>${zookeeper.version}</version>
    21.  
      <exclusions>
    22.  
      <exclusion>
    23.  
      <groupId>io.netty</groupId>
    24.  
      <artifactId>netty</artifactId>
    25.  
      </exclusion>
    26.  
      <exclusion>
    27.  
      <groupId>org.slf4j</groupId>
    28.  
      <artifactId>slf4j-api</artifactId>
    29.  
      </exclusion>
    30.  
      <exclusion>
    31.  
      <groupId>log4j</groupId>
    32.  
      <artifactId>log4j</artifactId>
    33.  
      </exclusion>
    34.  
      <exclusion>
    35.  
      <groupId>org.slf4j</groupId>
    36.  
      <artifactId>slf4j-log4j12</artifactId>
    37.  
      </exclusion>
    38.  
      <exclusion>
    39.  
      <groupId>jline</groupId>
    40.  
      <artifactId>jline</artifactId>
    41.  
      </exclusion>
    42.  
      </exclusions>
    43.  
      </dependency>
    44.  
      <dependency>
    45.  
      <groupId>org.slf4j</groupId>
    46.  
      <artifactId>slf4j-api</artifactId>
    47.  
      <version>${slf4j.version}</version>
    48.  
      </dependency>
    49.  
      <dependency>
    50.  
      <groupId>com.101tec</groupId>
    51.  
      <artifactId>zkclient</artifactId>
    52.  
      <version>0.2</version>
    53.  
      </dependency>
    54.  
      <dependency>
    55.  
      <groupId>ch.qos.logback</groupId>
    56.  
      <artifactId>logback-classic</artifactId>
    57.  
      <version>1.0.13</version>
    58.  
      </dependency>
    59.  
      </dependencies>

    2.2.5在bazinga-provider编写调用接口IDemoService:

    1.  
      package org.bazinga.service;
    2.  
       
    3.  
      public interface IDemoService {
    4.  
       
    5.  
      public String sayHello();
    6.  
       
    7.  
      }

    具体的实现:

     

    1.  
      package org.bazinga.service.impl;
    2.  
       
    3.  
      import org.bazinga.service.IDemoService;
    4.  
       
    5.  
      public class IDemoServiceImpl implements IDemoService {
    6.  
       
    7.  
      public String sayHello() {
    8.  
      return "hello dubbox";
    9.  
      }
    10.  
       
    11.  
      }

    2.2.6在src/main/resources下配置dubbo基于Spring的配置文件spring-dubbo-provider.xml,在这里需要配置注册中心的地址,通信的协议方式,服务提供者的应用名,最后就是最关键的需要暴露的服务,我们这里就是

     

    1.  
      <?xml version="1.1" encoding="UTF-8"?>
    2.  
      <beans xmlns="http://www.springframework.org/schema/beans"
    3.  
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    4.  
      xsi:schemaLocation="http://www.springframework.org/schema/beans
    5.  
      http://www.springframework.org/schema/beans/spring-beans.xsd
    6.  
      http://code.alibabatech.com/schema/dubbo
    7.  
      http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    8.  
       
    9.  
      <dubbo:application owner="lyncc" name="bazinga-app" />
    10.  
      <!--zookeeper注册中心 -->
    11.  
      <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
    12.  
       
    13.  
      <dubbo:protocol name ="dubbo" port="20880" />
    14.  
      <!-- 发布这个服务 -->
    15.  
      <dubbo:service protocol="dubbo" timeout="4000" connections="100" interface ="org.bazinga.service.IDemoService" ref="demoService" />
    16.  
      <!-- 和本地bean一样实现服务 -->
    17.  
      <bean id="demoService" class="org.bazinga.service.impl.IDemoServiceImpl" />
    18.  
       
    19.  
      </beans>

    细心的你会发现左侧有报错的提示:

     

    这个报错是因为不识别dubbo的命名空间,所以需要导入xsd文件,dubbo.xsd文件源码中有,网上也可以下载到,下载好或者在源码中找到之后,选择Windw->Perferences->XML->XMLCatelog->User Specifed Entries

     

    点击add,点击FileSystem 选择你下载好的dubbo.xsd,输入key值,key值要与schema值一样,点击OK:

     

    一路保存刚才的设置,重新打开spring-dubbo-provider.xml文件,可以发现报错消失。到此为止,dubbox的服务提供者端的代码已经编写完毕,我们写个测试类测试一下:

    1.  
      package org.bazinga.service.test;
    2.  
       
    3.  
      import org.springframework.context.support.ClassPathXmlApplicationContext;
    4.  
       
    5.  
      public class DubboxProviderDemoService {
    6.  
       
    7.  
      public static void main(String[] args) throws InterruptedException {
    8.  
      ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
    9.  
      "spring-dubbo-provider.xml");
    10.  
      context.start();
    11.  
      Thread.sleep(2000000l);
    12.  
      }
    13.  
       
    14.  
      }

    启动main函数之后,会发现dubbo-admin的控制台中提供者发现了该服务:



    2.2.7服务提供者模块的编写,服务消费者只要有服务的接口就可以了,把服务提供者的接口复制到bazinga-consumer项目的同一个package下,注意必须放在同一个package下,也就是说服务消费者和服务提供者的接口的路径必须完全相同,因为这是服务的唯一标识,是一一对应的:



    2.2.8服务消费端dubbo的配置文件的编写spring-dubbo-consumer.xml,因为dubbo具有服务自动发现的功能,所以我们这边只需要配置注册中心,服务消费者的名字,和需要订阅的服务接口信息,如下:

    1.  
      <?xml version="1.1" encoding="UTF-8"?>
    2.  
      <beans xmlns="http://www.springframework.org/schema/beans"
    3.  
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    4.  
      xsi:schemaLocation="http://www.springframework.org/schema/beans
    5.  
      http://www.springframework.org/schema/beans/spring-beans.xsd
    6.  
      http://code.alibabatech.com/schema/dubbo
    7.  
      http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    8.  
       
    9.  
      <dubbo:application owner="lyncc" name="bazinga-consumer" />
    10.  
      <!--zookeeper注册中心 -->
    11.  
      <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
    12.  
       
    13.  
      <dubbo:reference id="demoService" interface="org.bazinga.service.IDemoService"/>
    14.  
       
    15.  
      </beans>
    16.  
       

    2.2.9编写测试类DubboConsumerDemoService:

    1.  
      package org.bazinga.service.test;
    2.  
       
    3.  
      import org.bazinga.service.IDemoService;
    4.  
      import org.springframework.context.support.ClassPathXmlApplicationContext;
    5.  
       
    6.  
      public class DubboConsumerDemoService {
    7.  
       
    8.  
      public static void main(String[] args) throws InterruptedException {
    9.  
      ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
    10.  
      "spring-dubbo-consumer.xml");
    11.  
      context.start();
    12.  
      IDemoService demoService = (IDemoService)context.getBean("demoService");
    13.  
      System.out.println(demoService.sayHello());
    14.  
      Thread.sleep(2000000l);
    15.  
      }
    16.  
       
    17.  
      }

    先运行DubboxProviderDemoService的情况下,启动DubboConsumerDemoService的main函数,运行结果:



    同时控制台admin页面也会显示消费者的信息:

    好了,到此为止,最简单的Dubbo的Helloworld搭建完毕

     

     

    2.3 本章小结

     

      本章简单的搭建了一个Dubbo的Demo,配置了dubbo-admin控制页面平台,编写了一个简单的Hello World,服务提供者向zookeeper注册中心注册服务,服务消费者从注册中心订阅服务,发现服务的暴露地址,完成远程调用,下一个章节,我们稍微深入体验一下dubbo给我们带来的丰富的RPC的一些特性

    mysql
  • 相关阅读:
    ArcMap+ArcCatalog手工新建点层及添加数据
    zedGraph 图表控件总结(一)
    Android 开发学习笔记(五)—— 最简单的注册界面
    repeater实现删除按钮
    repeater分页实例
    FCKeditor的使用说明
    javascript:void(0)是什么意思
    为asp.net控件点击事件添加Confirm()
    LINQ判断素数
    U盘文件不能删除,怎么处理
  • 原文地址:https://www.cnblogs.com/excellent-vb/p/9373392.html
Copyright © 2011-2022 走看看