diff --git a/core/src/main/java/com/ctrip/xpipe/api/migration/auto/MonitorService.java b/core/src/main/java/com/ctrip/xpipe/api/migration/auto/MonitorService.java index fe1cf745c..e6e7c6d23 100644 --- a/core/src/main/java/com/ctrip/xpipe/api/migration/auto/MonitorService.java +++ b/core/src/main/java/com/ctrip/xpipe/api/migration/auto/MonitorService.java @@ -21,6 +21,8 @@ public interface MonitorService { void setWeight(int weight); + void updateHost(String host); + Set fetchAllClusters(String system); void registerCluster(String system, String clusterName, Set groups); diff --git a/core/src/main/java/com/ctrip/xpipe/migration/auto/DefaultMonitorService.java b/core/src/main/java/com/ctrip/xpipe/migration/auto/DefaultMonitorService.java index ce8ecdc4b..117b7357a 100644 --- a/core/src/main/java/com/ctrip/xpipe/migration/auto/DefaultMonitorService.java +++ b/core/src/main/java/com/ctrip/xpipe/migration/auto/DefaultMonitorService.java @@ -42,6 +42,11 @@ public void setWeight(int weight) { this.weight = weight; } + @Override + public void updateHost(String host) { + // do nothing + } + @Override public Set fetchAllClusters(String system) { return Collections.emptySet(); diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/migration/auto/DefaultMonitorClusterManager.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/migration/auto/DefaultMonitorClusterManager.java index 9c1a3524a..9cb16d740 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/migration/auto/DefaultMonitorClusterManager.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/migration/auto/DefaultMonitorClusterManager.java @@ -202,4 +202,9 @@ public void startSyncRingTaskNow() { this.executor.schedule(new ClustersRingSyncTask(), 5, TimeUnit.MILLISECONDS); } + @VisibleForTesting + public SortedMap getRing() { + return ring; + } + } diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/migration/auto/DefaultMonitorManager.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/migration/auto/DefaultMonitorManager.java index d3ae26057..f6a62ee66 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/migration/auto/DefaultMonitorManager.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/migration/auto/DefaultMonitorManager.java @@ -118,6 +118,7 @@ private void init() { monitorClusterManager.removeService(service); } else if (!newClusterRoute.equals(oldClusterRoute)) { monitorClusterManager.updateServiceWeight(service, newClusterRoute.getWeight()); + nameServiceMap.get(name).updateHost(newClusterRoute.getHost()); } }); newHostRouteMap.forEach((name, newClusterRoute) -> { diff --git a/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/beacon/DefaultMonitorClusterManagerTest.java b/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/beacon/DefaultMonitorClusterManagerTest.java index 84fddff7d..42ca3b8d8 100644 --- a/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/beacon/DefaultMonitorClusterManagerTest.java +++ b/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/beacon/DefaultMonitorClusterManagerTest.java @@ -15,13 +15,7 @@ import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; -import java.util.Arrays; -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; @RunWith(MockitoJUnitRunner.class) public class DefaultMonitorClusterManagerTest { @@ -135,4 +129,26 @@ public void testUpdateServiceWeight() throws Exception { } + @Test + public void testUpdateServiceHost() throws Exception { + DefaultMonitorClusterManager monitorClusterManager1 = new DefaultMonitorClusterManager(metaCache, 10, + new ArrayList<>(), 1L); + monitorClusterManager1.addService(monitorService1); + monitorClusterManager1.addService(monitorService2); + + DefaultMonitorClusterManager monitorClusterManager2 = new DefaultMonitorClusterManager(metaCache, 10, + new ArrayList<>(), 1L); + monitorClusterManager2.addService(monitorService1); + monitorClusterManager2.addService(monitorService2); + monitorClusterManager2.removeService(monitorService1); + monitorClusterManager2.addService(monitorService1); + for (String cluster : clusters) { + Assert.assertEquals(monitorClusterManager1.getService(cluster), monitorClusterManager2.getService(cluster)); + } + SortedMap ring1 = monitorClusterManager1.getRing(); + SortedMap ring2 = monitorClusterManager2.getRing(); + Assert.assertEquals(ring1, ring2); + Assert.assertEquals(ring1.hashCode(), ring2.hashCode()); + } + } diff --git a/services/ctrip-service/src/main/java/com/ctrip/xpipe/service/beacon/BeaconService.java b/services/ctrip-service/src/main/java/com/ctrip/xpipe/service/beacon/BeaconService.java index d8ae79495..55d8878f6 100644 --- a/services/ctrip-service/src/main/java/com/ctrip/xpipe/service/beacon/BeaconService.java +++ b/services/ctrip-service/src/main/java/com/ctrip/xpipe/service/beacon/BeaconService.java @@ -26,8 +26,8 @@ public class BeaconService implements MonitorService { protected static final String PATH_GET_CLUSTERS = "/api/v1/monitor/{system}/clusters"; protected static final String PATH_CLUSTER = "/api/v1/monitor/{system}/cluster/{cluster}"; - private final String getAllClustersPath; - private final String clusterPath; + private String getAllClustersPath; + private String clusterPath; private String name; private String host; @@ -66,6 +66,13 @@ public void setWeight(int weight) { this.weight = weight; } + @Override + public void updateHost(String host) { + this.host = host; + getAllClustersPath = host + PATH_GET_CLUSTERS; + clusterPath = host + PATH_CLUSTER; + } + @Override public Set fetchAllClusters(String system) { ResponseEntity>> responseEntity = restTemplate.exchange(getAllClustersPath, HttpMethod.GET, null, clustersRespTypeDef, system);