zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Sentinel的使用笔记

2023-03-14 22:54:20 时间

Sentinel简介


Sentinel是分布式系统的流量防卫兵 


https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D


Sentinel安装


注意:

1)8080和8719端口要开放

2)window和Linux安装一样

3)建议刚开始学的时候微服务和Sentinel在一台服务器上,否则有坑(见文章下面的坑)


下载地址

https://github.com/alibaba/Sentinel/releases


16.png


java -jar sentinel-dashboard-1.7.0.jar


http://ip:8080/    账号和密码都是sentinel


17.png


Sentinel监控微服务


下面的项目是 SpringBoot2.2.2 + springcloud-alibaba 2.1.0

在pringboot+web的基础之上


添加依赖


 <!--alibaba-nacos-discovery-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
        <!--   sentinel     -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>


修改applicationyml,添加注册中心地址和sentinel地址


server:
  port: 8401
spring:
  application:
    name: sentinel-service
  cloud:
    nacos:
      discovery: #Nacos注册中心地址
        server-addr: 39.105.30.146:8848
    sentinel:
      transport: #dashboard地址
        dashboard: localhost:8080
        port: 8719  #默认端口,如果被占用则从8719依次+1扫描
 
management:
  endpoints:
    web:
      exposure:
        include: "*"


修改主启动类


@EnableDiscoveryClient


添加controller


@RestController
public class HelloController {
 
    @GetMapping("/testA")
    public String testA() {
 
        return "---------testA";
    }
 
    @GetMapping("/testB")
    public String testB() {
        return "---------testB";
    }
}


启动项目并且多访问几次


http://localhost:8401/testA

http://localhost:8401/testB


结果如下图所示


18.png


自定义限流处理类


//CustomerBlockHandler
    @GetMapping("/rateLimit/CustomerBlockHandler")
    @SentinelResource(value = "CustomerBlockHandler",//热点规则的资源名称
            blockHandlerClass = CustomerBlockHandler.class,//自定义限流处理类
            blockHandler = "handlerException2")//流量达到限制时调用  自定义限流处理类(CustomerBlockHandler)里的方法(handlerException2)
    public CommonResult CustomerBlockHandler(){
        return new CommonResult(200,"按客户自定义限流测试OK", new Payment(2020L, "serial003"));
    }


public class CustomerBlockHandler {
 
 
    public static CommonResult handlerException2(BlockException exception) {
        return new CommonResult(4444, "按客户自定义2,global handlerException", new Payment(2020L, "serial0003"));
    }
}


遇到的坑(没有解决,但是有参考意义)


日志中出现这种错误


2019-07-23 14:57:33.256 ERROR 14788 --- [pool-2-thread-1] c.a.c.s.dashboard.metric.MetricFetcher   : 
Failed to fetch metric from <http://192.16.100.141:8721/metric?startTime=1563865044000&endTime=1563865050000&refetch=false>
 (ConnectionException: Connection refused: no further information)


 我当时是把Sentinel启动在阿里云服务器上,然后我电脑在启动SpringBoot微服务A(需要Sentinel监控)时候微服务A给Sentinel发送的地址默认是内网地址(192.xxx),而不是外网地址,所以当Sentinel给192.xx发送心跳就发送不到,就监控不到服务和请求


解决步骤:


1)手动设置一个参数(csp.sentinel.heartbeat.client.ip)为真实的外网地址

2)保证A能ping通B,B能ping通A  (有时候A能ping通B并不一定B能ping通A)


@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosSentinelOpenfeign80Application {
 
    public static void main(String[] args) {
        String ip = IPUntils.getOutIPV4();
        System.setProperty(TransportConfig.HEARTBEAT_CLIENT_IP, ip);
        SpringApplication.run(NacosSentinelOpenfeign80Application.class, args);
    }
 
}


sentinel监控不到服务或者监控不到请求 


出现的根本原因还是上面的那个问题,下下策把sentinel和微服务放在一起就没这问题