springcloud中sentinel配置参考地址:https://gitee.com/fhh/fans-cloud-alibaba
第一步:下载代码后用maven打包sentinel-dashboard-nacos工程
第二步:打包完成后产生sentinel-dashboard-nacos-1.7.2.jar的jar包
sentinel-dashboard-nacos-1.7.2.jar下载地址:https://gitee.com/fhh/fans-sentinel-master/blob/master/jar/sentinel-dashboard-nacos-1.7.2.jar
第三步:然后用以下命令在doc或者Linux命令行中启动(或者直接在工具中启动):
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar -Xms512m -Xmx512m sentinel-dashboard-nacos-1.7.2.jar
sentinel-dashboard-nacos是在sentinel-dashboard基础之上经过改造后的Sentinel Dashboard,实现了Sentinel Dashboard中修改规则同步到Nacos的功能; 代码实现 下面直接来看看如何实现的具体改造步骤,这里参考了Sentinel Dashboard源码中关于Nacos实现的测试用例。但是由于考虑到与Spring Cloud Alibaba的结合使用,略作修改。
第一步:修改pom.xml中的sentinel-datasource-nacos的依赖,将test注释掉,这样才能在主程序中使用。
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<!--<scope>test</scope>-->
</dependency>
第二步:找到resources/app/scripts/directives/sidebar/sidebar.html中的这段代码:
<li ui-sref-active="active">
<a ui-sref="dashboard.flowV1({app: entry.app})">
<i class="glyphicon glyphicon-filter"></i> 流控规则
</a>
</li>
修改为:
<li ui-sref-active="active">
<a ui-sref="dashboard.flow({app: entry.app})">
<i class="glyphicon glyphicon-filter"></i> 流控规则
</a>
</li>
第三步:在com.alibaba.csp.sentinel.dashboard.rule包下新建一个nacos包,用来编写针对Nacos的扩展实现。
第四步:创建Nacos的配置类,具体代码如下:
@Configuration
public class NacosConfig {
@Bean
public Converter<List<FlowRuleEntity>, String> flowRuleEntityEncoder() {
return JSON::toJSONString;
}
@Bean
public Converter<String, List<FlowRuleEntity>> flowRuleEntityDecoder() {
return s -> JSON.parseArray(s, FlowRuleEntity.class);
}
@Bean
public ConfigService nacosConfigService() throws Exception {
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR, "localhost");
//properties.put(PropertyKeyConst.NAMESPACE, "130e71fa-97fe-467d-ad77-967456f2c16d");
return ConfigFactory.createConfigService(properties);
}
}
如果用到了namespace隔离环境,可以在nacosConfigService方法中再加入配置,比如:properties.put(PropertyKeyConst.NAMESPACE, "130e71fa-97fe-467d-ad77-967456f2c16d");
第五步:实现Nacos的配置拉取。
@Component("flowRuleNacosProvider")
public class FlowRuleNacosProvider implements DynamicRuleProvider<List<FlowRuleEntity>> {
@Autowired
private ConfigService configService;
@Autowired
private Converter<String, List<FlowRuleEntity>> converter;
public static final String FLOW_DATA_ID_POSTFIX = "-sentinel";
public static final String GROUP_ID = "DEFAULT_GROUP";
@Override
public List<FlowRuleEntity> getRules(String appName) throws Exception {
String rules = configService.getConfig(appName + FLOW_DATA_ID_POSTFIX, GROUP_ID, 3000);
if (StringUtil.isEmpty(rules)) {
return new ArrayList<>();
}
return converter.convert(rules);
}
}
getRules方法中的appName参数是Sentinel中的服务名称。 configService.getConfig方法是从Nacos中获取配置信息的具体操作。其中,DataId和GroupId分别对应客户端使用时候的对应配置。比如这里的例子对应了之前我们在《Sentinel使用Nacos存储规则》一文中的配置,具体如下:
spring.cloud.sentinel.datasource.ds.nacos.groupId=DEFAULT_GROUP
spring.cloud.sentinel.datasource.ds.nacos.dataId=${spring.application.name}-sentinel
注意:两边的DataId和GroupId必须对应上。
第六步:实现Nacos的配置推送。
@Component("flowRuleNacosPublisher")
public class FlowRuleNacosPublisher implements DynamicRulePublisher<List<FlowRuleEntity>> {
@Autowired
private ConfigService configService;
public static final String FLOW_DATA_ID_POSTFIX = "-sentinel";
public static final String GROUP_ID = "DEFAULT_GROUP";
@Override
public void publish(String app, List<FlowRuleEntity> rules) throws Exception {
AssertUtil.notEmpty(app, "app name cannot be empty");
if (rules == null) {
return;
}
List<FlowRule> list = new ArrayList<FlowRule>();
for (FlowRuleEntity rule : rules) {
FlowRule flowRule = JSON.parseObject(JSON.toJSONString(rule), FlowRule.class);
list.add(flowRule);
}
// 把对象转成格式化json字符串
String content = JSON.toJSONString(list, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteDateUseDateFormat);
configService.publishConfig(app + FLOW_DATA_ID_POSTFIX, GROUP_ID, content);
}
}
这里的大部分内容与上一步中的实现一致。主要就是Nacos中存储配置的DataId和GroupId不要弄错。
第七步:修改com.alibaba.csp.sentinel.dashboard.controller.v2.FlowControllerV2中DynamicRuleProvider和DynamicRulePublisher注入的Bean,改为上面我们编写的针对Apollo的实现:
@Autowired
@Qualifier("flowRuleNacosProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
@Autowired
@Qualifier("flowRuleNacosPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
As distributed systems become increasingly popular, the reliability between services is becoming more important than ever before. Sentinel takes "flow" as breakthrough point, and works on multiple fields including flow control, circuit breaking and system adaptive protection, to guarantee reliability of microservices.
Sentinel has the following features:
Features overview:
See the 中文文档 for document in Chinese.
See the Wiki for full documentation, examples, blog posts, operational details and other information.
Sentinel provides integration modules for various open-source frameworks (e.g. Spring Cloud, Apache Dubbo, gRPC, Spring WebFlux, Reactor) and service mesh. You can refer to the document for more information.
If you are using Sentinel, please leave a comment here to tell us your scenario to make Sentinel better. It's also encouraged to add the link of your blog post, tutorial, demo or customized components to Awesome Sentinel.
Below is a simple demo that guides new users to use Sentinel in just 3 steps. It also shows how to monitor this demo using the dashboard.
Note: Sentinel requires Java 7 or later.
If your application is build in Maven, just add the following dependency in pom.xml
.
<!-- replace here with the latest version -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>1.7.1</version>
</dependency>
If not, you can download JAR in Maven Center Repository.
Wrap your code snippet via Sentinel API: SphU.entry(resourceName)
.
In below example, it is System.out.println("hello world");
:
try (Entry entry = SphU.entry("HelloWorld")) {
// Your business logic here.
System.out.println("hello world");
} catch (BlockException e) {
// Handle rejected request.
e.printStackTrace();
}
// try-with-resources auto exit
So far the code modification is done. We also provide annotation support module to define resource easier.
If we want to limit the access times of the resource, we can set rules to the resource. The following code defines a rule that limits access to the resource to 20 times per second at the maximum.
List<FlowRule> rules = new ArrayList<>();
FlowRule rule = new FlowRule();
rule.setResource("HelloWorld");
// set limit qps to 20
rule.setCount(20);
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
rules.add(rule);
FlowRuleManager.loadRules(rules);
For more information, please refer to How To Use.
After running the demo for a while, you can see the following records in ~/logs/csp/${appName}-metrics.log.{date}
(When using the default DateFileLogHandler
).
|--timestamp-|------date time----|-resource-|p |block|s |e|rt |occupied
1529998904000|2018-06-26 15:41:44|HelloWorld|20|0 |20|0|0 |0
1529998905000|2018-06-26 15:41:45|HelloWorld|20|5579 |20|0|728 |0
1529998906000|2018-06-26 15:41:46|HelloWorld|20|15698|20|0|0 |0
1529998907000|2018-06-26 15:41:47|HelloWorld|20|19262|20|0|0 |0
1529998908000|2018-06-26 15:41:48|HelloWorld|20|19502|20|0|0 |0
1529998909000|2018-06-26 15:41:49|HelloWorld|20|18386|20|0|0 |0
p stands for incoming request, block for blocked by rules, success for success handled by Sentinel, e for exception count, rt for average response time (ms), occupied stands for occupiedPassQps since 1.5.0 which enable us booking more than 1 shot when entering.
This shows that the demo can print "hello world" 20 times per second.
More examples and information can be found in the How To Use section.
The working principles of Sentinel can be found in How it works section.
Samples can be found in the sentinel-demo module.
Sentinel also provides a simple dashboard application, on which you can monitor the clients and configure the rules in real time.
For details please refer to Dashboard.
Sentinel will generate logs for troubleshooting and real-time monitoring. All the information can be found in logs.
For bug report, questions and discussions please submit GitHub Issues.
Contact us: sentinel@linux.alibaba.com
Contributions are always welcomed! Please see CONTRIBUTING for detailed guidelines.
You can start with the issues labeled with good first issue
.
Thanks Guava, which provides some inspiration on rate limiting.
And thanks for all contributors of Sentinel!
These are only part of the companies using Sentinel, for reference only. If you are using Sentinel, please add your company here to tell us your scenario to make Sentinel better
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型