1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

update examples

This commit is contained in:
fangjian0423
2020-01-30 02:17:36 +08:00
parent 97528396fd
commit 57e3059d8d
23 changed files with 294 additions and 11 deletions

View File

@@ -60,14 +60,14 @@ public class TestController {
@GetMapping("/slow")
public String slow() {
return circuitBreakerFactory.create("show").run(() -> {
return circuitBreakerFactory.create("slow").run(() -> {
try {
Thread.sleep(1000L);
Thread.sleep(500L);
}
catch (InterruptedException e) {
e.printStackTrace();
}
return "success";
return "slow";
}, throwable -> "fallback");
}

View File

@@ -16,9 +16,16 @@
package com.alibaba.cloud.examples;
import java.util.Collections;
import com.alibaba.cloud.circuitbreaker.sentinel.ReactiveSentinelCircuitBreakerFactory;
import com.alibaba.cloud.circuitbreaker.sentinel.SentinelConfigBuilder;
import com.alibaba.csp.sentinel.adapter.spring.webflux.callback.BlockRequestHandler;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import reactor.core.publisher.Mono;
import org.springframework.cloud.client.circuitbreaker.Customizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
@@ -46,4 +53,23 @@ public class MyConfiguration {
};
}
@Bean
public Customizer<ReactiveSentinelCircuitBreakerFactory> slowCustomizer() {
return factory -> {
factory.configure(builder -> builder.rules(Collections.singletonList(
new DegradeRule("slow_mono").setGrade(RuleConstant.DEGRADE_GRADE_RT)
.setCount(100).setTimeWindow(5))),
"slow_mono");
factory.configure(builder -> builder.rules(Collections.singletonList(
new DegradeRule("slow_flux").setGrade(RuleConstant.DEGRADE_GRADE_RT)
.setCount(100).setTimeWindow(5))),
"slow_flux");
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());
};
}
}

View File

@@ -20,8 +20,11 @@ import com.alibaba.csp.sentinel.adapter.reactor.SentinelReactorTransformer;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.circuitbreaker.ReactiveCircuitBreakerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
/**
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
@@ -29,6 +32,9 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
public class SentinelWebFluxController {
@Autowired
private ReactiveCircuitBreakerFactory circuitBreakerFactory;
@GetMapping("/mono")
public Mono<String> mono() {
return Mono.just("simple string")
@@ -50,4 +56,26 @@ public class SentinelWebFluxController {
.transform(new SentinelReactorTransformer<>("flux"));
}
@GetMapping("/cbSlow")
public Mono<String> cbSlow() {
int delaySecs = 2;
return WebClient.builder().baseUrl("http://httpbin.org/").build().get()
.uri("/delay/" + delaySecs).retrieve().bodyToMono(String.class)
.transform(it -> circuitBreakerFactory.create("slow_mono").run(it, t -> {
t.printStackTrace();
return Mono.just("fallback");
}));
}
@GetMapping("/cbError")
public Mono<String> cbError() {
String code = "500";
return WebClient.builder().baseUrl("http://httpbin.org/").build().get()
.uri("/status/" + code).retrieve().bodyToMono(String.class)
.transform(it -> circuitBreakerFactory.create("cbError").run(it, t -> {
t.printStackTrace();
return Mono.just("fallback");
}));
}
}