Skip to content

Commit 9d9b6f8

Browse files
committed
本文主要演示springboot整合alibaba sentinel各种降级规则遇到的问题
1 parent 6ffe60d commit 9d9b6f8

File tree

18 files changed

+1035
-0
lines changed

18 files changed

+1035
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,3 +662,5 @@
662662
/springboot-xxl-job-executor/springboot-xxl-job-executor.iml
663663
/springboot-aop-spel/springboot-aop-spel.iml
664664
/springboot-aop-spel/target/classes/
665+
/springboot-sentinel/target/classes/
666+
/springboot-sentinel/springboot-sentinel.iml

pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<module>springboot-xxl-job-executor</module>
4040
<module>springboot-aop-spel</module>
4141
<module>springboot-custom-eureka</module>
42+
<module>springboot-sentinel</module>
4243
</modules>
4344
<parent>
4445
<groupId>org.springframework.boot</groupId>
@@ -103,6 +104,8 @@
103104
<hutool.version>5.4.1</hutool.version>
104105
<spring-cloud.version>Greenwich.RC2</spring-cloud.version>
105106
<xxl-job.version>2.2.0</xxl-job.version>
107+
<spring-cloud.alibaba.version>2.2.3.RELEASE</spring-cloud.alibaba.version>
108+
<sentinel.version>1.8.0</sentinel.version>
106109
</properties>
107110

108111
<dependencyManagement>
@@ -114,6 +117,15 @@
114117
<type>pom</type>
115118
<scope>import</scope>
116119
</dependency>
120+
121+
<dependency>
122+
<groupId>com.alibaba.cloud</groupId>
123+
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
124+
<version>${spring-cloud.alibaba.version}</version>
125+
<type>pom</type>
126+
<scope>import</scope>
127+
</dependency>
128+
117129
<dependency>
118130
<groupId>org.yaml</groupId>
119131
<artifactId>snakeyaml</artifactId>
@@ -478,6 +490,12 @@
478490
<version>${xxl-job.version}</version>
479491
</dependency>
480492

493+
<dependency>
494+
<groupId>com.alibaba.csp</groupId>
495+
<artifactId>sentinel-datasource-extension</artifactId>
496+
<version>${sentinel.version}</version>
497+
</dependency>
498+
481499

482500

483501
</dependencies>

springboot-sentinel/pom.xml

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>springboot-learning</artifactId>
7+
<groupId>com.github.lybgeek</groupId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>springboot-sentinel</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-starter-web</artifactId>
18+
</dependency>
19+
20+
<dependency>
21+
<groupId>com.alibaba.cloud</groupId>
22+
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>org.springframework.cloud</groupId>
27+
<artifactId>spring-cloud-commons</artifactId>
28+
<version>2.2.3.RELEASE</version>
29+
</dependency>
30+
31+
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-aop</artifactId>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>io.springfox</groupId>
39+
<artifactId>springfox-swagger-ui</artifactId>
40+
</dependency>
41+
<dependency>
42+
<groupId>io.springfox</groupId>
43+
<artifactId>springfox-swagger2</artifactId>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>io.swagger</groupId>
48+
<artifactId>swagger-annotations</artifactId>
49+
</dependency>
50+
<dependency>
51+
<groupId>io.swagger</groupId>
52+
<artifactId>swagger-models</artifactId>
53+
</dependency>
54+
55+
<dependency>
56+
<groupId>org.projectlombok</groupId>
57+
<artifactId>lombok</artifactId>
58+
<optional>true</optional>
59+
</dependency>
60+
61+
<dependency>
62+
<groupId>org.apache.commons</groupId>
63+
<artifactId>commons-collections4</artifactId>
64+
</dependency>
65+
66+
<dependency>
67+
<groupId>com.alibaba.csp</groupId>
68+
<artifactId>sentinel-datasource-extension</artifactId>
69+
</dependency>
70+
71+
</dependencies>
72+
73+
74+
<build>
75+
<plugins>
76+
<plugin>
77+
<groupId>org.apache.maven.plugins</groupId>
78+
<artifactId>maven-compiler-plugin</artifactId>
79+
<configuration>
80+
<source>${java.version}</source>
81+
<target>${java.version}</target>
82+
</configuration>
83+
</plugin>
84+
<plugin>
85+
<groupId>org.springframework.boot</groupId>
86+
<artifactId>spring-boot-maven-plugin</artifactId>
87+
<configuration>
88+
<excludes>
89+
<exclude>
90+
<groupId>org.projectlombok</groupId>
91+
<artifactId>lombok</artifactId>
92+
</exclude>
93+
</excludes>
94+
</configuration>
95+
</plugin>
96+
</plugins>
97+
</build>
98+
99+
100+
</project>

