zoukankan      html  css  js  c++  java
  • dubbo学习之服务消费者

    1、简介

      上节讲了如何发布一个dubbo服务,这节主要讲如何进行消费,创建一个消费者。

    2、详细步骤

      2.1 项目目录结构

        

      2.2 创建maven项目

        这里演示时其实通过一个main方法就可以了,没必要创建web项目,但是实际情况中,一般都是各个应用之间进行调用。大家根据自己需要选择创建哪种类型的项目,我这里还用maven格式的web项目。步骤省略,可以参考http://www.cnblogs.com/bookwed/p/4602120.html

      2.3 添加maven依赖

        修改pom.xml,加载基础的spring jar包 和 dubbo的jar 包,如下:

      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring-version>4.1.6.RELEASE</spring-version>
      </properties>
      
      <dependencies>
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-core</artifactId>
              <version>${spring-version}</version>
          </dependency>
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-context</artifactId>
              <version>${spring-version}</version>
          </dependency>
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-web</artifactId>
              <version>${spring-version}</version>
          </dependency>
          <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>dubbo</artifactId>
          <version>2.6.2-SNAPSHOT</version>
        </dependency>
          
      </dependencies>

      2.4 修改applicationContext-base.xml配置文件,增加dubbo的配置,如下:

    <?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:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:d="http://code.alibabatech.com/schema/dubbo"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
           http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
        
        <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
        <d:application name="dubbo_customer"  />
        <!-- 使用zookeeper集群注册中心暴露发现服务地址 -->
        <d:registry address="zookeeper://192.168.1.102:2181?backup=192.168.1.102:2182,192.168.1.102:2183" default="true" />
        
        <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
        <d:reference id="demoService" interface="com.wei.interfaces.DemoService" />
        
    </beans>

      2.5 引入提供者jar包,我这里引用上节打包名为dubbo_provider.jar的包。如果不引入,测试类会提示找不到DemoService。  

      2.6 编写main方法测试类,如下:

    package com.wei.services;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.wei.interfaces.DemoService;
    
    public class CustomerTest {
    
        public static void main(String[] args) {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext-base.xml"});
            context.start();
     
            DemoService demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理
            String hello = demoService.sayHello("world"); // 执行远程方法
     
            System.out.println( hello ); // 显示调用结果
        }
    }

      运行效果如下:  

    log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
    log4j:WARN Please initialize the log4j system properly.
    log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
    SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
    SLF4J: Defaulting to no-operation (NOP) logger implementation
    SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
    hello world

      证实可用。

      2.7 查看dubbo管理控制台,可以看到消费者增加了一个,如下:  

      说明:这两节只是对dubbo一个浅显的基础入门,更深层次的东西还没了解到,以后继续学习。。

  • 相关阅读:
    ASP.NET MVC5+EF6+EasyUI 后台管理系统(20)-权限管理系统-根据权限获取菜单
    ASP.NET MVC5+EF6+EasyUI 后台管理系统(19)-权限管理系统-用户登录
    ASP.NET MVC5+EF6+EasyUI 后台管理系统(18)-权限管理系统-表数据
    ASP.NET MVC5+EF6+EasyUI 后台管理系统(17)-LinQ动态排序
    构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(16)-权限管理系统-漂亮的验证码
    构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(15)-权限管理系统准备
    MVC解决Json DataGrid返回的日期格式是/Date(20130450000365)
    Easyui 让DataGrid适应浏览器宽度
    Easyui 让Window弹出居中与最大化后居中
    构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(14)-EasyUI缺陷修复与扩展
  • 原文地址:https://www.cnblogs.com/bookwed/p/4602181.html
Copyright © 2011-2022 走看看