Dubbo启动方式

服务容器是一个standalone的启动程序,因为后台服务不需要Tomcat或JBoss等Web容器的功能,如果硬要用Web容器去加载服务提供方,增加复杂性,也浪费资源。 
服务容器只是一个简单的Main方法,并加载一个简单的Spring容器,用于暴露服务。 
服务容器的加载内容可以扩展,内置了spring, jetty, log4j等加载,可通过Container扩展点进行扩展,参见:Container 
Spring Container 

    自动加载META-INF/spring目录下的所有Spring配置。 
    配置:(配在java命令-D参数或者dubbo.properties中) 
        dubbo.spring.config=classpath*:META-INF/spring/*.xml ----配置spring配置加载位置 

Jetty Container 

    启动一个内嵌Jetty,用于汇报状态。 
    配置:(配在java命令-D参数或者dubbo.properties中) 
        dubbo.jetty.port=8080 ----配置jetty启动端口 
        dubbo.jetty.directory=/foo/bar ----配置可通过jetty直接访问的目录,用于存放静态文件 
        dubbo.jetty.page=log,status,system ----配置显示的页面,缺省加载所有页面 

Log4j Container 

    自动配置log4j的配置,在多进程启动时,自动给日志文件按进程分目录。 
    配置:(配在java命令-D参数或者dubbo.properties中) 
        dubbo.log4j.file=/foo/bar.log ----配置日志文件路径 
        dubbo.log4j.level=WARN ----配置日志级别 
        dubbo.log4j.subdirectory=20880 ----配置日志子目录,用于多进程启动,避免冲突 

容器启动 

如:(缺省只加载spring) 
java com.alibaba.dubbo.container.Main 

或:(通过main函数参数传入要加载的容器) 
java com.alibaba.dubbo.container.Main spring jetty log4j 

或:(通过JVM启动参数传入要加载的容器) 
java com.alibaba.dubbo.container.Main -Ddubbo.container=spring,jetty,log4j 

或:(通过classpath下的dubbo.properties配置传入要加载的容器) 
dubbo.properties 
dubbo.container=spring,jetty,log4j 



public class SpringContainer implements Container { 

    private static final Logger logger = LoggerFactory.getLogger(SpringContainer.class); 

    public static final String SPRING_CONFIG = "dubbo.spring.config"; 
    
    public static final String DEFAULT_SPRING_CONFIG = "classpath*:spring/*.xml"; 

    static ClassPathXmlApplicationContext context; 
    
    public static ClassPathXmlApplicationContext getContext() { 
return context; 


public void start() { 
        String configPath = ConfigUtils.getProperty(SPRING_CONFIG); 
        if (configPath == null || configPath.length() == 0) { 
            configPath = DEFAULT_SPRING_CONFIG; 
        } 
        context = new ClassPathXmlApplicationContext(configPath.split("[,\\s]+")); 
        context.start(); 
    } 

    public void stop() { 
        try { 
            if (context != null) { 
                context.stop(); 
                context.close(); 
                context = null; 
            } 
        } catch (Throwable e) { 
            logger.error(e.getMessage(), e); 
        } 
    } 



测试: 
package cn.gooday.service.order.provider; 

import cn.gooday.framework.HelperLoader; 

/** 
* 主线程启动 
*/ 
public class TestMain { 

    public static void main(String[] args) throws Exception { 
        com.alibaba.dubbo.container.Main.main(args); 
    } 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章