@ -17,8 +17,8 @@
package com.tencent.cloud.polaris.config.tsf.adaptor ;
import java.util.Array s;
import java.util.Hash Set ;
import java.util.Array Li st ;
import java.util.Hash Map ;
import java.util.List ;
import java.util.Map ;
import java.util.Set ;
@ -30,14 +30,13 @@ import com.tencent.cloud.polaris.config.adapter.PolarisConfigPropertyAutoRefresh
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySource ;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager ;
import com.tencent.cloud.polaris.config.enums.ConfigFileFormat ;
import com.tencent.cloud.polaris.config.tsf.cache.PolarisPropertyCache ;
import com.tencent.cloud.polaris.config.tsf.encrypt.EncryptConfig ;
import com.tencent.cloud.polaris.config.utils.PolarisPropertySourceUtils ;
import com.tencent.polaris.configuration.api.core.ConfigFileGroup ;
import com.tencent.polaris.configuration.api.core.ConfigFileGroupChangeListener ;
import com.tencent.polaris.configuration.api.core.ConfigFileGroupChangedEvent ;
import com.tencent.polaris.configuration.api.core.ConfigFileMetadata ;
import com.tencent.polaris.configuration.api.core.ConfigFileService ;
import com.tencent.polaris.configuration.api.core.ConfigKVFile ;
import com.tencent.polaris.configuration.client.internal.CompositeConfigFile ;
import org.apache.commons.lang.StringUtils ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
@ -102,16 +101,41 @@ public class PolarisAdaptorTsfConfigExtensionLayer implements PolarisConfigCusto
@Override
public void initConfigFiles ( Environment environment , CompositePropertySource compositePropertySource ,
ConfigFileService configFileService ) {
String tsfApplicationId = environment . getProperty ( TSF_APPLICATION_ID ) ;
String tsfGroupId = environment . getProperty ( TSF_GROUP_ID ) ;
String tsfNamespaceId = environment . getProperty ( TSF_NAMESPACE_ID ) ;
String polarisAdaptorTsfConfigFormat = environment . getProperty ( POLARIS_ADAPTOR_TSF_CONFIG_FORMAT ) ;
String tsfApplicationId = "" ;
String tsfGroupId = "" ;
String tsfNamespaceId = "" ;
String polarisAdaptorTsfConfigFormat = "" ;
if ( environment ! = null ) {
tsfApplicationId = environment . getProperty ( TSF_APPLICATION_ID ) ;
tsfGroupId = environment . getProperty ( TSF_GROUP_ID ) ;
tsfNamespaceId = environment . getProperty ( TSF_NAMESPACE_ID ) ;
polarisAdaptorTsfConfigFormat = environment . getProperty ( POLARIS_ADAPTOR_TSF_CONFIG_FORMAT ) ;
}
else {
tsfApplicationId = System . getProperty ( TSF_APPLICATION_ID ) ;
if ( StringUtils . isEmpty ( tsfApplicationId ) ) {
tsfApplicationId = System . getenv ( TSF_APPLICATION_ID ) ;
}
tsfGroupId = System . getProperty ( TSF_GROUP_ID ) ;
if ( StringUtils . isEmpty ( tsfGroupId ) ) {
tsfGroupId = System . getenv ( TSF_GROUP_ID ) ;
}
tsfNamespaceId = System . getProperty ( TSF_NAMESPACE_ID ) ;
if ( StringUtils . isEmpty ( tsfNamespaceId ) ) {
tsfNamespaceId = System . getenv ( TSF_NAMESPACE_ID ) ;
}
polarisAdaptorTsfConfigFormat = System . getProperty ( POLARIS_ADAPTOR_TSF_CONFIG_FORMAT ) ;
if ( StringUtils . isEmpty ( polarisAdaptorTsfConfigFormat ) ) {
polarisAdaptorTsfConfigFormat = System . getenv ( POLARIS_ADAPTOR_TSF_CONFIG_FORMAT ) ;
}
}
LOGGER . info (
"[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer initConfigFiles start, tsfNamespaceId:{}, tsfApplicationId:{}, tsfGroupId:{}" ,
" PolarisAdaptorTsfConfigExtensionLayer initConfigFiles start, tsfNamespaceId:{}, tsfApplicationId:{}, tsfGroupId:{}",
tsfNamespaceId , tsfApplicationId , tsfGroupId ) ;
loadAllPolarisConfigFile ( compositePropertySource , configFileService ,
tsfNamespaceId , tsfApplicationId , tsfGroupId , polarisAdaptorTsfConfigFormat ) ;
LOGGER . info ( "[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer initConfigFiles end" ) ;
LOGGER . info ( " PolarisAdaptorTsfConfigExtensionLayer initConfigFiles end") ;
}
private void loadAllPolarisConfigFile ( CompositePropertySource compositePropertySource ,
@ -130,126 +154,142 @@ public class PolarisAdaptorTsfConfigExtensionLayer implements PolarisConfigCusto
compositePropertySource , configFileService , pubConfigGroup ) ;
}
private PolarisPropertySource loadPolarisPropertySource ( String namespace , String group , String fileName ,
String polarisAdaptorTsfConfigFormat , ConfigFileService configFileService ) {
ConfigKVFile configKVFile ;
if ( StringUtils . isNotBlank ( polarisAdaptorTsfConfigFormat ) ) {
switch ( polarisAdaptorTsfConfigFormat ) {
case "properties" :
configKVFile = configFileService . getConfigPropertiesFile ( namespace , group , fileName ) ;
case "yaml" :
default :
private PolarisPropertySource loadPolarisPropertySource ( String configFileGroupNamespace , String configFileGroupName ,
List < ConfigFileMetadata > configFileMetadataList , String polarisAdaptorTsfConfigFormat ,
ConfigFileService configFileService ) {
List < ConfigKVFile > configKVFiles = new ArrayList < > ( ) ;
for ( ConfigFileMetadata configFileMetadata : configFileMetadataList ) {
ConfigKVFile configKVFile ;
String namespace = configFileMetadata . getNamespace ( ) ;
String group = configFileMetadata . getFileGroup ( ) ;
String fileName = configFileMetadata . getFileName ( ) ;
if ( StringUtils . isNotBlank ( polarisAdaptorTsfConfigFormat ) ) {
switch ( polarisAdaptorTsfConfigFormat ) {
case "properties" :
configKVFile = configFileService . getConfigPropertiesFile ( namespace , group , fileName ) ;
break ;
case "yaml" :
default :
configKVFile = configFileService . getConfigYamlFile ( namespace , group , fileName ) ;
}
}
// unknown extension is resolved as yaml file
else if ( ConfigFileFormat . isYamlFile ( fileName ) | | ConfigFileFormat . isUnknownFile ( fileName ) ) {
configKVFile = configFileService . getConfigYamlFile ( namespace , group , fileName ) ;
}
}
// unknown extension is resolved as yaml file
else if ( ConfigFileFormat . isYamlFile ( fileName ) | | ConfigFileFormat . isUnknownFile ( fileName ) ) {
configKVFile = configFileService . getConfigYamlFile ( namespace , group , fileName ) ;
}
else if ( ConfigFileFormat . isPropertyFile ( fileName ) ) {
configKVFile = configFileService . getConfigPropertiesFile ( namespace , group , fileName ) ;
}
else {
LOGGER . warn ( "[SCTT Config] Unsupported config file. namespace = {}, group = {}, fileName = {}" , namespace ,
group , fileName ) ;
else if ( ConfigFileFormat . isPropertyFile ( fileName ) ) {
configKVFile = configFileService . getConfigPropertiesFile ( namespace , group , fileName ) ;
}
else {
LOGGER . warn ( "Unsupported config file. namespace = {}, group = {}, fileName = {}" , namespace ,
group , fileName ) ;
throw new IllegalStateException ( "Only configuration files in the format of properties / yaml / yaml"
+ " can be injected into the spring context" ) ;
throw new IllegalStateException ( "Only configuration files in the format of properties / yaml / yaml"
+ " can be injected into the spring context" ) ;
}
configKVFiles . add ( configKVFile ) ;
}
CompositeConfigFile compositeConfigFile = new CompositeConfigFile ( configKVFiles ) ;
Map < String , Object > map = new ConcurrentHashMap < > ( ) ;
for ( String key : configKVFile . getPropertyNames ( ) ) {
String value = co nfigKV File. getProperty ( key , null ) ;
for ( String key : co mpositeCo nfigFile. getPropertyNames ( ) ) {
String value = co mpositeCo nfigFile. getProperty ( key , null ) ;
if ( EncryptConfig . needDecrypt ( value ) ) {
LOGGER . debug ( " [SCTT Config] Need Decrypt {}: {}", key , value ) ;
LOGGER . debug ( " Need Decrypt {}: {}", key , value ) ;
value = EncryptConfig . getProvider ( )
. decrypt ( EncryptConfig . realContent ( value ) , EncryptConfig . getPassword ( ) ) ;
}
map . put ( key , value ) ;
}
return new PolarisPropertySource ( namespace , group , fileName , configKVFile , map ) ;
if ( LOGGER . isDebugEnabled ( ) ) {
LOGGER . debug ( "namespace='" + configFileGroupNamespace + '\''
+ ", group='" + configFileGroupName + '\'' + ", fileName='" + compositeConfigFile + '\''
+ ", map='" + map + '\'' ) ;
}
return new PolarisPropertySource ( configFileGroupNamespace , configFileGroupName , "" , compositeConfigFile , map ) ;
}
private void loadPolarisConfigFile ( String namespace , String tsfApplicationId , String tsfGroupId ,
String polarisAdaptorTsfConfigFormat , CompositePropertySource compositePropertySource ,
ConfigFileService configFileService , String configGroup ) {
LOGGER . debug (
" [SCTT Config] PolarisAdaptorTsfConfigExtensionLayer loadPolarisConfigFile start, namespace:{}, group:{}",
" PolarisAdaptorTsfConfigExtensionLayer loadPolarisConfigFile start, namespace:{}, group:{}",
namespace , configGroup ) ;
ConfigFileGroup configFileGroup = configFileService . getConfigFileGroup ( namespace , configGroup ) ;
if ( configFileGroup = = null ) {
throw new IllegalStateException (
" [SCTT Config] PolarisAdaptorTsfConfigExtensionLayer configFileGroup is null") ;
" PolarisAdaptorTsfConfigExtensionLayer configFileGroup is null") ;
}
List < ConfigFileMetadata > configFileMetadataList = configFileGroup . getConfigFileMetadataList ( ) ;
if ( ! CollectionUtils . isEmpty ( configFileMetadataList ) ) {
LOGGER . info ( " [SCTT Config] PolarisAdaptorTsfConfigExtensionLayer getConfigFileMetadataList:{}",
LOGGER . info ( " PolarisAdaptorTsfConfigExtensionLayer getConfigFileMetadataList:{}",
configFileMetadataList ) ;
for ( ConfigFileMetadata configFile : configFileMetadataList ) {
PolarisPropertySource polarisPropertySource = loadPolarisPropertySource ( configFile . getNamespace ( ) ,
configFile . getFileGroup ( ) , configFile . getFileName ( ) , polarisAdaptorTsfConfigFormat ,
configFileService ) ;
}
else {
LOGGER . info ( "PolarisAdaptorTsfConfigExtensionLayer getConfigFileMetadataList empty. file = {}:{}" ,
configFileGroup . getNamespace ( ) , configFileGroup . getNamespace ( ) ) ;
}
PolarisPropertySource polarisPropertySource = loadPolarisPropertySource ( namespace , configGroup ,
configFileMetadataList , polarisAdaptorTsfConfigFormat , configFileService ) ;
compositePropertySource . addPropertySource ( polarisPropertySource ) ;
compositePropertySource . addPropertySource ( polarisPropertySource ) ;
PolarisPropertySourceManager . addPropertySource ( polarisPropertySource ) ;
PolarisPropertySourceManager . addPropertySource ( polarisPropertySource ) ;
LOGGER . info ( "PolarisAdaptorTsfConfigExtensionLayer Load and inject polaris config file from config group:{}. file = {}" ,
configGroup , polarisPropertySource . getConfigKVFile ( ) ) ;
LOGGER . info (
"[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer Load and inject polaris config file from config group:{}. file = {}" ,
configGroup , configFile ) ;
}
}
String namespaceConfigGroup = namespace + "-" + configGroup ;
// 用ConcurrentHashSet保证不重复添加ConfigFileGroupChangeListener
if ( registedConfigFileGroupListenerSets . add ( namespaceConfigGroup ) ) {
LOGGER . info (
"[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer configFileGroup addChangeListener namespaceConfigGroup:{}" ,
LOGGER . info ( "PolarisAdaptorTsfConfigExtensionLayer configFileGroup addChangeListener namespaceConfigGroup:{}" ,
namespaceConfigGroup ) ;
configFileGroup . addChangeListener ( new ConfigFileGroupChangeListener ( ) {
@Override
public void onChange ( ConfigFileGroupChangedEvent event ) {
try {
LOGGER . info ( "[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer receive onChange event:{}" ,
event ) ;
List < ConfigFileMetadata > configFileMetadataList = event . getConfigFileMetadataList ( ) ;
if ( CollectionUtils . isEmpty ( configFileMetadataList ) ) {
LOGGER . info (
"[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer receive configFileMetadataList is empty" ) ;
return ;
}
boolean needRefreshAll = false ;
for ( ConfigFileMetadata configFile : configFileMetadataList ) {
PolarisPropertySource polarisPropertySource = loadPolarisPropertySource (
configFile . getNamespace ( ) , configFile . getFileGroup ( ) , configFile . getFileName ( ) ,
polarisAdaptorTsfConfigFormat , configFileService ) ;
configFileGroup . addChangeListener ( event - > {
try {
LOGGER . info ( "PolarisAdaptorTsfConfigExtensionLayer receive onChange event:{}" ,
event ) ;
List < ConfigFileMetadata > oldConfigFileMetadataList = event . getOldConfigFileMetadataList ( ) ;
List < ConfigFileMetadata > newConfigFileMetadataList = event . getNewConfigFileMetadataList ( ) ;
List < String > toUnregister = calculateUnregister ( oldConfigFileMetadataList , newConfigFileMetadataList ) ;
toUnregister . forEach ( registedPolarisPropertySets : : remove ) ;
if ( CollectionUtils . isEmpty ( newConfigFileMetadataList ) ) {
LOGGER . info ( "PolarisAdaptorTsfConfigExtensionLayer receive configFileMetadataList is empty" ) ;
}
else {
PolarisPropertySource polarisPropertySource1 = loadPolarisPropertySource (
event . getConfigFileGroupMetadata ( ) . getNamespace ( ) ,
event . getConfigFileGroupMetadata ( ) . getFileGroupName ( ) ,
newConfigFileMetadataList , polarisAdaptorTsfConfigFormat , configFileService ) ;
// 用ConcurrentHashSet保证不重复注册PolarisConfigPublishChangeListener
CompositeConfigFile configKVFile = ( CompositeConfigFile ) polarisPropertySource1 . getConfigKVFile ( ) ;
for ( ConfigKVFile cf : configKVFile . getConfigKVFiles ( ) ) {
LOGGER . info (
"[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer Load and inject polaris config file from onChange event config group:{}. file = {}" ,
configGroup , configFile ) ;
// 用ConcurrentHashSet保证不重复注册PolarisConfigPublishChangeListener
if ( executeRegisterPublishChangeListener ( polarisPropertySource ) ) {
polarisConfigPropertyAutoRefresher . registerPolarisConfigPublishChangeListener (
polarisPropertySource ) ;
needRefreshAll = true ;
"PolarisAdaptorTsfConfigExtensionLayer Load and inject polaris config file from onChange event config group:{}. file = {}" ,
configGroup , cf ) ;
PolarisPropertySource p = new PolarisPropertySource ( cf . getNamespace ( ) , cf . getFileGroup ( ) , cf . getFileName ( ) , cf , new HashMap < > ( ) ) ;
if ( executeRegisterPublishChangeListener ( p ) ) {
polarisConfigPropertyAutoRefresher . registerPolarisConfigPublishChangeListener ( p ) ;
}
}
if ( needRefreshAll ) {
LOGGER . info ( "[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer start refresh All Config" ) ;
polarisConfigPropertyAutoRefresher . refreshConfigurationProperties ( null ) ;
}
}
catch ( Exception e ) {
LOGGER . info ( "[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer receive onChange exception:" ,
e ) ;
}
LOGGER . info ( "PolarisAdaptorTsfConfigExtensionLayer start refresh All Config" ) ;
polarisConfigPropertyAutoRefresher . refreshConfigurationProperties ( null ) ;
}
catch ( Exception e ) {
LOGGER . info ( "PolarisAdaptorTsfConfigExtensionLayer receive onChange exception:" ,
e ) ;
}
} ) ;
}
LOGGER . info (
"[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer loadPolarisConfigFile end, namespace:{}, group:{}" ,
LOGGER . info ( "PolarisAdaptorTsfConfigExtensionLayer loadPolarisConfigFile end, namespace:{}, group:{}" ,
namespace , configGroup ) ;
}
@ -258,10 +298,7 @@ public class PolarisAdaptorTsfConfigExtensionLayer implements PolarisConfigCusto
* /
@Override
public void executeAfterLocateConfigReturning ( CompositePropertySource compositePropertySource ) {
PolarisPropertyCache . getInstance ( ) . clear ( ) ;
PolarisPropertyCache . getInstance ( ) . getCache ( )
. addAll ( new HashSet < > ( Arrays . asList ( compositePropertySource . getPropertyNames ( ) ) ) ) ;
LOGGER . info ( "[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer executeAfterLocateConfigReturning finished" ) ;
}
/ * *
@ -270,7 +307,7 @@ public class PolarisAdaptorTsfConfigExtensionLayer implements PolarisConfigCusto
@Override
public void initRegisterConfig ( PolarisConfigPropertyAutoRefresher polarisConfigPropertyAutoRefresher ) {
LOGGER . info (
" [SCTT Config] PolarisAdaptorTsfConfigExtensionLayer initRegisterConfig polarisConfigPropertyAutoRefresher:{}",
" PolarisAdaptorTsfConfigExtensionLayer initRegisterConfig polarisConfigPropertyAutoRefresher:{}",
polarisConfigPropertyAutoRefresher . getClass ( ) ) ;
this . polarisConfigPropertyAutoRefresher = polarisConfigPropertyAutoRefresher ;
}
@ -283,9 +320,27 @@ public class PolarisAdaptorTsfConfigExtensionLayer implements PolarisConfigCusto
boolean isRegisterSuccess = registedPolarisPropertySets . add ( polarisPropertySource . getPropertySourceName ( ) ) ;
if ( isRegisterSuccess ) {
// 已防止重复注册,仅打印注册成功的即可
LOGGER . info ( " [SCTT Config] start to register configFile polarisConfigPublishChangeListener:{}",
LOGGER . info ( " start to register configFile polarisConfigPublishChangeListener:{}",
polarisPropertySource ) ;
}
return isRegisterSuccess ;
}
private List < String > calculateUnregister ( List < ConfigFileMetadata > oldConfigFileMetadataList , List < ConfigFileMetadata > newConfigFileMetadataList ) {
List < String > newNameList = new ArrayList < > ( ) ;
for ( ConfigFileMetadata configFileMetadata : newConfigFileMetadataList ) {
newNameList . add ( PolarisPropertySourceUtils . generateName ( configFileMetadata . getNamespace ( ) ,
configFileMetadata . getFileGroup ( ) , configFileMetadata . getFileName ( ) ) ) ;
}
List < String > toUnregister = new ArrayList < > ( ) ;
for ( ConfigFileMetadata configFileMetadata : oldConfigFileMetadataList ) {
String oldName = ( PolarisPropertySourceUtils . generateName ( configFileMetadata . getNamespace ( ) ,
configFileMetadata . getFileGroup ( ) , configFileMetadata . getFileName ( ) ) ) ;
if ( ! newNameList . contains ( oldName ) ) {
toUnregister . add ( oldName ) ;
}
}
return toUnregister ;
}
}