【黑马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后熔断结束后会发送一个请求看商品服务查询是否正常,如果不正常继续熔断,反之。。。。。。

相关推荐
尚学教辅学习资料1 小时前
Ruoyi-vue-plus-5.x第五篇Spring框架核心技术:5.1 Spring Boot自动配置
vue.js·spring boot·spring
晚安里2 小时前
Spring 框架(IoC、AOP、Spring Boot) 的必会知识点汇总
java·spring boot·spring
上官浩仁2 小时前
springboot ioc 控制反转入门与实战
java·spring boot·spring
deepwater_zone2 小时前
Spring 微服务
spring·微服务
掘金-我是哪吒2 小时前
分布式微服务系统架构第169集:1万~10万QPS的查当前订单列表
分布式·微服务·云原生·架构·系统架构
叫我阿柒啊3 小时前
从Java全栈到前端框架:一位程序员的实战之路
java·spring boot·微服务·消息队列·vue3·前端开发·后端开发
attitude.x3 小时前
微服务架构的五大核心挑战与应对策略
微服务·云原生·架构
刘一说8 小时前
Linux调试命令速查:Java/微服务必备
java·linux·微服务
刘一说8 小时前
Spring Boot+Nacos+MySQL微服务问题排查指南
spring boot·mysql·微服务
IT·陈寒8 小时前
怎么这么多 StringUtils —— Apache、Spring、Hutool 全面对比
java·spring·apache