您好,登录后才能下订单哦!
密码登录
            
            
            
            
        登录注册
            
            
            
        点击 登录注册 即表示同意《亿速云用户服务条款》
        使用场景
是一个分布式的配置中心。适用于微服务;
核心功能
开发技术
概念
| 缩写 | 全称 | 说明 | 
|---|---|---|
| FAT | 功能测试环境 | |
| UAT | 集成测试环境 | |
| PRO | 生产环境 | |
| DEV | 开发环境 | 
详细功能
后台界面操作
客户端接入文档
公共组件的操作
公共组件: 提供给应用使用的其它组织的客户端代码,比如cat的客户端;本质上也是应用的一部分;
区别 : 通常情况下,公共组件的使用的配置由原始开发团队维护,但是实际的应用在运行时,环境不一样,所以我们也允许应用在实际使用的时候能够覆盖公共组件的部分配置;
需要自己创建自己唯一的namespace ;
公共组件的操作
集群独立配置
灰度发布 
操作 
要求:jdk1.7+ , guava15.0+
客户端配置参数
- app.id (systemProperty > System Environment > springboot application.properties > META-INF/app.properties) 对应项目的id
 - apollo.meta 访问地址 (SystemProperty > SpringBoot > SystemEnvironment>/opt/settings/server.properties > app.properties)
 - 本地缓存路径 /opt/data/{appId}/config-cache/ key: apollo.cacheDir
 - 环境配置 key : env
 - 集群配置 key: apollo.cluster
 
客户端依赖
 <dependency>
        <groupId>com.ctrip.framework.apollo</groupId>
        <artifactId>apollo-client</artifactId>
        <version>1.1.0</version>
 </dependency>
接入方式对比
| 接入方式 | 特点 | 
|---|---|
| api | 灵活,功能完备 | 
| spring | placeholder方式 | 
| springboot | @ConfigurationProperties | 
获取application namespace配置
Config config = ConfigService.getAppConfig(); 
//config instance is singleton for each namespace and is never null
String value = config.getProperty(someKey, someDefaultValue);
获取公共namespace
String somePublicNamespace = "CAT";
Config config = ConfigService.getConfig(somePublicNamespace); 
//config instance is singleton for each namespace and is never null
String value = config.getProperty(someKey, someDefaultValue);
获取非properties格式的namespace配置
Config config = ConfigService.getConfig("application.yml");
String value = config.getProperty(someKey, someDefaultValue);
xml格式文件获取
String someNamespace = "test";
ConfigFile configFile = ConfigService.getConfigFile("test", ConfigFileFormat.XML);
String content = configFile.getContent();
事件监听
Config config = ConfigService.getAppConfig(); 
//config instance is singleton for each namespace and is never null
config.addChangeListener(new ConfigChangeListener() {
    @Override
    public void onChange(ConfigChangeEvent changeEvent) {
        System.out.println("Changes for namespace " + changeEvent.getNamespace());
        for (String key : changeEvent.changedKeys()) {
            ConfigChange change = changeEvent.getChange(key);
            System.out.println(String.format("Found change - key: %s, oldValue: %s, newValue: %s, changeType: %s", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType()));
        }
    }
});
基于xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:apollo="http://www.ctrip.com/schema/apollo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.ctrip.com/schema/apollo http://www.ctrip.com/schema/apollo.xsd">
    <apollo:config order="2"/>
    <!-- 这个是最复杂的配置形式,指示Apollo注入FX.apollo和application.yml namespace的配置到Spring环境中,并且顺序在application前面 -->
    <apollo:config namespaces="FX.apollo,application.yml" order="1"/>
    <bean class="com.ctrip.framework.apollo.spring.TestXmlBean">
        <property name="timeout" value="${timeout:100}"/>
基于javaconfig
//这个是最复杂的配置形式,指示Apollo注入FX.apollo和application.yml namespace的配置到Spring环境中,并且顺序在application前面
@Configuration
@EnableApolloConfig(order = 2)
public class SomeAppConfig {
  @Bean
  public TestJavaConfigBean javaConfigBean() {
    return new TestJavaConfigBean();
  }
}
@Configuration
@EnableApolloConfig(value = {"FX.apollo", "application.yml"}, order = 1)
public class AnotherAppConfig {}
直接配置属性
#加载应用对应的application namespace的配置
 apollo.bootstrap.enabled = true
 #加载其它namespace 的配置
 apollo.bootstrap.namespaces = application,FX.apollo,application.yml
 #在日志系统启动之前加载阿波罗
 apollo.bootstrap.eagerLoad.enabled=true
一些公共的注解
新增的spring注解
@ApolloJsonValue
用来把配置的json字符串自动注入为对象
配置的迁移
删除本地的配置;
增加apollo的xml配置;
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
   xmlns:apollo="http://www.ctrip.com/schema/apollo"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-4.0.xsd
                    http://code.alibabatech.com/schema/dubbo
                    http://files.sports.lifesense.com/xsd/dubbo.xsd
                    http://www.ctrip.com/schema/apollo
                    http://www.ctrip.com/schema/apollo.xsd">
<apollo:config order="1"/>
<apollo:config order="2" namespaces="lx-doctor.redis,lx-doctor.global,lx-doctor.filesystem,lx-doctor.kafka,lx-doctor.dubbo,log4j2.xml"/>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。