Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
SpringCloudAlibaba
提交
0e0aa7fd
S
SpringCloudAlibaba
项目概览
Kwan的解忧杂货铺@新空间代码工作室
/
SpringCloudAlibaba
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
SpringCloudAlibaba
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
0e0aa7fd
编写于
2月 13, 2023
作者:
Kwan的解忧杂货铺@新空间代码工作室
🐭
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:自定义异常
上级
1fdf90cf
变更
7
显示空白变更内容
内联
并排
Showing
7 changed file
with
66 addition
and
29 deletion
+66
-29
nacos-server-common/src/main/java/com/kwan/springcloudalibaba/util/FileContains.java
...n/java/com/kwan/springcloudalibaba/util/FileContains.java
+0
-2
nacos-server-consumer/src/main/resources/bootstrap.yml
nacos-server-consumer/src/main/resources/bootstrap.yml
+0
-13
nacos-server-sentinel-consumer/pom.xml
nacos-server-sentinel-consumer/pom.xml
+6
-0
nacos-server-sentinel-consumer/src/main/java/com/kwan/springcloudalibaba/constant/RespConstant.java
...va/com/kwan/springcloudalibaba/constant/RespConstant.java
+6
-0
nacos-server-sentinel-consumer/src/main/java/com/kwan/springcloudalibaba/controller/SentinelController.java
...wan/springcloudalibaba/controller/SentinelController.java
+4
-1
nacos-server-sentinel-consumer/src/main/java/com/kwan/springcloudalibaba/handler/UrlBlockHandler.java
.../com/kwan/springcloudalibaba/handler/UrlBlockHandler.java
+47
-0
nacos-server-sentinel-consumer/src/main/resources/bootstrap.yml
...server-sentinel-consumer/src/main/resources/bootstrap.yml
+3
-13
未找到文件。
nacos-server-common/src/main/java/com/kwan/springcloudalibaba/util/FileContains.java
浏览文件 @
0e0aa7fd
...
...
@@ -5,7 +5,6 @@ import cn.hutool.core.date.StopWatch;
import
com.alibaba.fastjson2.JSON
;
import
com.google.common.base.Charsets
;
import
com.google.common.io.Files
;
import
lombok.extern.slf4j.Slf4j
;
import
java.io.File
;
import
java.io.IOException
;
...
...
@@ -19,7 +18,6 @@ import java.util.List;
* @version : 2.2.0
* @date : 2023/2/8 10:45
*/
@Slf4j
public
class
FileContains
{
/**
...
...
nacos-server-consumer/src/main/resources/bootstrap.yml
浏览文件 @
0e0aa7fd
...
...
@@ -2,19 +2,6 @@
server
:
port
:
8085
#端点打开
management
:
endpoint
:
health
:
show-details
:
always
endpoints
:
jmx
:
exposure
:
include
:
'
*'
web
:
exposure
:
include
:
'
*'
#spring配置
spring
:
application
:
...
...
nacos-server-sentinel-consumer/pom.xml
浏览文件 @
0e0aa7fd
...
...
@@ -78,6 +78,12 @@
<artifactId>
spring-cloud-starter-netflix-ribbon
</artifactId>
<version>
2.0.0.RELEASE
</version>
</dependency>
<dependency>
<groupId>
com.alibaba.csp
</groupId>
<artifactId>
sentinel-spring-webmvc-adapter
</artifactId>
<version>
1.8.2
</version>
</dependency>
</dependencies>
...
...
nacos-server-sentinel-consumer/src/main/java/com/kwan/springcloudalibaba/constant/RespConstant.java
0 → 100644
浏览文件 @
0e0aa7fd
package
com.kwan.springcloudalibaba.constant
;
public
class
RespConstant
{
public
static
final
Integer
RESP_CODE_FLOW_LIMITING_EXCEPTION
=
441
;
public
static
final
String
RESP_MSG_FLOW_LIMITING_EXCEPTION
=
"流量控制"
;
}
nacos-server-sentinel-consumer/src/main/java/com/kwan/springcloudalibaba/controller/SentinelController.java
浏览文件 @
0e0aa7fd
...
...
@@ -2,6 +2,7 @@ package com.kwan.springcloudalibaba.controller;
import
com.alibaba.csp.sentinel.annotation.SentinelResource
;
import
com.alibaba.csp.sentinel.slots.block.BlockException
;
import
com.kwan.springcloudalibaba.api.FeignUserService
;
import
com.kwan.springcloudalibaba.common.Result
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.MediaType
;
...
...
@@ -43,12 +44,14 @@ public class SentinelController {
* 获取配置的变量
* http://localhost:8086/sentinel/nameInfo
*/
// @SentinelResource(value = "SentinelExceptionHandler",
// blockHandlerClass = SentinelExceptionHandler.class,
// blockHandler = "handleRRException")
@GetMapping
(
value
=
"/nameInfo"
,
produces
=
MediaType
.
APPLICATION_PROBLEM_JSON_VALUE
)
public
Result
nameInfo
()
{
return
Result
.
ok
();
}
/**
* http://localhost:8086/sentinel/world
*
...
...
nacos-server-sentinel-consumer/src/main/java/com/kwan/springcloudalibaba/handler/UrlBlockHandler.java
0 → 100644
浏览文件 @
0e0aa7fd
package
com.kwan.springcloudalibaba.handler
;
import
com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler
;
import
com.alibaba.csp.sentinel.slots.block.BlockException
;
import
com.alibaba.csp.sentinel.slots.block.authority.AuthorityException
;
import
com.alibaba.csp.sentinel.slots.block.degrade.DegradeException
;
import
com.alibaba.csp.sentinel.slots.block.flow.FlowException
;
import
com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException
;
import
com.alibaba.csp.sentinel.slots.system.SystemBlockException
;
import
com.fasterxml.jackson.annotation.JsonInclude
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.kwan.springcloudalibaba.common.Result
;
import
org.springframework.stereotype.Component
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
@Component
public
class
UrlBlockHandler
implements
BlockExceptionHandler
{
@Override
public
void
handle
(
HttpServletRequest
httpServletRequest
,
HttpServletResponse
httpServletResponse
,
BlockException
e
)
throws
Exception
{
String
msg
=
null
;
if
(
e
instanceof
FlowException
)
{
//限流异常
msg
=
"接口已被限流"
;
}
else
if
(
e
instanceof
DegradeException
)
{
//熔断异常
msg
=
"接口已被熔断,请稍后再试"
;
}
else
if
(
e
instanceof
ParamFlowException
)
{
//热点参数限流
msg
=
"热点参数限流"
;
}
else
if
(
e
instanceof
SystemBlockException
)
{
//系统规则异常
msg
=
"系统规则(负载不满足要求)"
;
}
else
if
(
e
instanceof
AuthorityException
)
{
//授权规则异常
msg
=
"授权规则不通过"
;
}
httpServletResponse
.
setStatus
(
500
);
httpServletResponse
.
setCharacterEncoding
(
"UTF-8"
);
httpServletResponse
.
setContentType
(
"application/json;charset=utf-8"
);
//ObjectMapper是内置Jackson的序列化工具类,这用于将对象转为JSON字符串
ObjectMapper
mapper
=
new
ObjectMapper
();
//某个对象属性为null时不进行序列化输出
mapper
.
setSerializationInclusion
(
JsonInclude
.
Include
.
NON_NULL
);
mapper
.
writeValue
(
httpServletResponse
.
getWriter
(),
Result
.
error
(
300
,
msg
)
);
}
}
\ No newline at end of file
nacos-server-sentinel-consumer/src/main/resources/bootstrap.yml
浏览文件 @
0e0aa7fd
...
...
@@ -2,18 +2,6 @@
server
:
port
:
8086
#断电打开
management
:
endpoint
:
health
:
show-details
:
always
endpoints
:
jmx
:
exposure
:
include
:
'
*'
web
:
exposure
:
include
:
'
*'
#spring配置
spring
:
application
:
...
...
@@ -32,4 +20,6 @@ spring:
refresh-enabled
:
true
#默认自动刷新
sentinel
:
transport
:
dashboard
:
120.79.36.53:8181
\ No newline at end of file
dashboard
:
127.0.0.1:8181
port
:
8719
#默认端口,如果被占用则从8719依次+1扫描
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录