CollectorStarter.java 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright 2017, OpenSkywalking Organization All rights reserved.
 *
 * 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.
 *
 * Project repository: https://github.com/OpenSkywalking/skywalking
 */

19
package org.skywalking.apm.collector.boot;
P
pengys5 已提交
20 21

import java.util.Map;
P
pengys5 已提交
22
import org.skywalking.apm.collector.cluster.ClusterModuleGroupDefine;
P
pengys5 已提交
23
import org.skywalking.apm.collector.core.CollectorException;
24
import org.skywalking.apm.collector.core.framework.Starter;
P
pengys5 已提交
25 26 27
import org.skywalking.apm.collector.core.module.ModuleConfigLoader;
import org.skywalking.apm.collector.core.module.ModuleDefine;
import org.skywalking.apm.collector.core.module.ModuleDefineLoader;
P
pengys5 已提交
28 29
import org.skywalking.apm.collector.core.module.ModuleGroupDefine;
import org.skywalking.apm.collector.core.module.ModuleGroupDefineLoader;
30 31
import org.skywalking.apm.collector.core.server.ServerException;
import org.skywalking.apm.collector.core.server.ServerHolder;
P
pengys5 已提交
32
import org.skywalking.apm.collector.core.util.CollectionUtils;
P
pengys5 已提交
33 34 35 36 37 38 39 40 41
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author pengys5
 */
public class CollectorStarter implements Starter {

    private final Logger logger = LoggerFactory.getLogger(CollectorStarter.class);
P
pengys5 已提交
42
    private Map<String, ModuleGroupDefine> moduleGroupDefineMap;
P
pengys5 已提交
43

P
pengys5 已提交
44
    @Override public void start() throws CollectorException {
P
pengys5 已提交
45 46 47
        ModuleConfigLoader configLoader = new ModuleConfigLoader();
        Map<String, Map> configuration = configLoader.load();

P
pengys5 已提交
48
        ModuleGroupDefineLoader groupDefineLoader = new ModuleGroupDefineLoader();
P
pengys5 已提交
49
        moduleGroupDefineMap = groupDefineLoader.load();
P
pengys5 已提交
50

51 52 53
        ModuleDefineLoader defineLoader = new ModuleDefineLoader();
        Map<String, Map<String, ModuleDefine>> moduleDefineMap = defineLoader.load();

54
        ServerHolder serverHolder = new ServerHolder();
55
        for (ModuleGroupDefine moduleGroupDefine : moduleGroupDefineMap.values()) {
56 57 58
            if (moduleGroupDefine.groupConfigParser() != null) {
                moduleGroupDefine.groupConfigParser().parse(configuration.get(moduleGroupDefine.name()));
            }
P
pengys5 已提交
59 60
            moduleGroupDefine.moduleInstaller().injectConfiguration(configuration.get(moduleGroupDefine.name()), moduleDefineMap.get(moduleGroupDefine.name()));
            moduleGroupDefine.moduleInstaller().injectServerHolder(serverHolder);
P
pengys5 已提交
61 62 63
            moduleGroupDefine.moduleInstaller().preInstall();
        }

P
pengys5 已提交
64 65
        moduleGroupDefineMap.get(ClusterModuleGroupDefine.GROUP_NAME).moduleInstaller().install();

P
pengys5 已提交
66
        for (ModuleGroupDefine moduleGroupDefine : moduleGroupDefineMap.values()) {
P
pengys5 已提交
67 68 69
            if (!(moduleGroupDefine instanceof ClusterModuleGroupDefine)) {
                moduleGroupDefine.moduleInstaller().install();
            }
P
pengys5 已提交
70
        }
71 72 73 74 75 76 77 78

        serverHolder.getServers().forEach(server -> {
            try {
                server.start();
            } catch (ServerException e) {
                logger.error(e.getMessage(), e);
            }
        });
P
pengys5 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

        dependenceAfterInstall();
    }

    private void dependenceAfterInstall() throws CollectorException {
        for (ModuleGroupDefine moduleGroupDefine : moduleGroupDefineMap.values()) {
            moduleInstall(moduleGroupDefine);
        }
    }

    private void moduleInstall(ModuleGroupDefine moduleGroupDefine) throws CollectorException {
        if (CollectionUtils.isNotEmpty(moduleGroupDefine.moduleInstaller().dependenceModules())) {
            for (String groupName : moduleGroupDefine.moduleInstaller().dependenceModules()) {
                moduleInstall(moduleGroupDefineMap.get(groupName));
            }
            logger.info("after install module group: {}", moduleGroupDefine.name());
            moduleGroupDefine.moduleInstaller().afterInstall();
        } else {
            logger.info("after install module group: {}", moduleGroupDefine.name());
            moduleGroupDefine.moduleInstaller().afterInstall();
        }
P
pengys5 已提交
100 101
    }
}