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

以2.2.1.BUILD-SNAPSHOT 为基准,适配Greenwich版本的Spring Cloud

This commit is contained in:
theonefx
2020-04-03 14:14:47 +08:00
parent e963a39bd4
commit 9444117f59
654 changed files with 7971 additions and 17442 deletions

View File

@@ -30,6 +30,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--<dependency>-->
<!--<groupId>com.alibaba.csp</groupId>-->

View File

@@ -193,14 +193,27 @@ Now ReadableDataSource type support 5 categories: `file`, `nacos`, `zk`, `apollo
If you want to use `nacos`, `zk`, `apollo` or `redis` ReadableDataSource, you could add `sentinel-datasource-nacos`, `sentinel-datasource-zookeeper`,`sentinel-datasource-apollo` or `sentinel-datasource-redis` dependency.
When ReadableDataSource load rule data successfully, console will print some logs:
```
[Sentinel Starter] DataSource ds1-sentinel-file-datasource load 3 DegradeRule
[Sentinel Starter] DataSource ds2-sentinel-nacos-datasource load 2 FlowRule
```
## Warning
You should use `file` ReadableDataSource in a fatjar carefully or you may get error like this below
```
java.lang.RuntimeException: [Sentinel Starter] DataSource ds1 handle file [classpath: flowrule.json] error: class path resource [flowrule.json] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:xxx/xxx.jar!/BOOT-INF/classes!/flowrule.jso
```
You could use absolute path when you use File datasource & fat jar.
It is recommended to use Nacos/Apollo/Zookeeper/Redis datasource to store rules.
https://github.com/alibaba/spring-cloud-alibaba/issues/428
## More
For more information about Sentinel, see [Sentinel Project](https://github.com/alibaba/Sentinel).
If you have any ideas or suggestions for Spring Cloud Sentinel starter, please don't hesitate to tell us by submitting github issues.
If you have any ideas or suggestions for Spring Cloud Sentinel starter, please don't hesitate to tell us by submitting github issues.

View File

@@ -1,3 +1,19 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://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.cloud.examples;
import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
@@ -9,7 +25,11 @@ import org.springframework.http.client.ClientHttpRequestExecution;
/**
* @author fangjian
*/
public class ExceptionUtil {
public final class ExceptionUtil {
private ExceptionUtil() {
}
public static SentinelClientHttpResponse handleException(HttpRequest request,
byte[] body, ClientHttpRequestExecution execution, BlockException ex) {

View File

@@ -1,3 +1,19 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://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.cloud.examples;
import java.util.List;
@@ -11,9 +27,11 @@ import com.alibaba.fastjson.TypeReference;
* @author fangjian
*/
public class JsonFlowRuleListConverter implements Converter<String, List<FlowRule>> {
@Override
public List<FlowRule> convert(String source) {
return JSON.parseObject(source, new TypeReference<List<FlowRule>>() {
});
}
}

View File

@@ -1,3 +1,19 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://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.cloud.examples;
import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;

View File

@@ -1,3 +1,19 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://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.cloud.examples;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
@@ -16,24 +32,24 @@ public class TestController {
@Autowired
private RestTemplate restTemplate;
@GetMapping(value = "/hello")
@GetMapping("/hello")
@SentinelResource("resource")
public String hello() {
return "Hello";
}
@GetMapping(value = "/aa")
@GetMapping("/aa")
@SentinelResource("aa")
public String aa(int b, int a) {
return "Hello test";
}
@GetMapping(value = "/test")
@GetMapping("/test")
public String test1() {
return "Hello test";
}
@GetMapping(value = "/template")
@GetMapping("/template")
public String client() {
return restTemplate.getForObject("http://www.taobao.com/test", String.class);
}

View File

@@ -0,0 +1,36 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://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.cloud.examples;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @author yuhuangbin
*/
@Configuration
@EnableWebMvc
public class WebMvcConfiguration implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/errorPage").setViewName("errorPage");
}
}

View File

@@ -9,6 +9,9 @@ management.health.diskspace.enabled=false
spring.cloud.sentinel.transport.dashboard=localhost:8080
spring.cloud.sentinel.eager=true
#spring.cloud.sentinel.block-page=/errorPage
#spring.cloud.sentinel.filter.enabled=false
#spring.cloud.sentinel.http-method-specify=false
spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json

View File

@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
This is error page.
</body>
</html>

View File

@@ -17,6 +17,12 @@
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>

View File

@@ -23,9 +23,7 @@ import org.apache.dubbo.config.annotation.Reference;
*/
public class FooServiceConsumer {
@Reference(version = "${foo.service.version}",
application = "${dubbo.application.id}",
url = "dubbo://localhost:12345?version=1.0.0", timeout = 30000)
@Reference(version = "${foo.service.version}", application = "${dubbo.application.id}", url = "dubbo://localhost:12345?version=1.0.0", timeout = 30000)
private FooService fooService;
public String hello(String name) {

View File

@@ -17,6 +17,12 @@
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
@@ -42,7 +48,6 @@
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
</dependencies>
<build>

View File

@@ -21,8 +21,7 @@ import org.apache.dubbo.config.annotation.Service;
/**
* @author fangjian
*/
@Service(version = "${foo.service.version}", application = "${dubbo.application.id}",
protocol = "${dubbo.protocol.id}", registry = "${dubbo.registry.id}")
@Service(version = "${foo.service.version}", application = "${dubbo.application.id}", protocol = "${dubbo.protocol.id}", registry = "${dubbo.registry.id}")
public class FooServiceImpl implements FooService {
@Override

View File

@@ -1,3 +1,19 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://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.cloud.examples;
import org.springframework.boot.SpringApplication;

View File

@@ -1,3 +1,19 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://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.cloud.examples.controller;
import com.alibaba.cloud.examples.service.EchoService;
@@ -16,7 +32,7 @@ public class TestController {
@Autowired
private EchoService echoService;
@GetMapping(value = "/echo-feign/{str}")
@GetMapping("/echo-feign/{str}")
public String feign(@PathVariable String str) {
return echoService.echo(str);
}

View File

@@ -1,3 +1,19 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://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.cloud.examples.fallback;
import com.alibaba.cloud.examples.service.EchoService;
@@ -9,6 +25,7 @@ import com.alibaba.cloud.examples.service.EchoService;
* sentinel 降级处理
*/
public class EchoServiceFallback implements EchoService {
private Throwable throwable;
EchoServiceFallback(Throwable throwable) {
@@ -16,8 +33,7 @@ public class EchoServiceFallback implements EchoService {
}
/**
* 调用服务提供方的输出接口
*
* 调用服务提供方的输出接口.
* @param str 用户输入
* @return
*/
@@ -25,4 +41,5 @@ public class EchoServiceFallback implements EchoService {
public String echo(String str) {
return "consumer-fallback-default-str" + throwable.getMessage();
}
}

View File

@@ -1,3 +1,19 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://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.cloud.examples.fallback;
import feign.hystrix.FallbackFactory;
@@ -10,8 +26,10 @@ import org.springframework.stereotype.Component;
*/
@Component
public class EchoServiceFallbackFactory implements FallbackFactory<EchoServiceFallback> {
@Override
public EchoServiceFallback create(Throwable throwable) {
return new EchoServiceFallback(throwable);
}
}

View File

@@ -1,3 +1,19 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://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.cloud.examples.service;
import com.alibaba.cloud.examples.fallback.EchoServiceFallbackFactory;
@@ -16,11 +32,11 @@ import org.springframework.web.bind.annotation.PathVariable;
public interface EchoService {
/**
* 调用服务提供方的输出接口
*
* 调用服务提供方的输出接口.
* @param str 用户输入
* @return
* @return echo result
*/
@GetMapping(value = "/echo/{str}")
@GetMapping("/echo/{str}")
String echo(@PathVariable("str") String str);
}

View File

@@ -1,3 +1,19 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://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.cloud.examples;
import org.springframework.boot.SpringApplication;
@@ -14,4 +30,5 @@ public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}

View File

@@ -1,3 +1,19 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://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.cloud.examples.controller;
import org.springframework.web.bind.annotation.GetMapping;

View File

@@ -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,7 +17,6 @@
package com.alibaba.cloud.examples;
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
import reactor.core.publisher.Mono;
import org.springframework.context.annotation.Bean;
@@ -40,8 +39,7 @@ public class MySCGConfiguration {
@Override
public Mono<ServerResponse> handleRequest(ServerWebExchange exchange,
Throwable t) {
return ServerResponse.status(444)
.contentType(MediaType.APPLICATION_JSON_UTF8)
return ServerResponse.status(444).contentType(MediaType.APPLICATION_JSON)
.body(fromObject("SCS Sentinel block"));
}
};

View File

@@ -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.
@@ -25,7 +25,6 @@ import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import reactor.core.publisher.Mono;
import org.springframework.web.bind.annotation.GetMapping;

View File

@@ -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.

View File

@@ -31,8 +31,8 @@ spring:
ruleType: gw-api-group
transport:
dashboard: localhost:8080
# filter:
# enabled: true
filter:
enabled: true
scg.fallback:
mode: response
response-status: 444

View File

@@ -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,7 +17,6 @@
package com.alibaba.cloud.examples;
import com.alibaba.csp.sentinel.adapter.spring.webflux.callback.BlockRequestHandler;
import reactor.core.publisher.Mono;
import org.springframework.context.annotation.Bean;
@@ -42,7 +41,7 @@ public class MyConfiguration {
public Mono<ServerResponse> handleRequest(ServerWebExchange exchange,
Throwable t) {
return ServerResponse.status(HttpStatus.TOO_MANY_REQUESTS)
.contentType(MediaType.APPLICATION_JSON_UTF8)
.contentType(MediaType.APPLICATION_JSON)
.body(fromObject("block"));
}
};

View File

@@ -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.

View File

@@ -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,7 +17,6 @@
package com.alibaba.cloud.examples;
import com.alibaba.csp.sentinel.adapter.reactor.SentinelReactorTransformer;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

View File

@@ -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

View File

@@ -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.

View File

@@ -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.

View File

@@ -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.

View File

@@ -13,8 +13,8 @@ spring:
ruleType: gw-api-group
transport:
dashboard: localhost:8080
# filter:
# enabled: true
filter:
enabled: false
management.endpoints.web.exposure.include: "*"