springboot-sentinel/readMe.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
本文主要演示springboot整合alibaba sentinel各种降级规则遇到的问题
2+
> 1、降级不生效问题
3+
4+
原因:项目引入了自定义全局异常com.github.lybgeek.common.exception.GlobalExceptionHandler,
5+
而异常数的统计在com.alibaba.csp.sentinel.adapter.spring.webmvc.AbstractSentinelInterceptor.afterCompletion这个方法执行,
6+
自定义全局异常的处理会先于com.alibaba.csp.sentinel.adapter.spring.webmvc.AbstractSentinelInterceptor.afterCompletion这个方法执行执行,
7+
因为我们在全局异常里面已经对异常进行处理,转换为全局对象了,这样导致AbstractSentinelInterceptor.afterCompletion无法获取到异常,进而无法统计异常数或者异常比例
8+
9+
解决方法:参考https://github.com/alibaba/Sentinel/issues/1281或者https://github.com/alibaba/Sentinel/issues/404
10+
当然可以直接引用本文的例子
11+
12+
> 2、授权规则不生效问题
13+
14+
原因:项目中没有实现com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser接口,导致无法解析请求来源
15+
16+
解决方法:新增一个请求来源解析器com.github.lybgeek.sentinel.authority.CustomRequestOriginParser,用来解析请求来源
17+
18+
> 3、热点规则不生效问题
19+
20+
原因:web埋点如果以url作为资源名,规则不生效
21+
解决方法:以@SentinelResource注解定义的name作为资源名,参考:https://github.com/alibaba/Sentinel/issues/1734
22+
23+
热点规则配置@SentinelResource后,可能还会出现java.lang.reflect.UndeclaredThrowableException: null
24+
解决方法:需要在方法中添加throws BlockException或添加blockHandler来处理异常
25+
参考:https://github.com/alibaba/Sentinel/issues/776
26+
27+
## 新增基于拉模式的规则持久化存储
28+
29+
参考:http://www.itmuch.com/spring-cloud-alibaba/sentinel-rules-persistence-pull-mode
30+
31+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.github.lybgeek;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.client.CommonsClientAutoConfiguration;
6+
import org.springframework.cloud.client.discovery.noop.NoopDiscoveryClientAutoConfiguration;
7+
8+
@SpringBootApplication(exclude = {CommonsClientAutoConfiguration.class,NoopDiscoveryClientAutoConfiguration.class})
9+
public class SpringbootSentinelApplication {
10+
11+
12+
13+
public static void main(String[] args) {
14+
SpringApplication.run(SpringbootSentinelApplication.class, args);
15+
}
16+
17+
18+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
2+
package com.github.lybgeek.common.config;
3+
4+
import com.google.common.collect.Lists;
5+
import io.swagger.annotations.ApiOperation;
6+
import org.springframework.beans.factory.annotation.Value;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
import springfox.documentation.builders.ApiInfoBuilder;
10+
import springfox.documentation.builders.PathSelectors;
11+
import springfox.documentation.builders.RequestHandlerSelectors;
12+
import springfox.documentation.service.*;
13+
import springfox.documentation.spi.DocumentationType;
14+
import springfox.documentation.spi.service.contexts.SecurityContext;
15+
import springfox.documentation.spring.web.plugins.Docket;
16+
import springfox.documentation.swagger2.annotations.EnableSwagger2;
17+
18+
import java.util.List;
19+
20+
/**
21+
* @description: swagger配置
22+
* @author:
23+
**/
24+
@Configuration
25+
@EnableSwagger2
26+
public class SwaggerConfig {
27+
28+
29+
/** 是否开启swagger */
30+
@Value("${swagger.enabled:true}")
31+
private boolean enabled;
32+
33+
/** 设置请求的统一前缀 */
34+
@Value("${swagger.pathMapping:/}")
35+
private String pathMapping;
36+
37+
38+
@Bean
39+
public Docket groupRestApi() {
40+
return new Docket(DocumentationType.SWAGGER_2)
41+
// 是否启用Swagger
42+
.enable(enabled)
43+
// 用来创建该API的基本信息,展示在文档的页面中(自定义展示的信息)
44+
.apiInfo(groupApiInfo())
45+
// 设置哪些接口暴露给Swagger展示
46+
.select()
47+
// 扫描所有有注解的api,用这种方式更灵活
48+
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
49+
// 扫描指定包中的swagger注解
50+
// .apis(RequestHandlerSelectors.basePackage("com.jomoo.retail"))
51+
// 扫描所有 .apis(RequestHandlerSelectors.any())
52+
.paths(PathSelectors.any())
53+
.build()
54+
// 设置安全模式,swagger可以设置访问token
55+
.securityContexts(Lists.newArrayList(securityContext()))
56+
.securitySchemes(Lists.<SecurityScheme>newArrayList(apiKey()))
57+
.pathMapping(pathMapping);
58+
}
59+
60+
/**
61+
* 添加摘要信息
62+
*/
63+
private ApiInfo groupApiInfo(){
64+
return new ApiInfoBuilder()
65+
.title("lyb-geek sentinel")
66+
.description("<div style='font-size:14px;color:red;'>SENTINEL RESTful APIs</div>")
67+
.termsOfServiceUrl("https://github.com/lyb-geek")
68+
.version("1.0")
69+
.build();
70+
}
71+
72+
73+
/**
74+
* 指定header key
75+
*/
76+
private ApiKey apiKey() {
77+
return new ApiKey("Authorization", "Authorization", "header");
78+
}
79+
80+
81+
/**
82+
* 安全上下文
83+
*/
84+
private SecurityContext securityContext() {
85+
return SecurityContext.builder()
86+
.securityReferences(defaultAuth())
87+
.forPaths(PathSelectors.regex("/.*"))
88+
.build();
89+
}
90+
91+
/**
92+
* 安全模式,这里指定token通过Authorization头请求头传递
93+
*/
94+
List<SecurityReference> defaultAuth() {
95+
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
96+
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
97+
authorizationScopes[0] = authorizationScope;
98+
return Lists.newArrayList(new SecurityReference("Authorization", authorizationScopes));
99+
}
100+
101+
102+
103+
104+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.github.lybgeek.common.exception;
2+
3+
public class BizException extends RuntimeException {
4+
private static final long serialVersionUID = 1L;
5+
protected final String message;
6+
protected Integer errorCode;
7+
8+
public BizException(String message) {
9+
this.message = message;
10+
}
11+
12+
public BizException(String message, Throwable e) {
13+
super(message, e);
14+
this.message = message;
15+
}
16+
17+
public BizException(Integer errorCode, String message, Throwable e) {
18+
super(message, e);
19+
this.message = message;
20+
this.errorCode = errorCode;
21+
}
22+
23+
public BizException(Integer errorCode, String message) {
24+
this.message = message;
25+
this.errorCode = errorCode;
26+
}
27+
28+
public String getMessage() {
29+
return this.message;
30+
}
31+
32+
public Integer getErrorCode() {
33+
return this.errorCode;
34+
}
35+
36+
public void setErrorCode(Integer errorCode) {
37+
this.errorCode = errorCode;
38+
}
39+
}

0 commit comments

Comments
 (0)