1- /*
2- * Copyright 1999-2011 Alibaba Group.
3- *
4- * Licensed under the Apache License, Version 2.0 (the "License");
5- * you may not use this file except in compliance with the License.
6- * You may obtain a copy of the License at
7- *
8- * http://www.apache.org/licenses/LICENSE-2.0
9- *
10- * Unless required by applicable law or agreed to in writing, software
11- * distributed under the License is distributed on an "AS IS" BASIS,
12- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13- * See the License for the specific language governing permissions and
14- * limitations under the License.
15- */
16- package com .alibaba .dubbo .registry .support ;
17-
18- import java .util .Collection ;
19- import java .util .Collections ;
20- import java .util .Map ;
21- import java .util .concurrent .ConcurrentHashMap ;
22- import java .util .concurrent .locks .ReentrantLock ;
23-
24- import com .alibaba .dubbo .common .URL ;
25- import com .alibaba .dubbo .common .logger .Logger ;
26- import com .alibaba .dubbo .common .logger .LoggerFactory ;
27- import com .alibaba .dubbo .registry .Registry ;
28- import com .alibaba .dubbo .registry .RegistryFactory ;
29-
30- /**
31- * AbstractRegistryFactory. (SPI, Singleton, ThreadSafe)
32- *
33- * @see com.alibaba.dubbo.registry.RegistryFactory
34- * @author william.liangf
35- */
36- public abstract class AbstractRegistryFactory implements RegistryFactory {
37-
38- // 日志输出
39- private static final Logger LOGGER = LoggerFactory .getLogger (AbstractRegistryFactory .class );
40-
41- // 注册中心获取过程锁
42- private static final ReentrantLock LOCK = new ReentrantLock ();
43-
44- // 注册中心集合 Map<RegistryAddress, Registry>
45- private static final Map <String , Registry > REGISTRIES = new ConcurrentHashMap <String , Registry >();
46-
47- /**
48- * 获取所有注册中心
49- *
50- * @return 所有注册中心
51- */
52- public static Collection <Registry > getRegistries () {
53- return Collections .unmodifiableCollection (REGISTRIES .values ());
54- }
55-
56- /**
57- * 关闭所有已创建注册中心
58- */
59- public static void destroyAll () {
60- if (LOGGER .isInfoEnabled ()) {
61- LOGGER .info ("Close all registries " + getRegistries ());
62- }
63- // 锁定注册中心关闭过程
64- LOCK .lock ();
65- try {
66- for (Registry registry : getRegistries ()) {
67- try {
68- registry .destroy ();
69- } catch (Throwable e ) {
70- LOGGER .error (e .getMessage (), e );
71- }
72- }
73- REGISTRIES .clear ();
74- } finally {
75- // 释放锁
76- LOCK .unlock ();
77- }
78- }
79-
80- public Registry getRegistry (URL url ) {
81- // 锁定注册中心获取过程,保证注册中心单一实例
82- LOCK .lock ();
83- try {
84- String key = url .getProtocol () + "://" + url .getUsername () + ":" + url .getPassword () + "@" + url .getIp () + ":" + url .getPort ();
85- Registry registry = REGISTRIES .get (key );
86- if (registry != null ) {
87- return registry ;
88- }
89- registry = createRegistry (url );
90- if (registry == null ) {
91- throw new IllegalStateException ("Can not create registry " + url );
92- }
93- REGISTRIES .put (key , registry );
94- return registry ;
95- } finally {
96- // 释放锁
97- LOCK .unlock ();
98- }
99- }
100-
101- protected abstract Registry createRegistry (URL url );
102-
1+ /*
2+ * Copyright 1999-2011 Alibaba Group.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+ package com .alibaba .dubbo .registry .support ;
17+
18+ import com .alibaba .dubbo .common .Constants ;
19+ import com .alibaba .dubbo .common .URL ;
20+ import com .alibaba .dubbo .common .logger .Logger ;
21+ import com .alibaba .dubbo .common .logger .LoggerFactory ;
22+ import com .alibaba .dubbo .registry .Registry ;
23+ import com .alibaba .dubbo .registry .RegistryFactory ;
24+
25+ import java .util .Collection ;
26+ import java .util .Collections ;
27+ import java .util .Map ;
28+ import java .util .concurrent .ConcurrentHashMap ;
29+ import java .util .concurrent .locks .ReentrantLock ;
30+
31+ /**
32+ * AbstractRegistryFactory. (SPI, Singleton, ThreadSafe)
33+ *
34+ * @see com.alibaba.dubbo.registry.RegistryFactory
35+ * @author william.liangf
36+ */
37+ public abstract class AbstractRegistryFactory implements RegistryFactory {
38+
39+ // 日志输出
40+ private static final Logger LOGGER = LoggerFactory .getLogger (AbstractRegistryFactory .class );
41+
42+ // 注册中心获取过程锁
43+ private static final ReentrantLock LOCK = new ReentrantLock ();
44+
45+ // 注册中心集合 Map<RegistryAddress, Registry>
46+ private static final Map <String , Registry > REGISTRIES = new ConcurrentHashMap <String , Registry >();
47+
48+ /**
49+ * 获取所有注册中心
50+ *
51+ * @return 所有注册中心
52+ */
53+ public static Collection <Registry > getRegistries () {
54+ return Collections .unmodifiableCollection (REGISTRIES .values ());
55+ }
56+
57+ /**
58+ * 关闭所有已创建注册中心
59+ */
60+ public static void destroyAll () {
61+ if (LOGGER .isInfoEnabled ()) {
62+ LOGGER .info ("Close all registries " + getRegistries ());
63+ }
64+ // 锁定注册中心关闭过程
65+ LOCK .lock ();
66+ try {
67+ for (Registry registry : getRegistries ()) {
68+ try {
69+ registry .destroy ();
70+ } catch (Throwable e ) {
71+ LOGGER .error (e .getMessage (), e );
72+ }
73+ }
74+ REGISTRIES .clear ();
75+ } finally {
76+ // 释放锁
77+ LOCK .unlock ();
78+ }
79+ }
80+
81+ public Registry getRegistry (URL url ) {
82+ // 锁定注册中心获取过程,保证注册中心单一实例
83+ LOCK .lock ();
84+ try {
85+ String key = url .getProtocol () + "://" + url .getUsername () + ":" + url .getPassword () + "@" + url .getIp () + ":" + url .getPort ()
86+ + url .getParameter (Constants .GROUP_KEY , "" );
87+ Registry registry = REGISTRIES .get (key );
88+ if (registry != null ) {
89+ return registry ;
90+ }
91+ registry = createRegistry (url );
92+ if (registry == null ) {
93+ throw new IllegalStateException ("Can not create registry " + url );
94+ }
95+ REGISTRIES .put (key , registry );
96+ return registry ;
97+ } finally {
98+ // 释放锁
99+ LOCK .unlock ();
100+ }
101+ }
102+
103+ protected abstract Registry createRegistry (URL url );
104+
103105}
0 commit comments