33

springcloud(二)——spring-cloud-alibaba集成sentinel入门

 4 years ago
source link: https://www.zplxjj.com/2019/05/11/springcloud/?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

随着微服务的流行,服务和服务之间的稳定性变得越来越重要。 Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。

Sentinel 具有以下特征:

  1. 丰富的应用场景: Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、实时熔断下游不可用应用等。

  2. 完备的实时监控: Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。

  3. 广泛的开源生态: Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。

  4. 完善的 SPI 扩展点: Sentinel 提供简单易用、完善的 SPI 扩展点。您可以通过实现扩展点,快速的定制逻辑。例如定制规则管理、适配数据源等。

springcloud如何使用 Sentinel

第一步:引入pom

    <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>

第二步:新建一个启动类和controller

@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/Sentinel ,执行命令

mvn clean package

会得到:sentinel-dashboard.jar,执行命令启动dashboard:

java -jar sentinel-dashboard.jar

这样默认是8080端口,在浏览器输入: http://localhost:8080,默认账号密码:sentinel:sentinel,看到控制台界面为部署成功:

第四步:引入配置:

server.port=8088
spring.application.name=spring-cloud-alibaba-sentinel-demo

# sentinel dashboard
spring.cloud.sentinel.transport.dashboard=localhost:8080

第五步:启动spring boot 项目,继续访问localhost:8080,会看到如下界面 pringcloud001.png

第六步:使用Sentinel实现接口限流(在控制台) ZR7nAj3.png!web

第七步:测试

通过上面的配置,实现的是/hello接口qps最大是2,如果qps大于2,则快速失败,配置完成,点击保存,我们快速刷新浏览器,会发现快速失败

NF7bqaN.png!web

结语

这篇文章只是springcloud和sentinel的入门,复杂的一些代码配置和文件配置后期介绍


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK