kylin源码走读_kylinConfig

本文详细介绍了Kylin中配置文件的加载过程,包括如何通过getInstanceFromEnv方法获取配置实例,以及如何通过bulidSiteProperties方法从多个配置源加载配置。

kylin配置文件获取

public class KylinConfig extends KylinConfigBase  KylinConfig继承自KylinConfigBase

 public static KylinConfig getInstanceFromEnv() {
        //同步锁
        synchronized (KylinConfig.class) {
            //获取本地实例的config
            KylinConfig config = THREAD_ENV_INSTANCE.get();
//判断实例中的config是否存在,如果存在则直接返回config
            if (config != null) {
                return config;
            }
            //如果不存在则读本地配置文件
            if (SYS_ENV_INSTANCE == null) {
                try {
                    config = new KylinConfig();
                    config.reloadKylinConfig(buildSiteProperties());

                    logger.info("Initialized a new KylinConfig from getInstanceFromEnv : "
                            + System.identityHashCode(config));
                    SYS_ENV_INSTANCE = config;
                } catch (IllegalArgumentException e) {
                    throw new IllegalStateException("Failed to find KylinConfig ", e);
                }
            }
            return SYS_ENV_INSTANCE;
        }
    }

KylinConfig config =  KylinConfig.getInstanceFromEnv();


getInstanceFromEnv方法的具体实现如下:首先获取从本地实例的THREAD_ENV_INSTANCE中获取kylinConfig,如果不为null则直接返回实例中config,如果为null,则读取本地配置问题,然后调用kylinConfig自身的bulidSiteProperties()方法返回一个Properties 最后通过config.reloadKylinConfig()方法把配置项加载到config中。赋值给SYS_ENV_INSTANCE 返回。

bulidSiteProperties()方法实现如下:

 private static Properties buildSiteProperties() {
        Properties conf = new Properties();

        OrderedProperties orderedProperties = buildSiteOrderedProps();
        for (Map.Entry<String, String> each : orderedProperties.entrySet()) {
            conf.put(each.getKey(), each.getValue());
        }

        return conf;
    }


