Update code according to the checkstyle

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
This commit is contained in:
Eric Zhao
2019-03-14 23:23:29 +08:00
parent f75dde34c4
commit cd174f0b76
6 changed files with 175 additions and 104 deletions

View File

@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.circuitbreaker.sentinel;
import java.util.ArrayList;
@@ -42,11 +43,13 @@ import org.springframework.util.Assert;
public class SentinelCircuitBreaker implements CircuitBreaker {
private final String resourceName;
private final EntryType entryType;
private final List<DegradeRule> rules;
public SentinelCircuitBreaker(String resourceName, EntryType entryType, 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;
@@ -78,7 +81,8 @@ public class SentinelCircuitBreaker implements CircuitBreaker {
Entry entry = null;
try {
entry = SphU.entry(resourceName, entryType);
// If the SphU.entry() does not throw `BlockException`, it means that the request can pass.
// If the SphU.entry() does not throw `BlockException`, it means that the
// request can pass.
return toRun.get();
}
catch (BlockException ex) {
@@ -88,7 +92,8 @@ public class SentinelCircuitBreaker implements CircuitBreaker {
return fallback.apply(ex);
}
catch (Exception ex) {
// For other kinds of exceptions, we'll trace the exception count via Tracer.trace(ex).
// For other kinds of exceptions, we'll trace the exception count via
// Tracer.trace(ex).
Tracer.trace(ex);
return fallback.apply(ex);
}
@@ -99,4 +104,5 @@ public class SentinelCircuitBreaker implements CircuitBreaker {
}
}
}
}

View File

@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.circuitbreaker.sentinel;
import java.util.ArrayList;
@@ -31,10 +32,12 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Auto configuration for {@link SentinelCircuitBreaker}.
*
* @author Eric Zhao
*/
@Configuration
@ConditionalOnClass({SphU.class})
@ConditionalOnClass({ SphU.class })
public class SentinelCircuitBreakerAutoConfiguration {
@Bean
@@ -45,15 +48,18 @@ public class SentinelCircuitBreakerAutoConfiguration {
@Configuration
public static class SentinelCustomizerConfiguration {
@Autowired(required = false)
public List<Customizer<SentinelCircuitBreakerFactory>> customizers = new ArrayList<>();
@Autowired(required = false)
public SentinelCircuitBreakerFactory factory;
private List<Customizer<SentinelCircuitBreakerFactory>> customizers = new ArrayList<>();
@Autowired(required = false)
private SentinelCircuitBreakerFactory factory;
@PostConstruct
public void init() {
customizers.forEach(customizer -> customizer.customize(factory));
}
}
}
}

View File

@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.circuitbreaker.sentinel;
import java.util.ArrayList;
@@ -26,13 +27,11 @@ import org.springframework.util.Assert;
/**
* @author Eric Zhao
*/
public class SentinelCircuitBreakerFactory extends CircuitBreakerFactory<SentinelConfigBuilder.SentinelCircuitBreakerConfiguration, SentinelConfigBuilder> {
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) {
@@ -48,7 +47,9 @@ public class SentinelCircuitBreakerFactory extends CircuitBreakerFactory<Sentine
}
@Override
public void configureDefault(Function<String, SentinelCircuitBreakerConfiguration> defaultConfiguration) {
public void configureDefault(
Function<String, SentinelCircuitBreakerConfiguration> defaultConfiguration) {
this.defaultConfiguration = defaultConfiguration;
}
}

View File

@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.circuitbreaker.sentinel;
import java.util.ArrayList;
@@ -28,10 +29,13 @@ import org.springframework.util.Assert;
/**
* @author Eric Zhao
*/
public class SentinelConfigBuilder implements ConfigBuilder<SentinelConfigBuilder.SentinelCircuitBreakerConfiguration> {
public class SentinelConfigBuilder implements
ConfigBuilder<SentinelConfigBuilder.SentinelCircuitBreakerConfiguration> {
private String resourceName;
private EntryType entryType;
private List<DegradeRule> rules;
public SentinelConfigBuilder() {
@@ -59,17 +63,19 @@ public class SentinelConfigBuilder implements ConfigBuilder<SentinelConfigBuilde
@Override
public SentinelCircuitBreakerConfiguration build() {
Assert.hasText(resourceName, "resourceName cannot be empty");
List<DegradeRule> rules = Optional.ofNullable(this.rules).orElse(new ArrayList<>());
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)
.setResourceName(this.resourceName).setEntryType(entryType)
.setRules(rules);
}
public static class SentinelCircuitBreakerConfiguration {
private String resourceName;
private EntryType entryType;
private List<DegradeRule> rules;
@@ -100,5 +106,7 @@ public class SentinelConfigBuilder implements ConfigBuilder<SentinelConfigBuilde
this.rules = rules;
return this;
}
}
}

View File

@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.circuitbreaker.sentinel;
import java.util.ArrayList;
@@ -22,10 +23,10 @@ import java.util.List;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
@@ -40,7 +41,7 @@ import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
/**
@@ -51,88 +52,102 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
@DirtiesContext
public class SentinelCircuitBreakerIntegrationTest {
@Configuration
@EnableAutoConfiguration
@RestController
protected static class Application {
@GetMapping("/slow")
public String slow() throws InterruptedException {
Thread.sleep(500);
return "slow";
}
@Autowired
private Application.DemoControllerService service;
@GetMapping("/normal")
public String normal() {
return "normal";
}
@Test
public void testSlow() throws Exception {
// The first 5 requests should pass.
assertThat(service.slow()).isEqualTo("slow");
assertThat(service.slow()).isEqualTo("slow");
assertThat(service.slow()).isEqualTo("slow");
assertThat(service.slow()).isEqualTo("slow");
assertThat(service.slow()).isEqualTo("slow");
@Bean
public Customizer<SentinelCircuitBreakerFactory> slowCustomizer() {
String slowId = "slow";
List<DegradeRule> rules = Collections.singletonList(
new DegradeRule(slowId).setGrade(RuleConstant.DEGRADE_GRADE_RT)
.setCount(100)
.setTimeWindow(10)
);
return factory -> factory.configure(builder -> builder.rules(rules), slowId);
}
// Then in the next 10s, the fallback method should be called.
for (int i = 0; i < 10; i++) {
assertThat(service.slow()).isEqualTo("fallback");
Thread.sleep(1000);
}
@Service
public static class DemoControllerService {
private TestRestTemplate rest;
private CircuitBreakerFactory cbFactory;
// Recovered.
assertThat(service.slow()).isEqualTo("slow");
}
public DemoControllerService(TestRestTemplate rest, CircuitBreakerFactory cbFactory) {
this.rest = rest;
this.cbFactory = cbFactory;
}
@Test
public void testNormal() {
assertThat(service.normal()).isEqualTo("normal");
}
public String slow() {
return cbFactory.create("slow").run(() -> rest.getForObject("/slow", String.class), t -> "fallback");
}
@Before
public void setUp() {
DegradeRuleManager.loadRules(new ArrayList<>());
}
public String normal() {
return cbFactory.create("normal").run(() -> rest.getForObject("/normal", String.class),
t -> "fallback");
}
}
}
@Before
public void tearDown() {
DegradeRuleManager.loadRules(new ArrayList<>());
}
@Autowired
Application.DemoControllerService service;
@Configuration
@EnableAutoConfiguration
@RestController
protected static class Application {
@Test
public void testSlow() throws Exception {
// The first 5 requests should pass.
assertEquals("slow", service.slow());
assertEquals("slow", service.slow());
assertEquals("slow", service.slow());
assertEquals("slow", service.slow());
assertEquals("slow", service.slow());
@GetMapping("/slow")
public String slow() throws InterruptedException {
Thread.sleep(500);
return "slow";
}
// Then in the next 10s, the fallback method should be called.
for (int i = 0; i < 10; i++) {
assertEquals("fallback", service.slow());
Thread.sleep(1000);
}
@GetMapping("/normal")
public String normal() {
return "normal";
}
// Recovered.
assertEquals("slow", service.slow());
}
@Bean
public Customizer<SentinelCircuitBreakerFactory> slowCustomizer() {
String slowId = "slow";
List<DegradeRule> rules = Collections.singletonList(
new DegradeRule(slowId).setGrade(RuleConstant.DEGRADE_GRADE_RT)
.setCount(100).setTimeWindow(10));
return factory -> {
factory.configure(builder -> builder.rules(rules), slowId);
factory.configureDefault(id -> new SentinelConfigBuilder()
.resourceName(id)
.rules(Collections.singletonList(new DegradeRule(id)
.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT)
.setCount(0.5).setTimeWindow(10)))
.build());
};
}
@Test
public void testNormal() {
assertEquals("normal", service.normal());
}
@Service
public static class DemoControllerService {
private TestRestTemplate rest;
@Before
public void setUp() {
DegradeRuleManager.loadRules(new ArrayList<>());
}
private CircuitBreakerFactory cbFactory;
DemoControllerService(TestRestTemplate rest,
CircuitBreakerFactory cbFactory) {
this.rest = rest;
this.cbFactory = cbFactory;
}
public String slow() {
return cbFactory.create("slow").run(
() -> rest.getForObject("/slow", String.class), t -> "fallback");
}
public String normal() {
return cbFactory.create("normal").run(
() -> rest.getForObject("/normal", String.class),
t -> "fallback");
}
}
}
@Before
public void tearDown() {
DegradeRuleManager.loadRules(new ArrayList<>());
}
}

View File

@@ -13,29 +13,64 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.circuitbreaker.sentinel;
import java.util.ArrayList;
import java.util.Collections;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import org.junit.After;
import org.junit.Test;
import org.springframework.cloud.circuitbreaker.commons.CircuitBreaker;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Eric Zhao
*/
public class SentinelCircuitBreakerTest {
@Test
public void testRun() {
CircuitBreaker cb = new SentinelCircuitBreakerFactory().create("testSentinelRun");
assertEquals("foobar", cb.run(() -> "foobar"));
}
@After
public void tearDown() {
// Clear the rules.
DegradeRuleManager.loadRules(new ArrayList<>());
}
@Test
public void testCreateDirectlyThenRun() {
// Create a circuit breaker without any circuit breaking rules.
CircuitBreaker cb = new SentinelCircuitBreaker(
"testSentinelCreateDirectlyThenRunA");
assertThat(cb.run(() -> "Sentinel")).isEqualTo("Sentinel");
assertThat(DegradeRuleManager.hasConfig("testSentinelCreateDirectlyThenRunA"))
.isFalse();
CircuitBreaker cb2 = new SentinelCircuitBreaker(
"testSentinelCreateDirectlyThenRunB",
Collections.singletonList(
new DegradeRule("testSentinelCreateDirectlyThenRunB")
.setCount(100).setTimeWindow(10)));
assertThat(cb2.run(() -> "Sentinel")).isEqualTo("Sentinel");
assertThat(DegradeRuleManager.hasConfig("testSentinelCreateDirectlyThenRunB"))
.isTrue();
}
@Test
public void testCreateFromFactoryThenRun() {
CircuitBreaker cb = new SentinelCircuitBreakerFactory().create("testSentinelRun");
assertThat(cb.run(() -> "foobar")).isEqualTo("foobar");
}
@Test
public void testRunWithFallback() {
CircuitBreaker cb = new SentinelCircuitBreakerFactory()
.create("testSentinelRunWithFallback");
assertThat(cb.<String>run(() -> {
throw new RuntimeException("boom");
}, t -> "fallback")).isEqualTo("fallback");
}
@Test
public void testRunWithFallback() {
CircuitBreaker cb = new SentinelCircuitBreakerFactory().create("testSentinelRunWithFallback");
assertEquals("fallback", cb.run(() -> {
throw new RuntimeException("boom");
}, t -> "fallback"));
}
}