【黑马SpringCloud微服务开发与实战】(五)微服务保护

1.雪崩问题------原因分析



2.雪崩问题------解决方案




3.Sentinel------快速入门

本地启动:从资料里面获取jar包并且把版本后缀去掉和命令的名称一致

powershell 复制代码
java -Dserver.port=8090 -Dcsp.sentinel.dashboard.server=localhost:8090 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar

http://localhost:8090 访问sentinel

cart服务整合sentinel

xml 复制代码
<!--sentinel-->
<dependency>
    <groupId>com.alibaba.cloud</groupId> 
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
yaml 复制代码
spring:
  cloud: 
    sentinel:
      transport:
        dashboard: localhost:8090
yaml 复制代码
spring:
  cloud:
    sentinel:
      transport:
        dashboard: localhost:8090
      http-method-specify: true # 开启请求方式前缀

4.Sentinel------请求限流

用户信息

5.Sentinel------线程隔离



6.Sentinel------Fallback

java 复制代码
@Slf4j
public class ItemClientFallback implements FallbackFactory<ItemClient> {
    @Override
    public ItemClient create(Throwable cause) {
        return new ItemClient() {
            @Override
            public List<ItemDTO> queryItemByIds(Collection<Long> ids) {
                log.error("远程调用ItemClient#queryItemByIds方法出现异常,参数:{}", ids, cause);
                // 查询购物车允许失败,查询失败,返回空集合
                return CollUtils.emptyList();
            }

            @Override
            public void deductStock(List<OrderDetailDTO> items) {
                // 库存扣减业务需要触发事务回滚,查询失败,抛出异常
                throw new BizIllegalException(cause);
            }
        };
    }
}

放置DefaultFeignConfig中进行声明

java 复制代码
    @Bean
    public ItemClientFallback itemClientFallback(){
        return new ItemClientFallback();
    }
java 复制代码
@FeignClient(value = "item-service",
        configuration = DefaultFeignConfig.class,
        fallbackFactory = ItemClientFallback.class)
public interface ItemClient {

    @GetMapping("/items")
    List<ItemDTO> queryItemByIds(@RequestParam("ids") Collection<Long> ids);

    @PutMapping("/items/stock/deduct")
    void deductStock(@RequestBody List<OrderDetailDTO> items);
}
yaml 复制代码
feign:
  okhttp:
    enabled: true # 开启OKHttp功能
  sentinel:
    enabled: true

![

在这里插入图片描述
](https://i-blog.csdnimg.cn/direct/a0e64688bd644f5881334da5a2866c37.png)

7.Sentinel------服务熔断

20s后熔断结束后会发送一个请求看商品服务查询是否正常,如果不正常继续熔断,反之。。。。。。

相关推荐
七夜zippoe35 分钟前
微服务配置中心高可用设计:从踩坑到落地的实战指南(二)
微服务·架构·php
whltaoin8 小时前
SpringCloud 项目阶段九:Kafka 接入实战指南 —— 从基础概念、安装配置到 Spring Boot 实战及高可用设计
spring boot·spring cloud·kafka
callJJ9 小时前
从 0 开始理解 Spring 的核心思想 —— IoC 和 DI(2)
java·开发语言·后端·spring·ioc·di
lisw0511 小时前
连接蓝牙时“无媒体信号”怎么办?
人工智能·机器学习·微服务
可我不想做饼干11 小时前
微服务注册中心 Spring Cloud Eureka是什么?
微服务
北风朝向12 小时前
Spring Boot参数校验8大坑与生产级避坑指南
java·spring boot·后端·spring
龙茶清欢14 小时前
Spring Boot 应用启动组件加载顺序与优先级详解
java·spring boot·后端·微服务
RainbowSea14 小时前
4. ChatClient 的初始,快速使用上手
java·spring·ai编程
RainbowSea14 小时前
3. Ollama 安装,流式输出,多模态,思考模型
java·spring·ai编程
蜚鸣16 小时前
Spring依赖注入方式
spring