bulidSiteOrderedProps()方法实现如下:

    // build kylin properties from site deployment, a.k.a KYLIN_HOME/conf/kylin.properties
    private static OrderedProperties buildSiteOrderedProps() {

        try {
            // 1. load default configurations from classpath. 
            // we have a kylin-defaults.properties in kylin/core-common/src/main/resources 
            URL resource = Thread.currentThread().getContextClassLoader().getResource("kylin-defaults.properties");
            Preconditions.checkNotNull(resource);
            logger.info("Loading kylin-defaults.properties from {}", resource.getPath());
            OrderedProperties orderedProperties = new OrderedProperties();
            loadPropertiesFromInputStream(resource.openStream(), orderedProperties);

            for (int i = 0; i < 10; i++) {
                String fileName = "kylin-defaults" + (i) + ".properties";
                URL additionalResource = Thread.currentThread().getContextClassLoader().getResource(fileName);
                if (additionalResource != null) {
                    logger.info("Loading {} from {} ", fileName, additionalResource.getPath());
                    loadPropertiesFromInputStream(additionalResource.openStream(), orderedProperties);
                }
            }

            // 2. load site conf, to keep backward compatibility it's still named kylin.properties
            // actually it's better to be named kylin-site.properties
            File propFile = getSitePropertiesFile();
            if (propFile == null || !propFile.exists()) {
                logger.error("fail to locate " + KYLIN_CONF_PROPERTIES_FILE);
                throw new RuntimeException("fail to locate " + KYLIN_CONF_PROPERTIES_FILE);
            }
            loadPropertiesFromInputStream(new FileInputStream(propFile), orderedProperties);

            // 3. still support kylin.properties.override as secondary override
            // not suggest to use it anymore
            File propOverrideFile = new File(propFile.getParentFile(), propFile.getName() + ".override");
            if (propOverrideFile.exists()) {
                loadPropertiesFromInputStream(new FileInputStream(propOverrideFile), orderedProperties);
            }
            return orderedProperties;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


bulidSiteProperties() 方法读取 KYLIN_HOME/conf/kylin.properties 文件,具体逻辑:


1.首先从classpath中读取默认kylin-defaults.properties文件,文件位置:kylin/core-common/src/main/resources
生成URL路径,Preconditions.checkNotNull(resource)判断是否为NULL,如果为null,则抛出空指针异常,不为null则使用url.openStream()返回一个InputSteam对象。调用kylinConfig.loadPropertiesFromInputStream()方法,把配置文件中的配置项通过kv形式赋值给orderedProperties对象,接着循环10次判断是否有额外新增的配置文件命名格式以kylin-defaults" + (i) + ".properties,如果有,则重复上述步骤调用kylinConfig.loadPropertiesFromInputStream()方法获取配置项

2.为了保持向后兼容性,调用kylin.getSitePropertiesFile()  返回一个文件对象,然后判断是否为null,接着同上调用kylin.getSitePropertiesFile()  返回一个文件对象重复上述步骤调用kylinConfig.loadPropertiesFromInputStream()方法获取配置项

3.还支持kylin.properties 的重写这里不再鳌述。


kylinConfig.reloadKylinConfig() 方法主要是通过调用
BackwardCompatibilityConfig.check()方法把key,value强制转化为String类型,类似于格式化

### 配置 kylin_kms_daemon 在启动时运行 为了确保 `kylin_kms_daemon` 服务在系统启动时自动运行,可以使用 systemd 系统的服务管理工具进行配置。以下是实现此目标的详细方法: 1. **创建或修改 systemd 服务文件** 首先需要为 `kylin_kms_daemon` 创建一个 systemd 服务文件。如果该服务文件已存在,则可以直接编辑它。服务文件通常位于 `/etc/systemd/system/` 或 `/lib/systemd/system/` 目录下。 ```bash sudo nano /etc/systemd/system/kylin_kms_daemon.service ``` 在文件中添加以下内容以定义服务行为: ```ini [Unit] Description=Kylin KMS Daemon Service After=network.target [Service] ExecStart=/path/to/kylin_kms_daemon start ExecStop=/path/to/kylin_kms_daemon stop Restart=on-failure User=root [Install] WantedBy=multi-user.target ``` 上述配置中: - `Description` 定义了服务的描述信息。 - `After=network.target` 表示该服务将在网络服务启动后运行。 - `ExecStart` 和 `ExecStop` 分别指定了服务启动和停止的命令路径[^3]。 - `Restart=on-failure` 表示服务在失败时会自动重启。 - `User=root` 指定服务以 root 用户权限运行。 2. **重新加载 systemd 配置** 修改或创建服务文件后,需要重新加载 systemd 配置以使更改生效。 ```bash sudo systemctl daemon-reload ``` 3. **启用服务以在启动时运行** 使用以下命令启用服务,使其在系统启动时自动运行。 ```bash sudo systemctl enable kylin_kms_daemon ``` 4. **测试服务配置** 启动服务并检查其状态,以验证配置是否正确。 ```bash sudo systemctl start kylin_kms_daemon sudo systemctl status kylin_kms_daemon ``` 如果一切正常,服务将在系统启动时自动运行。 5. **验证服务是否在启动时运行** 可以通过模拟系统重启来验证服务是否成功启动。 ```bash sudo reboot ``` 系统重启后,检查服务状态以确认其是否已启动。 ```bash sudo systemctl status kylin_kms_daemon ``` ### 注意事项 - 确保 `kylin_kms_daemon` 的可执行文件路径正确,并具有适当的权限[^4]。 - 如果服务依赖其他组件(如数据库或网络服务),需在 `[Unit]` 部分添加相应的 `After` 参数以确保正确的启动顺序。 ```bash sudo chmod +x /path/to/kylin_kms_daemon ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值