关联知识:
内容提要:
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
server:
port: 8001
spring:
application:
# 服务注册时使用的别名
name: sentinel-provider
cloud:
nacos:
discovery:
# nacos的地址
server-addr: localhost:8848
sentinel:
# 取消控制台懒加载
eager: true
transport:
# 配置sentinel dashboard地址
dashboard: localhost:8080
# sentinel客户端启动的端口,默认8719端口(占用时会依次+1找未占用端口)
port: 8719
package learn.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SentinelProvider {
public static void main(String[] args) {
SpringApplication.run(SentinelProvider.class, args);
}
}
package learn.demo.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
@RestController
@RequestMapping("/sentinel/provider/")
public class ProviderController {
@Value("${spring.application.name} ${server.port}")
private String serverInfo;
@GetMapping("test")
public String test() {
return serverInfo+"\t"+ UUID.randomUUID();
}
}
localhost:8001/sentinel/provider/test
访问接口,能正确返回接口信息 <dependency>
<groupId>com.alibaba.cspgroupId>
<artifactId>sentinel-datasource-nacosartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
server:
port: 8001
spring:
application:
# 服务注册时使用的别名
name: sentinel-provider
profiles:
active: test
cloud:
nacos:
config:
server-addr: localhost:8848
file-extension: yaml
discovery:
# nacos的地址
server-addr: localhost:8848
sentinel:
# 取消控制台懒加载
eager: true
transport:
# 配置sentinel dashboard地址
dashboard: localhost:8080
# sentinel客户端启动的端口,默认8719端口(占用时会依次+1找未占用端口)
port: 8719
datasource:
# 配置的数据源名称,可任意起名
flow:
# 数据源类型
nacos:
# nacos地址
server-addr: ${spring.cloud.nacos.config.server-addr}
# 配置文件名称
data-id: ${spring.application.name}-flow
# namespace(默认用public命名空间时不需要显示声明)和groupId
group-id: DEFAULT_GROUP
data-type: json
# 读取来的json字符串用于sentinel的哪项配置
rule-type: flow
localhost:8001/sentinel/provider/test
访问接口,能正确返回接口信息[
{
"resource":"/sentinel/provider/test",
"limitApp":"default",
"grade":1,
"count":1,
"strategy":0,
"controlBehavior":0,
"clusterMode":false
}
]
localhost:8001/sentinel/provider/test
接口,发现有几次提示Blocked by Sentinel (flow limiting)
server:
port: 8001
spring:
application:
# 服务注册时使用的别名
name: sentinel-provider
profiles:
active: test
cloud:
nacos:
config:
server-addr: localhost:8848
file-extension: yaml
discovery:
# nacos的地址
server-addr: localhost:8848
sentinel:
# 取消控制台懒加载
eager: true
transport:
# 配置sentinel dashboard地址
dashboard: localhost:8080
# sentinel客户端启动的端口,默认8719端口(占用时会依次+1找未占用端口)
port: 8719
datasource:
# 配置的数据源名称,可任意起名
# 热点参数
authority:
# 数据源类型
nacos:
# nacos地址
server-addr: ${spring.cloud.nacos.config.server-addr}
# 配置文件名称
data-id: ${spring.application.name}-authority
# namespace(默认用public命名空间时不需要显示声明)和groupId
group-id: DEFAULT_GROUP
data-type: json
rule-type: authority
package learn.demo.config;
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Component;
@Component
public class SentinelOriginParser implements RequestOriginParser {
@Override
public String parseOrigin(HttpServletRequest request) {
String source = request.getHeader("source");
if (source==null||"".equals(source)) {
source = "unknown";
}
return source;
}
}
[
{
"resource": "/sentinel/provider/test",
"limitApp": "test"
}
]
localhost:8001/sentinel/provider/test
发送get请求,提示Blocked by Sentinel (flow limiting)