From bcbc19c77232ea780326e1301aac0f3ea1ae4a20 Mon Sep 17 00:00:00 2001
From: Eric Zhao
Date: Wed, 19 Aug 2020 10:46:43 +0800
Subject: [PATCH] Polish code and README.md of sentinel-datasource-eureka
Signed-off-by: Eric Zhao
---
.../sentinel-datasource-eureka/README.md | 12 +---
.../datasource/eureka/EurekaDataSource.java | 56 ++++++++-----------
2 files changed, 27 insertions(+), 41 deletions(-)
diff --git a/sentinel-extension/sentinel-datasource-eureka/README.md b/sentinel-extension/sentinel-datasource-eureka/README.md
index 927b4cf4..7e336e6e 100644
--- a/sentinel-extension/sentinel-datasource-eureka/README.md
+++ b/sentinel-extension/sentinel-datasource-eureka/README.md
@@ -20,13 +20,7 @@ SDK usage:
```java
EurekaDataSource> eurekaDataSource = new EurekaDataSource("app-id", "instance-id",
Arrays.asList("http://localhost:8761/eureka", "http://localhost:8762/eureka", "http://localhost:8763/eureka"),
- "rule-key", new Converter>() {
- @Override
- public List convert(String o) {
- return JSON.parseObject(o, new TypeReference>() {
- });
- }
-});
+ "rule-key", flowRuleParser);
FlowRuleManager.register2Property(eurekaDataSource.getProperty());
```
@@ -54,11 +48,11 @@ public EurekaDataSource> eurekaDataSource(EurekaInstanceConfig eu
```
-To refresh the rule dynamically,you need to call [Eureka-REST-operations](https://github.com/Netflix/eureka/wiki/Eureka-REST-operations)
+To refresh the rule dynamically, you need to call [Eureka-REST-operations](https://github.com/Netflix/eureka/wiki/Eureka-REST-operations)
to update instance metadata:
```
PUT /eureka/apps/{appID}/{instanceID}/metadata?{ruleKey}={json of the rules}
```
-Note: don't forget to encode your json string in the url.
\ No newline at end of file
+Note: don't forget to encode your JSON string in the url.
\ No newline at end of file
diff --git a/sentinel-extension/sentinel-datasource-eureka/src/main/java/com/alibaba/csp/sentinel/datasource/eureka/EurekaDataSource.java b/sentinel-extension/sentinel-datasource-eureka/src/main/java/com/alibaba/csp/sentinel/datasource/eureka/EurekaDataSource.java
index 8029e8e9..b7796c48 100644
--- a/sentinel-extension/sentinel-datasource-eureka/src/main/java/com/alibaba/csp/sentinel/datasource/eureka/EurekaDataSource.java
+++ b/sentinel-extension/sentinel-datasource-eureka/src/main/java/com/alibaba/csp/sentinel/datasource/eureka/EurekaDataSource.java
@@ -1,11 +1,11 @@
/*
- * Copyright 1999-2018 Alibaba Group Holding Ltd.
+ * Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed 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
+ * https://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,
@@ -41,55 +41,51 @@ import java.util.List;
* it may take longer to take effect.
*
*
- * @author: liyang
- * @create: 2020-05-23 12:01
+ * @author liyang
+ * @since 1.8.0
*/
public class EurekaDataSource extends AutoRefreshDataSource {
private static final long DEFAULT_REFRESH_MS = 10000;
/**
- * connect timeout: 3s
+ * Default connect timeout: 3s
*/
private static final int DEFAULT_CONNECT_TIMEOUT_MS = 3000;
/**
- * read timeout: 30s
+ * Default read timeout: 30s
*/
private static final int DEFAULT_READ_TIMEOUT_MS = 30000;
-
- private int connectTimeoutMills;
-
-
- private int readTimeoutMills;
+ private final int connectTimeoutMills;
+ private final int readTimeoutMills;
/**
- * eureka instance appid
+ * Eureka instance app ID.
*/
- private String appId;
+ private final String appId;
/**
- * eureka instance id
+ * Eureka instance id.
*/
- private String instanceId;
+ private final String instanceId;
/**
- * collect of eureka server urls
+ * Eureka server URL list.
*/
- private List serviceUrls;
+ private final List serviceUrls;
/**
- * metadata key of the rule source
+ * Metadata key of the rule source.
*/
- private String ruleKey;
-
+ private final String ruleKey;
public EurekaDataSource(String appId, String instanceId, List serviceUrls, String ruleKey,
Converter configParser) {
- this(appId, instanceId, serviceUrls, ruleKey, configParser, DEFAULT_REFRESH_MS, DEFAULT_CONNECT_TIMEOUT_MS, DEFAULT_READ_TIMEOUT_MS);
+ this(appId, instanceId, serviceUrls, ruleKey, configParser, DEFAULT_REFRESH_MS, DEFAULT_CONNECT_TIMEOUT_MS,
+ DEFAULT_READ_TIMEOUT_MS);
}
-
public EurekaDataSource(String appId, String instanceId, List serviceUrls, String ruleKey,
Converter configParser, long refreshMs, int connectTimeoutMills,
int readTimeoutMills) {
@@ -110,7 +106,6 @@ public class EurekaDataSource extends AutoRefreshDataSource {
this.readTimeoutMills = readTimeoutMills;
}
-
private List ensureEndWithSlash(List serviceUrls) {
List newServiceUrls = new ArrayList<>();
for (String serviceUrl : serviceUrls) {
@@ -130,7 +125,6 @@ public class EurekaDataSource extends AutoRefreshDataSource {
return fetchStringSourceFromEurekaMetadata(this.appId, this.instanceId, this.serviceUrls, ruleKey);
}
-
private String fetchStringSourceFromEurekaMetadata(String appId, String instanceId, List serviceUrls,
String ruleKey) throws Exception {
List shuffleUrls = new ArrayList<>(serviceUrls.size());
@@ -152,18 +146,19 @@ public class EurekaDataSource extends AutoRefreshDataSource {
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
String s = toString(conn.getInputStream());
String ruleString = JSON.parseObject(s)
- .getJSONObject("instance")
- .getJSONObject("metadata")
- .getString(ruleKey);
+ .getJSONObject("instance")
+ .getJSONObject("metadata")
+ .getString(ruleKey);
return ruleString;
}
RecordLog.warn("[EurekaDataSource] Warn: retrying on another server if available " +
- "due to response code: {}, response message: {}", conn.getResponseCode(), toString(conn.getErrorStream()));
+ "due to response code: {}, response message: {}", conn.getResponseCode(),
+ toString(conn.getErrorStream()));
} catch (Exception e) {
try {
if (conn != null) {
RecordLog.warn("[EurekaDataSource] Warn: failed to request " + conn.getURL() + " from "
- + InetAddress.getByName(conn.getURL().getHost()).getHostAddress(), e);
+ + InetAddress.getByName(conn.getURL().getHost()).getHostAddress(), e);
}
} catch (Exception e1) {
RecordLog.warn("[EurekaDataSource] Warn: failed to request ", e1);
@@ -180,7 +175,6 @@ public class EurekaDataSource extends AutoRefreshDataSource {
throw new EurekaMetadataFetchException("Can't get any data");
}
-
public static class EurekaMetadataFetchException extends Exception {
public EurekaMetadataFetchException(String message) {
@@ -188,7 +182,6 @@ public class EurekaDataSource extends AutoRefreshDataSource {
}
}
-
private String toString(InputStream input) throws IOException {
if (input == null) {
return null;
@@ -209,5 +202,4 @@ public class EurekaDataSource extends AutoRefreshDataSource {
return count;
}
-
}