admin管理员组

文章数量:1608628

Sentinel简介

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

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

Sentinel安装

注意:

1)8080和8719端口要开放

2)window和Linux安装一样

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

下载地址

https://github/alibaba/Sentinel/releases

java -jar sentinel-dashboard-1.7.0.jar 

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

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

结果如下图所示

 

自定义限流处理类

//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和微服务放在一起就没这问题

参考:

java获取为本机外网的ip: java获取本机的外网IP地址(亲测有效)_CBeann的博客-CSDN博客_java获取外网ip地址

https://blog.csdn/wk52525/article/details/104587239/
http://www.hellojava/a/82317.html
https://wwwblogs/sky-chen/p/11237133.html

参考

https://segmentfault/a/1190000021256340

尚硅谷SpringCloud框架开发教程(SpringCloudAlibaba微服务分布式架构丨Spring Cloud)_哔哩哔哩_bilibili

https://github/alibaba/Sentinel

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

Spring Cloud Alibaba Reference Documentation

本文标签: 笔记sentinel