随着微服务的流行,服务和服务之间的稳定性变得愈来愈重要。 Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。java
Sentinel 具备如下特征:git
第一步:引入pomgithub
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>0.2.1.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
第二步:新建一个启动类和controllerweb
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } @RestController public class TestController { @GetMapping(value = "/hello") @SentinelResource("hello") public String hello() { return "Hello Sentinel"; } }
第三步:引入dashboard
能够直接下载sentinel-dashboard的jar包,也能够本身编译,我这边这里clone了代码本身编译,代码地址:https://github.com/alibaba/Se...,执行命令spring
mvn clean package
会获得:sentinel-dashboard.jar,执行命令启动dashboard:浏览器
java -jar sentinel-dashboard.jar
这样默认是8080端口,在浏览器输入:http://localhost:8080,默认帐号密码:sentinel:sentinel,看到控制台界面为部署成功:app
第四步:引入配置:框架
server.port=8088 spring.application.name=spring-cloud-alibaba-sentinel-demo # sentinel dashboard spring.cloud.sentinel.transport.dashboard=localhost:8080
第五步:启动spring boot 项目,继续访问localhost:8080,会看到以下界面spring-boot
第六步:使用Sentinel实现接口限流(在控制台)微服务
第七步:测试
经过上面的配置,实现的是/hello接口qps最大是2,若是qps大于2,则快速失败,配置完成,点击保存,咱们快速刷新浏览器,会发现快速失败
这篇文章只是springcloud和sentinel的入门,复杂的一些代码配置和文件配置后期介绍
更多文章欢迎关注我的博客:https://www.zplxjj.com和公众号