当前位置: 首页> 汽车> 报价 > 网站页面设计报价模板_河南造价信息网_桂平seo快速优化软件_关键词歌曲

网站页面设计报价模板_河南造价信息网_桂平seo快速优化软件_关键词歌曲

时间:2025/7/10 0:40:12来源:https://blog.csdn.net/qq_45722630/article/details/143131282 浏览次数: 0次
网站页面设计报价模板_河南造价信息网_桂平seo快速优化软件_关键词歌曲

1. 雪崩问题

一个微服务出现问题导致一系列微服务都不可以正常工作。
服务保护方案:

  1. 请求限流。
  2. 线程隔离。
    在这里插入图片描述
  3. 服务熔断
    在这里插入图片描述

2. Sentinel

  1. 启动Sentinel
java -Dserver.port=8090 -Dcsp.sentinel.dashboard.server=localhost:8090 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar
  1. 依赖
<!--sentinel-->
<dependency><groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
  1. 配置
spring:cloud: sentinel:transport:dashboard: localhost:8090http-method-specify: true # 将请求方式也作为簇点链路

簇点链路: Controller中的Rest接口

3. Fallback

  1. 配置openfeign使之能够成为簇点链路
feign:okhttp:enabled: true # 开启OKHttp功能sentinel: # 让feign可以成为簇点链路enabled: true
  1. 设置FallbackFactory
package com.hmall.api.client.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> {@Overridepublic ItemClient create(Throwable cause) {return new ItemClient() {@Overridepublic List<ItemDTO> queryItemByIds(Collection<Long> ids) {log.info("查询商品失败!", cause);return CollUtils.emptyList();}@Overridepublic void deductStock(List<OrderDetailDTO> items) {log.info("扣减商品库存失败!", cause);throw new RuntimeException(cause);}};}
}
  1. 注册Bean
package com.hmall.api.config;import com.hmall.api.client.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 {@Beanpublic Logger.Level feignLogLevel() {return Logger.Level.FULL;}@Beanpublic RequestInterceptor userInfoRequestInterceptor() {return new RequestInterceptor() {@Overridepublic void apply(RequestTemplate requestTemplate) {Long userId = UserContext.getUser();if (userId!=null) {requestTemplate.header("user-info", userId.toString());}}};}@Beanpublic ItemClientFallbackFactory itemClientFallbackFactory() {return new ItemClientFallbackFactory();}
}
  1. 配置Bean
@FeignClient(value = "item-service", fallbackFactory = ItemClientFallbackFactory.class)
public interface ItemClient {@GetMapping("/items")List<ItemDTO> queryItemByIds(@RequestParam("ids") Collection<Long> ids);@PutMapping("/items/stock/deduct")void deductStock(@RequestBody List<OrderDetailDTO> items);
}

4. 熔断

拒绝发送请求给故障的微服务
在这里插入图片描述

关键字:网站页面设计报价模板_河南造价信息网_桂平seo快速优化软件_关键词歌曲

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: