Apollo的Portal界面操作

上一讲主要讲了Apollo配置中心如何通过client与之交互,演示了java如何接入的方式,接下来介绍的是Apollo界面的一些常规操作。

创建部门

方式一:在ApolloPortalDB的ServerConfig中直接修改key=organizations的value值。
在这里插入图片描述
方式二:在Portal界面上选择管理员工具-系统参数
在这里插入图片描述

创建用户

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

修改项目信息

部门选择“技术研发部”,项目负责人选择“jeff”
在这里插入图片描述
在这里插入图片描述
回到首页进入JeffApp项目就可以看到,信息已经更新了
在这里插入图片描述

创建public命名空间

添加public类型的Namespace
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
添加配置项
在这里插入图片描述
在这里插入图片描述
发布一下即可。
在这里插入图片描述

公共Namespace演示

代码在ApolloConfigDemo

package com.jeff.apollo.demo;

import com.ctrip.framework.apollo.*;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.internals.YamlConfigFile;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.model.ConfigFileChangeEvent;
import com.ctrip.framework.foundation.Foundation;
import com.google.common.base.Charsets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author Jason Song(song_s@ctrip.com)
 */
public class ApolloConfigDemo {
  private static final Logger logger = LoggerFactory.getLogger(ApolloConfigDemo.class);
  private String DEFAULT_VALUE = "undefined";
  private Config config;
  private Config yamlConfig;
  private Config publicConfig;
  private ConfigFile publicConfigFile;
  private ConfigFile applicationConfigFile;
  private ConfigFile xmlConfigFile;
  private YamlConfigFile yamlConfigFile;

  public ApolloConfigDemo() {
    ConfigChangeListener changeListener = new ConfigChangeListener() {
      @Override
      public void onChange(ConfigChangeEvent changeEvent) {
        logger.info("Changes for namespace {}", changeEvent.getNamespace());
        for (String key : changeEvent.changedKeys()) {
          ConfigChange change = changeEvent.getChange(key);
          logger.info("Change - key: {}, oldValue: {}, newValue: {}, changeType: {}",
              change.getPropertyName(), change.getOldValue(), change.getNewValue(),
              change.getChangeType());
        }
      }
    };
    config = ConfigService.getAppConfig();
    config.addChangeListener(changeListener);
    yamlConfig = ConfigService.getConfig("application.yaml");
    yamlConfig.addChangeListener(changeListener);
    publicConfig = ConfigService.getConfig("TECH.public");
    publicConfig.addChangeListener(changeListener);
    applicationConfigFile = ConfigService.getConfigFile("application", ConfigFileFormat.Properties);
    publicConfigFile = ConfigService.getConfigFile("TECH.public", ConfigFileFormat.Properties);
    // datasources.xml
    xmlConfigFile = ConfigService.getConfigFile("datasources", ConfigFileFormat.XML);
    xmlConfigFile.addChangeListener(new ConfigFileChangeListener() {
      @Override
      public void onChange(ConfigFileChangeEvent changeEvent) {
        logger.info(changeEvent.toString());
      }
    });
    // application.yaml
    yamlConfigFile = (YamlConfigFile) ConfigService.getConfigFile("application", ConfigFileFormat.YAML);
  }

  private String getConfig(String key) {
    String result = config.getProperty(key, DEFAULT_VALUE);
    if (DEFAULT_VALUE.equals(result)) {
      result = publicConfig.getProperty(key, DEFAULT_VALUE);
    }
    if (DEFAULT_VALUE.equals(result)) {
      result = yamlConfig.getProperty(key, DEFAULT_VALUE);
    }
    logger.info(String.format("Loading key : %s with value: %s", key, result));
    return result;
  }

  private void print(String namespace) {
    switch (namespace) {
      case "TECH.public":
        print(publicConfigFile);
        return;
      case "application":
        print(applicationConfigFile);
        return;
      case "xml":
        print(xmlConfigFile);
        return;
      case "yaml":
        printYaml(yamlConfigFile);
        return;
    }
  }

