瀏覽代碼

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

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
master
Eric Zhao 4 年之前
父節點
當前提交
bcbc19c772
共有 2 個檔案被更改,包括 27 行新增41 行删除
  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 查看文件

@@ -20,13 +20,7 @@ SDK usage:
```java
EurekaDataSource<List<FlowRule>> 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<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());
```

@@ -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:

```
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 查看文件

@@ -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.
* </p>
*
* @author: liyang
* @create: 2020-05-23 12:01
* @author liyang
* @since 1.8.0
*/
public class EurekaDataSource<T> extends AutoRefreshDataSource<String, T> {

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<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,
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,
Converter<String, T> configParser, long refreshMs, int connectTimeoutMills,
int readTimeoutMills) {
@@ -110,7 +106,6 @@ public class EurekaDataSource<T> extends AutoRefreshDataSource<String, T> {
this.readTimeoutMills = readTimeoutMills;
}


private List<String> ensureEndWithSlash(List<String> serviceUrls) {
List<String> newServiceUrls = new ArrayList<>();
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);
}


private String fetchStringSourceFromEurekaMetadata(String appId, String instanceId, List<String> serviceUrls,
String ruleKey) throws Exception {
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) {
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<T> extends AutoRefreshDataSource<String, T> {
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<T> extends AutoRefreshDataSource<String, T> {
}
}


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


}

Loading…
取消
儲存