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一個淺顯的基礎入門,更深層次的東西還沒了解到,以后繼續學習。。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM