Skip to content

Commit 175eab1

Browse files
beiwei30CrazyHZM
authored andcommitted
start to use ClusterConstants, MonitorConstants (#4045)
1 parent 7de8abb commit 175eab1

File tree

45 files changed

+305
-338
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+305
-338
lines changed

dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/Configurator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717
package org.apache.dubbo.rpc.cluster;
1818

19-
import org.apache.dubbo.common.Constants;
2019
import org.apache.dubbo.common.URL;
2120
import org.apache.dubbo.common.extension.ExtensionLoader;
2221
import org.apache.dubbo.common.utils.CollectionUtils;
@@ -28,6 +27,7 @@
2827
import java.util.Map;
2928
import java.util.Optional;
3029

30+
import static org.apache.dubbo.common.constants.ClusterConstants.PRIORITY_KEY;
3131
import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_KEY;
3232
import static org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL;
3333

@@ -110,8 +110,8 @@ default int compareTo(Configurator o) {
110110
int ipCompare = getUrl().getHost().compareTo(o.getUrl().getHost());
111111
// host is the same, sort by priority
112112
if (ipCompare == 0) {
113-
int i = getUrl().getParameter(Constants.PRIORITY_KEY, 0);
114-
int j = o.getUrl().getParameter(Constants.PRIORITY_KEY, 0);
113+
int i = getUrl().getParameter(PRIORITY_KEY, 0);
114+
int j = o.getUrl().getParameter(PRIORITY_KEY, 0);
115115
return Integer.compare(i, j);
116116
} else {
117117
return ipCompare;

dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/configurator/AbstractConfigurator.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717
package org.apache.dubbo.rpc.cluster.configurator;
1818

19-
import org.apache.dubbo.common.Constants;
2019
import org.apache.dubbo.common.URL;
2120
import org.apache.dubbo.common.constants.RemotingConstants;
2221
import org.apache.dubbo.common.utils.NetUtils;
@@ -27,6 +26,8 @@
2726
import java.util.Map;
2827
import java.util.Set;
2928

29+
import static org.apache.dubbo.common.constants.ClusterConstants.CONFIG_VERSION_KEY;
30+
import static org.apache.dubbo.common.constants.ClusterConstants.OVERRIDE_PROVIDERS_KEY;
3031
import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_VALUE;
3132
import static org.apache.dubbo.common.constants.CommonConstants.ANY_VALUE;
3233
import static org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY;
@@ -67,7 +68,7 @@ public URL configure(URL url) {
6768
/**
6869
* This if branch is created since 2.7.0.
6970
*/
70-
String apiVersion = configuratorUrl.getParameter(Constants.CONFIG_VERSION_KEY);
71+
String apiVersion = configuratorUrl.getParameter(CONFIG_VERSION_KEY);
7172
if (StringUtils.isNotEmpty(apiVersion)) {
7273
String currentSide = url.getParameter(SIDE_KEY);
7374
String configuratorSide = configuratorUrl.getParameter(SIDE_KEY);
@@ -108,7 +109,7 @@ private URL configureDeprecated(URL url) {
108109
private URL configureIfMatch(String host, URL url) {
109110
if (ANYHOST_VALUE.equals(configuratorUrl.getHost()) || host.equals(configuratorUrl.getHost())) {
110111
// TODO, to support wildcards
111-
String providers = configuratorUrl.getParameter(Constants.OVERRIDE_PROVIDERS_KEY);
112+
String providers = configuratorUrl.getParameter(OVERRIDE_PROVIDERS_KEY);
112113
if (StringUtils.isEmpty(providers) || providers.contains(url.getAddress()) || providers.contains(ANYHOST_VALUE)) {
113114
String configApplication = configuratorUrl.getParameter(APPLICATION_KEY,
114115
configuratorUrl.getUsername());
@@ -124,7 +125,7 @@ private URL configureIfMatch(String host, URL url) {
124125
conditionKeys.add(VERSION_KEY);
125126
conditionKeys.add(APPLICATION_KEY);
126127
conditionKeys.add(SIDE_KEY);
127-
conditionKeys.add(Constants.CONFIG_VERSION_KEY);
128+
conditionKeys.add(CONFIG_VERSION_KEY);
128129
for (Map.Entry<String, String> entry : configuratorUrl.getParameters().entrySet()) {
129130
String key = entry.getKey();
130131
String value = entry.getValue();

dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/configurator/parser/ConfigParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717
package org.apache.dubbo.rpc.cluster.configurator.parser;
1818

19-
import org.apache.dubbo.common.Constants;
2019
import org.apache.dubbo.common.URL;
2120
import org.apache.dubbo.common.utils.CollectionUtils;
2221
import org.apache.dubbo.common.utils.StringUtils;
@@ -31,6 +30,7 @@
3130
import java.util.List;
3231
import java.util.Map;
3332

33+
import static org.apache.dubbo.common.constants.ClusterConstants.OVERRIDE_PROVIDERS_KEY;
3434
import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_VALUE;
3535
import static org.apache.dubbo.common.constants.RegistryConstants.APP_DYNAMIC_CONFIGURATORS_CATEGORY;
3636
import static org.apache.dubbo.common.constants.RegistryConstants.DYNAMIC_CONFIGURATORS_CATEGORY;
@@ -148,7 +148,7 @@ private static String toParameterString(ConfigItem item) {
148148

149149
if (CollectionUtils.isNotEmpty(item.getProviderAddresses())) {
150150
sb.append("&");
151-
sb.append(Constants.OVERRIDE_PROVIDERS_KEY);
151+
sb.append(OVERRIDE_PROVIDERS_KEY);
152152
sb.append("=");
153153
sb.append(CollectionUtils.join(item.getProviderAddresses(), ","));
154154
}

dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/AbstractDirectory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.List;
3333
import java.util.Map;
3434

35+
import static org.apache.dubbo.common.constants.MonitorConstants.MONITOR_KEY;
3536
import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_PROTOCOL;
3637

3738
/**
@@ -66,7 +67,7 @@ public AbstractDirectory(URL url, URL consumerUrl, RouterChain<T> routerChain) {
6667

6768
if (url.getProtocol().equals(REGISTRY_PROTOCOL)) {
6869
Map<String, String> queryMap = StringUtils.parseQueryString(url.getParameterAndDecoded(Constants.REFER_KEY));
69-
this.url = url.addParameters(queryMap).removeParameter(Constants.MONITOR_KEY);
70+
this.url = url.addParameters(queryMap).removeParameter(MONITOR_KEY);
7071
} else {
7172
this.url = url;
7273
}

dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/AbstractLoadBalance.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717
package org.apache.dubbo.rpc.cluster.loadbalance;
1818

19-
import org.apache.dubbo.common.Constants;
2019
import org.apache.dubbo.common.URL;
2120
import org.apache.dubbo.common.utils.CollectionUtils;
2221
import org.apache.dubbo.rpc.Invocation;
@@ -25,6 +24,12 @@
2524

2625
import java.util.List;
2726

27+
import static org.apache.dubbo.common.constants.ClusterConstants.DEFAULT_WARMUP;
28+
import static org.apache.dubbo.common.constants.ClusterConstants.DEFAULT_WEIGHT;
29+
import static org.apache.dubbo.common.constants.ClusterConstants.REMOTE_TIMESTAMP_KEY;
30+
import static org.apache.dubbo.common.constants.ClusterConstants.WARMUP_KEY;
31+
import static org.apache.dubbo.common.constants.ClusterConstants.WEIGHT_KEY;
32+
2833
/**
2934
* AbstractLoadBalance
3035
*/
@@ -66,12 +71,12 @@ public <T> Invoker<T> select(List<Invoker<T>> invokers, URL url, Invocation invo
6671
* @return weight
6772
*/
6873
protected int getWeight(Invoker<?> invoker, Invocation invocation) {
69-
int weight = invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT);
74+
int weight = invoker.getUrl().getMethodParameter(invocation.getMethodName(), WEIGHT_KEY, DEFAULT_WEIGHT);
7075
if (weight > 0) {
71-
long timestamp = invoker.getUrl().getParameter(Constants.REMOTE_TIMESTAMP_KEY, 0L);
76+
long timestamp = invoker.getUrl().getParameter(REMOTE_TIMESTAMP_KEY, 0L);
7277
if (timestamp > 0L) {
7378
int uptime = (int) (System.currentTimeMillis() - timestamp);
74-
int warmup = invoker.getUrl().getParameter(Constants.WARMUP_KEY, Constants.DEFAULT_WARMUP);
79+
int warmup = invoker.getUrl().getParameter(WARMUP_KEY, DEFAULT_WARMUP);
7580
if (uptime > 0 && uptime < warmup) {
7681
weight = calculateWarmupWeight(uptime, warmup, weight);
7782
}

dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/ConditionRouter.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,15 @@
3939
import java.util.regex.Matcher;
4040
import java.util.regex.Pattern;
4141

42-
import static org.apache.dubbo.common.constants.CommonConstants.METHOD_KEY;
42+
import static org.apache.dubbo.common.constants.ClusterConstants.ADDRESS_KEY;
43+
import static org.apache.dubbo.common.constants.ClusterConstants.FORCE_KEY;
44+
import static org.apache.dubbo.common.constants.ClusterConstants.PRIORITY_KEY;
45+
import static org.apache.dubbo.common.constants.ClusterConstants.RULE_KEY;
46+
import static org.apache.dubbo.common.constants.ClusterConstants.RUNTIME_KEY;
4347
import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_KEY_PREFIX;
4448
import static org.apache.dubbo.common.constants.CommonConstants.ENABLED_KEY;
4549
import static org.apache.dubbo.common.constants.CommonConstants.METHODS_KEY;
50+
import static org.apache.dubbo.common.constants.CommonConstants.METHOD_KEY;
4651

4752
/**
4853
* ConditionRouter
@@ -66,10 +71,10 @@ public ConditionRouter(String rule, boolean force, boolean enabled) {
6671

6772
public ConditionRouter(URL url) {
6873
this.url = url;
69-
this.priority = url.getParameter(Constants.PRIORITY_KEY, 0);
70-
this.force = url.getParameter(Constants.FORCE_KEY, false);
74+
this.priority = url.getParameter(PRIORITY_KEY, 0);
75+
this.force = url.getParameter(FORCE_KEY, false);
7176
this.enabled = url.getParameter(ENABLED_KEY, true);
72-
init(url.getParameterAndDecoded(Constants.RULE_KEY));
77+
init(url.getParameterAndDecoded(RULE_KEY));
7378
}
7479

7580
public void init(String rule) {
@@ -188,7 +193,7 @@ public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation
188193
if (!result.isEmpty()) {
189194
return result;
190195
} else if (force) {
191-
logger.warn("The route result is empty and force execute. consumer: " + NetUtils.getLocalHost() + ", service: " + url.getServiceKey() + ", router: " + url.getParameterAndDecoded(Constants.RULE_KEY));
196+
logger.warn("The route result is empty and force execute. consumer: " + NetUtils.getLocalHost() + ", service: " + url.getServiceKey() + ", router: " + url.getParameterAndDecoded(RULE_KEY));
192197
return result;
193198
}
194199
} catch (Throwable t) {
@@ -201,7 +206,7 @@ public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation
201206
public boolean isRuntime() {
202207
// We always return true for previously defined Router, that is, old Router doesn't support cache anymore.
203208
// return true;
204-
return this.url.getParameter(Constants.RUNTIME_KEY, false);
209+
return this.url.getParameter(RUNTIME_KEY, false);
205210
}
206211

207212
@Override
@@ -226,7 +231,7 @@ private boolean matchCondition(Map<String, MatchPair> condition, URL url, URL pa
226231
//get real invoked method name from invocation
227232
if (invocation != null && (METHOD_KEY.equals(key) || METHODS_KEY.equals(key))) {
228233
sampleValue = invocation.getMethodName();
229-
} else if (Constants.ADDRESS_KEY.equals(key)) {
234+
} else if (ADDRESS_KEY.equals(key)) {
230235
sampleValue = url.getAddress();
231236
} else if (Constants.HOST_KEY.equals(key)) {
232237
sampleValue = url.getHost();

dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/file/FileRouterFactory.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717
package org.apache.dubbo.rpc.cluster.router.file;
1818

19-
import org.apache.dubbo.common.Constants;
2019
import org.apache.dubbo.common.URL;
2120
import org.apache.dubbo.common.URLBuilder;
2221
import org.apache.dubbo.common.utils.IOUtils;
@@ -28,6 +27,11 @@
2827
import java.io.FileReader;
2928
import java.io.IOException;
3029

30+
import static org.apache.dubbo.common.constants.ClusterConstants.ROUTER_KEY;
31+
import static org.apache.dubbo.common.constants.ClusterConstants.RULE_KEY;
32+
import static org.apache.dubbo.common.constants.ClusterConstants.RUNTIME_KEY;
33+
import static org.apache.dubbo.common.constants.ClusterConstants.TYPE_KEY;
34+
3135
public class FileRouterFactory implements RouterFactory {
3236

3337
public static final String NAME = "file";
@@ -43,7 +47,7 @@ public Router getRouter(URL url) {
4347
try {
4448
// Transform File URL into Script Route URL, and Load
4549
// file:///d:/path/to/route.js?router=script ==> script:///d:/path/to/route.js?type=js&rule=<file-content>
46-
String protocol = url.getParameter(Constants.ROUTER_KEY, ScriptRouterFactory.NAME); // Replace original protocol (maybe 'file') with 'script'
50+
String protocol = url.getParameter(ROUTER_KEY, ScriptRouterFactory.NAME); // Replace original protocol (maybe 'file') with 'script'
4751
String type = null; // Use file suffix to config script type, e.g., js, groovy ...
4852
String path = url.getPath();
4953
if (path != null) {
@@ -55,12 +59,12 @@ public Router getRouter(URL url) {
5559
String rule = IOUtils.read(new FileReader(new File(url.getAbsolutePath())));
5660

5761
// FIXME: this code looks useless
58-
boolean runtime = url.getParameter(Constants.RUNTIME_KEY, false);
62+
boolean runtime = url.getParameter(RUNTIME_KEY, false);
5963
URL script = URLBuilder.from(url)
6064
.setProtocol(protocol)
61-
.addParameter(Constants.TYPE_KEY, type)
62-
.addParameter(Constants.RUNTIME_KEY, runtime)
63-
.addParameterAndEncoded(Constants.RULE_KEY, rule)
65+
.addParameter(TYPE_KEY, type)
66+
.addParameter(RUNTIME_KEY, runtime)
67+
.addParameterAndEncoded(RULE_KEY, rule)
6468
.build();
6569

6670
return routerFactory.getRouter(script);

dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/mock/MockInvokersSelector.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717
package org.apache.dubbo.rpc.cluster.router.mock;
1818

19-
import org.apache.dubbo.common.Constants;
2019
import org.apache.dubbo.common.URL;
2120
import org.apache.dubbo.common.utils.CollectionUtils;
2221
import org.apache.dubbo.rpc.Invocation;
@@ -27,6 +26,9 @@
2726
import java.util.ArrayList;
2827
import java.util.List;
2928

29+
import static org.apache.dubbo.common.constants.ClusterConstants.INVOCATION_NEED_MOCK;
30+
import static org.apache.dubbo.common.constants.ClusterConstants.MOCK_PROTOCOL;
31+
3032
/**
3133
* A specific Router designed to realize mock feature.
3234
* If a request is configured to use mock, then this router guarantees that only the invokers with protocol MOCK appear in final the invoker list, all other invokers will be excluded.
@@ -50,7 +52,7 @@ public <T> List<Invoker<T>> route(final List<Invoker<T>> invokers,
5052
if (invocation.getAttachments() == null) {
5153
return getNormalInvokers(invokers);
5254
} else {
53-
String value = invocation.getAttachments().get(Constants.INVOCATION_NEED_MOCK);
55+
String value = invocation.getAttachments().get(INVOCATION_NEED_MOCK);
5456
if (value == null) {
5557
return getNormalInvokers(invokers);
5658
} else if (Boolean.TRUE.toString().equalsIgnoreCase(value)) {
@@ -66,7 +68,7 @@ private <T> List<Invoker<T>> getMockedInvokers(final List<Invoker<T>> invokers)
6668
}
6769
List<Invoker<T>> sInvokers = new ArrayList<Invoker<T>>(1);
6870
for (Invoker<T> invoker : invokers) {
69-
if (invoker.getUrl().getProtocol().equals(Constants.MOCK_PROTOCOL)) {
71+
if (invoker.getUrl().getProtocol().equals(MOCK_PROTOCOL)) {
7072
sInvokers.add(invoker);
7173
}
7274
}
@@ -79,7 +81,7 @@ private <T> List<Invoker<T>> getNormalInvokers(final List<Invoker<T>> invokers)
7981
} else {
8082
List<Invoker<T>> sInvokers = new ArrayList<Invoker<T>>(invokers.size());
8183
for (Invoker<T> invoker : invokers) {
82-
if (!invoker.getUrl().getProtocol().equals(Constants.MOCK_PROTOCOL)) {
84+
if (!invoker.getUrl().getProtocol().equals(MOCK_PROTOCOL)) {
8385
sInvokers.add(invoker);
8486
}
8587
}
@@ -90,7 +92,7 @@ private <T> List<Invoker<T>> getNormalInvokers(final List<Invoker<T>> invokers)
9092
private <T> boolean hasMockProviders(final List<Invoker<T>> invokers) {
9193
boolean hasMockProvider = false;
9294
for (Invoker<T> invoker : invokers) {
93-
if (invoker.getUrl().getProtocol().equals(Constants.MOCK_PROTOCOL)) {
95+
if (invoker.getUrl().getProtocol().equals(MOCK_PROTOCOL)) {
9496
hasMockProvider = true;
9597
break;
9698
}

dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/script/ScriptRouter.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717
package org.apache.dubbo.rpc.cluster.router.script;
1818

19-
import org.apache.dubbo.common.Constants;
2019
import org.apache.dubbo.common.URL;
2120
import org.apache.dubbo.common.logger.Logger;
2221
import org.apache.dubbo.common.logger.LoggerFactory;
@@ -40,6 +39,13 @@
4039
import java.util.concurrent.ConcurrentHashMap;
4140
import java.util.stream.Collectors;
4241

42+
import static org.apache.dubbo.common.constants.ClusterConstants.DEFAULT_SCRIPT_TYPE_KEY;
43+
import static org.apache.dubbo.common.constants.ClusterConstants.FORCE_KEY;
44+
import static org.apache.dubbo.common.constants.ClusterConstants.PRIORITY_KEY;
45+
import static org.apache.dubbo.common.constants.ClusterConstants.RULE_KEY;
46+
import static org.apache.dubbo.common.constants.ClusterConstants.RUNTIME_KEY;
47+
import static org.apache.dubbo.common.constants.ClusterConstants.TYPE_KEY;
48+
4349
/**
4450
* ScriptRouter
4551
*/
@@ -58,7 +64,7 @@ public class ScriptRouter extends AbstractRouter {
5864

5965
public ScriptRouter(URL url) {
6066
this.url = url;
61-
this.priority = url.getParameter(Constants.PRIORITY_KEY, SCRIPT_ROUTER_DEFAULT_PRIORITY);
67+
this.priority = url.getParameter(PRIORITY_KEY, SCRIPT_ROUTER_DEFAULT_PRIORITY);
6268

6369
engine = getEngine(url);
6470
rule = getRule(url);
@@ -77,7 +83,7 @@ public ScriptRouter(URL url) {
7783
* get rule from url parameters.
7884
*/
7985
private String getRule(URL url) {
80-
String vRule = url.getParameterAndDecoded(Constants.RULE_KEY);
86+
String vRule = url.getParameterAndDecoded(RULE_KEY);
8187
if (StringUtils.isEmpty(vRule)) {
8288
throw new IllegalStateException("route rule can not be empty.");
8389
}
@@ -88,7 +94,7 @@ private String getRule(URL url) {
8894
* create ScriptEngine instance by type from url parameters, then cache it
8995
*/
9096
private ScriptEngine getEngine(URL url) {
91-
String type = url.getParameter(Constants.TYPE_KEY, Constants.DEFAULT_SCRIPT_TYPE_KEY);
97+
String type = url.getParameter(TYPE_KEY, DEFAULT_SCRIPT_TYPE_KEY);
9298

9399
return engines.computeIfAbsent(type, t -> {
94100
ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName(type);
@@ -142,12 +148,12 @@ private <T> Bindings createBindings(List<Invoker<T>> invokers, Invocation invoca
142148

143149
@Override
144150
public boolean isRuntime() {
145-
return this.url.getParameter(Constants.RUNTIME_KEY, false);
151+
return this.url.getParameter(RUNTIME_KEY, false);
146152
}
147153

148154
@Override
149155
public boolean isForce() {
150-
return url.getParameter(Constants.FORCE_KEY, false);
156+
return url.getParameter(FORCE_KEY, false);
151157
}
152158

153159
}

0 commit comments

Comments
 (0)