這個是apollo中的datasource的namespace.java
sb項目中的配置以下:spring
package com.miaoyouche.user.config; import com.ctrip.framework.apollo.model.ConfigChangeEvent; import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; @Slf4j @Configuration @EnableConfigurationProperties(DataSourceProperties.class) public class DataSourceConfig { @Autowired ApplicationContext context; @Autowired private org.springframework.cloud.context.scope.refresh.RefreshScope refreshScope; @ApolloConfigChangeListener("datasource1") private void onChange(ConfigChangeEvent changeEvent) { DataSourceProperties dataSourceProperties = context.getBean(DataSourceProperties.class); changeEvent.changedKeys().stream().forEach(s -> { if (s.contains("spring.datasource.password")) { dataSourceProperties.setPassword(changeEvent.getChange(s).getNewValue()); } }); refreshScope.refresh("dataSource"); } @RefreshScope @Bean public DataSource dataSource(DataSourceProperties dataSourceProperties) { return dataSourceProperties.initializeDataSourceBuilder().build(); } }
@ApolloConfigChanageListener會監聽到datasource1的namespace變化,變化後從新爲DataSourceProperties的屬性賦值.Apollo自身會在檢測變化的時候,更新@RefreshScope的註解的beansql