Spring项目集成Sentinel,自定义BlockException异常返回值

背景

Sentinel 官网文档:introduction

springmvc项目中集成sentinel,通过sentinel提供的控制台配置限流、熔断等规则,

项目实现UrlBlockHandler,实现在catch到blockexception时进行自定义返回值处理

sentinel的官网文档非常详细的介绍了如何下载部署控制台、以及如何接入各类型的项目框架,本文以mvc项目为例,集成sentinel。

sentinel 控制台jar下载地址:Releases · alibaba/Sentinel · GitHub

Spring项目集成Sentinel,自定义BlockException异常返回值_第1张图片

启动控制台

java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.5.jar

集成

maven配置 

项目maven依赖,sentinel-web-servlet是web项目与sentinel客户端的适配器项目,又引用了sentinel-core核心依赖,web-servlet主要提供一个filter,用来集成sentinel功能

Spring项目集成Sentinel,自定义BlockException异常返回值_第2张图片

sentinel-transport-simple-http实现与控制台通讯

		
			com.alibaba.csp
			sentinel-web-servlet
			1.8.5
		
		
		    com.alibaba.csp
		    sentinel-transport-simple-http
		    1.8.5
		

web.xml配置

通过配置web.xml 的filter开启sentinel支持


	SentinelCommonFilter
	com.alibaba.csp.sentinel.adapter.servlet.CommonFilter



	SentinelCommonFilter
	/*

 Sentinel配置

SentinelConfiguration配置类主要自定义一个UrlBlockHandler,用来处理异常,要确保这个配置类会被框架扫描到

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Configuration;

import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler;
import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
import com.alibaba.csp.sentinel.adapter.servlet.config.WebServletConfig;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
import com.alibaba.csp.sentinel.util.StringUtil;

@Configuration
public class SentinelConfiguration {

	public UrlBlockHandler urlBlockHandler() {
		return new UrlBlockHandler() {

			@Override
			public void blocked(HttpServletRequest request, HttpServletResponse response, BlockException ex) throws IOException {
				if (ex instanceof FlowException) {
					// 触发接口限流
				} else if (ex instanceof DegradeException) {
					// 触发熔断降级
				} else if (ex instanceof SystemBlockException) {
					// 触发系统保护
				} else if (ex instanceof AuthorityException) {
					// 触发授权规则
				}
				String errMsg = "Blocked by Sentinel (flow limiting)!" + ex.getRule().getResource();
				StringBuffer url = request.getRequestURL();

				if ("GET".equals(request.getMethod()) && StringUtil.isNotBlank(request.getQueryString())) {
					url.append("?").append(request.getQueryString());
				}

				if (StringUtil.isBlank(WebServletConfig.getBlockPage())) {
					response.setStatus(WebServletConfig.getBlockPageHttpStatus());
					PrintWriter out = response.getWriter();
					out.print("{\"code\":0,\"message\":\"" + errMsg + "\"}");
					out.flush();
					out.close();

				} else {
					String redirectUrl = WebServletConfig.getBlockPage() + "?http_referer=" + url.toString();
					// Redirect to the customized block page.
					response.sendRedirect(redirectUrl);
				}

			}

		};
	}

	@PostConstruct
	public void init() {
		WebCallbackManager.setUrlBlockHandler(urlBlockHandler());
	}
}

 启动

项目启动时加入 JVM 参数 -Dcsp.sentinel.dashboard.server=localhost:8080 指定控制台地址和端口

更多启动参数配置参考startup-configuration

你可能感兴趣的:(sentinel,java,开发语言)