集群配置(nacos伪集群)
1.复制nacos重命名

2.修改配置文件
修改端口号

3.配置cluster.conf


4.导入数据源

5.连接数据库

6.启动

访问

7.反向代理
启动nginx

修改配置文件

8.注册到集群
修改配置文件

启动项目 访问nacos

服务雪崩
假设存在如下调用链
1000
1000
500

而此时,Service A的流量波动很大,流量经常会突然性增加!那么在这种情况下,就算Service A能扛得住请求,Service B和Service C未必能扛得住这突发的请求。
此时,如果Service C因为抗不住请求,变得不可用。那么Service B的请求也会阻塞,慢慢耗尽Service B的线程资源,Service B就会变得不可用。紧接着,Service A也会不可用,这一过程如下图所示

如上图所示,由于服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是 服务故障的 "雪崩效应" 。
那么,服务熔断和服务降级就可以视为解决服务雪崩的手段之一。
熔断
在互联网系统中,当下游服务因访问压力过大而响应变慢或失败,上游服务为了保护系统整 体的可用性,可以暂时切断对下游服务的调用。这种牺牲局部,保全整体的措施就叫做熔断。

服务熔断一般有三种状态:
熔断关闭状态(Closed)
a->b ->c
服务没有故障时,熔断器所处的状态,对调用方的调用不做任何限制
熔断开启状态(Open)
a->b->c(x)
a->b
后续对该服务接口的调用不再经过网络,直接执行本地的fallback方法
半熔断状态(Half-Open)
c
尝试恢复服务调用,允许有限的流量调用该服务,并监控调用成功率。如果成功率达到预 期,则说明服务已恢复,进入熔断关闭状态;如果成功率仍旧很低,则重新进入熔断关闭状态
降级
降级其实就是为服务提供一个备用方案,一旦服务无法正常调用,就使用备用方案

常见的容错组件
Hystrix
Hystrix是由Netflix开源的一个延迟和容错库,用于隔离访问远程系统、服务或者第三方库,防止 级联失败,从而提升系统的可用性与容错性。
Resilience4J
Resilicence4J一款非常轻量、简单,并且文档非常清晰、丰富的熔断工具,这也是Hystrix官方推 荐的替代产品。不仅如此,Resilicence4j还原生支持Spring Boot 1.x/2.x,而且监控也支持和 prometheus等多款主流产品进行整合。
Sentinel
Sentinel 是阿里巴巴开源的一款断路器实现,本身在阿里内部已经被大规模采用,非常稳定

Sentinel
1.Sentinel入门
https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D
Sentinel (分布式系统的流量防卫兵) 是阿里开源的一套用于服务容错 的综合性解决方案。它以流量为切入点, 从流量控制、熔断降级、系统负载保护等多个维度来保护服务的稳定性。
Sentinel 具有以下特征:
丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景, 例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等。
完备的实时监控:Sentinel 提供了实时的监控功能。通过控制台可以看到接入应用的单台机器秒级数据, 甚至 500 台以下规模的集群的汇总运行情况。
广泛的开源生态:Sentinel 提供开箱即用的与其它开源框架/库的整合模块, 例如与 Spring Cloud、Dubbo、gRPC 的整合。只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。
完善的 SPI 扩展点:Sentinel 提供简单易用、完善的 SPI 扩展接口。您可以通过实现扩展接口快
速地定制逻辑。例如定制规则管理、适配动态数据源等
Sentinel 分为两个部分:
核心库(Java 客户端)不依赖任何框架/库,能够运行于所有 Java 运行时环境,同时对 Dubbo /Spring Cloud 等框架也有较好的支持。
控制台(Dashboard)基于 Spring Boot 开发,打包后可以直接运行,不需要额外的 Tomcat 等应用容器。
2.微服务集成Sentinel
1)添加jar
为微服务集成Sentinel非常简单, 只需要加入Sentinel的依赖即可
在pom.xml中加入下面依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
2) 编写消费者的controller并调用
@RestController
@RequestMapping("test")
public class TestController {
@GetMapping
public String aaaa(){
return "hahaha";
}
}

2.安装Sentinel控制台
1) 下载jar包,解压到文件夹
https://github.com/alibaba/Sentinel/releases/tag/1.8.1
2)启动
java -Dserver.port=8888 -Dcsp.sentinel.dashboard.server=localhost:8888 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.1.jar

访问页面
输入localhost:8888 进入控制台

3) 修改 product ,在里面加入有关控制台的配置
spring.cloud.sentinel.transport.port=9999
# 该端口号为sentinal于服务之间的交互 随便写只要不被占用
spring.cloud.sentinel.transport.dashboard=localhost:8888
#sentinal服务所在的地址和端口号
8081与8080交互
3.实现接口的限流
设置流控规则

一秒内访问次数大于1

4.自定义先流的异常处理
新建配置文件

访问 重启项目后要重新配置流控规则

5.关联流控模式
控流模式
sentinel共有三种流控模式,分别是:
l 直接(默认):接口达到限流条件时,开启限流
l 关联:当关联的资源达到限流条件时,开启限流 [适合做应用让步]
l 链路:当从某个接口过来的资源达到限流条件时,开启限流


6.链路流控模式
链路流控模式
链路流控模式指的是,当从某个接口过来的资源达到限流条件时,开启限流**。**
注解:@SentinelResource
我们可以对某一个方法进行限流控制,无论是谁在何处调用了它,这里需要使用到@SentinelResource
,一旦方法被标注,那么就会进行监控@SentinelResource
,一旦方法被标注,那么就会进行监控。
创建测试资源


配置 禁止收敛URL的入口 context
在application.properties中添加:
spring.cloud.sentinel.web-context-unify=false


自定义限流
创建工具类

public static Result handler(BlockException e){
return new Result(500,"限流了","heiheihei");
}


设置流控规则

