apollo应用配置集成及使用

apollo应用配置集成及使用

1. 开发环境Apollo地址

    用户名:apollo 密码:admin
    开发环境Apollo管理台地址:http://localhost:8070/
    开发环境Apollo Eureka地址:http://localhost:8080/

2. pom.xml引用客户端

<dependency>
    <groupId>com.ctrip.framework.apollo</groupId>
    <artifactId>apollo-client</artifactId>
    <version>1.1.0</version>
</dependency>

3. springboot项目集成

  • properties配置:

    //配置唯一appid既项目创建时的唯一id

    app.id=110000001

    //指定配置中心Eureka地址既metaservice

    [email protected]@

    //启动的bootstrap阶段,向Spring容器注入

    apollo.bootstrap.enabled = true

    //指定该项目关联的相关作用域

    apollo.bootstrap.namespaces=application,TEST1.jdbc,部门.作用域

4. spring项目集成

创建/META-INF/app.properties 内容app.id=110000001

<?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">
<!-- application 的优先级别 -->
<apollo:config order="2"/>
<!-- 这个是最复杂的配置形式,指示Apollo注入TEST1.jdbc和TEST1.xxljob namespace的配置到Spring环境中,并且顺序在application前面 -->
<apollo:config namespaces="TEST1.xxljob,TEST1.jdbc" order="1"/>    
</beans>

5. 使用及应用

   
//=========== springboot start ===========
// 启动类加上
@Configuration
@EnableApolloConfig
//=========== springboot end ===========


//=========== start ===========
 
// 获取注入某个作用域的配置
// 如TEST1.jdbc
@ApolloConfig("部门.作用域")
private Config config;
 
// 获取某个值
@Value("${key}")
private String value;
 
// 监听多个或单个作用域
@ApolloConfigChangeListener({"application", "TEST1.jdbc"})
private void anotherOnChange(ConfigChangeEvent changeEvent) {
System.out.println("===================");
// 获取key为test的change属性 包括新值老值等
ConfigChange change = changeEvent.getChange("test");
System.out.println(String.format("Found change - key: %s, oldValue: %s,"
+ " newValue: %s, changeType: %s", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType()));
System.out.println("===================");
}

//监听所有作用域
@ApolloConfigChangeListener
private void someOnChange(ConfigChangeEvent changeEvent) {
//如果值发生修改了 获取属性
if (changeEvent.isChanged("batch")) {
 batch = config.getIntProperty("batch", 100);
}
}
 
//获取指定类型属性
public int getTimeout() {
return config.getIntProperty("timeout", 200);
}
   
//=========== end ===========

6. 注意项

  • XXXXXXConfigurer类

    org.springframework.beans.factory.config.PropertyPlaceholderConfigurer如使用以上类请替换成org.springframework.context.support.PropertySourcesPlaceholderConfigurer
    另外Spring 3.1以后就不建议使用PropertyPlaceholderConfigurer了,要改用PropertySourcesPlaceholderConfigurer
  • 多环境eureka地址动态切换

    1. VM options修改eureka地址
    -apollo.meta.service=XXXXX.XXX.XXX

    2. 使用maven profile
    <profiles>
        <profile>
            <id>local</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <apollo.meta.service>
                    http://localhost:8080
                </apollo.meta.service>
            </properties>
        </profile>
        <profile>
            <id>dev</id>
            <properties>
                <apollo.meta.service>
                    http://devhost:38080
                </apollo.meta.service>
            </properties>
        </profile>
        <profile>
            <id>pro</id>
            <properties>
                <apollo.meta.service>
                    http://prohost:38080
                </apollo.meta.service>
            </properties>
        </profile>
    </profiles>

猜你喜欢

转载自www.cnblogs.com/huyunfan/p/10282911.html