spring boot 使用携程Apollo配置Druid

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

使用包


    com.ctrip.framework.apollo
    apollo-client



    com.alibaba
    druid

获取本地数据源配置代码

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * 获取数据源配置
 * Created by yanhao on 2017/8/9.
 */
@ConfigurationProperties(prefix = "spring.datasource")
public class DataSourceProperties {

    private String url;

    private String username;

    private String password;

    private String driverClassName;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDriverClassName() {
        return driverClassName;
    }

    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }
}

apollo配置代码

import com.alibaba.druid.pool.DruidDataSource;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.SQLException;

/**
 * 配置数据源
 * Created by yanhao on 2017/8/9.
 */
public class ApolloDataSourceConfiguration {

    private final Logger log = LoggerFactory.getLogger(ApolloDataSourceConfiguration.class);

    private DataSourceProperties dataSourceProperties;

    private DruidDataSource dataSource;

    // @ApolloConfig用来自动注入Config对象
    @ApolloConfig
    private Config config;

    /**
     * apollo 注解监听配置信息更新
     * @param changeEvent
     */
    @ApolloConfigChangeListener
    private void onChange(ConfigChangeEvent changeEvent) {

        if (changeEvent.isChanged("spring.datasource.url"))
            dataSourceProperties.setUrl(config.getProperty("spring.datasource.url", dataSourceProperties.getUrl()));
        if (changeEvent.isChanged("spring.datasource.driver-class-name"))
            dataSourceProperties.setDriverClassName(config.getProperty("spring.datasource.driver-class-name", dataSourceProperties.getDriverClassName()));
        if (changeEvent.isChanged("spring.datasource.username"))
            dataSourceProperties.setUsername(config.getProperty("spring.datasource.username", dataSourceProperties.getUsername()));
        if (changeEvent.isChanged("spring.datasource.password"))
            dataSourceProperties.setPassword(config.getProperty("spring.datasource.password", dataSourceProperties.getPassword()));
        // 对dataSource更新
        dataSource = createDruidDataSource();
    }
    /**
     * 创建DruidDataSource
     * @return
     */
    public DruidDataSource createDruidDataSource(){
        log.warn(String.format("apollo configutation = datasource connection factory driver-class-name = %s, " +
                        "url = %s ,username = %s ,password = %s",
                dataSourceProperties.getDriverClassName(), dataSourceProperties.getUrl(),
                dataSourceProperties.getUsername(), dataSourceProperties.getPassword()));
        // 判断是否存在dataSource
        if(dataSource == null)
            dataSource = new DruidDataSource();
        // 重启dataSource
        try {
            dataSource.restart();
        } catch (SQLException e) {
            dataSource = new DruidDataSource();
        }
        // 设置配置信息
        dataSource.setDriverClassName(dataSourceProperties.getDriverClassName());
        dataSource.setUrl(dataSourceProperties.getUrl());
        dataSource.setUsername(dataSourceProperties.getUsername());
        dataSource.setPassword(dataSourceProperties.getPassword());
        return dataSource;
    }
    public void setDataSourceProperties(DataSourceProperties dataSourceProperties){
        this.dataSourceProperties = dataSourceProperties;
    }
}

数据源配置bean

import com.alibaba.druid.pool.DruidDataSource;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

/**
 * Created by yanhao on 2017/8/9.
 */
@Order(1)
@Configuration
@EnableApolloConfig
@EnableConfigurationProperties({DataSourceProperties.class})
public class DataSourceConfiguration {

    private final Logger log = LoggerFactory.getLogger(DataSourceConfiguration.class);

    @Autowired
    private DataSourceProperties dataSourceProperties;

    @Bean
    public ApolloDataSourceConfiguration apolloDataSourceConfiguration(){
        ApolloDataSourceConfiguration apolloDataSourceConfiguration = new ApolloDataSourceConfiguration();
        apolloDataSourceConfiguration.setDataSourceProperties(dataSourceProperties);
        return apolloDataSourceConfiguration;
    }

    @Bean
    public DruidDataSource druidDataSource(){
        ApolloDataSourceConfiguration apolloDataSourceConfiguration = apolloDataSourceConfiguration();
        return apolloDataSourceConfiguration.createDruidDataSource();
    }
}

转载于:https://my.oschina.net/yan5845hao/blog/1505355

你可能感兴趣的:(spring boot 使用携程Apollo配置Druid)