mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
import changes from master
reorganizing modules
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2018 the original author or authors.
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,10 +16,16 @@
|
||||
|
||||
package com.alibaba.cloud.examples;
|
||||
|
||||
import com.alibaba.csp.sentinel.adapter.spring.webflux.callback.BlockRequestHandler;
|
||||
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;
|
||||
@@ -42,10 +48,28 @@ public class MyConfiguration {
|
||||
public Mono<ServerResponse> handleRequest(ServerWebExchange exchange,
|
||||
Throwable t) {
|
||||
return ServerResponse.status(HttpStatus.TOO_MANY_REQUESTS)
|
||||
.contentType(MediaType.APPLICATION_JSON_UTF8)
|
||||
.body(fromObject("block"));
|
||||
.contentType(MediaType.APPLICATION_JSON).body(fromObject("block"));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@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());
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2018 the original author or authors.
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2018 the original author or authors.
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,13 +17,14 @@
|
||||
package com.alibaba.cloud.examples;
|
||||
|
||||
import com.alibaba.csp.sentinel.adapter.reactor.SentinelReactorTransformer;
|
||||
import com.alibaba.csp.sentinel.annotation.SentinelResource;
|
||||
|
||||
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>
|
||||
@@ -31,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")
|
||||
@@ -38,6 +42,13 @@ public class SentinelWebFluxController {
|
||||
.transform(new SentinelReactorTransformer<>("mono"));
|
||||
}
|
||||
|
||||
@GetMapping("/test")
|
||||
public Mono<String> test() {
|
||||
return Mono.just("simple string")
|
||||
// transform the publisher here.
|
||||
.transform(new SentinelReactorTransformer<>("test"));
|
||||
}
|
||||
|
||||
@GetMapping("/flux")
|
||||
public Flux<String> flux() {
|
||||
return Flux.fromArray(new String[] { "a", "b", "c" })
|
||||
@@ -45,19 +56,26 @@ public class SentinelWebFluxController {
|
||||
.transform(new SentinelReactorTransformer<>("flux"));
|
||||
}
|
||||
|
||||
@GetMapping("/aaa")
|
||||
@SentinelResource("abc")
|
||||
public Flux<String> aaa() {
|
||||
return Flux.fromArray(new String[] { "a", "b", "c" })
|
||||
// transform the publisher here.
|
||||
.transform(new SentinelReactorTransformer<>("aaa"));
|
||||
@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("/test")
|
||||
public Flux<String> test() {
|
||||
return Flux.fromArray(new String[] { "a", "b", "c" })
|
||||
// transform the publisher here.
|
||||
.transform(new SentinelReactorTransformer<>("test"));
|
||||
@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");
|
||||
}));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,8 +3,6 @@ server.port=18084
|
||||
management.endpoints.web.exposure.include=*
|
||||
spring.cloud.sentinel.transport.dashboard=localhost:8080
|
||||
spring.cloud.sentinel.eager=true
|
||||
|
||||
|
||||
spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json
|
||||
spring.cloud.sentinel.datasource.ds1.file.data-type=json
|
||||
spring.cloud.sentinel.datasource.ds1.file.rule-type=flow
|
||||
spring.cloud.sentinel.datasource.ds1.file.rule-type=flow
|
||||
|
||||
@@ -15,13 +15,4 @@
|
||||
"limitApp": "default",
|
||||
"strategy": 0
|
||||
}
|
||||
,
|
||||
{
|
||||
"resource": "abc",
|
||||
"controlBehavior": 0,
|
||||
"count": 0,
|
||||
"grade": 1,
|
||||
"limitApp": "default",
|
||||
"strategy": 0
|
||||
}
|
||||
]
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user