黑马商城(五)微服务保护和分布式事务

一、雪崩问题

二、雪崩-解决方案(服务保护方案)

请求限流:

线程隔离:

服务熔断:

服务保护组件:

三、Sentinel

引入依赖:

java 复制代码
<!--sentinel-->
<dependency>
    <groupId>com.alibaba.cloud</groupId> 
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
bash 复制代码
server:
  port: 8082
feign:
  okhttp:
    enabled: true #配置连接池开关
  swagger:
    title: "黑马商城购物车服务接口文档"
    package: "com.hmall.cart.controller"
    description: "购物车服务接口"
spring:
  cloud:
    sentinel:
      transport:
        dashboard: localhost:8090 #sentinel控制台地址

簇点链路:

Endpoint==Controller里的各种路径

请求限流:

QPS:每秒钟请求的数量

线程隔离:

fallback:

案例-给FeignClient添加Fallback逻辑:
java 复制代码
package com.hmall.api.fallback;

import com.hmall.api.client.ItemClient;
import com.hmall.api.dto.ItemDTO;
import com.hmall.api.dto.OrderDetailDTO;
import com.hmall.common.utils.CollUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;

import java.util.Collection;
import java.util.List;

@Slf4j
public class ItemClientFallbackFactory implements FallbackFactory<ItemClient> {
    @Override
    public ItemClient create(Throwable cause) {
        return new ItemClient() {
            @Override
            public List<ItemDTO> queryItemByIds(Collection<Long> ids) {
                log.error("查询商品失败!",cause);
                //查询不到返回一个空集合
                return CollUtils.emptyList();
            }

            @Override
            public void deductStock(List<OrderDetailDTO> items) {
                log.error("扣减商品库存失败!",cause);
                throw new RuntimeException(cause);
            }
        };
    }
}
java 复制代码
package com.hmall.api.config;

import com.hmall.api.fallback.ItemClientFallbackFactory;
import com.hmall.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;

public class DefaultFeignConfig { //配置类中声明bean对象

    @Bean
    public Logger.Level feignLoggerLevel(){
        return Logger.Level.FULL;
    }

    @Bean
    public RequestInterceptor userInfoRequestInterceptor(){
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                Long userId = UserContext.getUser();
                if(userId!=null) {
                    requestTemplate.header("user-info", userId.toString());
                }
            }
        };
    }

    @Bean
    public ItemClientFallbackFactory itemClientFallbackFactory(){
        return new ItemClientFallbackFactory();
    }
}

服务熔断:

断路器:

四、分布式事务

Seata:

Seata架构:

部署TC服务:

bash 复制代码
docker run --name seata \
-p 8099:8099 \
-p 7099:7099 \
-e SEATA_IP=192.168.50.129 \    #自己的IP地址
-v ./seata:/seata-server/resources \
--privileged=true \
--network hmall \    #确保和nacos mysql在一个网络下
-d \
seataio/seata-server:1.5.2

微服务继承Seata:

通过nacos共享配置:

bash 复制代码
seata:
  registry: # TC服务注册中心的配置,微服务根据这些信息去注册中心获取tc服务地址
    type: nacos # 注册中心类型 nacos
    nacos:
      server-addr: 192.168.50.129:8848 # nacos地址
      namespace: "" # namespace,默认为空
      group: DEFAULT_GROUP # 分组,默认是DEFAULT_GROUP
      application: seata-server # seata服务名称
      username: nacos
      password: nacos
  tx-service-group: hmall # 事务组名称
  service:
    vgroup-mapping: # 事务组与tc集群的映射关系
      hmall: "default"

XA模式:

实现XA模式:

整个事务中其他微服务端的事务因为后续也需要实现事务管理,都需要在对应方法上加上@Transactional

AT模式:

实现AT模式:

AT与XA区别:

追求一致性选XA

追求性能选AT

相关推荐
天空属于哈夫克35 天前
企业微信二次开发:精准实现关键词自动回复
架构·企业微信
晨米酱6 天前
AGENTS.md:Agent 的上下文策略层
面试·架构·agent
这个DBA有点耶6 天前
MVCC深入:Read View、版本链与快照读——InnoDB并发控制的内核
数据库·mysql·架构
码流子6 天前
高速公路安全监测实践:碰撞监测预警+物联网底座,从感知到处置的闭环
大数据·人工智能·物联网·算法·架构
moMo6 天前
从固定流程到问题路由:让 LangGraph RAG 按需检索
架构
白远山6 天前
上海24小时自助健身房解决方案实战指南与经验分享
java·数据库·架构·需求分析
LorryJovens6 天前
【LAAP科研】双系统具身AGI范式研究——基于LAAP认知架构与Jev概率决策模型的系统性技术调研与范式验证
人工智能·gpt·安全·架构
Thomas.Sir6 天前
第21课:PyTorch|GPU多卡训练与分布式训练基础【让多卡并行成为你的加速引擎】
人工智能·pytorch·分布式
Cicada1286 天前
库存消息消费的正确性设计——从幂等窗口到批量流水线
分布式·系统架构
JQweryuiop6 天前
中小型游戏陪练工作室数字化管理实践:基于七彩屋电竞护航小程序订单、履约与绩效系统的落地复盘
大数据·游戏·小程序·架构