dubbo配置及使用

一、浩言

曾国藩说的"既往不恋,当下不杂,未来不迎"


二、背景

dubbo的一直说要来记载下,写了又一直没有完整的整理下,今天加班把这篇整理了下,公司最近我们改的项目都使用了dubbo,但是分布式事物这一块还是没有处理,就类型一个http调用失败那么如何处理样,这事需要思考的,这篇算是入门篇了,后续会在记载更多知识,后面的dubbo启动方式很多人的选择应该不一样的吧,我感觉dubbo自带的用命令启动就很不错,现在类似的springboot模块,打包成jar的模式,直接运行jar启动也是可以的。看个人需要了。


三、dubbo配置

3.1、dubbo的配置

maven的jar如下:

   <dependency>
           <groupId>com.alibaba</groupId>
           <artifactId>dubbo</artifactId>
           <version>${dubbo.version}</version>
           <exclusions>
               <exclusion>
                   <groupId>org.springframework</groupId>
                   <artifactId>spring</artifactId>
               </exclusion>
           </exclusions>
       </dependency>
       <dependency>
           <groupId>org.apache.zookeeper</groupId>
           <artifactId>zookeeper</artifactId>
           <version>${zookeeper.version}</version>
       </dependency>
       <dependency>
           <groupId>com.github.sgroschupf</groupId>
           <artifactId>zkclient</artifactId>
           <version>${zkclient.version}</version>
       </dependency>

根据dubbo官网<a href="http://dubbo.io/User+Guide-zh.htm">用户教程</a>中的配置可以自己搭建一个dubbo项目,当然,dubbo在<a href=" https://github.com/alibaba/dubbo.git ">github</a>上的源码里面也有demo的,那么现在看看我自己搭建的项目

dubbo-provider.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"
    xmlns:context="http://www.springframework.org/schema/context"
    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
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!-- 读入配置属性文件 -->
  <context:property-placeholder location="classpath*:service.properties" /> 
    
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="mouse-moon-app-web"/>
 
    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
      <!-- <dubbo:registry address="zookeeper://127.0.0.1:2181" /> -->
 
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
 
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.mouse.moon.api.UserService" ref="userService" />
 
    <!-- 和本地bean一样实现服务 -->
    <bean id="userService" class="com.mouse.moon.service.UserServiceImpl" />
    
</beans>

使用main方法进行发布暴露代码

package com.mouse.moon.service;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AppMain {
    public static void main(String[] args) throws Exception{
         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"/provider-test/spring-mybatis.xml"});
         context.start();
          System.in.read();
    }
}

配置消费端dubbo-consumer.xml的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-mouse-monn"  />
 
    <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    <dubbo:registry  protocol="zookeeper"  address="127.0.0.1:2181" /> 
    
    <!--用户查询  -->
    <dubbo:reference id="userService" interface="com.mouse.moon.api.UserService"/>
</beans>

消费端代码如下

package org.mouse.consumer;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mouse.moon.api.UserService;
public class Client {
    public static void main(String args[]){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"/consumer-test/dubbo-consumer.xml"});
        context.start();
        UserService userService = (UserService)context.getBean("userService");
        System.out.println(userService.getUserInfoById("1"));
    }
}

发布接口的日志:


Paste_Image.png

暴露接口列表


Paste_Image.png

暴露接口的详情:


Paste_Image.png

消费端在监控平台显示:


Paste_Image.png

调用端查询数据的相关日志:


Paste_Image.png

集群provider.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="mouse-moon-app-web"/>
 
    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://121.12.60.22:2183?backup=121.12.60.22:2181,121.12.60.22:2182" />
    <!--  <dubbo:registry address="zookeeper://121.12.60.22:2181" /> -->
 
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20881" />
 
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.mouse.moon.api.UserService" ref="userService"  />
 
    <!-- 和本地bean一样实现服务 -->
    <bean id="userService" class="com.mouse.moon.service.UserServiceImpl" />
    
</beans>

上面的集群地址是在我的这篇<a href="http://www.jianshu.com/p/950fb55ea53a">zookeeper集群配置</a>文章中配置的。
上面给的是集群配置,consumer.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-of-helloworld-app" />
       <!-- 使用multicast广播注册中心暴露发现服务地址 -->
       <dubbo:registry protocol="zookeeper" address="121.12.60.22:2183,121.12.60.22::2181,121.12.60.22::2182" />   
       </bean>

其实这里面主要就是这些xml的配置,其他代码就是分了模块的,比如我这项目结构如下

Paste_Image.png

新建module时候可以如此,在父项目上右键(我的IDE是eclipse),然后根据自己的需要新建不同的module


Paste_Image.png

上面的监控的说明见下面的说明

3.2 dubbo监控
将dubbo源码下的dubbo-admin项目编译后的war包放到tomcat下,访问项目提示需要输入账号密码
用户名:root
密码:root

dubbo-admin.png

监控中的账号面属性在源码里面可以看到为止如下图位置


Paste_Image.png
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest

四、有关启动的方式

4.1、启动方式
上面看到了,我其实是使用main方法来启动暴露dubbo,这事一种方式。dubbo源码中自带了可以转成命令的形式操作,也是用main方式,只是加载固定位置的配置文件,以shell脚本的形式启动。

