dubbo配置及使用

一、浩言

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


二、背景

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


三、dubbo配置

3.1、dubbo的配置

maven的jar如下:

   
           com.alibaba
           dubbo
           ${dubbo.version}
           
               
                   org.springframework
                   spring
               
           
       
       
           org.apache.zookeeper
           zookeeper
           ${zookeeper.version}
       
       
           com.github.sgroschupf
           zkclient
           ${zkclient.version}
       

根据dubbo官网用户教程中的配置可以自己搭建一个dubbo项目,当然,dubbo在github上的源码里面也有demo的,那么现在看看我自己搭建的项目

dubbo-provider.xml



    
    
   
    
    
    
 
    
    
      
 
    
    
 
    
    
 
    
    
    

使用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文件如下:



        
    
 
    
     
    
    
    

消费端代码如下

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"));
    }
}

发布接口的日志:


dubbo配置及使用_第1张图片
Paste_Image.png

暴露接口列表


dubbo配置及使用_第2张图片
Paste_Image.png

暴露接口的详情:


dubbo配置及使用_第3张图片
Paste_Image.png

消费端在监控平台显示:


dubbo配置及使用_第4张图片
Paste_Image.png

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


dubbo配置及使用_第5张图片
Paste_Image.png

集群provider.xml



    
    
 
    
    
    
 
    
    
 
    
    
 
    
    
    

上面的集群地址是在我的这篇zookeeper集群配置文章中配置的。
上面给的是集群配置,consumer.xml如下



        
      
       
       
          
       

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

dubbo配置及使用_第6张图片
Paste_Image.png

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


dubbo配置及使用_第7张图片
Paste_Image.png

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

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

dubbo配置及使用_第8张图片
dubbo-admin.png

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


dubbo配置及使用_第9张图片
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项目的配置就是如此的。

dubbo配置及使用_第10张图片
Paste_Image.png

五:浩语

                                 __                                                     
                  __  _  ____ __|  |__ _____    ___
                  \ \/ \/ /  |  \  |  \\__  \  /  _ \   
                   \     /|  |  /   Y  \/ __ \(  <_> )
                    \/\_/ |____/|___|  (____  /\____/ 
                                          \/     \/          
                       任何事情都是要靠努力和用心。                                                   

你可能感兴趣的:(dubbo配置及使用)