4
4
import com .alibaba .nacos .api .PropertyKeyConst ;
5
5
import com .alibaba .nacos .api .exception .NacosException ;
6
6
import com .alibaba .nacos .api .naming .NamingService ;
7
+ import com .alibaba .nacos .api .naming .listener .AbstractEventListener ;
8
+ import com .alibaba .nacos .api .naming .listener .Event ;
7
9
import com .alibaba .nacos .api .naming .listener .EventListener ;
10
+ import com .alibaba .nacos .api .naming .listener .NamingEvent ;
8
11
import com .alibaba .nacos .api .naming .pojo .Instance ;
9
12
import com .alibaba .nacos .api .naming .pojo .ServiceInfo ;
10
13
import com .alibaba .nacos .api .naming .utils .NamingUtils ;
11
14
import com .alibaba .nacos .client .naming .utils .UtilAndComs ;
12
15
import lombok .extern .slf4j .Slf4j ;
13
16
import org .apache .commons .lang3 .StringUtils ;
14
- import org .apache .eventmesh .common .config .CommonConfiguration ;
15
17
import org .apache .eventmesh .common .config .ConfigService ;
16
- import org .apache .eventmesh .common . utils . ConfigurationContextUtil ;
18
+ import org .apache .eventmesh .registry . NotifyEvent ;
17
19
import org .apache .eventmesh .registry .QueryInstances ;
18
20
import org .apache .eventmesh .registry .RegisterServerInfo ;
19
21
import org .apache .eventmesh .registry .RegistryListener ;
28
30
import java .util .Objects ;
29
31
import java .util .Optional ;
30
32
import java .util .Properties ;
33
+ import java .util .concurrent .Executor ;
34
+ import java .util .concurrent .LinkedBlockingQueue ;
35
+ import java .util .concurrent .ThreadPoolExecutor ;
36
+ import java .util .concurrent .TimeUnit ;
31
37
import java .util .concurrent .atomic .AtomicBoolean ;
32
38
import java .util .concurrent .locks .Lock ;
33
39
import java .util .concurrent .locks .ReentrantLock ;
34
40
import java .util .stream .Collectors ;
35
41
36
42
@ Slf4j
37
43
public class NacosDiscoveryService implements RegistryService {
38
- private final AtomicBoolean initFlag = new AtomicBoolean (false );
39
44
40
- private CommonConfiguration configuration ;
45
+ private final AtomicBoolean initFlag = new AtomicBoolean ( false ) ;
41
46
42
47
private NacosRegistryConfiguration nacosConf ;
43
48
44
49
private NamingService namingService ;
45
50
46
51
private final Map <String , Map <RegistryListener , EventListener >> listeners = new HashMap <>();
47
52
53
+ private static final Executor notifyExecutor = new ThreadPoolExecutor (1 , 1 , 60L , TimeUnit .SECONDS ,
54
+ new LinkedBlockingQueue <>(20 ), r -> {
55
+ Thread t = new Thread (r );
56
+ t .setName ("org.apache.eventmesh.registry.nacos.executor" );
57
+ t .setDaemon (true );
58
+ return t ;
59
+ }, new ThreadPoolExecutor .DiscardOldestPolicy ()
60
+ );
61
+
48
62
private final Lock lock = new ReentrantLock ();
49
- private static final String GROUP_NAME = "admin" ;
63
+
50
64
51
65
@ Override
52
66
public void init () throws RegistryException {
53
67
if (!initFlag .compareAndSet (false , true )) {
54
68
return ;
55
69
}
56
- configuration = ConfigurationContextUtil .get (RegistryService .ConfigurationKey );
57
- if (configuration == null ) {
58
- throw new RegistryException ("registry config instance is null" );
59
- }
60
70
nacosConf = ConfigService .getInstance ().buildConfigInstance (NacosRegistryConfiguration .class );
61
71
if (nacosConf == null ) {
62
72
log .info ("nacos registry configuration is null" );
@@ -73,12 +83,13 @@ public void init() throws RegistryException {
73
83
74
84
private Properties buildProperties () {
75
85
Properties properties = new Properties ();
76
- properties .setProperty (PropertyKeyConst .SERVER_ADDR , configuration .getRegistryAddr ());
77
- properties .setProperty (PropertyKeyConst .USERNAME , configuration .getEventMeshRegistryPluginUsername ());
78
- properties .setProperty (PropertyKeyConst .PASSWORD , configuration .getEventMeshRegistryPluginPassword ());
79
86
if (nacosConf == null ) {
80
87
return properties ;
81
88
}
89
+ properties .setProperty (PropertyKeyConst .SERVER_ADDR , nacosConf .getRegistryAddr ());
90
+ properties .setProperty (PropertyKeyConst .USERNAME , nacosConf .getEventMeshRegistryPluginUsername ());
91
+ properties .setProperty (PropertyKeyConst .PASSWORD , nacosConf .getEventMeshRegistryPluginPassword ());
92
+
82
93
String endpoint = nacosConf .getEndpoint ();
83
94
if (Objects .nonNull (endpoint ) && endpoint .contains (":" )) {
84
95
int index = endpoint .indexOf (":" );
@@ -87,7 +98,8 @@ private Properties buildProperties() {
87
98
} else {
88
99
Optional .ofNullable (endpoint ).ifPresent (value -> properties .put (PropertyKeyConst .ENDPOINT , endpoint ));
89
100
String endpointPort = nacosConf .getEndpointPort ();
90
- Optional .ofNullable (endpointPort ).ifPresent (value -> properties .put (PropertyKeyConst .ENDPOINT_PORT , endpointPort ));
101
+ Optional .ofNullable (endpointPort ).ifPresent (value -> properties .put (PropertyKeyConst .ENDPOINT_PORT ,
102
+ endpointPort ));
91
103
}
92
104
String accessKey = nacosConf .getAccessKey ();
93
105
Optional .ofNullable (accessKey ).ifPresent (value -> properties .put (PropertyKeyConst .ACCESS_KEY , accessKey ));
@@ -96,7 +108,8 @@ private Properties buildProperties() {
96
108
String clusterName = nacosConf .getClusterName ();
97
109
Optional .ofNullable (clusterName ).ifPresent (value -> properties .put (PropertyKeyConst .CLUSTER_NAME , clusterName ));
98
110
String logFileName = nacosConf .getLogFileName ();
99
- Optional .ofNullable (logFileName ).ifPresent (value -> properties .put (UtilAndComs .NACOS_NAMING_LOG_NAME , logFileName ));
111
+ Optional .ofNullable (logFileName ).ifPresent (value -> properties .put (UtilAndComs .NACOS_NAMING_LOG_NAME ,
112
+ logFileName ));
100
113
String logLevel = nacosConf .getLogLevel ();
101
114
Optional .ofNullable (logLevel ).ifPresent (value -> properties .put (UtilAndComs .NACOS_NAMING_LOG_LEVEL , logLevel ));
102
115
Integer pollingThreadCount = nacosConf .getPollingThreadCount ();
@@ -122,19 +135,54 @@ public void subscribe(RegistryListener listener, String serviceName) {
122
135
lock .lock ();
123
136
try {
124
137
ServiceInfo serviceInfo = ServiceInfo .fromKey (serviceName );
125
- Map <RegistryListener , EventListener > eventListenerMap = listeners .computeIfAbsent (serviceName , k -> new HashMap <>());
138
+ Map <RegistryListener , EventListener > eventListenerMap = listeners .computeIfAbsent (serviceName ,
139
+ k -> new HashMap <>());
126
140
if (eventListenerMap .containsKey (listener )) {
127
- log .warn ("already use same listener subscribe service name {}" , serviceName );
141
+ log .warn ("already use same listener subscribe service name {}" , serviceName );
128
142
return ;
129
143
}
130
- EventListener eventListener = listener ::onChange ;
131
- List <String > clusters ;
144
+ EventListener eventListener = new AbstractEventListener () {
145
+ @ Override
146
+ public Executor getExecutor () {
147
+ return notifyExecutor ;
148
+ }
149
+
150
+ @ Override
151
+ public void onEvent (Event event ) {
152
+ if (!(event instanceof NamingEvent )) {
153
+ log .warn ("received notify event type isn't not as expected" );
154
+ return ;
155
+ }
156
+ try {
157
+ NamingEvent namingEvent = (NamingEvent ) event ;
158
+ List <Instance > instances = namingEvent .getInstances ();
159
+ List <RegisterServerInfo > list = new ArrayList <>();
160
+ if (instances != null ) {
161
+ for (Instance instance : instances ) {
162
+ RegisterServerInfo info = new RegisterServerInfo ();
163
+ info .setAddress (instance .getIp () + ":" + instance .getPort ());
164
+ info .setMetadata (instance .getMetadata ());
165
+ info .setHealth (instance .isHealthy ());
166
+ info .setServiceName (
167
+ ServiceInfo .getKey (NamingUtils .getGroupedName (namingEvent .getServiceName (),
168
+ namingEvent .getGroupName ()),
169
+ namingEvent .getClusters ()));
170
+ list .add (info );
171
+ }
172
+ }
173
+ listener .onChange (new NotifyEvent (list ));
174
+ } catch (Exception e ) {
175
+ log .warn ("" );
176
+ }
177
+ }
178
+ };
179
+ List <String > clusters ;
132
180
if (serviceInfo .getClusters () == null || serviceInfo .getClusters ().isEmpty ()) {
133
181
clusters = new ArrayList <>();
134
182
} else {
135
183
clusters = Arrays .stream (serviceInfo .getClusters ().split ("," )).collect (Collectors .toList ());
136
184
}
137
- namingService .subscribe (serviceInfo .getName (),serviceInfo .getGroupName (), clusters , eventListener );
185
+ namingService .subscribe (serviceInfo .getName (), serviceInfo .getGroupName (), clusters , eventListener );
138
186
eventListenerMap .put (listener , eventListener );
139
187
} catch (Exception e ) {
140
188
log .error ("subscribe service name {} fail" , serviceName , e );
@@ -152,7 +200,7 @@ public void unsubscribe(RegistryListener registryListener, String serviceName) {
152
200
if (map == null ) {
153
201
return ;
154
202
}
155
- List <String > clusters ;
203
+ List <String > clusters ;
156
204
if (serviceInfo .getClusters () == null || serviceInfo .getClusters ().isEmpty ()) {
157
205
clusters = new ArrayList <>();
158
206
} else {
@@ -177,14 +225,18 @@ public List<RegisterServerInfo> selectInstances(QueryInstances queryInstances) {
177
225
if (StringUtils .isNotBlank (serviceInfo .getClusters ())) {
178
226
clusters .addAll (Arrays .asList (serviceInfo .getClusters ().split ("," )));
179
227
}
180
- List <Instance > instances = namingService .selectInstances (serviceInfo .getName (), serviceInfo .getGroupName (), clusters , queryInstances .isHealth ());
228
+ List <Instance > instances = namingService .selectInstances (serviceInfo .getName (),
229
+ serviceInfo .getGroupName (), clusters ,
230
+ queryInstances .isHealth ());
181
231
if (instances != null ) {
182
232
instances .forEach (x -> {
183
233
RegisterServerInfo instanceInfo = new RegisterServerInfo ();
184
234
instanceInfo .setMetadata (x .getMetadata ());
185
235
instanceInfo .setHealth (x .isHealthy ());
186
236
instanceInfo .setAddress (x .getIp () + ":" + x .getPort ());
187
- instanceInfo .setServiceName (ServiceInfo .getKey (NamingUtils .getGroupedName (x .getServiceName (), serviceInfo .getGroupName ()), x .getClusterName ()));
237
+ instanceInfo .setServiceName (
238
+ ServiceInfo .getKey (NamingUtils .getGroupedName (x .getServiceName (),
239
+ serviceInfo .getGroupName ()), x .getClusterName ()));
188
240
list .add (instanceInfo );
189
241
});
190
242
}
@@ -228,7 +280,9 @@ public boolean unRegister(RegisterServerInfo eventMeshRegisterInfo) throws Regis
228
280
return false ;
229
281
}
230
282
ServiceInfo serviceInfo = ServiceInfo .fromKey (eventMeshRegisterInfo .getServiceName ());
231
- namingService .deregisterInstance (serviceInfo .getName (), serviceInfo .getGroupName (), ipPort [0 ], Integer .parseInt (ipPort [1 ]), serviceInfo .getClusters ());
283
+ namingService .deregisterInstance (serviceInfo .getName (), serviceInfo .getGroupName (), ipPort [0 ],
284
+ Integer .parseInt (ipPort [1 ]),
285
+ serviceInfo .getClusters ());
232
286
return true ;
233
287
} catch (Exception e ) {
234
288
log .error ("unregister instance service {} fail" , eventMeshRegisterInfo , e );
0 commit comments