初步认识dubbo--小案例

Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合)。从服务模型的角度来看,Dubbo采用的是一种非常简单的模型,要么是提供方提供服务,要么是消费方消费服务,所以基于这一点可以抽象出服务提供方(Provider)和服务消费方(Consumer)两个角色。

 

 

先上一个小例子进行分析:

首先新建2个maven项目来充当消费方,服务方

 

服务方:

pom.xml文件主要配置内容:

  <!-- spring begin -->  
  <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-context</artifactId>  
            <version>4.1.6.RELEASE</version>  
    </dependency>  
    
    <!-- dubbo begin -->  
    <dependency>  
            <groupId>com.alibaba</groupId>  
            <artifactId>dubbo</artifactId>  
            <version>2.5.3</version>  
    </dependency>
    
        <!-- 注册中心zookeeper begin -->
        <dependency>  
            <groupId>org.apache.zookeeper</groupId>  
            <artifactId>zookeeper</artifactId>  
            <version>3.3.6</version>  
        </dependency>
        
         <!-- log begin -->  
        <dependency>  
            <groupId>commons-logging</groupId>  
            <artifactId>commons-logging</artifactId>  
            <version>1.1.1</version>  
        </dependency>  
        <dependency>  
            <groupId>log4j</groupId>  
            <artifactId>log4j</artifactId>  
            <version>1.2.15</version>  
            <exclusions>  
                <exclusion>  
                    <groupId>com.sun.jdmk</groupId>  
                    <artifactId>jmxtools</artifactId>  
                </exclusion>  
                <exclusion>  
                    <groupId>com.sun.jmx</groupId>  
                    <artifactId>jmxri</artifactId>  
                </exclusion>  
                <exclusion>  
                    <artifactId>jms</artifactId>  
                    <groupId>javax.jms</groupId>  
                </exclusion>  
                <exclusion>  
                    <artifactId>mail</artifactId>  
                    <groupId>javax.mail</groupId>  
                </exclusion>  
            </exclusions>  
        </dependency>
         
        <!-- 其他配置begin -->
        <dependency>  
            <groupId>org.jboss.netty</groupId>  
            <artifactId>netty</artifactId>  
            <version>3.2.0.Final</version>  
        </dependency>  
        <dependency>  
            <groupId>com.101tec</groupId>  
            <artifactId>zkclient</artifactId>  
            <version>0.8</version>  
        </dependency>  
        
      <!-- junit begin -->     
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

配置文件截图如下:

 

applicationProvider.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="server-dubbo-demo" />  
    <!-- zookeeper注册中心 -->  
   <!--  <dubbo:registry address="zookeeper://127.0.0.1:2181" />  -->
    
    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry address="multicast://224.5.6.7:1234" />
    
    <dubbo:protocol name="dubbo" port="20880" />  
      
    <!-- 和本地bean一样实现服务 -->   
    <bean id="demoService" class="serviceImp.DemoServiceImp" />  
    <!-- 向注册中心注册暴漏服务地址,注册服务 -->  
    <dubbo:service interface="service.DemoService"  
        ref="demoService" executes="10" />  
</beans> 

 

接下来编写我们提供服务的接口service :

public interface DemoService {

    String sayHello(String name);

    String PrintString();
}

接下来编写我们的实现类serviceImp:

import service.DemoService;

public class DemoServiceImp implements DemoService {

    @Override
    public String sayHello(String name) {
        System.out.println("start....");
        return "你好  " + name;
    }

    @Override
    public String PrintString() {
        return "Hello World";
    }
}

这样就定义好了我们提供的服务,接下来就是启动服务方的服务,进行发布,提供给消费方进行调用

这里本地使用的是junit进行测试:

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ProviderTest {
    @Test
    public void mainTest() throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationProvider.xml" });
        context.start();
        System.out.println("输入任意按键退出 ~ ");
        System.in.read();
        context.close();
    }
}

 文件的目录结构如下:

 

 

消费方:

pom.xml主要配置文件内容:

