All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.herodotus.engine.facility.alibaba.autoconfigure.sentinel.FacilityAlibabaSentinelAutoConfiguration Maven / Gradle / Ivy

The newest version!
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2020-2030 郑庚伟 ZHENGGENGWEI (码匠君),  Licensed under the AGPL License
 *
 * This file is part of Herodotus Engine.
 *
 * Herodotus Engine is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Herodotus Engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 */

package cn.herodotus.engine.facility.alibaba.autoconfigure.sentinel;

import cn.herodotus.engine.assistant.core.json.jackson2.utils.Jackson2Utils;
import cn.herodotus.engine.assistant.definition.domain.Result;
import cn.herodotus.engine.facility.alibaba.autoconfigure.sentinel.enhance.HerodotusSentinelFeign;
import com.alibaba.csp.sentinel.adapter.spring.webflux.callback.BlockRequestHandler;
import com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.callback.BlockExceptionHandler;
import feign.Feign;
import jakarta.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerResponse;

/**
 * 

Description: 基础设施 Sentinel 配置

* * @author : gengwei.zheng * @date : 2022/2/5 17:57 */ @AutoConfiguration public class FacilityAlibabaSentinelAutoConfiguration { private static final Logger log = LoggerFactory.getLogger(FacilityAlibabaSentinelAutoConfiguration.class); @PostConstruct public void postConstruct() { log.info("[Herodotus] |- Module [Facility Alibaba Sentinel Starter] Auto Configure."); } @Bean @Scope("prototype") @ConditionalOnMissingBean @ConditionalOnProperty(name = "feign.sentinel.enabled") public Feign.Builder feignSentinelBuilder() { return HerodotusSentinelFeign.builder(); } /** * 限流、熔断统一处理类 */ @Configuration(proxyBeanMethods = false) @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) public static class WebMvcHandler { @Bean public BlockExceptionHandler webmvcBlockExceptionHandler() { return (request, response, resource, e) -> { response.setStatus(HttpStatus.TOO_MANY_REQUESTS.value()); Result result = Result.failure("Too many request, please retry later."); result.path(resource); response.getWriter().print(Jackson2Utils.toJson(result)); }; } } /** * 限流、熔断统一处理类 */ @Configuration(proxyBeanMethods = false) @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE) public static class WebfluxHandler { @Bean public BlockRequestHandler webfluxBlockExceptionHandler() { return (exchange, t) -> ServerResponse.status(HttpStatus.TOO_MANY_REQUESTS) .contentType(MediaType.APPLICATION_JSON) .body(BodyInserters.fromValue(Result.failure(t.getMessage()))); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy