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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsdhttp://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
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的dubbo学习之服务消费者的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP开发
- 下一篇: “吃神么,买神么”的第三个Sprint冲