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一个浅显的基础入门,更深层次的东西还没了解到,以后继续学习。。

转载于:https://www.cnblogs.com/bookwed/p/4602181.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值