Commit 002d6d9e by 李根

初始化

parent 6d512971
# springcloud 2.x整合 sentinel
包含如下两个部分
## 控制台
控制台是一个独立的springboot项目。可以和我们的服务完全解耦,独立运行。适当修改配置直接启动即可
## 普通服务
## 网关服务
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.alibaba.csp:sentinel-core:1.7.1" level="project" />
<orderEntry type="library" name="Maven: com.ning:async-http-client:1.9.40" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty:3.10.6.Final" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.12" level="project" />
</component>
</module>
\ No newline at end of file
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo.sentinel</groupId>
<artifactId>Test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>1.7.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.ning/async-http-client -->
<dependency>
<groupId>com.ning</groupId>
<artifactId>async-http-client</artifactId>
<version>1.9.40</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
package com.demo.senthinel;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.ListenableFuture;
import com.ning.http.client.Response;
import java.util.Random;
/**
* @author ligen
* @title: Test
* @projectName sentinel
* @description:
* @date 2020/3/2411:48
*/
public class Test {
public static void main(String[] args) {
// 测试限流
test(5, "/test/test2");
// 测试降级
// test(5, "/test3");
}
/**
* @ desc : 测试限流
* @ params
* @ return
* @ date 2020/3/31
* @ author ligen
*/
public static void test(int threadCnt, String resource) {
for (int i1 = 0; i1 < threadCnt; i1++) {
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
Random random = new Random();
Thread thread = new Thread(
() -> {
while (true) {
ListenableFuture<Response> execute = asyncHttpClient.prepareGet("http://localhost:7051/columbia" + resource).execute();
System.out.println("resources:"+resource+"-----"+Thread.currentThread().getName());
try {
Thread.sleep(random.nextInt(1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
);
thread.start();
}
}
}
......@@ -5,15 +5,13 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo.sentinel</groupId>
<artifactId>sentinel-dashboard-plus</artifactId>
<artifactId>spring-cloud-sentinel</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>sentinel-dashboard</module>
<module>sentinel-demo-springboot-nacos</module>
<module>sentinel-demo-springboot-cluster</module>
<module>Test</module>
<!-- <module></module>-->
<module>sentinel-demo-gateway-nacos</module>
</modules>
......
......@@ -58,4 +58,22 @@ java -Dserver.port=8080 \
客户端正确配置并启动后,会**在初次调用后**主动向控制台发送心跳包,汇报自己的存在;
控制台收到客户端心跳包之后,会在左侧导航栏中显示该客户端信息。如果控制台能够看到客户端的机器信息,则表明客户端接入成功了。
更多:[控制台功能介绍](./Sentinel_Dashboard_Feature.md)。
\ No newline at end of file
更多:[控制台功能介绍](./Sentinel_Dashboard_Feature.md)。
## 4 改进。
官方的demo 主要是将规则直接推送至客户端,给出了nacos持久化流控规则的demo。拓展主要目标是,做成一个可以生产环境方便使用的控制台。
通过配置将规则持久化至配置中心。
已完成:
- 普通服务的流控规则,降级规则的nacos持久化
- 网关服务的 api 分组,网关流控规则的nacos持久化
- 监控数据的mongo持久化
未完成:
- 系统规则持久化至nacos
- 集群规则持久化至nacos
- 授权规则持久化至nacos
- 监控数据的持久化至mysql
spring:
application:
name: sentinel-dashboard-plus
data:
mongodb:
port: 27017
database: db1
uri: mongodb://lg:123456@118.25.153.63
authentication-database: admin
# 如果要监控数据持久化的话需要
# data:
# mongodb:
# port: 27017
# database: db1
# uri: mongodb://lg:123456@118.25.153.63
# authentication-database: admin
http:
encoding:
charset: utf-8
......@@ -19,27 +20,28 @@ sentinel:
auth:
enabled: false
# password: sentinel
# username: sentinel
filter:
exclude-urls: /,/auth/login,/auth/logout,/registry/machine,/version,/test
exclude-url-suffixes: htm,html,js,css,map,ico,ttf,woff,png
# password: sentinel
# username: sentinel
#logging:
# file: ${user.home}/logs/csp/sentinel-dashboard.log
# pattern:
# file: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"
# level:
# springframework:
# web: INFO
nacos:
degradeDataIdPrefix: degrade-rules
serverAddr: 10.16.10.139
groupId: DEFAULT_GROUP
flowDataIdPrefix: flow-rules
namespace: 31b168cb-51ee-4b8b-adfa-29b42d4afa0b
paramFlowDataIdPrefix: param-rules
# 控制台日志文件保存路径
logging:
file: C:/Users/xx/Desktop/sentinel-log/sentinel-dashboard.log
pattern:
file: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"
# nacos 持久化规则配置
nacos:
serverAddr: 10.16.10.139 # nacos地址
groupId: DEFAULT_GROUP # group
namespace: 31b168cb-51ee-4b8b-adfa-29b42d4afa0b # namespace
degradeDataIdPrefix: degrade-rules # 降级规则
flowDataIdPrefix: flow-rules # 流控规则
paramFlowDataIdPrefix: param-rules # 热点参数规则
gatewayFlowRuleDataIdPrefix: gateway-flow-rules # 网关流控规则
gatewayApiDefineDataIdPrefix: gateway-api-define-rules # 网关api分组规则
spring:
application:
name: sentinel-dashboard-plus
data:
mongodb:
port: 27017
database: db1
uri: mongodb://lg:123456@xx.xx.xx.xx
authentication-database: admin
http:
encoding:
charset: utf-8
force: true
enabled: true
sentinel:
dashboard:
version: ${project.version}
auth:
enabled: false
filter:
exclude-urls: /,/auth/login,/auth/logout,/registry/machine,/version,/test
exclude-url-suffixes: htm,html,js,css,map,ico,ttf,woff,png
# password: sentinel
# username: sentinel
#logging:
# file: ${user.home}/logs/csp/sentinel-dashboard.log
# pattern:
# file: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"
# level:
# springframework:
# web: INFO
#nacos:
# degradeDataIdPrefix: degrade-rules
# serverAddr: xx.xx.xx.xx
# groupId: DEFAULT_GROUP
# flowDataIdPrefix: flow-rules
# namespace: 31b168cb-51ee-4b8b-adfa-29b42d4afa0b
# paramFlowDataIdPrefix: param-rules
/*
* Copyright 1999-2019 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.demo.alibaba.sentinel.config;
import com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
import com.alibaba.csp.sentinel.adapter.gateway.sc.exception.SentinelGatewayBlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.web.reactive.result.view.ViewResolver;
import javax.annotation.PostConstruct;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @author Eric Zhao
*/
@Configuration
public class SentinelGatewayConfig {
private final List<ViewResolver> viewResolvers;
private final ServerCodecConfigurer serverCodecConfigurer;
public SentinelGatewayConfig(ObjectProvider<List<ViewResolver>> viewResolversProvider,
ServerCodecConfigurer serverCodecConfigurer) {
this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);
this.serverCodecConfigurer = serverCodecConfigurer;
}
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() {
// Register the block exception handler for Spring Cloud Gateway.
return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);
}
/**
* @说明: 添加初始化规则(API分组规则, 限流规则)
*/
@PostConstruct
public void doInit() {
// initCustomizedApis();
// initGatewayRules();
}
/**
* @说明: ApiDefinition 自定义的API分组, 可以针对分组进行限流
* <p>
* 这里只是对Api进行分组,并没有限流的任何设置, 在初始化网关限流规则,资源模式的 的时候,
* 才指定,规则应用于API分组还是资源id(配置文件中routes下的id),
*/
private void initCustomizedApis() {
Set<ApiDefinition> definitions = new HashSet<>();
// api分组名称
ApiDefinition api1 = new ApiDefinition("challenger-simlink")
//分组规则集合
.setPredicateItems(new HashSet<ApiPredicateItem>() {{
// 模式,没有指顶匹配策略,默认完全匹配,共三种策略可选,前缀,完全匹配,正则
// 指定策略 匹配前缀
add(new ApiPathPredicateItem().setPattern("/challenger/**")
.setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
}});
ApiDefinition api2 = new ApiDefinition("columbia-simlink")
.setPredicateItems(new HashSet<ApiPredicateItem>() {{
add(new ApiPathPredicateItem().setPattern("/columbia/**")
.setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
}});
definitions.add(api1);
definitions.add(api2);
GatewayApiDefinitionManager.loadApiDefinitions(definitions);
}
/**
* @说明: 1 GatewayFlowRule 网关限流规则,争对API Gateway 的场景定制的限流规则,
* <p>
* 网关限流有两个维度 (API分组 routeId: 配置文件中的routeId)
* <p>
* 可以针对不同 route 或自定义的 API 分组进行限流,支持针对请求中的参数、Header、来源 IP 等进行定制化的限流。
* <p>
* 如果要初始化规则,如下 。通过设置ResourceMode 来说明规则是应用于routeId 还是 api分组
*/
private void initGatewayRules() {
Set<GatewayFlowRule> rules = new HashSet<>();
rules.add(new GatewayFlowRule("aliyun_route")
.setCount(10)
.setIntervalSec(1)
);
rules.add(new GatewayFlowRule("aliyun_route")
.setCount(2)
.setIntervalSec(2)
.setBurst(2)
.setParamItem(new GatewayParamFlowItem()
.setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_CLIENT_IP)
)
);
rules.add(new GatewayFlowRule("httpbin_route")
.setCount(10)
.setIntervalSec(1)
.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER)
.setMaxQueueingTimeoutMs(600)
.setParamItem(new GatewayParamFlowItem()
.setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_HEADER)
.setFieldName("X-Sentinel-Flag")
)
);
rules.add(new GatewayFlowRule("httpbin_route")
.setCount(1)
.setIntervalSec(1)
.setParamItem(new GatewayParamFlowItem()
.setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM)
.setFieldName("pa")
)
);
rules.add(new GatewayFlowRule("httpbin_route")
.setCount(2)
.setIntervalSec(30)
.setParamItem(new GatewayParamFlowItem()
.setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM)
.setFieldName("type")
.setPattern("warn")
.setMatchStrategy(SentinelGatewayConstants.PARAM_MATCH_STRATEGY_CONTAINS)
)
);
rules.add(new GatewayFlowRule("some_customized_api")
.setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME)
.setCount(5)
.setIntervalSec(1)
.setParamItem(new GatewayParamFlowItem()
.setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM)
.setFieldName("pn")
)
);
GatewayRuleManager.loadRules(rules);
}
}
package com.demo.alibaba.sentinel.config;
import com.alibaba.cloud.sentinel.SentinelProperties;
import com.alibaba.cloud.sentinel.datasource.RuleType;
import com.alibaba.cloud.sentinel.datasource.config.DataSourcePropertiesConfiguration;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.demo.alibaba.sentinel.entry.MyApiDefine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
import java.util.*;
/**
* @author ligen
* @title: NacosStartService
* @projectName dashboardplus
* @description:
* @date 2020/4/214:08
*/
@Configuration
public class SentinelInitConfig implements CommandLineRunner {
Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private SentinelProperties sentinelProperties;
@Override
public void run(String... args) throws Exception {
initNacosConfigFromNacos();
}
private void initNacosConfigFromNacos() {
logger.info("[NacosSource初始化,从Nacos中获取熔断规则]");
sentinelProperties.getDatasource().values().stream().filter(dataSourcePropertiesConfiguration -> dataSourcePropertiesConfiguration.getNacos() != null).map(DataSourcePropertiesConfiguration::getNacos).forEach(nacos -> {
Properties properties = new Properties() {{
setProperty("serverAddr", nacos.getServerAddr());
setProperty("namespace", "31b168cb-51ee-4b8b-adfa-29b42d4afa0b");
}};
// 网关分组规则
if (Objects.equals(nacos.getRuleType(), RuleType.GW_API_GROUP)) {
ReadableDataSource<String, Set<ApiDefinition>> apiDefinition = new NacosDataSource<>(properties,
nacos.getGroupId(), nacos.getDataId(),
source -> {
Set<MyApiDefine> myApiDefines = JSON.parseObject(source, new TypeReference<Set<MyApiDefine>>() {
});
Set<ApiDefinition> apiDefinitions = new HashSet<>();
myApiDefines.forEach(myApiDefine -> {
HashSet<ApiPredicateItem> apiPredicateItems = new HashSet<>();
// 匹配规则赋值
for (ApiPathPredicateItem p : myApiDefine.getPredicateItems()) {
apiPredicateItems.add(p);
}
ApiDefinition temp = new ApiDefinition() {{
setApiName(myApiDefine.getApiName());
setPredicateItems(apiPredicateItems);
}};
apiDefinitions.add(temp);
});
return apiDefinitions;
});
// 注册规则
GatewayApiDefinitionManager.register2Property(apiDefinition.getProperty());
}
// 网关限流规则
if (Objects.equals(nacos.getRuleType(), RuleType.GW_FLOW)) {
ReadableDataSource<String, Set<GatewayFlowRule>> flowRuleDataSource = new NacosDataSource<>(properties,
nacos.getGroupId(), nacos.getDataId(),
source -> JSON.parseObject(source, new TypeReference<Set<GatewayFlowRule>>() {
}));
GatewayRuleManager.register2Property(flowRuleDataSource.getProperty());
}
// 网关降级规则
if (Objects.equals(nacos.getRuleType(), RuleType.DEGRADE)) {
ReadableDataSource<String, List<DegradeRule>> degradeRuleDataSource = new NacosDataSource<>(properties,
nacos.getGroupId(), nacos.getDataId(),
source -> JSON.parseObject(source, new TypeReference<List<DegradeRule>>() {
}));
DegradeRuleManager.register2Property(degradeRuleDataSource.getProperty());
}
});
}
}
package com.demo.alibaba.sentinel.entry;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem;
import java.util.Objects;
import java.util.Set;
/**
* @author ligen
* @title: MyApiDefine
* @projectName simlink
* @description: 自定义一个ApiDefin, 用来反序列化naocs的网关api分组规则,
* 默认的ApiDefinition 中, 属性predicateItems定义的是接口,进行反序列化的时候。会丢失掉这部分数据,
* 导致nacos上的api分组数据,不能被正确的注入到sentinel中。
* @date 2020/4/1810:39
*/
public class MyApiDefine {
private String apiName;
private Set<ApiPathPredicateItem> predicateItems;
public MyApiDefine() {
}
public MyApiDefine(String apiName) {
this.apiName = apiName;
}
public String getApiName() {
return this.apiName;
}
public MyApiDefine setApiName(String apiName) {
this.apiName = apiName;
return this;
}
public Set<ApiPathPredicateItem> getPredicateItems() {
return predicateItems;
}
public MyApiDefine setPredicateItems(Set<ApiPathPredicateItem> predicateItems) {
this.predicateItems = predicateItems;
return this;
}
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (o != null && this.getClass() == o.getClass()) {
ApiDefinition that = (ApiDefinition) o;
return !Objects.equals(this.apiName, that.getApiName()) ? false : Objects.equals(this.predicateItems, that.getPredicateItems());
} else {
return false;
}
}
public int hashCode() {
int result = this.apiName != null ? this.apiName.hashCode() : 0;
result = 31 * result + (this.predicateItems != null ? this.predicateItems.hashCode() : 0);
return result;
}
public String toString() {
return "ApiDefinition{apiName='" + this.apiName + '\'' + ", predicateItems=" + this.predicateItems + '}';
}
}
spring:
application:
name: gateway-${APPLICATION_NAME:simlink}
cloud:
gateway:
enabled: true
discovery:
locator:
lower-case-service-id: true
routes:
# app1
- id: challenger
uri: http://127.0.0.1:7011/
predicates:
- Path=/challenger/**
filters:
- StripPrefix=1
# app2
- id: columbia
uri: http://127.0.0.1:7031/
predicates:
- Path=/columbia/**
filters:
- StripPrefix=1
# sentinel 配置 dataId 根据控制台推送到nacos上的配置文件名称
sentinel:
filter:
enabled: false
transport:
dashboard: 127.0.0.1:8080
datasource:
# 网关api分组规则
ds-api-group:
nacos:
server-addr: 10.16.10.139:8848
dataId: ${spring.application.name}-gateway-api-define-rules
groupId: DEFAULT_GROUP
data-type: json
rule-type: gw-api-group
# 网关流控规则
ds-gw-flow:
nacos:
server-addr: 10.16.10.139:8848
dataId: ${spring.application.name}-gateway-flow-rules
groupId: DEFAULT_GROUP
data-type: json
rule-type: gw-flow
# 网关降级规则
ds-degrade:
nacos:
server-addr: 10.16.10.139:8848
dataId: ${spring.application.name}-degrade-rules
groupId: DEFAULT_GROUP
data-type: json
rule-type: degrade
# 日志路径
log:
dir: C:/Users/xx/Desktop/sentinel-log/
switch-pid: true
server:
port: 8888
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="Spring" name="Spring">
<configuration />
</facet>
<facet type="web" name="Web">
<configuration>
<webroots />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.alibaba.csp:sentinel-core:1.7.1" level="project" />
<orderEntry type="library" name="Maven: com.alibaba.csp:sentinel-transport-simple-http:1.7.1" level="project" />
<orderEntry type="library" name="Maven: com.alibaba.csp:sentinel-transport-common:1.7.1" level="project" />
<orderEntry type="library" name="Maven: com.alibaba:fastjson:1.2.62" level="project" />
<orderEntry type="library" name="Maven: com.alibaba.cloud:spring-cloud-starter-alibaba-sentinel:2.1.0.RELEASE" level="project" />
<orderEntry type="library" name="Maven: com.alibaba.cloud:spring-cloud-alibaba-sentinel:2.1.0.RELEASE" level="project" />
<orderEntry type="library" name="Maven: com.alibaba.csp:sentinel-annotation-aspectj:1.7.1" level="project" />
<orderEntry type="library" name="Maven: org.aspectj:aspectjrt:1.9.2" level="project" />
<orderEntry type="library" name="Maven: org.aspectj:aspectjweaver:1.9.2" level="project" />
<orderEntry type="library" name="Maven: com.alibaba.csp:sentinel-web-servlet:1.6.3" level="project" />
<orderEntry type="library" name="Maven: com.alibaba.csp:sentinel-spring-webflux-adapter:1.6.3" level="project" />
<orderEntry type="library" name="Maven: com.alibaba.csp:sentinel-reactor-adapter:1.6.3" level="project" />
<orderEntry type="library" name="Maven: com.alibaba.cloud:spring-cloud-alibaba-sentinel-datasource:2.1.0.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-aop:2.1.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: com.alibaba.csp:sentinel-datasource-nacos:1.7.1" level="project" />
<orderEntry type="library" name="Maven: com.alibaba.csp:sentinel-datasource-extension:1.7.1" level="project" />
<orderEntry type="library" name="Maven: com.alibaba.nacos:nacos-client:1.1.4" level="project" />
<orderEntry type="library" name="Maven: com.alibaba.nacos:nacos-common:1.1.4" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.7" level="project" />
<orderEntry type="library" name="Maven: commons-io:commons-io:2.2" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.4" level="project" />
<orderEntry type="library" name="Maven: com.alibaba.nacos:nacos-api:1.1.4" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:guava:22.0" level="project" />
<orderEntry type="library" name="Maven: com.google.code.findbugs:jsr305:1.3.9" level="project" />
<orderEntry type="library" name="Maven: com.google.errorprone:error_prone_annotations:2.0.18" level="project" />
<orderEntry type="library" name="Maven: com.google.j2objc:j2objc-annotations:1.1" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.mojo:animal-sniffer-annotations:1.14" level="project" />
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.11" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.9.10" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.9.10" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.9.10" level="project" />
<orderEntry type="library" name="Maven: io.prometheus:simpleclient:0.5.0" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:2.0.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:2.0.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:2.0.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:2.0.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-logging:2.0.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: ch.qos.logback:logback-classic:1.2.3" level="project" />
<orderEntry type="library" name="Maven: ch.qos.logback:logback-core:1.2.3" level="project" />
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-to-slf4j:2.10.0" level="project" />
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-api:2.10.0" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:jul-to-slf4j:1.7.25" level="project" />
<orderEntry type="library" name="Maven: javax.annotation:javax.annotation-api:1.3.2" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-core:5.0.9.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-jcl:5.0.9.RELEASE" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: org.yaml:snakeyaml:1.19" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-json:2.0.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.9.6" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.9.6" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.module:jackson-module-parameter-names:2.9.6" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-tomcat:2.0.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-core:8.5.34" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-el:8.5.34" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-websocket:8.5.34" level="project" />
<orderEntry type="library" name="Maven: org.hibernate.validator:hibernate-validator:6.0.12.Final" level="project" />
<orderEntry type="library" name="Maven: javax.validation:validation-api:2.0.1.Final" level="project" />
<orderEntry type="library" name="Maven: org.jboss.logging:jboss-logging:3.3.2.Final" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml:classmate:1.3.4" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-web:5.0.9.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-beans:5.0.9.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-webmvc:5.0.9.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-aop:5.0.9.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-context:5.0.9.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-expression:5.0.9.RELEASE" level="project" />
<orderEntry type="library" name="Maven: com.alibaba.csp:sentinel-cluster-client-default:1.7.1" level="project" />
<orderEntry type="library" name="Maven: com.alibaba.csp:sentinel-cluster-common-default:1.7.1" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-handler:4.1.31.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-buffer:4.1.31.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-common:4.1.31.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-transport:4.1.31.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-resolver:4.1.31.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-codec:4.1.31.Final" level="project" />
<orderEntry type="library" name="Maven: com.alibaba.csp:sentinel-cluster-server-default:1.7.1" level="project" />
<orderEntry type="library" name="Maven: com.alibaba.csp:sentinel-parameter-flow-control:1.7.1" level="project" />
<orderEntry type="library" name="Maven: com.googlecode.concurrentlinkedhashmap:concurrentlinkedhashmap-lru:1.4.2" level="project" />
</component>
</module>
\ No newline at end of file
......@@ -16,19 +16,10 @@
</properties>
<dependencies>
<!-- <dependency>-->
<!-- <groupId>com.alibaba.csp</groupId>-->
<!-- <artifactId>sentinel-core</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.1.0.RELEASE</version>
<version>2.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
......@@ -41,72 +32,6 @@
</dependency>
</dependencies>
<!-- <dependencies>-->
<!-- &lt;!&ndash;nacos config&ndash;&gt;-->
<!-- &lt;!&ndash;<dependency>&ndash;&gt;-->
<!-- &lt;!&ndash;<groupId>com.alibaba.cloud</groupId>&ndash;&gt;-->
<!-- &lt;!&ndash;<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>&ndash;&gt;-->
<!-- &lt;!&ndash;</dependency>&ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>com.alibaba.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>-->
<!-- </dependency>-->
<!-- &lt;!&ndash;用nacos 和 sentinel 就用下面三个&ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>com.alibaba.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>-->
<!-- </dependency>-->
<!-- &lt;!&ndash;做持久化用&ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>com.alibaba.csp</groupId>-->
<!-- <artifactId>sentinel-datasource-nacos</artifactId>-->
<!-- </dependency>-->
<!-- &lt;!&ndash;<dependency>&ndash;&gt;-->
<!-- &lt;!&ndash;<groupId>org.springframework.cloud</groupId>&ndash;&gt;-->
<!-- &lt;!&ndash;<artifactId>spring-cloud-starter-openfeign</artifactId>&ndash;&gt;-->
<!-- &lt;!&ndash;</dependency>&ndash;&gt;-->
<!-- &lt;!&ndash;引入自己api&ndash;&gt;-->
<!--&lt;!&ndash; <dependency>&ndash;&gt;-->
<!--&lt;!&ndash; <groupId>cn.dongfang.clouddemo</groupId>&ndash;&gt;-->
<!--&lt;!&ndash; <artifactId>apicommons</artifactId>&ndash;&gt;-->
<!--&lt;!&ndash; <version>${project.version}</version>&ndash;&gt;-->
<!--&lt;!&ndash; </dependency>&ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-web</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-actuator</artifactId>-->
<!-- </dependency>-->
<!-- &lt;!&ndash;热部署&ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-devtools</artifactId>-->
<!-- <scope>runtime</scope>-->
<!-- <optional>true</optional>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.projectlombok</groupId>-->
<!-- <artifactId>lombok</artifactId>-->
<!-- <optional>true</optional>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-test</artifactId>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
<!-- </dependencies>-->
<build>
<plugins>
......
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.csp.sentinel.demo.springboot.nacos.config;
import org.springframework.context.annotation.Configuration;
/**
* @author Eric Zhao
*/
@Configuration
public class AopConfiguration {
// spring-colud-alibaba-sentinel 引入sentinel 不需要手动注入下面Bean 可以直接使用@SentinelResource
// @Bean
// public SentinelResourceAspect sentinelResourceAspect() {
// return new SentinelResourceAspect();
// }
}
package com.alibaba.csp.sentinel.demo.springboot.nacos.inti;
import com.alibaba.csp.sentinel.demo.springboot.nacos.config.NacosStartService;
import com.alibaba.csp.sentinel.init.InitFunc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @author lnj
* createTime 2018-11-07 22:37
**/
@Component
public class DemoNacosInitFunc implements InitFunc {
/*
* 因为sentinel是SPI拓展接口不能使用,spring的ioc,所以下面的注入会报NPE。。。。
* */
@Autowired
NacosStartService nacosStartService;
@Override
public void init() throws Exception {
nacosStartService.initNacosConfigFromNacos();
}
}
\ No newline at end of file
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.csp.sentinel.demo.springboot.nacos;
package com.demo.alibaba.sentinel;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
......
package com.alibaba.csp.sentinel.demo.springboot.nacos.config;
package com.demo.alibaba.sentinel.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
......@@ -6,18 +6,22 @@ import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
/**
* @author lnj
* createTime 2018-11-07 22:37
**/
* @ desc : 初始化的时候,从nacos中拉取配置
* @ params
* @ return
* @ date 2020/4/18
* @ author ligen
*/
@Component
public class ApplicationRunnerImpl implements ApplicationRunner {
@Autowired
private NacosStartService nacosStartService;
@Override
public void run(ApplicationArguments args) throws Exception {
// nacosStartService.initNacosConfigFromNacos();
nacosStartService.initNacosConfigFromNacos();
}
}
\ No newline at end of file
package com.alibaba.csp.sentinel.demo.springboot.nacos.config;
package com.demo.alibaba.sentinel.config;
import com.alibaba.cloud.sentinel.SentinelProperties;
import com.alibaba.cloud.sentinel.datasource.RuleType;
......@@ -45,6 +45,7 @@ public class NacosStartService {
setProperty("serverAddr", nacos.getServerAddr());
setProperty("namespace", "31b168cb-51ee-4b8b-adfa-29b42d4afa0b");
}};
// 流控规则
if (Objects.equals(nacos.getRuleType(), RuleType.FLOW)) {
ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(properties,
nacos.getGroupId(), nacos.getDataId(),
......@@ -52,7 +53,7 @@ public class NacosStartService {
}));
FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
}
// 降级规则
if (Objects.equals(nacos.getRuleType(), RuleType.DEGRADE)) {
ReadableDataSource<String, List<DegradeRule>> degradeRuleDataSource = new NacosDataSource<>(properties,
nacos.getGroupId(), nacos.getDataId(),
......@@ -60,6 +61,7 @@ public class NacosStartService {
}));
DegradeRuleManager.register2Property(degradeRuleDataSource.getProperty());
}
//... 其它规则
});
......
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.csp.sentinel.demo.springboot.nacos.controller;
package com.demo.alibaba.sentinel.controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.util.StringUtil;
......@@ -25,7 +25,11 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Eric Zhao
* @ desc : demo
* @ params
* @ return
* @ date 2020/4/18
* @ author ligen
*/
@RestController
public class DemoController {
......
package com.demo.alibaba.sentinel.inti;
import com.alibaba.csp.sentinel.init.InitFunc;
import org.springframework.stereotype.Component;
/**
* @ desc :Sentinel 功能拓展SPI
* @ params
* @ return
* @ date 2020/4/18
* @ author ligen
*/
@Component
public class DemoNacosInitFunc implements InitFunc {
/**
* @ desc : sentinel 预留的SPI 拓展接口。因为是SPI拓展,所以不能使用spring的依赖注入
* <p>
* 使用SPI拓展方式时,需要在resources下建立META-INF/service目录下创建...InitFunc文件(见目录)
* @ params
* @ return
* @ date 2020/4/18
* @ author ligen
*/
@Override
public void init() throws Exception {
// 初始化 doSomething.......
}
}
\ No newline at end of file
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.csp.sentinel.demo.springboot.nacos.service;
package com.demo.alibaba.sentinel.service;
import com.alibaba.csp.sentinel.slots.block.BlockException;
......
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.csp.sentinel.demo.springboot.nacos.service;
package com.demo.alibaba.sentinel.service;
/**
* @author Eric Zhao
......
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.csp.sentinel.demo.springboot.nacos.service;
package com.demo.alibaba.sentinel.service;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.stereotype.Service;
......
com.alibaba.csp.sentinel.demo.springboot.nacos.inti.DemoNacosInitFunc
\ No newline at end of file
com.demo.alibaba.sentinel.inti.DemoNacosInitFunc
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment