Skip to content

Commit e265696

Browse files
committed
Fixed the issue of subscription failure when specifying service groups in agent service discovery
1 parent 1c3b4d7 commit e265696

3 files changed

Lines changed: 20 additions & 17 deletions

File tree

joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/registry/ServiceId.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ public ServiceId(String service, String group, boolean interfaceMode) {
6565
this(null, service, group, null, interfaceMode);
6666
}
6767

68-
public ServiceId(String namespace, String service, String group) {
69-
this(namespace, service, group, null, false);
68+
public ServiceId(String service, String group, String catalog, boolean interfaceMode) {
69+
this(null, service, group, catalog, interfaceMode);
7070
}
7171

72-
public ServiceId(String namespace, String service, String group, boolean interfaceMode) {
73-
this(namespace, service, group, null, interfaceMode);
72+
public ServiceId(String namespace, String service, String group) {
73+
this(namespace, service, group, null, false);
7474
}
7575

7676
public ServiceId(String namespace, String service, String group, String catalog, boolean interfaceMode) {
@@ -139,23 +139,26 @@ public boolean match(ServiceId target, String defaultGroup) {
139139
/**
140140
* Determines if two service IDs match according to the specified matching rules.
141141
*
142-
* @param source the source service ID to match against (may be null)
143-
* @param target the target service ID to check (may be null)
144-
* @param defaultGroup the default group name to use when source group is empty (may be null)
142+
* @param source the source service ID to match against (maybe null)
143+
* @param target the target service ID to check (maybe null)
144+
* @param defaultGroup the default group name to use when source group is empty (maybe null)
145145
* @return true if the service IDs match according to the rules, false otherwise
146146
*/
147147
public static boolean match(ServiceId source, ServiceId target, String defaultGroup) {
148-
String sourceService = source == null ? null : source.getService();
149-
String targetService = target == null ? null : target.getService();
148+
if (source == null || target == null) {
149+
return false;
150+
}
151+
String sourceService = source.getService();
152+
String targetService = target.getService();
150153
if (sourceService == null || !sourceService.equalsIgnoreCase(targetService)) {
151154
return false;
152155
}
153-
String sourceGroup = source == null ? null : source.getGroup();
154-
String targetGroup = target == null ? null : target.getGroup();
155-
if (!DEFAULT_GROUP_BIPREDICATE.test(sourceGroup, defaultGroup)) {
156-
return DEFAULT_GROUP_BIPREDICATE.test(targetGroup, defaultGroup);
156+
String sourceGroup = source.getGroup();
157+
String targetGroup = target.getGroup();
158+
if (DEFAULT_GROUP_BIPREDICATE.test(sourceGroup, defaultGroup) || DEFAULT_GROUP_BIPREDICATE.test(targetGroup, defaultGroup)) {
159+
return true;
157160
}
158-
return sourceGroup.equalsIgnoreCase(targetGroup);
161+
return sourceGroup == null ? targetGroup == null : sourceGroup.equalsIgnoreCase(targetGroup);
159162
}
160163

161164
/**
@@ -186,4 +189,4 @@ public static String getServiceName(String serviceNameWithGroup, String splitter
186189
return index < 0 || index >= parts.size() ? StringUtils.EMPTY_STRING : parts.get(index);
187190
}
188191

189-
}
192+
}

joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/registry/ServiceInstance.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public ServiceInstance(String namespace,
6262
int port,
6363
int weight,
6464
Map<String, String> metadata) {
65-
super(namespace, service, group, interfaceMode);
65+
super(namespace, service, group, null, interfaceMode);
6666
this.id = id;
6767
this.framework = framework;
6868
this.version = version;

joylive-implement/joylive-service/joylive-service-nacos/src/main/java/com/jd/live/agent/implement/service/registry/nacos/NacosRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ protected static class InstanceListener implements EventListener {
333333
public void onEvent(Event event) {
334334
if (event instanceof NamingEvent) {
335335
NamingEvent e = (NamingEvent) event;
336-
ServiceId id = new ServiceId(e.getServiceName(), e.getGroupName(), interfaceMode);
336+
ServiceId id = new ServiceId(e.getServiceName(), null, e.getGroupName(), interfaceMode);
337337
consumer.accept(new RegistryEvent(id, toList(e.getInstances(), NacosEndpoint::new), Constants.DEFAULT_GROUP));
338338
}
339339
}

0 commit comments

Comments
 (0)