  private void print(ConfigFile configFile) {
    if (!configFile.hasContent()) {
      System.out.println("No config file content found for " + configFile.getNamespace());
      return;
    }
    System.out.println("=== Config File Content for " + configFile.getNamespace() + " is as follows: ");
    System.out.println(configFile.getContent());
  }

  private void printYaml(YamlConfigFile configFile) {
    System.out.println("=== Properties for " + configFile.getNamespace() + " is as follows: ");
    System.out.println(configFile.getContent());
  }

  private void printEnvInfo() {
    String message = String.format("AppId: %s, Env: %s, DC: %s, IP: %s", Foundation.app()
        .getAppId(), Foundation.server().getEnvType(), Foundation.server().getDataCenter(),
        Foundation.net().getHostAddress());
    System.out.println(message);
  }

  public static void main(String[] args) throws IOException {
    ApolloConfigDemo apolloConfigDemo = new ApolloConfigDemo();
    apolloConfigDemo.printEnvInfo();
    System.out.println(
        "Apollo Config Demo. Please input key to get the value.");
    while (true) {
      System.out.print("> ");
      String input = new BufferedReader(new InputStreamReader(System.in, Charsets.UTF_8)).readLine();
      if (input == null || input.length() == 0) {
        continue;
      }
      input = input.trim();
      try {
        if (input.equalsIgnoreCase("TECH.public")) {
          apolloConfigDemo.print("TECH.public");
          continue;
        }
        if (input.equalsIgnoreCase("application")) {
          apolloConfigDemo.print("application");
          continue;
        }
        if (input.equalsIgnoreCase("xml")) {
          apolloConfigDemo.print("xml");
          continue;
        }
        if (input.equalsIgnoreCase("yaml") || input.equalsIgnoreCase("yml")) {
          apolloConfigDemo.print("yaml");
          continue;
        }
        if (input.equalsIgnoreCase("quit")) {
          System.exit(0);
        }
        apolloConfigDemo.getConfig(input);
      } catch (Throwable ex) {
        logger.error("some error occurred", ex);
      }
    }
  }
}

启动后在控制台输入TECH.public,可以打印出公共命名空间中所有的配置项,这里只有一个是poolsize,然后输入poolsize,取值逻辑如下

  private String getConfig(String key) {
    String result = config.getProperty(key, DEFAULT_VALUE);
    if (DEFAULT_VALUE.equals(result)) {
      result = publicConfig.getProperty(key, DEFAULT_VALUE);
    }
    if (DEFAULT_VALUE.equals(result)) {
      result = yamlConfig.getProperty(key, DEFAULT_VALUE);
    }
    logger.info(String.format("Loading key : %s with value: %s", key, result));
    return result;
  }

解释:先从默认的namespace中获取如果没有占到则从公共命名空间中找。

> TECH.public
=== Config File Content for TECH.public.properties is as follows: 
poolsize=60

> poolsize
10:32:40.533 [main] INFO com.jeff.apollo.demo.ApolloConfigDemo - Loading key : poolsize with value: 60
> 

如果此时我们想要在application这个私有namespace中自己定义poolsize:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

那么现在输入poolsize时会打印多少呢?可以看到打印了30.自定义的poolsize覆盖了公共namespace。

poolsize
10:41:42.829 [main] INFO com.jeff.apollo.demo.ApolloConfigDemo - Loading key : poolsize with value: 30

XML私有空间演示

我们再再appId为JeffApp的项目下创建一个xml格式的私有命名空间并提交发布。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
继续输入xml

> xml
=== Config File Content for datasources.xml is as follows: 
<a>I am datasource</a>

> application
=== Config File Content for application.properties is as follows: 
batch=78
timeout=90
> 

本地缓存

其实我们创建的xml私有空间、public公共空间及私有默认空间都会在本地缓存文件。
可以看到在appId=JeffApp,默认集群default下三个命名空间:application、xml、public
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jeff.sheng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值