package com.mouse.moon.service;
public class AppMain {
    public static void main(String[] args) throws Exception{
          com.alibaba.dubbo.container.Main.main(args);
    }
}

以上述方式启动代码,下面是打印的日志

----------->[Ljava.lang.String;@15db9742
log4j:WARN No appenders could be found for logger (com.alibaba.dubbo.common.logger.LoggerFactory).
log4j:WARN Please initialize the log4j system properly.
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/E:/tool/repository/ch/qos/logback/logback-classic/1.1.3/logback-classic-1.1.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/E:/tool/repository/org/slf4j/slf4j-log4j12/1.6.1/slf4j-log4j12-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
.14:59:41.307 DEBUG [main] org.springframework.core.env.StandardEnvironment[109] - Adding [systemProperties] PropertySource with lowest search precedence
.14:59:41.311 DEBUG [main] org.springframework.core.env.StandardEnvironment[109] - Adding [systemEnvironment] PropertySource with lowest search precedence
.14:59:41.311 DEBUG [main] org.springframework.core.env.StandardEnvironment[127] - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
.14:59:41.312 INFO [main] org.springframework.context.support.ClassPathXmlApplicationContext[581] - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@14bf9759: startup date [Tue Feb 14 14:59:41 CST 2017]; root of context hierarchy
.14:59:41.342 DEBUG [main] org.springframework.core.env.StandardEnvironment[109] - Adding [systemProperties] PropertySource with lowest search precedence
.14:59:41.343 DEBUG [main] org.springframework.core.env.StandardEnvironment[109] - Adding [systemEnvironment] PropertySource with lowest search precedence
.14:59:41.343 DEBUG [main] org.springframework.core.env.StandardEnvironment[127] - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
.14:59:41.351 DEBUG [main] org.springframework.core.io.support.PathMatchingResourcePatternResolver[317] - Resolved classpath location [META-INF/spring/] to resources []
.14:59:41.352 DEBUG [main] org.springframework.core.io.support.PathMatchingResourcePatternResolver[483] - Resolved location pattern [classpath*:META-INF/spring/*.xml] to resources []
.14:59:41.352 DEBUG [main] org.springframework.beans.factory.xml.XmlBeanDefinitionReader[224] - Loaded 0 bean definitions from location pattern [classpath*:META-INF/spring/*.xml]
.14:59:41.354 DEBUG [main] org.springframework.context.support.ClassPathXmlApplicationContext[615] - Bean factory for org.springframework.context.support.ClassPathXmlApplicationContext@14bf9759: org.springframework.beans.factory.support.DefaultListableBeanFactory@6fb0d3ed: defining beans []; root of factory hierarchy
.14:59:41.384 DEBUG [main] org.springframework.context.support.ClassPathXmlApplicationContext[728] - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@6cc7b4de]
.14:59:41.386 DEBUG [main] org.springframework.context.support.ClassPathXmlApplicationContext[752] - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@2bbaf4f0]
.14:59:41.387 DEBUG [main] org.springframework.beans.factory.support.DefaultListableBeanFactory[744] - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@6fb0d3ed: defining beans []; root of factory hierarchy
.14:59:41.388 DEBUG [main] org.springframework.context.support.ClassPathXmlApplicationContext[779] - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@282003e1]
.14:59:41.388 DEBUG [main] org.springframework.beans.factory.support.DefaultListableBeanFactory[251] - Returning cached instance of singleton bean 'lifecycleProcessor'
.14:59:41.390 DEBUG [main] org.springframework.core.env.PropertySourcesPropertyResolver[91] - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source
.14:59:41.396 DEBUG [main] org.springframework.beans.factory.support.DefaultListableBeanFactory[251] - Returning cached instance of singleton bean 'lifecycleProcessor'
[2017-02-14 14:59:41] Dubbo service server started!

所以,如果使用这种方式启动发布,那么我们需要将dubbo的配置文件配置到[classpath:META-INF/spring/.xml]**下,我们现在的dubbo项目的配置就是如此的。

Paste_Image.png

五:浩语

                                 __                                                     
                  __  _  ____ __|  |__ _____    ___
                  \ \/ \/ /  |  \  |  \\__  \  /  _ \   
                   \     /|  |  /   Y  \/ __ \(  <_> )
                    \/\_/ |____/|___|  (____  /\____/ 
                                          \/     \/          
                       任何事情都是要靠努力和用心。                                                   
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,036评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,046评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,411评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,622评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,661评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,521评论 1 304
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,288评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,200评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,644评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,837评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,953评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,673评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,281评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,889评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,011评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,119评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,901评论 2 355

推荐阅读更多精彩内容

  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,811评论 6 342
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,656评论 18 139
  • 今天技术合伙人被坑的文章很火,冯大辉前辈写了一段文字,我觉得挺好,就转出来了。 By @Fenng 给创业公司的技...
    alsmn阅读 563评论 0 0
  • 文/曾曾是一匹野马 此地的雨确有些频繁 被打湿的人,揣着两兜心事 又有些失眠。像这雨总不消停 踢踏和猫叫交替控制着...
    曾曾是一匹野马阅读 550评论 8 10
  • 孤不知道好的朋友的定义是什么,但总归有一条是必须有的吧,那就是起码你心中还有孤,你还在乎孤的喜怒哀乐吧!孤曾经不计...
    花蹊阅读 244评论 1 1