ClusterUtils { public static URL mergeUrl(URL remoteUrl, Map<String, String> localMap) { Map<String, String> map = new HashMap<String, String>(); Map<String, String> remoteMap = remoteUrl.getParameters(); if (remoteMap != null && remoteMap.size() > 0) { map.putAll(remoteMap); map.remove(THREAD_NAME_KEY); map.remove(DEFAULT_KEY_PREFIX + THREAD_NAME_KEY); map.remove(THREADPOOL_KEY); map.remove(DEFAULT_KEY_PREFIX + THREADPOOL_KEY); map.remove(CORE_THREADS_KEY); map.remove(DEFAULT_KEY_PREFIX + CORE_THREADS_KEY); map.remove(THREADS_KEY); map.remove(DEFAULT_KEY_PREFIX + THREADS_KEY); map.remove(QUEUES_KEY); map.remove(DEFAULT_KEY_PREFIX + QUEUES_KEY); map.remove(ALIVE_KEY); map.remove(DEFAULT_KEY_PREFIX + ALIVE_KEY); map.remove(Constants.TRANSPORTER_KEY); map.remove(DEFAULT_KEY_PREFIX + Constants.TRANSPORTER_KEY); } if (localMap != null && localMap.size() > 0) { Map<String, String> copyOfLocalMap = new HashMap<>(localMap); if(map.containsKey(GROUP_KEY)){ copyOfLocalMap.remove(GROUP_KEY); } if(map.containsKey(VERSION_KEY)){ copyOfLocalMap.remove(VERSION_KEY); } copyOfLocalMap.remove(RELEASE_KEY); copyOfLocalMap.remove(DUBBO_VERSION_KEY); copyOfLocalMap.remove(METHODS_KEY); copyOfLocalMap.remove(TIMESTAMP_KEY); copyOfLocalMap.remove(TAG_KEY); map.putAll(copyOfLocalMap); map.put(REMOTE_APPLICATION_KEY, remoteMap.get(APPLICATION_KEY)); String remoteFilter = remoteMap.get(REFERENCE_FILTER_KEY); String localFilter = copyOfLocalMap.get(REFERENCE_FILTER_KEY); if (remoteFilter != null && remoteFilter.length() > 0 && localFilter != null && localFilter.length() > 0) { map.put(REFERENCE_FILTER_KEY, remoteFilter + "," + localFilter); } String remoteListener = remoteMap.get(INVOKER_LISTENER_KEY); String localListener = copyOfLocalMap.get(INVOKER_LISTENER_KEY); if (remoteListener != null && remoteListener.length() > 0 && localListener != null && localListener.length() > 0) { map.put(INVOKER_LISTENER_KEY, remoteListener + "," + localListener); } } return remoteUrl.clearParameters().addParameters(map); } private  ClusterUtils(); static URL mergeUrl(URL remoteUrl, Map<String, String> localMap); static URL mergeProviderUrl(URL remoteUrl, Map<String, String> localMap);  }
@Test public void testMergeUrl() throws Exception { URL providerURL = URL.valueOf("dubbo: providerURL = providerURL.setPath("path") .setUsername("username") .setPassword("password"); providerURL = URLBuilder.from(providerURL) .addParameter(GROUP_KEY, "dubbo") .addParameter(VERSION_KEY, "1.2.3") .addParameter(DUBBO_VERSION_KEY, "2.3.7") .addParameter(THREADPOOL_KEY, "fixed") .addParameter(THREADS_KEY, Integer.MAX_VALUE) .addParameter(THREAD_NAME_KEY, "test") .addParameter(CORE_THREADS_KEY, Integer.MAX_VALUE) .addParameter(QUEUES_KEY, Integer.MAX_VALUE) .addParameter(ALIVE_KEY, Integer.MAX_VALUE) .addParameter(DEFAULT_KEY_PREFIX + THREADS_KEY, Integer.MAX_VALUE) .addParameter(DEFAULT_KEY_PREFIX + THREADPOOL_KEY, "fixed") .addParameter(DEFAULT_KEY_PREFIX + CORE_THREADS_KEY, Integer.MAX_VALUE) .addParameter(DEFAULT_KEY_PREFIX + QUEUES_KEY, Integer.MAX_VALUE) .addParameter(DEFAULT_KEY_PREFIX + ALIVE_KEY, Integer.MAX_VALUE) .addParameter(DEFAULT_KEY_PREFIX + THREAD_NAME_KEY, "test") .addParameter(APPLICATION_KEY, "provider") .addParameter(REFERENCE_FILTER_KEY, "filter1,filter2") .build(); URL consumerURL = new URLBuilder(DUBBO_PROTOCOL, "localhost", 55555) .addParameter(PID_KEY, "1234") .addParameter(THREADPOOL_KEY, "foo") .addParameter(APPLICATION_KEY, "consumer") .addParameter(REFERENCE_FILTER_KEY, "filter3") .build(); URL url = ClusterUtils.mergeUrl(providerURL, consumerURL.getParameters()); Assertions.assertFalse(url.hasParameter(THREADS_KEY)); Assertions.assertFalse(url.hasParameter(DEFAULT_KEY_PREFIX + THREADS_KEY)); Assertions.assertFalse(url.hasParameter(DEFAULT_KEY_PREFIX + THREADPOOL_KEY)); Assertions.assertFalse(url.hasParameter(CORE_THREADS_KEY)); Assertions.assertFalse(url.hasParameter(DEFAULT_KEY_PREFIX + CORE_THREADS_KEY)); Assertions.assertFalse(url.hasParameter(QUEUES_KEY)); Assertions.assertFalse(url.hasParameter(DEFAULT_KEY_PREFIX + QUEUES_KEY)); Assertions.assertFalse(url.hasParameter(ALIVE_KEY)); Assertions.assertFalse(url.hasParameter(DEFAULT_KEY_PREFIX + ALIVE_KEY)); Assertions.assertFalse(url.hasParameter(THREAD_NAME_KEY)); Assertions.assertFalse(url.hasParameter(DEFAULT_KEY_PREFIX + THREAD_NAME_KEY)); Assertions.assertEquals(url.getPath(), "path"); Assertions.assertEquals(url.getUsername(), "username"); Assertions.assertEquals(url.getPassword(), "password"); Assertions.assertEquals(url.getParameter(PID_KEY), "1234"); Assertions.assertEquals(url.getParameter(THREADPOOL_KEY), "foo"); Assertions.assertEquals(url.getParameter(APPLICATION_KEY), "consumer"); Assertions.assertEquals(url.getParameter(REMOTE_APPLICATION_KEY), "provider"); Assertions.assertEquals(url.getParameter(REFERENCE_FILTER_KEY), "filter1,filter2,filter3"); }
@Test public void testUseProviderParams() { URL localURL = URL.valueOf("dubbo: "&methods=local&tag=local&timestamp=local"); URL remoteURL = URL.valueOf("dubbo: "&methods=remote&tag=remote&timestamp=remote"); URL mergedUrl = ClusterUtils.mergeUrl(remoteURL, localURL.getParameters()); Assertions.assertEquals(remoteURL.getParameter(VERSION_KEY), mergedUrl.getParameter(VERSION_KEY)); Assertions.assertEquals(remoteURL.getParameter(GROUP_KEY), mergedUrl.getParameter(GROUP_KEY)); Assertions.assertEquals(remoteURL.getParameter(DUBBO_VERSION_KEY), mergedUrl.getParameter(DUBBO_VERSION_KEY)); Assertions.assertEquals(remoteURL.getParameter(RELEASE_KEY), mergedUrl.getParameter(RELEASE_KEY)); Assertions.assertEquals(remoteURL.getParameter(METHODS_KEY), mergedUrl.getParameter(METHODS_KEY)); Assertions.assertEquals(remoteURL.getParameter(TIMESTAMP_KEY), mergedUrl.getParameter(TIMESTAMP_KEY)); Assertions.assertEquals(remoteURL.getParameter(TAG_KEY), mergedUrl.getParameter(TAG_KEY)); localURL = URL.valueOf("dubbo: "&methods=local&tag=local&timestamp=local"); remoteURL = URL.valueOf("dubbo: mergedUrl = ClusterUtils.mergeUrl(remoteURL, localURL.getParameters()); Assertions.assertEquals(mergedUrl.getParameter(VERSION_KEY),localURL.getParameter(VERSION_KEY)); Assertions.assertEquals(mergedUrl.getParameter(GROUP_KEY),localURL.getParameter(GROUP_KEY)); Assertions.assertNull(mergedUrl.getParameter(DUBBO_VERSION_KEY)); Assertions.assertNull(mergedUrl.getParameter(RELEASE_KEY)); Assertions.assertNull(mergedUrl.getParameter(METHODS_KEY)); Assertions.assertNull(mergedUrl.getParameter(TIMESTAMP_KEY)); Assertions.assertNull(mergedUrl.getParameter(TAG_KEY)); localURL = URL.valueOf("dubbo: "&methods=local&tag=local&timestamp=local"); remoteURL = URL.valueOf("dubbo: mergedUrl = ClusterUtils.mergeUrl(remoteURL, localURL.getParameters()); Assertions.assertEquals(mergedUrl.getParameter(VERSION_KEY),localURL.getParameter(VERSION_KEY)); Assertions.assertEquals(mergedUrl.getParameter(GROUP_KEY),localURL.getParameter(GROUP_KEY)); Assertions.assertNull(mergedUrl.getParameter(DUBBO_VERSION_KEY)); Assertions.assertNull(mergedUrl.getParameter(RELEASE_KEY)); Assertions.assertNull(mergedUrl.getParameter(METHODS_KEY)); Assertions.assertNull(mergedUrl.getParameter(TIMESTAMP_KEY)); Assertions.assertNull(mergedUrl.getParameter(TAG_KEY)); localURL = URL.valueOf("dubbo: remoteURL = URL.valueOf("dubbo: mergedUrl = ClusterUtils.mergeUrl(remoteURL, localURL.getParameters()); Assertions.assertEquals(localURL.getParameter(CLUSTER_KEY), mergedUrl.getParameter(CLUSTER_KEY)); Assertions.assertEquals(localURL.getParameter(TIMEOUT_KEY), mergedUrl.getParameter(TIMEOUT_KEY)); Assertions.assertEquals(localURL.getParameter(LOADBALANCE_KEY), mergedUrl.getParameter(LOADBALANCE_KEY)); }