SpringCloudAlibaba之Sentinel安装和使用(四)

上一篇介绍了使用nacos作为注册中心,Java内容提供者和消费者。本篇介绍sentinel的一些概念和安装使用。随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。

Sentinel 是什么?

sentinel 用来在微服务系统中保护微服务对的作用 如何 服务雪崩 服务熔断 服务降级 就是用来替换hystrix。

Sentinel 具有以下特征:

  • 丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等。
  • 完备的实时监控:Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。
  • 广泛的开源生态:Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Apache Dubbo、gRPC、Quarkus 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。同时 Sentinel 提供 Java/Go/C++ 等多语言的原生实现。
  • 完善的 SPI 扩展机制:Sentinel 提供简单易用、完善的 SPI 扩展接口。您可以通过实现扩展接口来快速地定制逻辑。例如定制规则管理、适配动态数据源等。

Sentinel 的主要特性:

Sentinel 的开源生态:

Sentinel 分为两个部分:

  • 核心库(Java 客户端)不依赖任何框架/库,能够运行于所有 Java 运行时环境,同时对 Dubbo / Spring Cloud 等框架也有较好的支持。
  • 控制台(Dashboard)基于 Spring Boot 开发,打包后可以直接运行,不需要额外的 Tomcat 等应用容器。

Dashboard版本

        下载控制台(Dashboard)版本,这里选择1.8.1,直接官网上下载。

启动jar包,更改端口号为9191,这里挂后台启动,操作命令如下:

nohup java -Dserver.port=9191 -jar sentinel-dashboard-1.8.1.jar >/dev/null 2>&1 &

打开浏览器访问如下:

http://127.0.0.1:9191/

默认页面账号密码一致为:sentinel

 创建Java客户端

项目右击新建Module,项目名称为Sentinel。pom.xml配置如下:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.juwusheng</groupId>
        <artifactId>springcloudalibabademo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>com.juwusheng</groupId>
    <artifactId>sentinel</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sentinel</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <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>
        <!--引入nacos client的依赖-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

引入spring-cloud-starter-alibaba-sentinel 相关库。

创建bootstrap.yaml资源文件,放在src/main/resources文件下。application.properties重命名为application.yaml,并清空文件中的内容。bootstrap.yaml增加配置

server:
  port: 8998
spring:
  application:
    name: SENTINEL
  cloud:
    nacos:
      server-addr:  192.168.43.85:8845,192.168.43.229:8846,192.168.43.251:8847
      config:
        namespace: bf3b249f-ee29-4c7c-af98-fa5c2b4fb568
        group: DEFAULT_GROUP
        name: sentinel-dev
        file-extension: yaml
      discovery:
        namespace: bf3b249f-ee29-4c7c-af98-fa5c2b4fb568
    sentinel:
      enabled: true
      transport:
        dashboard: 127.0.0.1:9191
        port: 8719

这里的sentinel.transport.dashboard地址和端口为控制台(Dashboard)地址和端口。

新建一个请求类DemoController,内容如下:

@RestController
@RefreshScope
public class DemoController {
    @Value("${sentinel.username}")
    private String username;

    @GetMapping("/demo/{id}")
    @SentinelResource(value = "aaa",blockHandler ="blockHandler",fallback = "fallCustomeer",defaultFallback = "fall")
    public String demo(@PathVariable("id") int id){
        if (id<0){
            throw new RuntimeException("id无效");
        }
        return "demo ok!!!"+ username+";id="+id;
    }
    public String blockHandler(int id, BlockException e){
        if (e instanceof FlowException){
            return "当前请求过于火爆,您已被流量控制!!!";
        }
        if (e instanceof DegradeException){
            return "当前请求过于火爆,你已被降级!!!";
        }
        if (e instanceof ParamFlowException){
            return  "当前请求过于火爆,您已被热点参数限流!!!";
        }
        return "服务器快爆了,请稍后再试";
    }
    public String fallCustomeer(int id){
        return "自定义处理----我们服务器出错了"+id;
    }
    public String fall(int id){
        return "默认处理----我们服务器出错了"+id;
    }
    @RequestMapping("/test")
    public String test(){
        return "test ok!!!";
    }
}

nacos中增加配置,配置data Id为sentinel-dev.yaml,增加sentinel.username属性的值,随便写。

主启动类不要忘记添加EnableDiscoveryClient注解。启动程序

刷新sentinel,查看

本节到此结束。

下一节我们将重点讲解DemoController 类中代码的含义。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值