2525import com .ctrip .framework .apollo .model .ConfigFileChangeEvent ;
2626import com .ctrip .framework .apollo .spring .config .PropertySourcesConstants ;
2727import com .google .common .base .Splitter ;
28+ import com .google .common .collect .Lists ;
29+ import com .google .common .collect .Maps ;
2830import lombok .extern .slf4j .Slf4j ;
2931import lombok .val ;
32+ import org .apache .commons .collections4 .MapUtils ;
33+ import org .apache .commons .lang3 .StringUtils ;
34+ import org .apache .commons .lang3 .tuple .Pair ;
3035import org .dromara .dynamictp .common .em .ConfigFileTypeEnum ;
3136import org .dromara .dynamictp .common .properties .DtpProperties ;
3237import org .dromara .dynamictp .core .handler .ConfigHandler ;
3540
3641import java .io .IOException ;
3742import java .util .List ;
43+ import java .util .Map ;
3844
3945import static org .dromara .dynamictp .common .constant .DynamicTpConst .MAIN_PROPERTIES_PREFIX ;
4046
4652 * @since 1.0.0
4753 **/
4854@ Slf4j
49- public class ApolloRefresher extends AbstractSpringRefresher implements ConfigFileChangeListener , InitializingBean {
55+ public class ApolloRefresher extends AbstractSpringRefresher implements InitializingBean , ConfigFileChangeListener {
5056
5157 private static final Splitter NAMESPACE_SPLITTER = Splitter .on ("," ).omitEmptyStrings ().trimResults ();
5258
59+ private final List <Pair <String , ConfigFileFormat >> namespacePairList = Lists .newArrayList ();
60+
5361 public ApolloRefresher (DtpProperties dtpProperties ) {
5462 super (dtpProperties );
5563 }
5664
5765 @ Override
5866 public void onChange (ConfigFileChangeEvent changeEvent ) {
59- String namespace = changeEvent .getNamespace ();
60- String newValue = changeEvent .getNewValue ();
61- ConfigFileTypeEnum configFileType = deduceConfigFileType (namespace );
62- refresh (newValue , configFileType );
67+ val configHandler = ConfigHandler .getInstance ();
68+ val mergedProperties = Maps .newHashMap ();
69+ namespacePairList .forEach (pair -> {
70+ String content ;
71+ if (changeEvent .getNamespace ().startsWith (pair .getLeft ())) {
72+ content = changeEvent .getNewValue ();
73+ } else {
74+ content = ConfigService .getConfigFile (pair .getLeft (), pair .getRight ()).getContent ();
75+ }
76+ if (StringUtils .isBlank (content )) {
77+ return ;
78+ }
79+ try {
80+ Map <Object , Object > properties = configHandler .parseConfig (content , ConfigFileTypeEnum .of (pair .getRight ().getValue ()));
81+ if (MapUtils .isNotEmpty (properties )) {
82+ mergedProperties .putAll (properties );
83+ }
84+ } catch (IOException e ) {
85+ log .error ("DynamicTp refresher, parse apollo config file error, namespace: {}" , pair .getLeft (), e );
86+ }
87+ });
88+ refresh (mergedProperties );
6389 }
6490
6591 @ Override
6692 public void afterPropertiesSet () {
67- String namespaces = environment .getProperty (PropertySourcesConstants .APOLLO_BOOTSTRAP_NAMESPACES ,
68- ConfigConsts .NAMESPACE_APPLICATION );
69- log .debug ("Apollo bootstrap namespaces: {}" , namespaces );
70- List <String > namespaceList = NAMESPACE_SPLITTER .splitToList (namespaces );
71-
93+ List <String > namespaceList = getNamespaceList ();
94+ log .info ("Apollo bootstrap namespaces: {}" , namespaceList );
7295 for (String namespace : namespaceList ) {
7396 ConfigFileFormat format = determineFileFormat (namespace );
7497 String actualNamespaceName = trimNamespaceFormat (namespace , format );
@@ -77,6 +100,7 @@ public void afterPropertiesSet() {
77100 continue ;
78101 }
79102 try {
103+ namespacePairList .add (Pair .of (actualNamespaceName , format ));
80104 configFile .addChangeListener (this );
81105 log .info ("DynamicTp refresher, add listener success, namespace: {}" , actualNamespaceName );
82106 } catch (Exception e ) {
@@ -103,9 +127,10 @@ private String trimNamespaceFormat(String namespaceName, ConfigFileFormat format
103127 return namespaceName .substring (0 , namespaceName .length () - extension .length ());
104128 }
105129
106- private ConfigFileTypeEnum deduceConfigFileType (String namespace ) {
107- ConfigFileFormat configFileFormat = determineFileFormat (namespace );
108- return ConfigFileTypeEnum .of (configFileFormat .getValue ());
130+ private List <String > getNamespaceList () {
131+ String namespaces = environment .getProperty (PropertySourcesConstants .APOLLO_BOOTSTRAP_NAMESPACES ,
132+ ConfigConsts .NAMESPACE_APPLICATION );
133+ return NAMESPACE_SPLITTER .splitToList (namespaces );
109134 }
110135
111136 private boolean isDtpNamespace (String content , ConfigFileTypeEnum configFileType ) {
0 commit comments