Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
S
spring-cloud-timeloit
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
loit-Infrastructure
spring-cloud-timeloit
Commits
a68e7168
提交
a68e7168
authored
8月 20, 2020
作者:
陈世营
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[修改]sentinel 自定义默认异常
上级
dd18ffee
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
111 行增加
和
69 行删除
+111
-69
SentinelWebAutoConfiguration.java
...timeloit/cloud/sentinel/SentinelWebAutoConfiguration.java
+72
-69
CustomBlockExceptionHandler.java
...loit/cloud/sentinel/loit/CustomBlockExceptionHandler.java
+29
-0
README.md
.../src/main/java/com/timeloit/cloud/sentinel/loit/README.md
+10
-0
没有找到文件。
spring-cloud-timeloit-sentinel/src/main/java/com/timeloit/cloud/sentinel/SentinelWebAutoConfiguration.java
浏览文件 @
a68e7168
...
@@ -16,17 +16,15 @@
...
@@ -16,17 +16,15 @@
package
com
.
timeloit
.
cloud
.
sentinel
;
package
com
.
timeloit
.
cloud
.
sentinel
;
import
java.util.Optional
;
import
com.alibaba.csp.sentinel.adapter.spring.webmvc.SentinelWebInterceptor
;
import
com.alibaba.csp.sentinel.adapter.spring.webmvc.SentinelWebInterceptor
;
import
com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler
;
import
com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler
;
import
com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.DefaultBlockExceptionHandler
;
import
com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.DefaultBlockExceptionHandler
;
import
com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser
;
import
com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser
;
import
com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.UrlCleaner
;
import
com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.UrlCleaner
;
import
com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcConfig
;
import
com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcConfig
;
import
com.timeloit.cloud.sentinel.loit.CustomBlockExceptionHandler
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
...
@@ -39,6 +37,8 @@ import org.springframework.util.StringUtils;
...
@@ -39,6 +37,8 @@ import org.springframework.util.StringUtils;
import
org.springframework.web.servlet.config.annotation.InterceptorRegistry
;
import
org.springframework.web.servlet.config.annotation.InterceptorRegistry
;
import
org.springframework.web.servlet.config.annotation.WebMvcConfigurer
;
import
org.springframework.web.servlet.config.annotation.WebMvcConfigurer
;
import
java.util.Optional
;
/**
/**
* @author xiaojing
* @author xiaojing
* @author yuhuangbin
* @author yuhuangbin
...
@@ -50,71 +50,74 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
...
@@ -50,71 +50,74 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@EnableConfigurationProperties
(
SentinelProperties
.
class
)
@EnableConfigurationProperties
(
SentinelProperties
.
class
)
public
class
SentinelWebAutoConfiguration
implements
WebMvcConfigurer
{
public
class
SentinelWebAutoConfiguration
implements
WebMvcConfigurer
{
private
static
final
Logger
log
=
LoggerFactory
private
static
final
Logger
log
=
LoggerFactory
.
getLogger
(
SentinelWebAutoConfiguration
.
class
);
.
getLogger
(
SentinelWebAutoConfiguration
.
class
);
@Autowired
@Autowired
private
SentinelProperties
properties
;
private
SentinelProperties
properties
;
@Autowired
@Autowired
private
Optional
<
UrlCleaner
>
urlCleanerOptional
;
private
Optional
<
UrlCleaner
>
urlCleanerOptional
;
@Autowired
@Autowired
private
Optional
<
BlockExceptionHandler
>
blockExceptionHandlerOptional
;
private
Optional
<
BlockExceptionHandler
>
blockExceptionHandlerOptional
;
@Autowired
@Autowired
private
Optional
<
RequestOriginParser
>
requestOriginParserOptional
;
private
Optional
<
RequestOriginParser
>
requestOriginParserOptional
;
@Autowired
@Autowired
private
Optional
<
SentinelWebInterceptor
>
sentinelWebInterceptorOptional
;
private
Optional
<
SentinelWebInterceptor
>
sentinelWebInterceptorOptional
;
@Override
public
void
addInterceptors
(
InterceptorRegistry
registry
)
{
@Override
if
(!
sentinelWebInterceptorOptional
.
isPresent
())
{
public
void
addInterceptors
(
InterceptorRegistry
registry
)
{
return
;
if
(!
sentinelWebInterceptorOptional
.
isPresent
())
{
}
return
;
SentinelProperties
.
Filter
filterConfig
=
properties
.
getFilter
();
}
registry
.
addInterceptor
(
sentinelWebInterceptorOptional
.
get
())
SentinelProperties
.
Filter
filterConfig
=
properties
.
getFilter
();
.
order
(
filterConfig
.
getOrder
())
registry
.
addInterceptor
(
sentinelWebInterceptorOptional
.
get
())
.
addPathPatterns
(
filterConfig
.
getUrlPatterns
());
.
order
(
filterConfig
.
getOrder
())
log
.
info
(
.
addPathPatterns
(
filterConfig
.
getUrlPatterns
());
"[Sentinel Starter] register SentinelWebInterceptor with urlPatterns: {}."
,
log
.
info
(
filterConfig
.
getUrlPatterns
());
"[Sentinel Starter] register SentinelWebInterceptor with urlPatterns: {}."
,
}
filterConfig
.
getUrlPatterns
());
}
@Bean
@ConditionalOnProperty
(
name
=
"spring.cloud.sentinel.filter.enabled"
,
@Bean
matchIfMissing
=
true
)
@ConditionalOnProperty
(
name
=
"spring.cloud.sentinel.filter.enabled"
,
public
SentinelWebInterceptor
sentinelWebInterceptor
(
matchIfMissing
=
true
)
SentinelWebMvcConfig
sentinelWebMvcConfig
)
{
public
SentinelWebInterceptor
sentinelWebInterceptor
(
return
new
SentinelWebInterceptor
(
sentinelWebMvcConfig
);
SentinelWebMvcConfig
sentinelWebMvcConfig
)
{
}
return
new
SentinelWebInterceptor
(
sentinelWebMvcConfig
);
}
@Bean
@ConditionalOnProperty
(
name
=
"spring.cloud.sentinel.filter.enabled"
,
@Bean
matchIfMissing
=
true
)
@ConditionalOnProperty
(
name
=
"spring.cloud.sentinel.filter.enabled"
,
public
SentinelWebMvcConfig
sentinelWebMvcConfig
()
{
matchIfMissing
=
true
)
SentinelWebMvcConfig
sentinelWebMvcConfig
=
new
SentinelWebMvcConfig
();
public
SentinelWebMvcConfig
sentinelWebMvcConfig
()
{
sentinelWebMvcConfig
.
setHttpMethodSpecify
(
properties
.
getHttpMethodSpecify
());
SentinelWebMvcConfig
sentinelWebMvcConfig
=
new
SentinelWebMvcConfig
();
sentinelWebMvcConfig
.
setHttpMethodSpecify
(
properties
.
getHttpMethodSpecify
());
if
(
blockExceptionHandlerOptional
.
isPresent
())
{
blockExceptionHandlerOptional
if
(
blockExceptionHandlerOptional
.
isPresent
())
{
.
ifPresent
(
sentinelWebMvcConfig:
:
setBlockExceptionHandler
);
blockExceptionHandlerOptional
}
.
ifPresent
(
sentinelWebMvcConfig:
:
setBlockExceptionHandler
);
else
{
}
else
{
if
(
StringUtils
.
hasText
(
properties
.
getBlockPage
()))
{
if
(
StringUtils
.
hasText
(
properties
.
getBlockPage
()))
{
sentinelWebMvcConfig
.
setBlockExceptionHandler
(((
request
,
response
,
sentinelWebMvcConfig
.
setBlockExceptionHandler
(((
request
,
response
,
e
)
->
response
.
sendRedirect
(
properties
.
getBlockPage
())));
e
)
->
response
.
sendRedirect
(
properties
.
getBlockPage
())));
}
}
else
{
else
{
// sentinelWebMvcConfig
sentinelWebMvcConfig
// .setBlockExceptionHandler(new DefaultBlockExceptionHandler());
.
setBlockExceptionHandler
(
new
DefaultBlockExceptionHandler
());
}
// for loitTime custom sentinel exception by chenshiying
}
sentinelWebMvcConfig
.
setBlockExceptionHandler
(
new
CustomBlockExceptionHandler
());
urlCleanerOptional
.
ifPresent
(
sentinelWebMvcConfig:
:
setUrlCleaner
);
}
requestOriginParserOptional
.
ifPresent
(
sentinelWebMvcConfig:
:
setOriginParser
);
}
return
sentinelWebMvcConfig
;
}
urlCleanerOptional
.
ifPresent
(
sentinelWebMvcConfig:
:
setUrlCleaner
);
requestOriginParserOptional
.
ifPresent
(
sentinelWebMvcConfig:
:
setOriginParser
);
return
sentinelWebMvcConfig
;
}
}
}
spring-cloud-timeloit-sentinel/src/main/java/com/timeloit/cloud/sentinel/loit/CustomBlockExceptionHandler.java
0 → 100644
浏览文件 @
a68e7168
package
com
.
timeloit
.
cloud
.
sentinel
.
loit
;
import
com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler
;
import
com.alibaba.csp.sentinel.slots.block.BlockException
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.MediaType
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.PrintWriter
;
public
class
CustomBlockExceptionHandler
implements
BlockExceptionHandler
{
@Override
public
void
handle
(
HttpServletRequest
request
,
HttpServletResponse
response
,
BlockException
e
)
throws
Exception
{
response
.
setStatus
(
HttpStatus
.
OK
.
value
());
System
.
out
.
println
(
MediaType
.
APPLICATION_JSON
.
toString
());
response
.
setContentType
(
MediaType
.
APPLICATION_JSON_UTF8_VALUE
);
PrintWriter
out
=
response
.
getWriter
();
out
.
print
(
"{\"success\":false,\"code\":\"444\",\"msg\":\"系统繁忙请稍后再试\"}"
);
out
.
flush
();
out
.
close
();
}
}
spring-cloud-timeloit-sentinel/src/main/java/com/timeloit/cloud/sentinel/loit/README.md
0 → 100644
浏览文件 @
a68e7168
# 时代凌宇sentinel 自定义异常类
SentinelWebAutoConfiguration.sentinelWebMvcConfig
```
$xslt
// for loitTime custom sentinel exception by chenshiying
sentinelWebMvcConfig
.setBlockExceptionHandler(new CustomBlockExceptionHandler());
```
\ No newline at end of file
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论