@@ -41,50 +41,53 @@ import org.springframework.util.Assert;
|
||||
*/
|
||||
public class SentinelCircuitBreaker implements CircuitBreaker {
|
||||
|
||||
private final String resourceName;
|
||||
private final EntryType entryType;
|
||||
private final String resourceName;
|
||||
private final EntryType entryType;
|
||||
|
||||
private final List<DegradeRule> rules;
|
||||
private final List<DegradeRule> rules;
|
||||
|
||||
public SentinelCircuitBreaker(String resourceName, EntryType entryType, List<DegradeRule> rules) {
|
||||
Assert.hasText(resourceName, "resourceName cannot be blank");
|
||||
Assert.notNull(rules, "rules should not be null");
|
||||
this.resourceName = resourceName;
|
||||
this.entryType = entryType;
|
||||
this.rules = Collections.unmodifiableList(rules);
|
||||
public SentinelCircuitBreaker(String resourceName, EntryType entryType, List<DegradeRule> rules) {
|
||||
Assert.hasText(resourceName, "resourceName cannot be blank");
|
||||
Assert.notNull(rules, "rules should not be null");
|
||||
this.resourceName = resourceName;
|
||||
this.entryType = entryType;
|
||||
this.rules = Collections.unmodifiableList(rules);
|
||||
|
||||
applyToSentinelRuleManager();
|
||||
}
|
||||
applyToSentinelRuleManager();
|
||||
}
|
||||
|
||||
public SentinelCircuitBreaker(String resourceName, List<DegradeRule> rules) {
|
||||
this(resourceName, EntryType.OUT, rules);
|
||||
}
|
||||
public SentinelCircuitBreaker(String resourceName, List<DegradeRule> rules) {
|
||||
this(resourceName, EntryType.OUT, rules);
|
||||
}
|
||||
|
||||
public SentinelCircuitBreaker(String resourceName) {
|
||||
this(resourceName, EntryType.OUT, Collections.emptyList());
|
||||
}
|
||||
public SentinelCircuitBreaker(String resourceName) {
|
||||
this(resourceName, EntryType.OUT, Collections.emptyList());
|
||||
}
|
||||
|
||||
private void applyToSentinelRuleManager() {
|
||||
Set<DegradeRule> ruleSet = new HashSet<>(DegradeRuleManager.getRules());
|
||||
ruleSet.addAll(this.rules);
|
||||
DegradeRuleManager.loadRules(new ArrayList<>(ruleSet));
|
||||
}
|
||||
private void applyToSentinelRuleManager() {
|
||||
Set<DegradeRule> ruleSet = new HashSet<>(DegradeRuleManager.getRules());
|
||||
ruleSet.addAll(this.rules);
|
||||
DegradeRuleManager.loadRules(new ArrayList<>(ruleSet));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T run(Supplier<T> toRun, Function<Throwable, T> fallback) {
|
||||
Entry entry = null;
|
||||
try {
|
||||
entry = SphU.entry(resourceName, entryType);
|
||||
return toRun.get();
|
||||
} catch (BlockException ex) {
|
||||
return fallback.apply(ex);
|
||||
} catch (Exception ex) {
|
||||
Tracer.trace(ex);
|
||||
return fallback.apply(ex);
|
||||
} finally {
|
||||
if (entry != null) {
|
||||
entry.exit();
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public <T> T run(Supplier<T> toRun, Function<Throwable, T> fallback) {
|
||||
Entry entry = null;
|
||||
try {
|
||||
entry = SphU.entry(resourceName, entryType);
|
||||
return toRun.get();
|
||||
}
|
||||
catch (BlockException ex) {
|
||||
return fallback.apply(ex);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
Tracer.trace(ex);
|
||||
return fallback.apply(ex);
|
||||
}
|
||||
finally {
|
||||
if (entry != null) {
|
||||
entry.exit();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,26 +34,26 @@ import org.springframework.context.annotation.Configuration;
|
||||
* @author Eric Zhao
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass({ SphU.class })
|
||||
@ConditionalOnClass({SphU.class})
|
||||
public class SentinelCircuitBreakerAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(CircuitBreakerFactory.class)
|
||||
public CircuitBreakerFactory sentinelCircuitBreakerFactory() {
|
||||
return new SentinelCircuitBreakerFactory();
|
||||
}
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(CircuitBreakerFactory.class)
|
||||
public CircuitBreakerFactory sentinelCircuitBreakerFactory() {
|
||||
return new SentinelCircuitBreakerFactory();
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class SentinelCustomizerConfiguration {
|
||||
@Autowired(required = false)
|
||||
public List<Customizer<SentinelCircuitBreakerFactory>> customizers = new ArrayList<>();
|
||||
@Configuration
|
||||
public static class SentinelCustomizerConfiguration {
|
||||
@Autowired(required = false)
|
||||
public List<Customizer<SentinelCircuitBreakerFactory>> customizers = new ArrayList<>();
|
||||
|
||||
@Autowired(required = false)
|
||||
public SentinelCircuitBreakerFactory factory;
|
||||
@Autowired(required = false)
|
||||
public SentinelCircuitBreakerFactory factory;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
customizers.forEach(customizer -> customizer.customize(factory));
|
||||
}
|
||||
}
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
customizers.forEach(customizer -> customizer.customize(factory));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,27 +28,27 @@ import org.springframework.util.Assert;
|
||||
*/
|
||||
public class SentinelCircuitBreakerFactory extends CircuitBreakerFactory<SentinelConfigBuilder.SentinelCircuitBreakerConfiguration, SentinelConfigBuilder> {
|
||||
|
||||
private Function<String, SentinelConfigBuilder.SentinelCircuitBreakerConfiguration> defaultConfiguration = id ->
|
||||
new SentinelConfigBuilder()
|
||||
.resourceName(id)
|
||||
.rules(new ArrayList<>())
|
||||
.build();
|
||||
private Function<String, SentinelConfigBuilder.SentinelCircuitBreakerConfiguration> defaultConfiguration = id ->
|
||||
new SentinelConfigBuilder()
|
||||
.resourceName(id)
|
||||
.rules(new ArrayList<>())
|
||||
.build();
|
||||
|
||||
@Override
|
||||
public CircuitBreaker create(String id) {
|
||||
Assert.hasText(id, "A CircuitBreaker must have an id.");
|
||||
SentinelConfigBuilder.SentinelCircuitBreakerConfiguration conf = getConfigurations()
|
||||
.computeIfAbsent(id, defaultConfiguration);
|
||||
return new SentinelCircuitBreaker(id, conf.getEntryType(), conf.getRules());
|
||||
}
|
||||
@Override
|
||||
public CircuitBreaker create(String id) {
|
||||
Assert.hasText(id, "A CircuitBreaker must have an id.");
|
||||
SentinelConfigBuilder.SentinelCircuitBreakerConfiguration conf = getConfigurations()
|
||||
.computeIfAbsent(id, defaultConfiguration);
|
||||
return new SentinelCircuitBreaker(id, conf.getEntryType(), conf.getRules());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SentinelConfigBuilder configBuilder(String id) {
|
||||
return new SentinelConfigBuilder(id);
|
||||
}
|
||||
@Override
|
||||
protected SentinelConfigBuilder configBuilder(String id) {
|
||||
return new SentinelConfigBuilder(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configureDefault(Function<String, SentinelCircuitBreakerConfiguration> defaultConfiguration) {
|
||||
this.defaultConfiguration = defaultConfiguration;
|
||||
}
|
||||
@Override
|
||||
public void configureDefault(Function<String, SentinelCircuitBreakerConfiguration> defaultConfiguration) {
|
||||
this.defaultConfiguration = defaultConfiguration;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,74 +30,75 @@ import org.springframework.util.Assert;
|
||||
*/
|
||||
public class SentinelConfigBuilder implements ConfigBuilder<SentinelConfigBuilder.SentinelCircuitBreakerConfiguration> {
|
||||
|
||||
private String resourceName;
|
||||
private EntryType entryType;
|
||||
private List<DegradeRule> rules;
|
||||
private String resourceName;
|
||||
private EntryType entryType;
|
||||
private List<DegradeRule> rules;
|
||||
|
||||
public SentinelConfigBuilder() {}
|
||||
public SentinelConfigBuilder() {
|
||||
}
|
||||
|
||||
public SentinelConfigBuilder(String resourceName) {
|
||||
this.resourceName = resourceName;
|
||||
}
|
||||
public SentinelConfigBuilder(String resourceName) {
|
||||
this.resourceName = resourceName;
|
||||
}
|
||||
|
||||
public SentinelConfigBuilder resourceName(String resourceName) {
|
||||
this.resourceName = resourceName;
|
||||
return this;
|
||||
}
|
||||
public SentinelConfigBuilder resourceName(String resourceName) {
|
||||
this.resourceName = resourceName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SentinelConfigBuilder entryType(EntryType entryType) {
|
||||
this.entryType = entryType;
|
||||
return this;
|
||||
}
|
||||
public SentinelConfigBuilder entryType(EntryType entryType) {
|
||||
this.entryType = entryType;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SentinelConfigBuilder rules(List<DegradeRule> rules) {
|
||||
this.rules = rules;
|
||||
return this;
|
||||
}
|
||||
public SentinelConfigBuilder rules(List<DegradeRule> rules) {
|
||||
this.rules = rules;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SentinelCircuitBreakerConfiguration build() {
|
||||
Assert.hasText(resourceName, "resourceName cannot be empty");
|
||||
List<DegradeRule> rules = Optional.ofNullable(this.rules).orElse(new ArrayList<>());
|
||||
@Override
|
||||
public SentinelCircuitBreakerConfiguration build() {
|
||||
Assert.hasText(resourceName, "resourceName cannot be empty");
|
||||
List<DegradeRule> rules = Optional.ofNullable(this.rules).orElse(new ArrayList<>());
|
||||
|
||||
EntryType entryType = Optional.ofNullable(this.entryType).orElse(EntryType.OUT);
|
||||
return new SentinelCircuitBreakerConfiguration()
|
||||
.setResourceName(this.resourceName)
|
||||
.setEntryType(entryType)
|
||||
.setRules(rules);
|
||||
}
|
||||
EntryType entryType = Optional.ofNullable(this.entryType).orElse(EntryType.OUT);
|
||||
return new SentinelCircuitBreakerConfiguration()
|
||||
.setResourceName(this.resourceName)
|
||||
.setEntryType(entryType)
|
||||
.setRules(rules);
|
||||
}
|
||||
|
||||
public static class SentinelCircuitBreakerConfiguration {
|
||||
private String resourceName;
|
||||
private EntryType entryType;
|
||||
public static class SentinelCircuitBreakerConfiguration {
|
||||
private String resourceName;
|
||||
private EntryType entryType;
|
||||
|
||||
private List<DegradeRule> rules;
|
||||
private List<DegradeRule> rules;
|
||||
|
||||
public String getResourceName() {
|
||||
return resourceName;
|
||||
}
|
||||
public String getResourceName() {
|
||||
return resourceName;
|
||||
}
|
||||
|
||||
public SentinelCircuitBreakerConfiguration setResourceName(String resourceName) {
|
||||
this.resourceName = resourceName;
|
||||
return this;
|
||||
}
|
||||
public SentinelCircuitBreakerConfiguration setResourceName(String resourceName) {
|
||||
this.resourceName = resourceName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public EntryType getEntryType() {
|
||||
return entryType;
|
||||
}
|
||||
public EntryType getEntryType() {
|
||||
return entryType;
|
||||
}
|
||||
|
||||
public SentinelCircuitBreakerConfiguration setEntryType(EntryType entryType) {
|
||||
this.entryType = entryType;
|
||||
return this;
|
||||
}
|
||||
public SentinelCircuitBreakerConfiguration setEntryType(EntryType entryType) {
|
||||
this.entryType = entryType;
|
||||
return this;
|
||||
}
|
||||
|
||||
public List<DegradeRule> getRules() {
|
||||
return rules;
|
||||
}
|
||||
public List<DegradeRule> getRules() {
|
||||
return rules;
|
||||
}
|
||||
|
||||
public SentinelCircuitBreakerConfiguration setRules(List<DegradeRule> rules) {
|
||||
this.rules = rules;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
public SentinelCircuitBreakerConfiguration setRules(List<DegradeRule> rules) {
|
||||
this.rules = rules;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user