美文网首页
Spring Boot Sentinel

Spring Boot Sentinel

作者: AGEGG | 来源:发表于2023-06-27 17:52 被阅读0次

sentinel 主要用于流控和降级

一、流控

1.项目安装依赖

quick-start | Sentinel (sentinelguard.io)

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

2.下载控制台并启动

dashboard | Sentinel (sentinelguard.io)
设置为8333端口
java -jar sentinel-dashboard-1.6.3.jar --server.port=8333
在控制台中可以调整参数(默认设置保存在内存中,项目重启失效)

3.添加配置信息

spring.cloud.sentinel.transport.port=8719
spring.cloud.sentinel.transport.dashboard=localhost:8333

4.增加实时监控(Endpoint 支持)

Sentinel · alibaba/spring-cloud-alibaba Wiki · GitHub

增加依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

配置信息:

management.endpoints.web.exposure.include=*

5.修改流控拦截返回信息

package com.agegg.gulimall.coupon.config;

import com.agegg.common.utils.R;
import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler;
import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import org.springframework.context.annotation.Configuration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Configuration
public class CouponSentinelConfig {

    public CouponSentinelConfig() {
        WebCallbackManager.setUrlBlockHandler(new UrlBlockHandler() {
            @Override
            public void blocked(HttpServletRequest httpServletRequest, HttpServletResponse response, BlockException e) throws IOException {
                R error = R.error(4000, "流量过大");
                response.setCharacterEncoding("UTF-8");
                response.setContentType("application/json");
                response.getWriter().write(JSON.toJSONString(error));

            }
        }
        );

    }

}

二、Feign熔断

1.开启Feign

调用方开启,此时控制台能监控到调用方调用的feign请求

feign.sentinel.enabled=true

2.fallback 方法

CouponFeignService

package com.agegg.gulimall.member.feign;

import com.agegg.common.utils.R;
import com.agegg.gulimall.member.feign.fallback.SentinelFeignFallbackService;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(value = "gulimall-coupon", fallback = SentinelFeignFallbackService.class)
public interface CouponFeignService {

    @RequestMapping("/coupon/coupon/member/list")
    public R memberCoupon();



}

SentinelFeignFallbackService

package com.agegg.gulimall.member.feign.fallback;


import com.agegg.common.utils.R;
import com.agegg.gulimall.member.feign.CouponFeignService;
import org.springframework.stereotype.Component;

@Component
public class SentinelFeignFallbackService implements CouponFeignService {


    @Override
    public R memberCoupon() {
        return R.error(4000, "error");
    }
}

3.在控制台可以设置降级

降级后,访问不通,触发熔断,并进行熔断回调

相关文章

网友评论

      本文标题:Spring Boot Sentinel

      本文链接:https://www.haomeiwen.com/subject/ystgydtx.html