Browse Source

Polish code and README.md of sentinel-datasource-eureka

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
master
Eric Zhao 4 years ago
parent
commit
bcbc19c772
2 changed files with 27 additions and 41 deletions
  1. +3
    -9
      sentinel-extension/sentinel-datasource-eureka/README.md
  2. +24
    -32
      sentinel-extension/sentinel-datasource-eureka/src/main/java/com/alibaba/csp/sentinel/datasource/eureka/EurekaDataSource.java

+ 3
- 9
sentinel-extension/sentinel-datasource-eureka/README.md View File

@@ -20,13 +20,7 @@ SDK usage:
```java ```java
EurekaDataSource<List<FlowRule>> eurekaDataSource = new EurekaDataSource("app-id", "instance-id", EurekaDataSource<List<FlowRule>> eurekaDataSource = new EurekaDataSource("app-id", "instance-id",
Arrays.asList("http://localhost:8761/eureka", "http://localhost:8762/eureka", "http://localhost:8763/eureka"), Arrays.asList("http://localhost:8761/eureka", "http://localhost:8762/eureka", "http://localhost:8763/eureka"),
"rule-key", new Converter<String, List<FlowRule>>() {
@Override
public List<FlowRule> convert(String o) {
return JSON.parseObject(o, new TypeReference<List<FlowRule>>() {
});
}
});
"rule-key", flowRuleParser);
FlowRuleManager.register2Property(eurekaDataSource.getProperty()); FlowRuleManager.register2Property(eurekaDataSource.getProperty());
``` ```


@@ -54,11 +48,11 @@ public EurekaDataSource<List<FlowRule>> 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: to update instance metadata:


``` ```
PUT /eureka/apps/{appID}/{instanceID}/metadata?{ruleKey}={json of the rules} PUT /eureka/apps/{appID}/{instanceID}/metadata?{ruleKey}={json of the rules}
``` ```


Note: don't forget to encode your json string in the url.
Note: don't forget to encode your JSON string in the url.

+ 24
- 32
sentinel-extension/sentinel-datasource-eureka/src/main/java/com/alibaba/csp/sentinel/datasource/eureka/EurekaDataSource.java View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * 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 * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * 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. * it may take longer to take effect.
* </p> * </p>
* *
* @author: liyang
* @create: 2020-05-23 12:01
* @author liyang
* @since 1.8.0
*/ */
public class EurekaDataSource<T> extends AutoRefreshDataSource<String, T> { public class EurekaDataSource<T> extends AutoRefreshDataSource<String, T> {


private static final long DEFAULT_REFRESH_MS = 10000; private static final long DEFAULT_REFRESH_MS = 10000;


/** /**
* connect timeout: 3s
* Default connect timeout: 3s
*/ */
private static final int DEFAULT_CONNECT_TIMEOUT_MS = 3000; 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 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<String> serviceUrls;
private final List<String> 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<String> serviceUrls, String ruleKey, public EurekaDataSource(String appId, String instanceId, List<String> serviceUrls, String ruleKey,
Converter<String, T> configParser) { Converter<String, T> 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<String> serviceUrls, String ruleKey, public EurekaDataSource(String appId, String instanceId, List<String> serviceUrls, String ruleKey,
Converter<String, T> configParser, long refreshMs, int connectTimeoutMills, Converter<String, T> configParser, long refreshMs, int connectTimeoutMills,
int readTimeoutMills) { int readTimeoutMills) {
@@ -110,7 +106,6 @@ public class EurekaDataSource<T> extends AutoRefreshDataSource<String, T> {
this.readTimeoutMills = readTimeoutMills; this.readTimeoutMills = readTimeoutMills;
} }



private List<String> ensureEndWithSlash(List<String> serviceUrls) { private List<String> ensureEndWithSlash(List<String> serviceUrls) {
List<String> newServiceUrls = new ArrayList<>(); List<String> newServiceUrls = new ArrayList<>();
for (String serviceUrl : serviceUrls) { for (String serviceUrl : serviceUrls) {
@@ -130,7 +125,6 @@ public class EurekaDataSource<T> extends AutoRefreshDataSource<String, T> {
return fetchStringSourceFromEurekaMetadata(this.appId, this.instanceId, this.serviceUrls, ruleKey); return fetchStringSourceFromEurekaMetadata(this.appId, this.instanceId, this.serviceUrls, ruleKey);
} }



private String fetchStringSourceFromEurekaMetadata(String appId, String instanceId, List<String> serviceUrls, private String fetchStringSourceFromEurekaMetadata(String appId, String instanceId, List<String> serviceUrls,
String ruleKey) throws Exception { String ruleKey) throws Exception {
List<String> shuffleUrls = new ArrayList<>(serviceUrls.size()); List<String> shuffleUrls = new ArrayList<>(serviceUrls.size());
@@ -152,18 +146,19 @@ public class EurekaDataSource<T> extends AutoRefreshDataSource<String, T> {
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
String s = toString(conn.getInputStream()); String s = toString(conn.getInputStream());
String ruleString = JSON.parseObject(s) String ruleString = JSON.parseObject(s)
.getJSONObject("instance")
.getJSONObject("metadata")
.getString(ruleKey);
.getJSONObject("instance")
.getJSONObject("metadata")
.getString(ruleKey);
return ruleString; return ruleString;
} }
RecordLog.warn("[EurekaDataSource] Warn: retrying on another server if available " + 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) { } catch (Exception e) {
try { try {
if (conn != null) { if (conn != null) {
RecordLog.warn("[EurekaDataSource] Warn: failed to request " + conn.getURL() + " from " 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) { } catch (Exception e1) {
RecordLog.warn("[EurekaDataSource] Warn: failed to request ", e1); RecordLog.warn("[EurekaDataSource] Warn: failed to request ", e1);
@@ -180,7 +175,6 @@ public class EurekaDataSource<T> extends AutoRefreshDataSource<String, T> {
throw new EurekaMetadataFetchException("Can't get any data"); throw new EurekaMetadataFetchException("Can't get any data");
} }



public static class EurekaMetadataFetchException extends Exception { public static class EurekaMetadataFetchException extends Exception {


public EurekaMetadataFetchException(String message) { public EurekaMetadataFetchException(String message) {
@@ -188,7 +182,6 @@ public class EurekaDataSource<T> extends AutoRefreshDataSource<String, T> {
} }
} }



private String toString(InputStream input) throws IOException { private String toString(InputStream input) throws IOException {
if (input == null) { if (input == null) {
return null; return null;
@@ -209,5 +202,4 @@ public class EurekaDataSource<T> extends AutoRefreshDataSource<String, T> {
return count; return count;
} }



} }

Loading…
Cancel
Save