22
22
import com .ctrip .framework .apollo .core .utils .DeprecatedPropertyNotifyUtil ;
23
23
import com .ctrip .framework .foundation .Foundation ;
24
24
import java .lang .reflect .Type ;
25
+ import java .util .Collections ;
25
26
import java .util .List ;
26
27
import java .util .Map ;
27
28
import java .util .concurrent .Executors ;
28
29
import java .util .concurrent .ScheduledExecutorService ;
29
30
import java .util .concurrent .atomic .AtomicReference ;
30
31
31
32
import org .slf4j .Logger ;
32
- import org .slf4j .LoggerFactory ;
33
33
34
34
import com .ctrip .framework .apollo .build .ApolloInjector ;
35
35
import com .ctrip .framework .apollo .core .dto .ServiceDTO ;
@@ -54,25 +54,33 @@ public class ConfigServiceLocator {
54
54
private static final Logger logger = DeferredLoggerFactory .getLogger (ConfigServiceLocator .class );
55
55
private HttpClient m_httpClient ;
56
56
private ConfigUtil m_configUtil ;
57
- private AtomicReference <List <ServiceDTO >> m_configServices ;
58
- private Type m_responseType ;
59
- private ScheduledExecutorService m_executorService ;
57
+ private final AtomicReference <List <ServiceDTO >> m_configServices = new AtomicReference <>(
58
+ Collections .emptyList ());
59
+ private static final Type m_responseType = new TypeToken <List <ServiceDTO >>() {
60
+ }.getType ();
61
+ ScheduledExecutorService m_executorService ;
60
62
private static final Joiner .MapJoiner MAP_JOINER = Joiner .on ("&" ).withKeyValueSeparator ("=" );
61
63
private static final Escaper queryParamEscaper = UrlEscapers .urlFormParameterEscaper ();
62
64
63
65
/**
64
66
* Create a config service locator.
65
67
*/
66
68
public ConfigServiceLocator () {
67
- List <ServiceDTO > initial = Lists .newArrayList ();
68
- m_configServices = new AtomicReference <>(initial );
69
- m_responseType = new TypeToken <List <ServiceDTO >>() {
70
- }.getType ();
71
- m_httpClient = ApolloInjector .getInstance (HttpClient .class );
72
- m_configUtil = ApolloInjector .getInstance (ConfigUtil .class );
73
- this .m_executorService = Executors .newScheduledThreadPool (1 ,
74
- ApolloThreadFactory .create ("ConfigServiceLocator" , true ));
75
- initConfigServices ();
69
+ }
70
+
71
+ /**
72
+ * only init once.
73
+ */
74
+ private void initialize () {
75
+ if (m_httpClient == null ) {
76
+ synchronized (this ) {
77
+ if (m_httpClient == null ) {
78
+ m_httpClient = ApolloInjector .getInstance (HttpClient .class );
79
+ m_configUtil = ApolloInjector .getInstance (ConfigUtil .class );
80
+ initConfigServices ();
81
+ }
82
+ }
83
+ }
76
84
}
77
85
78
86
private void initConfigServices () {
@@ -160,8 +168,14 @@ private String getDeprecatedCustomizedConfigService() {
160
168
* @return the services dto
161
169
*/
162
170
public List <ServiceDTO > getConfigServices () {
171
+ initialize ();
163
172
if (m_configServices .get ().isEmpty ()) {
164
- updateConfigServices ();
173
+ // quick fail
174
+ throw new ApolloConfigException (
175
+ "No available config service, "
176
+ + "server side maybe crash or network cannot connect to server from this ip, "
177
+ + "one of meta service url is " + assembleMetaServiceUrl ()
178
+ );
165
179
}
166
180
167
181
return m_configServices .get ();
@@ -178,6 +192,8 @@ private boolean tryUpdateConfigServices() {
178
192
}
179
193
180
194
private void schedulePeriodicRefresh () {
195
+ this .m_executorService = Executors .newScheduledThreadPool (1 ,
196
+ ApolloThreadFactory .create ("ConfigServiceLocator" , true ));
181
197
this .m_executorService .scheduleAtFixedRate (
182
198
new Runnable () {
183
199
@ Override
@@ -190,7 +206,7 @@ public void run() {
190
206
m_configUtil .getRefreshIntervalTimeUnit ());
191
207
}
192
208
193
- private synchronized void updateConfigServices () {
209
+ synchronized void updateConfigServices () {
194
210
String url = assembleMetaServiceUrl ();
195
211
196
212
HttpRequest request = new HttpRequest (url );
0 commit comments