sentinel服务保护

一、整合sentinel

1、下载并启动sentinel

启动命令(默认是8080端口,因此修改端口号为8070)

bash 复制代码
java -Dserver.port=8070 -Dcsp.sentinel.dashboard.server=localhost:8070 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.6.jar

2、引入依赖

XML 复制代码
<!--整合sentinel-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>

3、application.yaml增加sentinel配置

XML 复制代码
spring:
  cloud:
    sentinel:
      transport:
        dashboard: localhost:8070

4、重启微服务,并发起一些请求

二、利用sentinel实现请求的限流

2.1、流控

2.1.1、qps和并发线程数的区别

并发线程数:表示web请求进来服务器,tomcat开启几个线程来处理这些web请求。

qps:单位时间内,请求接口次数限制。

2.2、fallback

2.2.1、添加依赖

XML 复制代码
<dependency>
                <groupId>io.github.openfeign</groupId>
                <artifactId>feign-okhttp</artifactId>
            </dependency>

2.2.2、application.yaml配置

XML 复制代码
feign:
  okhttp:
    enabled: true
  sentinel:
    enabled: true

2.2.3、写FallbackFactory接口的实现类

java 复制代码
package com.niuniu.user.feignclient.fallback;

import com.niuniu.user.feignclient.OrderClient;
import com.niuniu.user.model.Order;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.List;

@Slf4j
public class OrderClientFallBackFactory implements FallbackFactory<OrderClient> {
    @Override
    public OrderClient create(Throwable throwable) {
        log.error("OrderClient error!", throwable);
        return new OrderClient() {
            @Override
            public List<Order> getOrdersByUserId(Long userId) {
                return Collections.emptyList();
            }
        };
    }
}

2.2.4、生成上一步实现类的对象

java 复制代码
package com.niuniu.user.config;

import com.niuniu.user.feignclient.fallback.OrderClientFallBackFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FallBackConfig {

    @Bean
    public OrderClientFallBackFactory orderClientFallBackFactory() {
        return new OrderClientFallBackFactory();
    }
}

2.2.5、feignclient接口增加fallbackFactory = OrderClientFallBackFactory.class

java 复制代码
package com.niuniu.user.feignclient;

import com.niuniu.common.config.DefaultFeignConfig;
import com.niuniu.user.feignclient.fallback.OrderClientFallBackFactory;
import com.niuniu.user.model.Order;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Component
@FeignClient(value = "order-service", configuration = DefaultFeignConfig.class, fallbackFactory = OrderClientFallBackFactory.class)
public interface OrderClient {
    @GetMapping(value = "/order-service/order/getOrdersByUserId")
    List<Order> getOrdersByUserId(@RequestParam("userId") Long userId);
}

2.2.6、测试

修改变调用微服务的代码

java 复制代码
/**
     * 根据用户查询订单
     * @param userId
     * @return
     */
    @GetMapping("/getOrdersByUserId")
    public List<Order> getOrdersByUserId(@RequestParam(name = "userId") Long userId){
        log.info(UserContext.getUser().toString());
        int i = 1;
        System.out.println(i / 0);
        return orderMapper.getByUserId(userId);
    }

2.3、服务熔断

相关推荐
꧁꫞꯭零꯭点꯭꫞꧂1 天前
G6绘制机柜 以及机柜设备的demo
前端·javascript·vue.js
开开心心就好1 天前
跨平台高速下载工具,支持浏览器功能强大
运维·服务器·windows·pdf·旅游·媒体·1024程序员节
chools1 天前
一篇文章带你搞懂Java“设计模式”! - - 超长文(涵盖23种)万字总结!【汇总篇】
java·开发语言·设计模式
良逍Ai出海1 天前
OpenClaw 新手最该先搞懂的 2 套命令
android·java·数据库
C澒1 天前
供应链产研交付提效:前端多业务线新增样板间页面统计方案
前端·mr
6+h1 天前
【Spring】深度剖析IoC
java·后端·spring
可视之道1 天前
低代码可视化平台的前端架构设计:从渲染引擎到插件系统
前端
Keanu-1 天前
Redis 主从复制及哨兵模式配置
服务器·数据库·redis
南城书生1 天前
Android Kotlin 协程原理分析
前端
Yupureki1 天前
《C++实战项目-高并发内存池》3.ThreadCache构造
服务器·c语言·c++·算法·哈希算法