Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
caopu16
whatsmars
提交
97654b2a
W
whatsmars
项目概览
caopu16
/
whatsmars
与 Fork 源项目一致
Fork自
武汉红喜 / whatsmars
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
whatsmars
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
97654b2a
编写于
9月 15, 2020
作者:
武汉红喜
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
sentinel
上级
3d42a21b
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
237 addition
and
1 deletion
+237
-1
README.md
README.md
+2
-1
pom.xml
pom.xml
+2
-0
whatsmars-sentinel/pom.xml
whatsmars-sentinel/pom.xml
+45
-0
whatsmars-sentinel/whatsmars-sentinel-webmvc/pom.xml
whatsmars-sentinel/whatsmars-sentinel-webmvc/pom.xml
+29
-0
whatsmars-sentinel/whatsmars-sentinel-webmvc/src/main/java/org/hongxi/whatsmars/sentinel/webmvc/Application.java
...ava/org/hongxi/whatsmars/sentinel/webmvc/Application.java
+15
-0
whatsmars-sentinel/whatsmars-sentinel-webmvc/src/main/java/org/hongxi/whatsmars/sentinel/webmvc/config/GlobalExceptionHandler.java
...tsmars/sentinel/webmvc/config/GlobalExceptionHandler.java
+24
-0
whatsmars-sentinel/whatsmars-sentinel-webmvc/src/main/java/org/hongxi/whatsmars/sentinel/webmvc/config/InterceptorConfig.java
...i/whatsmars/sentinel/webmvc/config/InterceptorConfig.java
+61
-0
whatsmars-sentinel/whatsmars-sentinel-webmvc/src/main/java/org/hongxi/whatsmars/sentinel/webmvc/controller/WebMvcTestController.java
...mars/sentinel/webmvc/controller/WebMvcTestController.java
+59
-0
未找到文件。
README.md
浏览文件 @
97654b2a
...
...
@@ -21,7 +21,8 @@ whatsmars-elasticsearch | Elasticsearch
whatsmars-mq | 消息中间件RocketMQ,Kafka等
whatsmars-netty | NIO框架首选
whatsmars-redis | Redis客户端简单封装
whatsmars-rpc | Transporter & Codec & Serialization
whatsmars-rpc | RPC核心实现
whatsmars-sentinel | 流量控制组件
whatsmars-shardingsphere | 分布式数据库中间件
whatsmars-spring | Spring Framework
whatsmars-spring-boot-samples | Spring Boot Samples
...
...
pom.xml
浏览文件 @
97654b2a
...
...
@@ -32,6 +32,7 @@
<module>
whatsmars-spring-data
</module>
<module>
whatsmars-archetypes
</module>
<module>
whatsmars-netty
</module>
<module>
whatsmars-sentinel
</module>
</modules>
<!--这里的properties会覆盖父pom里的重名的配置-->
...
...
@@ -61,6 +62,7 @@
<mybatis-spring.version>
1.3.1
</mybatis-spring.version>
<protobuf.version>
3.2.0
</protobuf.version>
<rocketmq.version>
4.5.1
</rocketmq.version>
<sentinel.version>
1.8.0
</sentinel.version>
<zkclient.version>
0.9
</zkclient.version>
</properties>
...
...
whatsmars-sentinel/pom.xml
0 → 100644
浏览文件 @
97654b2a
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<parent>
<artifactId>
whatsmars-parent
</artifactId>
<groupId>
org.hongxi
</groupId>
<version>
Rocket.S9
</version>
</parent>
<modelVersion>
4.0.0
</modelVersion>
<artifactId>
whatsmars-sentinel
</artifactId>
<packaging>
pom
</packaging>
<modules>
<module>
whatsmars-sentinel-webmvc
</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>
com.alibaba.csp
</groupId>
<artifactId>
sentinel-core
</artifactId>
<version>
${sentinel.version}
</version>
</dependency>
<dependency>
<groupId>
com.alibaba.csp
</groupId>
<artifactId>
sentinel-transport-simple-http
</artifactId>
<version>
${sentinel.version}
</version>
</dependency>
<dependency>
<groupId>
com.alibaba.csp
</groupId>
<artifactId>
sentinel-spring-webmvc-adapter
</artifactId>
<version>
${sentinel.version}
</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>
com.alibaba.csp
</groupId>
<artifactId>
sentinel-core
</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
whatsmars-sentinel/whatsmars-sentinel-webmvc/pom.xml
0 → 100644
浏览文件 @
97654b2a
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<parent>
<artifactId>
whatsmars-sentinel
</artifactId>
<groupId>
org.hongxi
</groupId>
<version>
Rocket.S9
</version>
</parent>
<modelVersion>
4.0.0
</modelVersion>
<artifactId>
whatsmars-sentinel-webmvc
</artifactId>
<dependencies>
<dependency>
<groupId>
com.alibaba.csp
</groupId>
<artifactId>
sentinel-spring-webmvc-adapter
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-web
</artifactId>
</dependency>
<dependency>
<groupId>
org.hongxi
</groupId>
<artifactId>
whatsmars-common
</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
whatsmars-sentinel/whatsmars-sentinel-webmvc/src/main/java/org/hongxi/whatsmars/sentinel/webmvc/Application.java
0 → 100644
浏览文件 @
97654b2a
package
org.hongxi.whatsmars.sentinel.webmvc
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
/**
* Created by shenhongxi on 2020/9/15.
*/
@SpringBootApplication
public
class
Application
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
Application
.
class
,
args
);
}
}
whatsmars-sentinel/whatsmars-sentinel-webmvc/src/main/java/org/hongxi/whatsmars/sentinel/webmvc/config/GlobalExceptionHandler.java
0 → 100644
浏览文件 @
97654b2a
package
org.hongxi.whatsmars.sentinel.webmvc.config
;
import
com.alibaba.csp.sentinel.slots.block.BlockException
;
import
org.hongxi.whatsmars.common.result.Result
;
import
org.hongxi.whatsmars.common.result.ResultHelper
;
import
org.springframework.core.annotation.Order
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ResponseBody
;
/**
* Created by shenhongxi on 2020/9/15.
*/
@ControllerAdvice
@Order
(
0
)
public
class
GlobalExceptionHandler
{
@ExceptionHandler
(
BlockException
.
class
)
@ResponseBody
public
Result
<
Void
>
handleBlockException
(
BlockException
e
)
{
System
.
out
.
println
(
"Blocked by Sentinel: "
+
e
.
getRule
());
return
ResultHelper
.
newErrorResult
(-
1
,
"Blocked by Sentinel"
);
}
}
whatsmars-sentinel/whatsmars-sentinel-webmvc/src/main/java/org/hongxi/whatsmars/sentinel/webmvc/config/InterceptorConfig.java
0 → 100644
浏览文件 @
97654b2a
package
org.hongxi.whatsmars.sentinel.webmvc.config
;
import
com.alibaba.csp.sentinel.adapter.spring.webmvc.SentinelWebInterceptor
;
import
com.alibaba.csp.sentinel.adapter.spring.webmvc.SentinelWebTotalInterceptor
;
import
com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.DefaultBlockExceptionHandler
;
import
com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcConfig
;
import
com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcTotalConfig
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.web.servlet.config.annotation.InterceptorRegistry
;
import
org.springframework.web.servlet.config.annotation.WebMvcConfigurer
;
/**
* Config sentinel interceptor
*/
@Configuration
public
class
InterceptorConfig
implements
WebMvcConfigurer
{
@Override
public
void
addInterceptors
(
InterceptorRegistry
registry
)
{
// Add Sentinel interceptor
addSpringMvcInterceptor
(
registry
);
}
private
void
addSpringMvcInterceptor
(
InterceptorRegistry
registry
)
{
SentinelWebMvcConfig
config
=
new
SentinelWebMvcConfig
();
// Depending on your situation, you can choose to process the BlockException via
// the BlockExceptionHandler or throw it directly, then handle it
// in Spring web global exception handler.
// config.setBlockExceptionHandler((request, response, e) -> { throw e; });
// Use the default handler.
config
.
setBlockExceptionHandler
(
new
DefaultBlockExceptionHandler
());
// Custom configuration if necessary
config
.
setHttpMethodSpecify
(
true
);
// By default web context is true, means that unify web context(i.e. use the default context name),
// in most scenarios that's enough, and it could reduce the memory footprint.
// If set it to false, entrance contexts will be separated by different URLs,
// which is useful to support "chain" relation flow strategy.
// We can change it and view different result in `Resource Chain` menu of dashboard.
config
.
setWebContextUnify
(
true
);
config
.
setOriginParser
(
request
->
request
.
getHeader
(
"S-user"
));
// Add sentinel interceptor
registry
.
addInterceptor
(
new
SentinelWebInterceptor
(
config
)).
addPathPatterns
(
"/**"
);
}
private
void
addSpringMvcTotalInterceptor
(
InterceptorRegistry
registry
)
{
//Config
SentinelWebMvcTotalConfig
config
=
new
SentinelWebMvcTotalConfig
();
//Custom configuration if necessary
config
.
setRequestAttributeName
(
"my_sentinel_spring_mvc_total_entity_container"
);
config
.
setTotalResourceName
(
"my-spring-mvc-total-url-request"
);
//Add sentinel interceptor
registry
.
addInterceptor
(
new
SentinelWebTotalInterceptor
(
config
)).
addPathPatterns
(
"/**"
);
}
}
whatsmars-sentinel/whatsmars-sentinel-webmvc/src/main/java/org/hongxi/whatsmars/sentinel/webmvc/controller/WebMvcTestController.java
0 → 100644
浏览文件 @
97654b2a
package
org.hongxi.whatsmars.sentinel.webmvc.controller
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.servlet.ModelAndView
;
import
java.util.Random
;
import
java.util.concurrent.TimeUnit
;
@Controller
public
class
WebMvcTestController
{
@GetMapping
(
"/hello"
)
@ResponseBody
public
String
apiHello
()
{
doBusiness
();
return
"Hello!"
;
}
@GetMapping
(
"/err"
)
@ResponseBody
public
String
apiError
()
{
doBusiness
();
return
"Oops..."
;
}
@GetMapping
(
"/foo/{id}"
)
@ResponseBody
public
String
apiFoo
(
@PathVariable
(
"id"
)
Long
id
)
{
doBusiness
();
return
"Hello "
+
id
;
}
@GetMapping
(
"/exclude/{id}"
)
@ResponseBody
public
String
apiExclude
(
@PathVariable
(
"id"
)
Long
id
)
{
doBusiness
();
return
"Exclude "
+
id
;
}
@GetMapping
(
"/forward"
)
public
ModelAndView
apiForward
()
{
ModelAndView
mav
=
new
ModelAndView
();
mav
.
setViewName
(
"hello"
);
return
mav
;
}
private
void
doBusiness
()
{
Random
random
=
new
Random
(
1
);
try
{
TimeUnit
.
MILLISECONDS
.
sleep
(
random
.
nextInt
(
100
));
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录