Skip to content

Commit

Permalink
Polish code
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Zhao <sczyh16@gmail.com>
  • Loading branch information
sczyh30 committed Mar 17, 2020
1 parent bc112c5 commit 4355dcb
Showing 1 changed file with 55 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
/*
* Copyright 1999-2019 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.csp.sentinel.datasource.spring.cloud.config;

import com.alibaba.csp.sentinel.log.RecordLog;

import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
import org.springframework.cloud.config.client.ConfigClientProperties;
import org.springframework.cloud.config.client.ConfigClientStateHolder;
Expand Down Expand Up @@ -38,10 +54,11 @@

/**
* <p>
* {@link SentinelRuleLocator} which pull sentinel rules from remote server.
* It retrieve configurations of spring-cloud-config client configurations from {@link org.springframework.core.env.Environment}
* Such as spring.cloud.config.uri=uri, spring.cloud.config.profile=profile .... and so on.
* When pull rules successfully, save to {@link SentinelRuleStorage} for ${@link SpringCloudConfigDataSource} retrieve.
* {@link SentinelRuleLocator} which pulls Sentinel rules from remote server.
* It retrieves configurations of spring-cloud-config client configurations from
* {@link org.springframework.core.env.Environment}, such as {@code spring.cloud.config.uri=uri},
* {@code spring.cloud.config.profile=profile}, and so on.
* When rules are pulled successfully, it will be stored to {@link SentinelRuleStorage}.
* </p>
*
* @author lianglin
Expand All @@ -54,12 +71,12 @@ public class SentinelRuleLocator implements PropertySourceLocator {
private ConfigClientProperties defaultProperties;
private org.springframework.core.env.Environment environment;

public SentinelRuleLocator(ConfigClientProperties defaultProperties, org.springframework.core.env.Environment environment) {
public SentinelRuleLocator(ConfigClientProperties defaultProperties,
org.springframework.core.env.Environment environment) {
this.defaultProperties = defaultProperties;
this.environment = environment;
}


/**
* Responsible for pull data from remote server
*
Expand All @@ -69,35 +86,35 @@ public SentinelRuleLocator(ConfigClientProperties defaultProperties, org.springf
@Override
@Retryable(interceptor = "configServerRetryInterceptor")
public org.springframework.core.env.PropertySource<?> locate(
org.springframework.core.env.Environment environment) {
org.springframework.core.env.Environment environment) {
ConfigClientProperties properties = this.defaultProperties.override(environment);
CompositePropertySource composite = new CompositePropertySource("configService");
RestTemplate restTemplate = this.restTemplate == null
? getSecureRestTemplate(properties)
: this.restTemplate;
? getSecureRestTemplate(properties)
: this.restTemplate;
Exception error = null;
String errorBody = null;
try {
String[] labels = new String[]{""};
String[] labels = new String[] {""};
if (StringUtils.hasText(properties.getLabel())) {
labels = StringUtils
.commaDelimitedListToStringArray(properties.getLabel());
.commaDelimitedListToStringArray(properties.getLabel());
}
String state = ConfigClientStateHolder.getState();
// Try all the labels until one works
for (String label : labels) {
Environment result = getRemoteEnvironment(restTemplate, properties,
label.trim(), state);
label.trim(), state);
if (result != null) {
log(result);
// result.getPropertySources() can be null if using xml
if (result.getPropertySources() != null) {
for (PropertySource source : result.getPropertySources()) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) source
.getSource();
Map<String, Object> map = (Map<String, Object>)source
.getSource();
composite.addPropertySource(
new MapPropertySource(source.getName(), map));
new MapPropertySource(source.getName(), map));
}
}
SentinelRuleStorage.setRulesSource(composite);
Expand All @@ -106,21 +123,20 @@ public org.springframework.core.env.PropertySource<?> locate(
}
} catch (HttpServerErrorException e) {
error = e;
if (MediaType.APPLICATION_JSON
.includes(e.getResponseHeaders().getContentType())) {
if (MediaType.APPLICATION_JSON.includes(e.getResponseHeaders().getContentType())) {
errorBody = e.getResponseBodyAsString();
}
} catch (Exception e) {
error = e;
}
if (properties.isFailFast()) {
throw new IllegalStateException(
"Could not locate PropertySource and the fail fast property is set, failing",
error);
"Could not locate PropertySource and the fail fast property is set, failing",
error);
}
RecordLog.warn("Could not locate PropertySource: " + (errorBody == null
? error == null ? "label not found" : error.getMessage()
: errorBody));
? error == null ? "label not found" : error.getMessage()
: errorBody));
return null;

}
Expand All @@ -132,22 +148,19 @@ public org.springframework.core.env.PropertySource<?> refresh() {
private void log(Environment result) {

RecordLog.info(String.format(
"Located environment: name=%s, profiles=%s, label=%s, version=%s, state=%s",
result.getName(),
result.getProfiles() == null ? ""
: Arrays.asList(result.getProfiles()),
result.getLabel(), result.getVersion(), result.getState()));
"Located environment: name=%s, profiles=%s, label=%s, version=%s, state=%s",
result.getName(),
result.getProfiles() == null ? "" : Arrays.asList(result.getProfiles()),
result.getLabel(), result.getVersion(), result.getState()));

List<PropertySource> propertySourceList = result.getPropertySources();
if (propertySourceList != null) {
int propertyCount = 0;
for (PropertySource propertySource : propertySourceList) {
propertyCount += propertySource.getSource().size();
}
RecordLog.info(String.format(
"Environment %s has %d property sources with %d properties.",
result.getName(), result.getPropertySources().size(),
propertyCount));
RecordLog.info("[SentinelRuleLocator] Environment {} has {} property sources with {} properties",
result.getName(), result.getPropertySources().size(), propertyCount);
}
}

Expand All @@ -159,18 +172,18 @@ private Environment getRemoteEnvironment(RestTemplate restTemplate,
String token = properties.getToken();
int noOfUrls = properties.getUri().length;
if (noOfUrls > 1) {
RecordLog.debug("Multiple Config Server Urls found listed.");
RecordLog.debug("[SentinelRuleLocator] Multiple Config Server Urls found listed.");
}

RecordLog.info("[SentinelRuleLocator] getRemoteEnvironment, properties = {},label={}, state={}",
RecordLog.info("[SentinelRuleLocator] getRemoteEnvironment, properties={}, label={}, state={}",
properties, label, state);

Object[] args = new String[]{name, profile};
Object[] args = new String[] {name, profile};
if (StringUtils.hasText(label)) {
if (label.contains("/")) {
label = label.replace("/", "(_)");
}
args = new String[]{name, profile, label};
args = new String[] {name, profile, label};
path = path + "/{label}";
}
ResponseEntity<Environment> response = null;
Expand All @@ -181,7 +194,7 @@ private Environment getRemoteEnvironment(RestTemplate restTemplate,
String username = credentials.getUsername();
String password = credentials.getPassword();

RecordLog.info("Fetching config from server at : " + uri);
RecordLog.info("[SentinelRuleLocator] Fetching config from server at: " + uri);

try {
HttpHeaders headers = new HttpHeaders();
Expand All @@ -193,16 +206,16 @@ private Environment getRemoteEnvironment(RestTemplate restTemplate,
headers.add(STATE_HEADER, state);
}

final HttpEntity<Void> entity = new HttpEntity<>((Void) null, headers);
final HttpEntity<Void> entity = new HttpEntity<>((Void)null, headers);
response = restTemplate.exchange(uri + path, HttpMethod.GET, entity,
Environment.class, args);
Environment.class, args);
} catch (HttpClientErrorException e) {
if (e.getStatusCode() != HttpStatus.NOT_FOUND) {
throw e;
}
} catch (ResourceAccessException e) {
RecordLog.info("Connect Timeout Exception on Url - " + uri
+ ". Will be trying the next url if available");
RecordLog.warn("[SentinelRuleLocator] ConnectTimeoutException on url <{}>."
+ " Will be trying the next url if available", uri);
if (i == noOfUrls - 1) {
throw e;
} else {
Expand All @@ -221,7 +234,6 @@ private Environment getRemoteEnvironment(RestTemplate restTemplate,
return null;
}


private RestTemplate getSecureRestTemplate(ConfigClientProperties client) {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
if (client.getRequestReadTimeout() < 0) {
Expand All @@ -236,7 +248,7 @@ private RestTemplate getSecureRestTemplate(ConfigClientProperties client) {
}
if (!headers.isEmpty()) {
template.setInterceptors(Arrays.<ClientHttpRequestInterceptor>asList(
new GenericRequestHeaderInterceptor(headers)));
new GenericRequestHeaderInterceptor(headers)));
}

return template;
Expand All @@ -248,7 +260,7 @@ private void addAuthorizationToken(ConfigClientProperties configClientProperties

if (password != null && authorization != null) {
throw new IllegalStateException(
"You must set either 'password' or 'authorization'");
"You must set either 'password' or 'authorization'");
}

if (password != null) {
Expand All @@ -264,9 +276,8 @@ public void setRestTemplate(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}


public static class GenericRequestHeaderInterceptor
implements ClientHttpRequestInterceptor {
implements ClientHttpRequestInterceptor {

private final Map<String, String> headers;

Expand Down

0 comments on commit 4355dcb

Please sign in to comment.