Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
SpringCloudAlibaba
提交
2177cfd0
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看板
提交
2177cfd0
编写于
2月 03, 2023
作者:
Q
qinyingjie
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:sentinel
上级
da1296d8
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
413 addition
and
0 deletion
+413
-0
springcloudalibaba-server-sentinel/pom.xml
springcloudalibaba-server-sentinel/pom.xml
+104
-0
springcloudalibaba-server-sentinel/src/main/java/com/kwan/springcloudalibaba/SentinelApplication.java
...java/com/kwan/springcloudalibaba/SentinelApplication.java
+20
-0
springcloudalibaba-server-sentinel/src/main/java/com/kwan/springcloudalibaba/common/Result.java
.../main/java/com/kwan/springcloudalibaba/common/Result.java
+119
-0
springcloudalibaba-server-sentinel/src/main/java/com/kwan/springcloudalibaba/constant/CommonConstant.java
.../com/kwan/springcloudalibaba/constant/CommonConstant.java
+16
-0
springcloudalibaba-server-sentinel/src/main/java/com/kwan/springcloudalibaba/controller/SentinelController.java
...wan/springcloudalibaba/controller/SentinelController.java
+51
-0
springcloudalibaba-server-sentinel/src/main/java/com/kwan/springcloudalibaba/controller/TestController.java
...om/kwan/springcloudalibaba/controller/TestController.java
+55
-0
springcloudalibaba-server-sentinel/src/main/resources/bootstrap.yml
...dalibaba-server-sentinel/src/main/resources/bootstrap.yml
+35
-0
springcloudalibaba-server-sentinel/src/test/java/com/kwan/springcloudalibaba/SentinelApplicationTests.java
...com/kwan/springcloudalibaba/SentinelApplicationTests.java
+13
-0
未找到文件。
springcloudalibaba-server-sentinel/pom.xml
0 → 100644
浏览文件 @
2177cfd0
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<groupId>
com.kwan.springcloudalibaba
</groupId>
<artifactId>
springcloudalibaba-server-sentinel
</artifactId>
<name>
springcloudalibaba-server-sentinel
</name>
<description>
springcloudalibaba-server-sentinel
</description>
<version>
1.0.0
</version>
<properties>
<java.version>
1.8
</java.version>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
<project.reporting.outputEncoding>
UTF-8
</project.reporting.outputEncoding>
<spring-boot.version>
2.3.0.RELEASE
</spring-boot.version>
<spring-cloud-alibaba.version>
2.2.5.RELEASE
</spring-cloud-alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-web
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-test
</artifactId>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
com.alibaba.cloud
</groupId>
<artifactId>
spring-cloud-starter-alibaba-nacos-discovery
</artifactId>
</dependency>
<dependency>
<groupId>
com.alibaba.cloud
</groupId>
<artifactId>
spring-cloud-starter-alibaba-nacos-config
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-actuator
</artifactId>
</dependency>
<dependency>
<groupId>
org.projectlombok
</groupId>
<artifactId>
lombok
</artifactId>
</dependency>
<dependency>
<groupId>
com.alibaba.csp
</groupId>
<artifactId>
sentinel-core
</artifactId>
</dependency>
<!-- 客户端需要引入 Transport 模块来与 Sentinel 控制台进行通信-->
<dependency>
<groupId>
com.alibaba.csp
</groupId>
<artifactId>
sentinel-transport-simple-http
</artifactId>
</dependency>
<!--sentinel启动器-->
<dependency>
<groupId>
com.alibaba.cloud
</groupId>
<artifactId>
spring-cloud-starter-alibaba-sentinel
</artifactId>
</dependency>
<!--sentinel持久化存储-->
<dependency>
<groupId>
com.alibaba.csp
</groupId>
<artifactId>
sentinel-datasource-nacos
</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>
com.alibaba.cloud
</groupId>
<artifactId>
spring-cloud-alibaba-dependencies
</artifactId>
<version>
${spring-cloud-alibaba.version}
</version>
<type>
pom
</type>
<scope>
import
</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-dependencies</artifactId>-->
<!-- <version>${spring-cloud.version}</version>-->
<!-- <type>pom</type>-->
<!-- <scope>import</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-dependencies
</artifactId>
<version>
${spring-boot.version}
</version>
<type>
pom
</type>
<scope>
import
</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-maven-plugin
</artifactId>
</plugin>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-compiler-plugin
</artifactId>
<configuration>
<source>
7
</source>
<target>
7
</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
springcloudalibaba-server-sentinel/src/main/java/com/kwan/springcloudalibaba/SentinelApplication.java
0 → 100644
浏览文件 @
2177cfd0
package
com.kwan.springcloudalibaba
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
/**
* http://localhost:18088/sentinel/nameInfo
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/1/17 12:37
*/
@SpringBootApplication
public
class
SentinelApplication
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
SentinelApplication
.
class
,
args
);
}
}
springcloudalibaba-server-sentinel/src/main/java/com/kwan/springcloudalibaba/common/Result.java
0 → 100644
浏览文件 @
2177cfd0
package
com.kwan.springcloudalibaba.common
;
import
com.kwan.springcloudalibaba.constant.CommonConstant
;
import
lombok.Data
;
import
java.io.Serializable
;
/**
* 接口返回数据格式
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/1/8 10:48
*/
@Data
public
class
Result
<
T
>
implements
Serializable
{
private
static
final
long
serialVersionUID
=
1L
;
/**
* 成功标志
*/
private
boolean
success
=
true
;
/**
* 返回处理消息
*/
private
String
message
=
"操作成功!"
;
/**
* 返回代码
*/
private
Integer
code
=
0
;
/**
* 返回数据对象 data
*/
private
T
result
;
/**
* 时间戳
*/
private
long
timestamp
=
System
.
currentTimeMillis
();
public
Result
()
{
}
public
Result
<
T
>
success
(
String
message
)
{
this
.
message
=
message
;
this
.
code
=
CommonConstant
.
SC_OK_200
;
this
.
success
=
true
;
return
this
;
}
public
Result
<
T
>
good
(
T
t
)
{
this
.
setResult
(
t
);
this
.
code
=
CommonConstant
.
SC_OK_200
;
this
.
success
=
true
;
return
this
;
}
public
Result
<
T
>
good
()
{
this
.
code
=
CommonConstant
.
SC_OK_200
;
this
.
success
=
true
;
this
.
setMessage
(
"成功"
);
return
this
;
}
public
Result
<
T
>
fail
(
String
msg
)
{
this
.
setCode
(
CommonConstant
.
SC_INTERNAL_SERVER_ERROR_500
);
this
.
setMessage
(
msg
);
this
.
setSuccess
(
false
);
return
this
;
}
public
static
Result
<
Object
>
ok
()
{
Result
<
Object
>
r
=
new
Result
<>();
r
.
setSuccess
(
true
);
r
.
setCode
(
CommonConstant
.
SC_OK_200
);
r
.
setMessage
(
"成功"
);
return
r
;
}
public
static
Result
<
Object
>
ok
(
String
msg
)
{
Result
<
Object
>
r
=
new
Result
<>();
r
.
setSuccess
(
true
);
r
.
setCode
(
CommonConstant
.
SC_OK_200
);
r
.
setMessage
(
msg
);
return
r
;
}
public
static
Result
<
Object
>
ok
(
Object
data
)
{
Result
<
Object
>
r
=
new
Result
<
Object
>();
r
.
setSuccess
(
true
);
r
.
setCode
(
CommonConstant
.
SC_OK_200
);
r
.
setResult
(
data
);
return
r
;
}
public
static
Result
<
Object
>
error
(
String
msg
)
{
return
error
(
CommonConstant
.
SC_INTERNAL_SERVER_ERROR_500
,
msg
);
}
public
static
Result
<
Object
>
error
(
int
code
,
String
msg
)
{
Result
<
Object
>
r
=
new
Result
<
Object
>();
r
.
setCode
(
code
);
r
.
setMessage
(
msg
);
r
.
setSuccess
(
false
);
return
r
;
}
public
Result
<
T
>
error500
(
String
message
)
{
this
.
message
=
message
;
this
.
code
=
CommonConstant
.
SC_INTERNAL_SERVER_ERROR_500
;
this
.
success
=
false
;
return
this
;
}
/**
* 无权限访问返回结果
*/
public
static
Result
<
Object
>
noauth
(
String
msg
)
{
return
error
(
CommonConstant
.
SC_JEECG_NO_AUTHZ
,
msg
);
}
}
\ No newline at end of file
springcloudalibaba-server-sentinel/src/main/java/com/kwan/springcloudalibaba/constant/CommonConstant.java
0 → 100644
浏览文件 @
2177cfd0
package
com.kwan.springcloudalibaba.constant
;
public
class
CommonConstant
{
/**
* 成功
*/
public
static
final
Integer
SC_OK_200
=
200
;
/**
* 服务器错误
*/
public
static
final
Integer
SC_INTERNAL_SERVER_ERROR_500
=
500
;
/**
* 未认证
*/
public
static
final
int
SC_JEECG_NO_AUTHZ
=
401
;
}
\ No newline at end of file
springcloudalibaba-server-sentinel/src/main/java/com/kwan/springcloudalibaba/controller/SentinelController.java
0 → 100644
浏览文件 @
2177cfd0
package
com.kwan.springcloudalibaba.controller
;
import
com.alibaba.csp.sentinel.annotation.SentinelResource
;
import
com.alibaba.csp.sentinel.slots.block.BlockException
;
import
com.kwan.springcloudalibaba.common.Result
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.http.MediaType
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
/**
* http://localhost:18088/sentinel/nameInfo
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/2/2 17:13
*/
@RestController
@RequestMapping
(
"/sentinel"
)
public
class
SentinelController
{
@Value
(
"${user.name}"
)
private
String
nameInfo
;
/**
* 获取配置的变量
*/
@GetMapping
(
value
=
"/nameInfo"
,
produces
=
MediaType
.
APPLICATION_PROBLEM_JSON_VALUE
)
public
Result
nameInfo
()
{
return
Result
.
ok
(
nameInfo
);
}
/**
* http://localhost:18088/sentinel/world
*
* @return
*/
@GetMapping
(
"/world"
)
@SentinelResource
(
value
=
"helloWorld"
,
blockHandler
=
"helloBlock"
)
public
String
helloWorld
()
{
return
"Hello world"
;
}
public
String
helloBlock
(
BlockException
e
)
{
return
"你已被流控"
;
}
}
springcloudalibaba-server-sentinel/src/main/java/com/kwan/springcloudalibaba/controller/TestController.java
0 → 100644
浏览文件 @
2177cfd0
package
com.kwan.springcloudalibaba.controller
;
import
com.alibaba.csp.sentinel.Entry
;
import
com.alibaba.csp.sentinel.SphU
;
import
com.alibaba.csp.sentinel.slots.block.BlockException
;
import
com.alibaba.csp.sentinel.slots.block.RuleConstant
;
import
com.alibaba.csp.sentinel.slots.block.flow.FlowRule
;
import
com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
* 流量控制代码实现-侵入式
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/2/2 16:58
*/
public
class
TestController
{
public
static
void
main
(
String
[]
args
)
{
initFlowRules
();
while
(
true
)
{
Entry
entry
=
null
;
try
{
entry
=
SphU
.
entry
(
"HelloWorld"
);
/*您的业务逻辑 - 开始*/
System
.
out
.
println
(
"hello world"
);
/*您的业务逻辑 - 结束*/
}
catch
(
BlockException
e1
)
{
/*流控逻辑处理 - 开始*/
System
.
out
.
println
(
"block!"
);
/*流控逻辑处理 - 结束*/
}
finally
{
if
(
entry
!=
null
)
{
entry
.
exit
();
}
}
}
}
/**
* 资源 HelloWorld 每秒最多只能通过 20 个请求。
*/
private
static
void
initFlowRules
()
{
List
<
FlowRule
>
rules
=
new
ArrayList
<>();
FlowRule
rule
=
new
FlowRule
();
rule
.
setResource
(
"HelloWorld"
);
rule
.
setGrade
(
RuleConstant
.
FLOW_GRADE_QPS
);
// Set limit QPS to 20.
rule
.
setCount
(
1
);
rules
.
add
(
rule
);
FlowRuleManager
.
loadRules
(
rules
);
}
}
\ No newline at end of file
springcloudalibaba-server-sentinel/src/main/resources/bootstrap.yml
0 → 100644
浏览文件 @
2177cfd0
#端口号
server
:
port
:
18088
#断电打开
management
:
endpoint
:
health
:
show-details
:
always
endpoints
:
jmx
:
exposure
:
include
:
'
*'
web
:
exposure
:
include
:
'
*'
#spring配置
spring
:
application
:
name
:
sentinel-demo
profiles
:
active
:
dev
cloud
:
nacos
:
discovery
:
server-addr
:
http://120.79.36.53:8848
#服务注册地址
config
:
server-addr
:
http://120.79.36.53:8848
#配置中心地址
file-extension
:
yaml
#文件类型
group
:
DEV_GROUP
#组别
namespace
:
e750dcd5-657b-489b-8d15-b7b71aa3e984
#命名空间
refresh-enabled
:
true
#默认自动刷新
sentinel
:
transport
:
dashboard
:
120.79.36.53:8181
springcloudalibaba-server-sentinel/src/test/java/com/kwan/springcloudalibaba/SentinelApplicationTests.java
0 → 100644
浏览文件 @
2177cfd0
package
com.kwan.springcloudalibaba
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.boot.test.context.SpringBootTest
;
@SpringBootTest
class
SentinelApplicationTests
{
@Test
void
contextLoads
()
{
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录