3333import com .ctrip .framework .apollo .enums .PropertyChangeType ;
3434import com .ctrip .framework .apollo .model .ConfigChange ;
3535
36+ import java .util .Arrays ;
37+ import java .util .Collections ;
3638import java .util .Set ;
3739import java .util .concurrent .ConcurrentHashMap ;
3840import java .util .concurrent .ConcurrentMap ;
3941import java .util .concurrent .CopyOnWriteArraySet ;
42+ import java .util .stream .Collectors ;
4043
4144/**
4245 * Apollo implementation, https://github.com/ctripcorp/apollo
@@ -46,6 +49,7 @@ public class ApolloDynamicConfiguration implements DynamicConfiguration {
4649 private static final String APOLLO_ENV_KEY = "env" ;
4750 private static final String APOLLO_ADDR_KEY = "apollo.meta" ;
4851 private static final String APOLLO_CLUSTER_KEY = "apollo.cluster" ;
52+ private static final String APOLLO_PROTOCOL_PREFIX = "http://" ;
4953
5054 private URL url ;
5155 private Config dubboConfig ;
@@ -55,12 +59,12 @@ public class ApolloDynamicConfiguration implements DynamicConfiguration {
5559 this .url = url ;
5660 // Instead of using Dubbo's configuration, I would suggest use the original configuration method Apollo provides.
5761 String configEnv = url .getParameter (APOLLO_ENV_KEY );
58- String configAddr = url . getBackupAddress ( );
62+ String configAddr = getAddressWithProtocolPrefix ( url );
5963 String configCluster = url .getParameter (Constants .CONFIG_CLUSTER_KEY );
6064 if (configEnv != null ) {
6165 System .setProperty (APOLLO_ENV_KEY , configEnv );
6266 }
63- if (StringUtils .isEmpty (configEnv ) && !Constants .ANYHOST_VALUE .equals (configAddr )) {
67+ if (StringUtils .isEmpty (System . getProperty ( APOLLO_ENV_KEY ) ) && !Constants .ANYHOST_VALUE .equals (configAddr )) {
6468 System .setProperty (APOLLO_ADDR_KEY , configAddr );
6569 }
6670 if (configCluster != null ) {
@@ -82,6 +86,21 @@ public class ApolloDynamicConfiguration implements DynamicConfiguration {
8286 }
8387 }
8488
89+ private String getAddressWithProtocolPrefix (URL url ) {
90+ String address = url .getBackupAddress ();
91+ if (StringUtils .isNotEmpty (address )) {
92+ address = Arrays .stream (Constants .COMMA_SPLIT_PATTERN .split (address ))
93+ .map (addr -> {
94+ if (addr .startsWith (APOLLO_PROTOCOL_PREFIX )) {
95+ return addr ;
96+ }
97+ return APOLLO_PROTOCOL_PREFIX + addr ;
98+ })
99+ .collect (Collectors .joining ("," ));
100+ }
101+ return address ;
102+ }
103+
85104 /**
86105 * Since all governance rules will lay under dubbo group, this method now always uses the default dubboConfig and
87106 * ignores the group parameter.
@@ -90,7 +109,7 @@ public class ApolloDynamicConfiguration implements DynamicConfiguration {
90109 public void addListener (String key , String group , ConfigurationListener listener ) {
91110 ApolloListener apolloListener = listeners .computeIfAbsent (group + key , k -> createTargetListener (key , group ));
92111 apolloListener .addListener (listener );
93- dubboConfig .addChangeListener (apolloListener );
112+ dubboConfig .addChangeListener (apolloListener , Collections . singleton ( key ) );
94113 }
95114
96115 @ Override
@@ -112,11 +131,8 @@ public void removeListener(String key, String group, ConfigurationListener liste
112131 @ Override
113132 public String getConfig (String key , String group , long timeout ) throws IllegalStateException {
114133 if (StringUtils .isNotEmpty (group ) && !url .getParameter (Constants .CONFIG_GROUP_KEY , DEFAULT_GROUP ).equals (group )) {
115- Config config = ConfigService .getConfig (group );
116- if (config != null ) {
117- return config .getProperty (key , null );
118- }
119- return null ;
134+ Config config = ConfigService .getAppConfig ();
135+ return config .getProperty (key , null );
120136 }
121137 return dubboConfig .getProperty (key , null );
122138 }
0 commit comments