SpringFramework 應用接入 Apollo 配置中心


環境:

SpringFramework:4.3.5.RELEASE

apollo-client:1.5.1

 

1.在項目的 resources/META-INF/ 目錄下添加 app.properties 文件:

#Apollo配置id
app.id = phpdragon-demo
apollo.bootstrap.enabled = true
apollo.eagerLoad.enabled = true
apollo.cacheDir = /data/app_data/apollo_cache/

2. 新建 ApolloConfigurer 類,負責處理合並本地properties配置:

import com.alibaba.dubbo.common.utils.ConfigUtils;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import java.util.Properties;
import java.util.Set;

/**
 * 處理apollo配置
 */
public class ApolloConfigurer extends PropertyPlaceholderConfigurer {

    static final String[] NAMESPACES = {"PUBLIC", "REDIS", "ZOOKEEPER", "application"};
    
    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
        try {
            this.reloadProperties(props);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("獲取apollo配置失敗");
        }

        //設置到dubbo的上下里
        ConfigUtils.addProperties(props);

        super.processProperties(beanFactoryToProcess, props);
    }

    private void reloadProperties(Properties props) {
        for (String namespace : NAMESPACES) {
            Config config = ConfigService.getConfig(namespace);
            Set<String> fieldNames = config.getPropertyNames();
            for (String attributeName : fieldNames) {
                props.put(attributeName, config.getProperty(attributeName, ""));
                //設置到系統變量里
                System.setProperty(attributeName, config.getProperty(attributeName, ""));
            }
        }
    }

    @Override
    protected String resolvePlaceholder(String placeholder, Properties props) {
        this.reloadProperties(props);
        return props.getProperty(placeholder);
    }
}

3.在 Spring 的配置xml文件中聲明配置:

<bean class="com.phpdragon.config.ApolloConfigurer">
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="trimValues" value="true"/>
    <property name="locations">
        <list>
            <value>classpath*:*.properties</value>
            <value>classpath*:properties/*.properties</value>
        </list>
    </property>
    <property name="fileEncoding" value="UTF-8"/>
</bean>

4.編寫配置類接收遠程配置變量:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

import lombok.Data;

/**
 *
 */
@Data
@Configuration
//@EnableApolloConfig不能使用該注解,否則會導致無效,該注解由 ApolloConfigurer 接管類似功能
public class RedisConfig {

    @Value("${sys.redis.host}")
    private String hostName;

    @Value("${sys.redis.port}")
    private int port;

    @Value("${redis.password}")
    private String password;

    @Value("${redis.timeout}")
    private int timeout;

    @Value("${redis.pool.maxTotal}")
    private int maxTotal;

    @Value("${redis.pool.minIdle}")
    private int minIdle;

    @Value("${redis.pool.maxIdle}")
    private int maxIdle;

    @Value("${redis.pool.maxWaitMillis}")
    private long maxWaitMillis;

    @Value("${redis.pool.testOnBorrow}")
    private boolean testOnBorrow;

    @Value("${redis.pool.testOnReturn}")
    private boolean testOnReturn;

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM