Dubbo---入門(一)

這裏先給出Dubbo的中文網站http://dubbo.apache.org/zh-cn/index.html,大家可以根據文檔對Dubbo有個初步的認識和了解。

大家可以根據Dubbo網站說明創建一個例子來學習,當然我這裏也一個簡單Demo,下載地址:https://github.com/godwar/dubbo-simple。這個初始化的demo是沒有配置註冊中心的。

當第一個Dubbo成功運行後,大家可能會存在一個問題,Dubbo到底是如何啓動使用的?那我們就來看看代碼

public class App 
{
    public static void main( String[] args ) throws IOException {
     ClassPathXmlApplicationContext classPathXmlApplicationContext =
                new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/application.xml"}) ;
        classPathXmlApplicationContext.start();
        System.in.read();
    }
}

Dubbo 提供了一個 Main.main 快速啓動相應的容器它會啓動Dubbo配置的多個container。默認情況下,只會啓動 spring 容器 

public class App 
{
    public static void main( String[] args ) throws IOException {
       Main.main(args);
    }
}

查看源碼如下

 public static void main(String[] args) {
        try {
            if (ArrayUtils.isEmpty(args)) {
                String config = ConfigUtils.getProperty("dubbo.container", loader.getDefaultExtensionName());
                args = CommonConstants.COMMA_SPLIT_PATTERN.split(config);
            }

            //申明瞭一個存放多個容器的集合
            final List<Container> containers = new ArrayList();

            for(int i = 0; i < args.length; ++i) {
                containers.add(loader.getExtension(args[i]));
            }

            logger.info("Use container type(" + Arrays.toString(args) + ") to run dubbo serivce.");
//對容器循環停止
            if ("true".equals(System.getProperty("dubbo.shutdown.hook"))) {
                Runtime.getRuntime().addShutdownHook(new Thread("dubbo-container-shutdown-hook") {
                    public void run() {
                        Iterator var1 = containers.iterator();

                
                        while(var1.hasNext()) {
                            Container container = (Container)var1.next();

}
 ....

//對多個容器進行循環啓動
    while(var12.hasNext()) {
                Container container = (Container)var12.next();
                container.start();
                logger.info("Dubbo " + container.getClass().getSimpleName() + " started!");
            }
....

通過跟着可以發現有三種容器

Spring Container
自動加載 META-INF/spring 目錄下的所有 Spring 配置。
logback Container
自動裝配 logback 日誌
Log4j Container
自動配置 log4j 的配置

 默認情況下,spring 容器,本質上,就是加在 spring ioc 容器,然後啓動一個 netty 服務實現服務的發佈。

 

public class SpringContainer implements Container {
    public static final String SPRING_CONFIG = "dubbo.spring.config";
    public static final String DEFAULT_SPRING_CONFIG = "classpath*:META-INF/spring/*.xml";
    private static final Logger logger = LoggerFactory.getLogger(SpringContainer.class);
    static ClassPathXmlApplicationContext context;

    public SpringContainer() {
    }

    public static ClassPathXmlApplicationContext getContext() {
        return context;
    }
    
         //dubbo啓動
    public void start() {
           //根據dubbo.spring.config獲取配置路徑
        String configPath = ConfigUtils.getProperty("dubbo.spring.config");
            
     //配置爲空則獲取spring下的xml文件
        if (StringUtils.isEmpty(configPath)) {
    
            configPath = "classpath*:META-INF/spring/*.xml";
        }

        context = new ClassPathXmlApplicationContext(configPath.split("[,\\s]+"), false);
        context.refresh();
        context.start();
    }

//dubbo停止服務
    public void stop() {
        try {
            if (context != null) {
                context.stop();
                context.close();
                context = null;
            }
        } catch (Throwable var2) {
            logger.error(var2.getMessage(), var2);
        }

    }
}

 

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