38
38
import org .apache .seata .config .ConfigurationChangeListener ;
39
39
import org .apache .seata .config .ConfigurationFactory ;
40
40
import org .apache .seata .config .ConfigurationKeys ;
41
+ import org .apache .seata .config .Dispose ;
41
42
import org .apache .seata .config .processor .ConfigProcessor ;
42
43
import org .slf4j .Logger ;
43
44
import org .slf4j .LoggerFactory ;
47
48
* The type Nacos configuration.
48
49
*
49
50
*/
50
- public class NacosConfiguration extends AbstractConfiguration {
51
+ public class NacosConfiguration extends AbstractConfiguration implements Dispose {
51
52
private static volatile NacosConfiguration instance ;
52
53
53
54
private static final Logger LOGGER = LoggerFactory .getLogger (NacosConfiguration .class );
@@ -66,10 +67,10 @@ public class NacosConfiguration extends AbstractConfiguration {
66
67
private static final String RAM_ROLE_NAME_KEY = "ramRoleName" ;
67
68
private static final String USE_PARSE_RULE = "false" ;
68
69
private static final String CONTEXT_PATH = "contextPath" ;
69
- private static final Configuration FILE_CONFIG = ConfigurationFactory .CURRENT_FILE_INSTANCE ;
70
+ private Configuration fileConfig = ConfigurationFactory .CURRENT_FILE_INSTANCE ;
70
71
private static volatile ConfigService configService ;
71
72
private static final int MAP_INITIAL_CAPACITY = 8 ;
72
- private static final ConcurrentMap <String , ConcurrentMap <ConfigurationChangeListener , NacosListener >> CONFIG_LISTENERS_MAP
73
+ private static volatile ConcurrentMap <String , ConcurrentMap <ConfigurationChangeListener , NacosListener >> CONFIG_LISTENERS_MAP
73
74
= new ConcurrentHashMap <>(MAP_INITIAL_CAPACITY );
74
75
private static volatile Properties seataConfig = new Properties ();
75
76
@@ -203,14 +204,14 @@ public Set<ConfigurationChangeListener> getConfigListeners(String dataId) {
203
204
}
204
205
}
205
206
206
- private static Properties getConfigProperties () {
207
+ private Properties getConfigProperties () {
207
208
Properties properties = new Properties ();
208
209
properties .setProperty (ConfigurationKeys .IS_USE_CLOUD_NAMESPACE_PARSING , USE_PARSE_RULE );
209
210
properties .setProperty (ConfigurationKeys .IS_USE_ENDPOINT_PARSING_RULE , USE_PARSE_RULE );
210
211
if (System .getProperty (PRO_SERVER_ADDR_KEY ) != null ) {
211
212
properties .setProperty (PRO_SERVER_ADDR_KEY , System .getProperty (PRO_SERVER_ADDR_KEY ));
212
213
} else {
213
- String address = FILE_CONFIG .getConfig (getNacosAddrFileKey ());
214
+ String address = fileConfig .getConfig (getNacosAddrFileKey ());
214
215
if (address != null ) {
215
216
properties .setProperty (PRO_SERVER_ADDR_KEY , address );
216
217
}
@@ -219,7 +220,7 @@ private static Properties getConfigProperties() {
219
220
if (System .getProperty (PRO_NAMESPACE_KEY ) != null ) {
220
221
properties .setProperty (PRO_NAMESPACE_KEY , System .getProperty (PRO_NAMESPACE_KEY ));
221
222
} else {
222
- String namespace = FILE_CONFIG .getConfig (getNacosNameSpaceFileKey ());
223
+ String namespace = fileConfig .getConfig (getNacosNameSpaceFileKey ());
223
224
if (namespace == null ) {
224
225
namespace = DEFAULT_NAMESPACE ;
225
226
}
@@ -228,7 +229,7 @@ private static Properties getConfigProperties() {
228
229
if (!initNacosAuthProperties (properties )) {
229
230
LOGGER .info ("Nacos config auth properties empty." );
230
231
}
231
- String contextPath = StringUtils .isNotBlank (System .getProperty (CONTEXT_PATH )) ? System .getProperty (CONTEXT_PATH ) : FILE_CONFIG .getConfig (getNacosContextPathKey ());
232
+ String contextPath = StringUtils .isNotBlank (System .getProperty (CONTEXT_PATH )) ? System .getProperty (CONTEXT_PATH ) : fileConfig .getConfig (getNacosContextPathKey ());
232
233
if (StringUtils .isNotBlank (contextPath )) {
233
234
properties .setProperty (CONTEXT_PATH , contextPath );
234
235
}
@@ -242,21 +243,21 @@ private static Properties getConfigProperties() {
242
243
* @param sourceProperties the source properties
243
244
* @return auth properties
244
245
*/
245
- private static boolean initNacosAuthProperties (Properties sourceProperties ) {
246
- String userName = StringUtils .isNotBlank (System .getProperty (USER_NAME )) ? System .getProperty (USER_NAME ) : FILE_CONFIG .getConfig (getNacosUserName ());
246
+ private boolean initNacosAuthProperties (Properties sourceProperties ) {
247
+ String userName = StringUtils .isNotBlank (System .getProperty (USER_NAME )) ? System .getProperty (USER_NAME ) : fileConfig .getConfig (getNacosUserName ());
247
248
if (StringUtils .isNotBlank (userName )) {
248
- String password = StringUtils .isNotBlank (System .getProperty (PASSWORD )) ? System .getProperty (PASSWORD ) : FILE_CONFIG .getConfig (getNacosPassword ());
249
+ String password = StringUtils .isNotBlank (System .getProperty (PASSWORD )) ? System .getProperty (PASSWORD ) : fileConfig .getConfig (getNacosPassword ());
249
250
if (StringUtils .isNotBlank (password )) {
250
251
sourceProperties .setProperty (USER_NAME , userName );
251
252
sourceProperties .setProperty (PASSWORD , password );
252
253
LOGGER .info ("Nacos check auth with userName/password." );
253
254
return true ;
254
255
}
255
256
} else {
256
- String accessKey = StringUtils .isNotBlank (System .getProperty (ACCESS_KEY )) ? System .getProperty (ACCESS_KEY ) : FILE_CONFIG .getConfig (getNacosAccessKey ());
257
- String ramRoleName = StringUtils .isNotBlank (System .getProperty (RAM_ROLE_NAME_KEY )) ? System .getProperty (RAM_ROLE_NAME_KEY ) : FILE_CONFIG .getConfig (getNacosRamRoleNameKey ());
257
+ String accessKey = StringUtils .isNotBlank (System .getProperty (ACCESS_KEY )) ? System .getProperty (ACCESS_KEY ) : fileConfig .getConfig (getNacosAccessKey ());
258
+ String ramRoleName = StringUtils .isNotBlank (System .getProperty (RAM_ROLE_NAME_KEY )) ? System .getProperty (RAM_ROLE_NAME_KEY ) : fileConfig .getConfig (getNacosRamRoleNameKey ());
258
259
if (StringUtils .isNotBlank (accessKey )) {
259
- String secretKey = StringUtils .isNotBlank (System .getProperty (SECRET_KEY )) ? System .getProperty (SECRET_KEY ) : FILE_CONFIG .getConfig (getNacosSecretKey ());
260
+ String secretKey = StringUtils .isNotBlank (System .getProperty (SECRET_KEY )) ? System .getProperty (SECRET_KEY ) : fileConfig .getConfig (getNacosSecretKey ());
260
261
if (StringUtils .isNotBlank (secretKey )) {
261
262
sourceProperties .put (ACCESS_KEY , accessKey );
262
263
sourceProperties .put (SECRET_KEY , secretKey );
@@ -310,15 +311,15 @@ public static String getNacosRamRoleNameKey() {
310
311
return String .join (ConfigurationKeys .FILE_CONFIG_SPLIT_CHAR , ConfigurationKeys .FILE_ROOT_CONFIG , CONFIG_TYPE , RAM_ROLE_NAME_KEY );
311
312
}
312
313
313
- private static String getNacosGroup () {
314
- return FILE_CONFIG .getConfig (getNacosGroupKey (), DEFAULT_GROUP );
314
+ private String getNacosGroup () {
315
+ return fileConfig .getConfig (getNacosGroupKey (), DEFAULT_GROUP );
315
316
}
316
317
317
- private static String getNacosDataId () {
318
- return FILE_CONFIG .getConfig (getNacosDataIdKey (), DEFAULT_DATA_ID );
318
+ private String getNacosDataId () {
319
+ return fileConfig .getConfig (getNacosDataIdKey (), DEFAULT_DATA_ID );
319
320
}
320
321
321
- private static String getNacosDataType () {
322
+ private String getNacosDataType () {
322
323
return ConfigProcessor .resolverConfigDataType (getNacosDataId ());
323
324
}
324
325
@@ -339,7 +340,7 @@ private static String getNacosContextPathKey() {
339
340
return String .join (ConfigurationKeys .FILE_CONFIG_SPLIT_CHAR , ConfigurationKeys .FILE_ROOT_CONFIG , CONFIG_TYPE , CONTEXT_PATH );
340
341
}
341
342
342
- private static void initSeataConfig () {
343
+ private void initSeataConfig () {
343
344
try {
344
345
String nacosDataId = getNacosDataId ();
345
346
String config = configService .getConfig (nacosDataId , getNacosGroup (), DEFAULT_CONFIG_TIMEOUT );
@@ -360,10 +361,27 @@ public String getTypeName() {
360
361
return CONFIG_TYPE ;
361
362
}
362
363
364
+ @ Override
365
+ public void dispose () {
366
+ if (null != CONFIG_LISTENERS_MAP ) {
367
+ CONFIG_LISTENERS_MAP .clear ();
368
+ }
369
+ if (null != seataConfig ) {
370
+ seataConfig .clear ();
371
+ }
372
+ if (null != configService ) {
373
+ configService = null ;
374
+ }
375
+ if (null != instance ) {
376
+ instance = null ;
377
+ }
378
+ fileConfig = ConfigurationFactory .CURRENT_FILE_INSTANCE ;
379
+ }
380
+
363
381
/**
364
382
* Non-blocking subscriptions prohibit adding subscriptions in the thread pool to prevent thread termination
365
383
*/
366
- public static class NacosListener extends AbstractSharedListener {
384
+ public class NacosListener extends AbstractSharedListener {
367
385
private final String dataId ;
368
386
private final ConfigurationChangeListener listener ;
369
387
0 commit comments