目前来说 分布式系统已经十分普遍,国内常用的架构通常为两种,dubbo与springCloud。虽然两种小弟都在本地耍过,可没有实际经验,基本还停留在纸上谈兵的阶段。
如今公司使用了dubbo,特意做些笔录:
以下是我的个人理解:
dubbo项目,在项目结构上,与传统的垂直分层项目基本一致。非要直观的说不同之处,那就是在最外层有一个pom.xml文件,如图
目前,dubbo项目又开始维护,并转移到了阿帕奇官网。只要项目结构不是特别奇葩,小弟用idea直接加载该pom文件 就可以导入所有的分支项目(因为该pom是父项目的)。
另:idea 已经集成了dubbo插件,下载安装后,可直接创建dubbo项目,与springCloud相比也不麻烦了。
因,公司再git仓库中没有properties文件,小弟我找了一圈的人去copy该文件,然后导入项目中,真是欲仙欲死。还好公司项目中规中矩。导入后没费什么劲就起来了。
启动没有使用tomcat 也没使用spring加载,而是dubbo官方推荐的main方法:
进入源码可发现:
/*
* Copyright 1999-2011 Alibaba Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.container;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.extension.ExtensionLoader;
import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.alibaba.dubbo.common.utils.ConfigUtils;
/**
* Main. (API, Static, ThreadSafe)
*
* @author william.liangf
*/
public class Main {
public static final String CONTAINER_KEY = "dubbo.container";
public static final String SHUTDOWN_HOOK_KEY = "dubbo.shutdown.hook";
private static final Logger logger = LoggerFactory.getLogger(Main.class);
private static final ExtensionLoader<Container> loader = ExtensionLoader.getExtensionLoader(Container.class);
private static volatile boolean running = true;
public static void main(String[] args) {
try {
if (args == null || args.length == 0) {
String config = ConfigUtils.getProperty(CONTAINER_KEY, loader.getDefaultExtensionName());
args = Constants.COMMA_SPLIT_PATTERN.split(config);
}
final List<Container> containers = new ArrayList<Container>();
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(SHUTDOWN_HOOK_KEY))) {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
for (Container container : containers) {
try {
container.stop();
logger.info("Dubbo " + container.getClass().getSimpleName() + " stopped!");
} catch (Throwable t) {
logger.error(t.getMessage(), t);
}
synchronized (Main.class) {
running = false;
Main.class.notify();
}
}
}
});
}
for (Container container : containers) {
container.start();
logger.info("Dubbo " + container.getClass().getSimpleName() + " started!");
}
System.out.println(new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss]").format(new Date()) + " Dubbo service server started!");
} catch (RuntimeException e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
System.exit(1);
}
synchronized (Main.class) {
while (running) {
try {
Main.class.wait();
} catch (Throwable e) {
}
}
}
}
}
以上,加载spring时,也就会加载dubbo配置的xml文件,从而使用指定的dubbo命名空间加载dubbo对象。