<!-- spring begin -->  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-context</artifactId>  
            <version>4.1.6.RELEASE</version>  
        </dependency>  
        <!-- spring end -->  
 
        <!-- dubbo begin -->  
        <dependency>  
            <groupId>com.alibaba</groupId>  
            <artifactId>dubbo</artifactId>  
            <version>2.5.3</version>  
        </dependency>  
        <!-- dubbo end -->  
 
        <!-- 注册中心zookeeper begin -->  
        <dependency>  
            <groupId>org.apache.zookeeper</groupId>  
            <artifactId>zookeeper</artifactId>  
            <version>3.3.6</version>  
        </dependency>  
        <!-- 注册中心zookeeper end -->  
 
        <!-- log begin -->  
        <dependency>  
            <groupId>log4j</groupId>  
            <artifactId>log4j</artifactId>  
            <version>1.2.15</version>  
            <exclusions>  
                <exclusion>  
                    <groupId>com.sun.jdmk</groupId>  
                    <artifactId>jmxtools</artifactId>  
                </exclusion>  
                <exclusion>  
                    <groupId>com.sun.jmx</groupId>  
                    <artifactId>jmxri</artifactId>  
                </exclusion>  
                <exclusion>  
                    <artifactId>jms</artifactId>  
                    <groupId>javax.jms</groupId>  
                </exclusion>  
                <exclusion>  
                    <artifactId>mail</artifactId>  
                    <groupId>javax.mail</groupId>  
                </exclusion>  
            </exclusions>  
        </dependency>  
        <!-- log end -->  
 
        <!-- other begin -->  
        <dependency>  
            <groupId>com.101tec</groupId>  
            <artifactId>zkclient</artifactId>  
            <version>0.8</version>  
        </dependency>  
        
<-- 消费方这里要进行引用服务方服务,所以需要引入服务方工程jar -->
        <dependency>  
            <groupId>test.mvnproject</groupId>
            <artifactId>dubboProvider</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>       

<!-- other end -->
          
    <!-- junit begin -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

pom.xml文件截图如下:

 

 

 

applicationConsumer.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="consumer-dubbo-demo" />  
    <!-- zookeeper注册中心 -->  
    <!-- <dubbo:registry address="zookeeper://127.0.0.1:2181" />  -->
    
    <dubbo:registry address="multicast://224.5.6.7:1234" />
      
      <!-- 向注册中心订阅服务 -->  
    <dubbo:reference id="demoService" interface="service.DemoService" />
</beans> 

applicationConsumer.xml配置文件如下

 

消费方这边我们也编写一个测试类调用服务:

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import service.DemoService;

public class ConsumerTest{
    @Test
    public void consumerTest() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationConsumer.xml" });
        context.start();

         //加载服务方的bean
        DemoService service = (DemoService) context.getBean("demoService");

        //这里调用服务方的服务
        System.out.println(service.sayHello("小明"));
        System.out.println(service.PrintString());
        context.close();
    }
}

接下来把我们的demo跑起来,首先发布我们服务方的服务,直接run  as ProviderTest,之后启动消费方的测试类进行服务调用: run  as consumerTest

结果显示:  start.....

                 你好  小明

                 Hello  World

 

到此完成一个demo的创建.

备注:这里主要使用的是多播模式实现服务自动注册与发现:主要用于applicationProvider跟applicationConsumer配置文件中进行注册中心配置:     <dubbo:registry address="multicast://224.5.6.7:1234" />

这里我们也可以使用  zoookeper作为服务中心,进行服务的发布,注册,订阅 ,使用.

说明一点使用zookeper作为注册中心的话,服务方,消费方  配置文件中需要修改注册中心地址:<!-- <dubbo:registry address="zookeeper://127.0.0.1:2181" />  -->

接下来先在本机上启动zookeper注册中心(直接解压到英文路径下直接运行bin/zkServer.cmd)

启动成功截图如下:

运行服务方服务,线程阻塞成功,运行消费方,输出结果:

                 你好  小明

                 Hello  World

 

附上zookeper下载链接:

 zookeeper即下即用版:http://download.csdn.net/detail/xingbaozhen1210/9532177

具体参考链接阿里的dubbo官网demo:   http://dubbo.io/#tutorial

文章参考链接:       http://blog.csdn.net/xingbaozhen1210/article/details/51507974

 

posted @ 2017-09-11 10:19  幽雨_rain  阅读(255)  评论(0编辑  收藏  举报