This is an automated email from the ASF dual-hosted git repository.
Aias00 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 381334d390 fix (registry-apollo) : refresh modified instance cache
entries. (#7276)
381334d390 is described below
commit 381334d390f0ecfb99586fb5a611fc6e065ed4c0
Author: JerryčAI <[email protected]>
AuthorDate: Sat Sep 26 12:59:58 2026 +0800
fix (registry-apollo) : refresh modified instance cache entries. (#7276)
Co-authored-by: aias00 <[email protected]>
---
.../apollo/ApolloInstanceRegisterRepository.java | 3 +-
.../ApolloInstanceRegisterRepositoryTest.java | 109 +++++++++++++++++++++
2 files changed, 111 insertions(+), 1 deletion(-)
diff --git
a/shenyu-registry/shenyu-registry-apollo/src/main/java/org/apache/shenyu/registry/apollo/ApolloInstanceRegisterRepository.java
b/shenyu-registry/shenyu-registry-apollo/src/main/java/org/apache/shenyu/registry/apollo/ApolloInstanceRegisterRepository.java
index 730bb6f0b0..94e8c22444 100644
---
a/shenyu-registry/shenyu-registry-apollo/src/main/java/org/apache/shenyu/registry/apollo/ApolloInstanceRegisterRepository.java
+++
b/shenyu-registry/shenyu-registry-apollo/src/main/java/org/apache/shenyu/registry/apollo/ApolloInstanceRegisterRepository.java
@@ -145,8 +145,9 @@ public class ApolloInstanceRegisterRepository implements
ShenyuInstanceRegisterR
if (key.startsWith(watchKey)) {
switch (changeEvent.getChange(key).getChangeType()) {
case ADDED:
+ case MODIFIED:
childrenList.put(key,
changeEvent.getChange(key).getNewValue());
- LOGGER.info("apollo instance register add {}",
changeEvent.getChange(key).getNewValue());
+ LOGGER.info("apollo instance register add or
update {}", changeEvent.getChange(key).getNewValue());
break;
case DELETED:
childrenList.remove(key);
diff --git
a/shenyu-registry/shenyu-registry-apollo/src/test/java/org/apache/shenyu/registry/apollo/ApolloInstanceRegisterRepositoryTest.java
b/shenyu-registry/shenyu-registry-apollo/src/test/java/org/apache/shenyu/registry/apollo/ApolloInstanceRegisterRepositoryTest.java
new file mode 100644
index 0000000000..bc31e1c60c
--- /dev/null
+++
b/shenyu-registry/shenyu-registry-apollo/src/test/java/org/apache/shenyu/registry/apollo/ApolloInstanceRegisterRepositoryTest.java
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.registry.apollo;
+
+import com.ctrip.framework.apollo.Config;
+import com.ctrip.framework.apollo.ConfigChangeListener;
+import com.ctrip.framework.apollo.enums.PropertyChangeType;
+import com.ctrip.framework.apollo.model.ConfigChange;
+import com.ctrip.framework.apollo.model.ConfigChangeEvent;
+import org.apache.shenyu.common.utils.GsonUtils;
+import org.apache.shenyu.registry.api.entity.InstanceEntity;
+import org.apache.shenyu.registry.api.path.InstancePathConstants;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+import org.springframework.test.util.ReflectionTestUtils;
+
+import java.net.URI;
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public final class ApolloInstanceRegisterRepositoryTest {
+
+ private ApolloInstanceRegisterRepository repository;
+
+ private ConfigChangeListener listener;
+
+ private String instanceKey;
+
+ private String originalValue;
+
+ @BeforeEach
+ public void setUp() {
+ repository = new ApolloInstanceRegisterRepository();
+ Config config = mock(Config.class);
+ ApolloClient client = mock(ApolloClient.class);
+ ReflectionTestUtils.setField(repository, "configService", config);
+ ReflectionTestUtils.setField(repository, "apolloClient", client);
+ instanceKey =
InstancePathConstants.buildRealNode(InstancePathConstants.buildInstanceParentPath("service"),
"instance");
+ originalValue = instanceJson("127.0.0.1", 8080);
+
when(config.getPropertyNames()).thenReturn(Collections.singleton(instanceKey));
+ when(client.getItemValue(instanceKey)).thenReturn(originalValue);
+ assertEquals("127.0.0.1",
repository.selectInstances("service").get(0).getHost());
+ ArgumentCaptor<ConfigChangeListener> captor =
ArgumentCaptor.forClass(ConfigChangeListener.class);
+ verify(config).addChangeListener(captor.capture());
+ listener = captor.getValue();
+ }
+
+ @Test
+ public void testModifiedInstanceRefreshesCache() {
+ publishChange(instanceKey, originalValue, instanceJson("127.0.0.2",
9090), PropertyChangeType.MODIFIED);
+ List<InstanceEntity> instances = repository.selectInstances("service");
+ assertEquals(1, instances.size());
+ assertEquals("127.0.0.2", instances.get(0).getHost());
+ assertEquals(9090, instances.get(0).getPort());
+ assertEquals(URI.create("http://127.0.0.2:9090"),
instances.get(0).getUri());
+ }
+
+ @Test
+ public void testAddedAndDeletedInstancesRefreshCache() {
+ String addedKey = instanceKey + "-second";
+ String addedValue = instanceJson("127.0.0.2", 9090);
+ publishChange(addedKey, null, addedValue, PropertyChangeType.ADDED);
+ assertEquals(2, repository.selectInstances("service").size());
+ publishChange(instanceKey, originalValue, null,
PropertyChangeType.DELETED);
+ assertEquals("127.0.0.2",
repository.selectInstances("service").get(0).getHost());
+ publishChange(addedKey, addedValue, null, PropertyChangeType.DELETED);
+ assertTrue(repository.selectInstances("service").isEmpty());
+ }
+
+ @Test
+ public void testUnrelatedServiceChangeDoesNotAffectCache() {
+ String unrelatedKey =
InstancePathConstants.buildRealNode(InstancePathConstants.buildInstanceParentPath("another-service"),
"instance");
+ publishChange(unrelatedKey, originalValue, instanceJson("127.0.0.2",
9090), PropertyChangeType.MODIFIED);
+ List<InstanceEntity> instances = repository.selectInstances("service");
+ assertEquals(1, instances.size());
+ assertEquals("127.0.0.1", instances.get(0).getHost());
+ }
+
+ private void publishChange(final String key, final String oldValue, final
String newValue, final PropertyChangeType type) {
+ ConfigChange change = new ConfigChange("application", key, oldValue,
newValue, type);
+ listener.onChange(new ConfigChangeEvent("application",
Collections.singletonMap(key, change)));
+ }
+
+ private String instanceJson(final String host, final int port) {
+ return
GsonUtils.getInstance().toJson(InstanceEntity.builder().appName("service").host(host).port(port).build());
+ }
+}