Sentinel是分布式系统的流量防卫兵
https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D
注意:
1)8080和8719端口要开放
2)window和Linux安装一样
3)建议刚开始学的时候微服务和Sentinel在一台服务器上,否则有坑(见文章下面的坑)
下载地址
https://github.com/alibaba/Sentinel/releases
java -jar sentinel-dashboard-1.7.0.jar
http://ip:8080/ 账号和密码都是sentinel
下面的项目是 SpringBoot2.2.2 + springcloud-alibaba 2.1.0
在pringboot+web的基础之上
添加依赖
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
2.1.0.RELEASE
com.alibaba.cloud
spring-cloud-starter-alibaba-sentinel
2.1.0.RELEASE
修改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
(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和微服务放在一起就没这问题
参考:
java获取为本机外网的ip: https://blog.csdn.net/qq_37171353/article/details/105480375
https://blog.csdn.net/wk52525/article/details/104587239/
http://www.hellojava.com/a/82317.html
https://www.cnblogs.com/sky-chen/p/11237133.html
https://segmentfault.com/a/1190000021256340
https://www.bilibili.com/video/BV18E411x7eT?from=search&seid=13868558813806884697
https://github.com/alibaba/Sentinel
https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D
https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html#_spring_cloud_alibaba_sentinel