SPRING CLOUD ALIBABA --SENTINEL组件的使用

对于sentinel的前置知识这里就不多说了:

直接上代码:

Release v1.8.1 · alibaba/Sentinel · GitHub  下载地址

springcloud Alibaba环境下创建soringboot的项目:

POM:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <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.apache.velocity</groupId>
        <artifactId>velocity</artifactId>
        <version>1.7</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.47</version>
    </dependency>
    <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>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
</dependencies>


application.yml
server:
  port: 8089
spring:
  application:
    name: cloudorder
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
    sentinel:
      #      eager: true
      transport:
        port: 8719
        dashboard: localhost:8080
management:
  endpoints:
    web:
      exposure:
        include: "*"
feign:
  sentinel:
    enabled: true
主启动
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class OrderApplication8088 {

    public static void main(String[] args) {
        SpringApplication.run(OrderApplication8088.class,args);
    }

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){

        return new RestTemplate();
    }
}
写一个controller接口就可以了:
@RequestMapping("/order")
public String test1(@RequestParam(name = "id")String id){

    return UUID.randomUUID().toString()+ "     id  :"+ id;
}
先启动nacos注册中心,再启动sentinel的检测服务,本次用的是1.8.1的版本:
java -jar sentinel-dashboard-1.8.1.jar 就可以启动服务了

再启动我们的springboot服务
访问 localhost:8080就是sentinel的服务界面了 可以在上面配置服务的限流和降级的配置,以及热点key的配置

 @RequestMapping("/getFeign")
    @SentinelResource(value = "getFeign",blockHandler = "demotionGetFeign",fallback = "deGetFeign")
    public String getFeign(@RequestParam(name = "id",required = false,defaultValue = "5")String id)  {

//        int i = 10/0;
        return "eeee           eee  "+proFeign.provide(id);
    }
 
public String demotionGetFeign(String id, BlockException ex){

    return id+"  服务降级了啊。。。o(╥﹏╥)o"+ex;
}

public String deGetFeign(String id){

    return "限流了啊。。。。o(╥﹏╥)o";
}
 
@SentinelResource(value = "getFeign",blockHandler = "demotionGetFeign",fallback = "deGetFeign")
说一下这个注解就是配置接口出现异常 或者限流后应该怎么处理:
blockHandler 这个属性是在sentinel控制台配置的规则出现问题的时候会作出相应的处理方案
fallback 这个是兜底的方法了 代码出现异常 或者其他问题就会走这个方法了
需要注意的是
blockHandler 的降级方法的返回值和参数要和原来的方法一样,同时要加上
BlockException ex属性,代表出现异常后的信息

至于sentinel界面的配置就不多说了 可以看看官网写的


sentinel的持久化配置:

步骤:

  • 添加pom

<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
  • 配置yml

spring:
  application:
    name: sentinel-service
  cloud:
    nacos:
      discovery:        #nacos服务注册中心地址
        server-addr: www.cjlly.com:8848
    sentinel:
      transport:
        dashboard: 127.0.0.1:8080
        port: 8719
      datasource:
        ds1:
          nacos:
            server-addr: www.cjlly.com:8848
            dataId: sentinel-service
            groupId: DEFAULT_GROUP
            data-type: json
            rule_type: flow
  • 登陆nacos,新建配置规则sentinel-service

[
{
"resource": "/findById",
"limitApp":"default",
"grade":1,
"count":1,
"strategy":0,
"controlBehavior":0,
"clusterMode":false
}
]

naocs配置解读:

resource:资源名称
limitApp:来源应用
grade:阀值类型,0---线程数,1---QPS
count:单机阀值
strategy:流控模式,0---直接,1---关联,2---链路
controlBehavior:流控效果,0---快速失败,1---warmUp,2---排队等待
clusterMode:是否集群

需要注意地方:

  • 此时如果是Nacos集群,每个节点务必要配置到同一个数据库上。并且保证每个
    节点都可用。如果有的节点宕掉了可能会导致配置持久化失败。
  • 部署在nacos上的配置文件的名字并没有太多要求,只需要跟微服务项目中yml文件中配置的dataId一致即可。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值