From 16ed6055a4634fa82e9996166b913e8c46d17f87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= Date: Fri, 11 Oct 2019 21:42:12 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=96=B0=E7=89=B9=E6=80=A7?= =?UTF-8?q?=E3=80=82=20sentinel=20->=20hytrix=20|=20add=20pigx-common-sent?= =?UTF-8?q?inel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pigx-auth/pom.xml | 5 + pigx-common/pigx-common-bom/pom.xml | 5 + pigx-common/pigx-common-core/pom.xml | 9 -- .../core/sensitive/SensitiveSerialize.java | 2 - .../common/core/util/DesensitizedUtils.java | 4 +- .../pigx/common/core/util/WebUtils.java | 16 ++-- .../cloud/openfeign/PigxFallbackFactory.java | 45 --------- .../cloud/openfeign/PigxFeignFallback.java | 83 ---------------- .../cloud/openfeign/PigxHystrixTargeter.java | 96 ------------------- .../main/resources/META-INF/spring.factories | 1 - pigx-common/pigx-common-sentinel/pom.xml | 30 ++++++ .../sentinel/SentinelAutoConfigure.java} | 27 +++--- .../sentinel/handle/PigxUrlBlockHandler.java | 50 ++++++++++ .../PigxHeaderRequestOriginParser.java} | 40 +++++--- .../main/resources/META-INF/spring.factories | 2 + pigx-common/pom.xml | 1 + pigx-gateway/pom.xml | 5 + .../config/RouterFunctionConfiguration.java | 24 ++--- .../handler/HystrixFallbackHandler.java | 53 ---------- pigx-register/pom.xml | 4 + pigx-upms/pigx-upms-api/pom.xml | 5 + pigx-upms/pigx-upms-biz/pom.xml | 5 + pom.xml | 5 - 23 files changed, 175 insertions(+), 342 deletions(-) delete mode 100644 pigx-common/pigx-common-security/src/main/java/org/springframework/cloud/openfeign/PigxFallbackFactory.java delete mode 100644 pigx-common/pigx-common-security/src/main/java/org/springframework/cloud/openfeign/PigxFeignFallback.java delete mode 100644 pigx-common/pigx-common-security/src/main/java/org/springframework/cloud/openfeign/PigxHystrixTargeter.java create mode 100755 pigx-common/pigx-common-sentinel/pom.xml rename pigx-common/{pigx-common-security/src/main/java/org/springframework/cloud/openfeign/PigxHystrixFeignTargeterConfiguration.java => pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/SentinelAutoConfigure.java} (60%) mode change 100644 => 100755 create mode 100644 pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/handle/PigxUrlBlockHandler.java rename pigx-common/{pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/util/package-info.java => pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/parser/PigxHeaderRequestOriginParser.java} (56%) create mode 100755 pigx-common/pigx-common-sentinel/src/main/resources/META-INF/spring.factories delete mode 100644 pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/handler/HystrixFallbackHandler.java diff --git a/pigx-auth/pom.xml b/pigx-auth/pom.xml index 911a157e1..47bbce472 100644 --- a/pigx-auth/pom.xml +++ b/pigx-auth/pom.xml @@ -63,6 +63,11 @@ com.pig4cloud pigx-common-data + + + com.pig4cloud + pigx-common-sentinel + org.springframework.boot diff --git a/pigx-common/pigx-common-bom/pom.xml b/pigx-common/pigx-common-bom/pom.xml index 17d1e60e4..846c4bdd8 100644 --- a/pigx-common/pigx-common-bom/pom.xml +++ b/pigx-common/pigx-common-bom/pom.xml @@ -66,6 +66,11 @@ pigx-common-security ${pigx.version} + + com.pig4cloud + pigx-common-sentinel + ${pigx.version} + com.pig4cloud pigx-common-sequence diff --git a/pigx-common/pigx-common-core/pom.xml b/pigx-common/pigx-common-core/pom.xml index fec0b2f67..4f2585b62 100644 --- a/pigx-common/pigx-common-core/pom.xml +++ b/pigx-common/pigx-common-core/pom.xml @@ -45,15 +45,6 @@ javax.servlet javax.servlet-api - - - io.github.openfeign - feign-okhttp - - - org.springframework.cloud - spring-cloud-starter-openfeign - org.springframework.boot diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/sensitive/SensitiveSerialize.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/sensitive/SensitiveSerialize.java index a1b683ebd..1b19b03e0 100644 --- a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/sensitive/SensitiveSerialize.java +++ b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/sensitive/SensitiveSerialize.java @@ -23,7 +23,6 @@ import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.ser.ContextualSerializer; -import com.google.common.base.Preconditions; import com.pig4cloud.pigx.common.core.util.DesensitizedUtils; import lombok.AllArgsConstructor; import lombok.NoArgsConstructor; @@ -49,7 +48,6 @@ public class SensitiveSerialize extends JsonSerializer implements Contex @Override public void serialize(final String origin, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider) throws IOException { - Preconditions.checkNotNull(type, "Sensitive type enum should not be null."); switch (type) { case CHINESE_NAME: jsonGenerator.writeString(DesensitizedUtils.chineseName(origin)); diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/DesensitizedUtils.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/DesensitizedUtils.java index 3724496e2..ce9d83a8b 100644 --- a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/DesensitizedUtils.java +++ b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/DesensitizedUtils.java @@ -18,7 +18,7 @@ package com.pig4cloud.pigx.common.core.util; -import org.apache.commons.lang.StringUtils; +import cn.hutool.core.util.StrUtil; /** * 脱敏工具类 @@ -120,7 +120,7 @@ public class DesensitizedUtils { if (email == null) { return null; } - int index = StringUtils.indexOf(email, "@"); + int index = StrUtil.indexOf(email, '@'); if (index <= 1) { return email; } diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/WebUtils.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/WebUtils.java index 6d600d9aa..300e5cb0f 100644 --- a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/WebUtils.java +++ b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/WebUtils.java @@ -20,12 +20,12 @@ package com.pig4cloud.pigx.common.core.util; import cn.hutool.core.codec.Base64; +import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONUtil; import com.pig4cloud.pigx.common.core.exception.CheckedException; import lombok.SneakyThrows; import lombok.experimental.UtilityClass; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang.StringUtils; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.server.reactive.ServerHttpRequest; @@ -179,25 +179,25 @@ public class WebUtils extends org.springframework.web.util.WebUtils { public String getIP(HttpServletRequest request) { Assert.notNull(request, "HttpServletRequest is null"); String ip = request.getHeader("X-Requested-For"); - if (StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) { + if (StrUtil.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) { ip = request.getHeader("X-Forwarded-For"); } - if (StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) { + if (StrUtil.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } - if (StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) { + if (StrUtil.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } - if (StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) { + if (StrUtil.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_CLIENT_IP"); } - if (StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) { + if (StrUtil.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_X_FORWARDED_FOR"); } - if (StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) { + if (StrUtil.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } - return StringUtils.isBlank(ip) ? null : ip.split(",")[0]; + return StrUtil.isBlank(ip) ? null : ip.split(",")[0]; } /** diff --git a/pigx-common/pigx-common-security/src/main/java/org/springframework/cloud/openfeign/PigxFallbackFactory.java b/pigx-common/pigx-common-security/src/main/java/org/springframework/cloud/openfeign/PigxFallbackFactory.java deleted file mode 100644 index 1cecb1537..000000000 --- a/pigx-common/pigx-common-security/src/main/java/org/springframework/cloud/openfeign/PigxFallbackFactory.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (c) 2018-2025, lengleng All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * Neither the name of the pig4cloud.com developer nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * Author: lengleng (wangiegie@gmail.com) - */ - -package org.springframework.cloud.openfeign; - -import feign.Target; -import feign.hystrix.FallbackFactory; -import lombok.AllArgsConstructor; -import org.springframework.cglib.proxy.Enhancer; - -/** - * @author 冷冷 - *

- * 默认 Fallback,避免写过多fallback类 - */ -@AllArgsConstructor -public class PigxFallbackFactory implements FallbackFactory { - private final Target target; - - @Override - @SuppressWarnings("unchecked") - public T create(Throwable cause) { - final Class targetType = target.type(); - final String targetName = target.name(); - Enhancer enhancer = new Enhancer(); - enhancer.setSuperclass(targetType); - enhancer.setUseCache(true); - enhancer.setCallback(new PigxFeignFallback<>(targetType, targetName, cause)); - return (T) enhancer.create(); - } -} diff --git a/pigx-common/pigx-common-security/src/main/java/org/springframework/cloud/openfeign/PigxFeignFallback.java b/pigx-common/pigx-common-security/src/main/java/org/springframework/cloud/openfeign/PigxFeignFallback.java deleted file mode 100644 index 76f52c408..000000000 --- a/pigx-common/pigx-common-security/src/main/java/org/springframework/cloud/openfeign/PigxFeignFallback.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright (c) 2018-2025, lengleng All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * Neither the name of the pig4cloud.com developer nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * Author: lengleng (wangiegie@gmail.com) - */ - -package org.springframework.cloud.openfeign; - -import cn.hutool.core.util.StrUtil; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.pig4cloud.pigx.common.core.util.R; -import com.pig4cloud.pigx.common.core.util.SpringContextHolder; -import feign.FeignException; -import lombok.AllArgsConstructor; -import lombok.SneakyThrows; -import lombok.extern.slf4j.Slf4j; -import org.springframework.cglib.proxy.MethodInterceptor; -import org.springframework.cglib.proxy.MethodProxy; -import org.springframework.lang.Nullable; - -import java.lang.reflect.Method; -import java.nio.charset.StandardCharsets; -import java.util.Objects; - -/** - * @author lengleng - *

- * fallback 代理处理 - */ -@Slf4j -@AllArgsConstructor -public class PigxFeignFallback implements MethodInterceptor { - private final Class targetType; - private final String targetName; - private final Throwable cause; - - @Nullable - @Override - @SneakyThrows - public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) { - Class returnType = method.getReturnType(); - if (R.class != returnType) { - return null; - } - FeignException exception = (FeignException) cause; - - byte[] content = exception.content(); - - String str = StrUtil.str(content, StandardCharsets.UTF_8); - - log.error("PigxFeignFallback:[{}.{}] serviceId:[{}] message:[{}]", targetType.getName(), method.getName(), targetName, str); - ObjectMapper objectMapper = SpringContextHolder.getBean(ObjectMapper.class); - return objectMapper.readValue(str, R.class); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - PigxFeignFallback that = (PigxFeignFallback) o; - return targetType.equals(that.targetType); - } - - @Override - public int hashCode() { - return Objects.hash(targetType); - } -} diff --git a/pigx-common/pigx-common-security/src/main/java/org/springframework/cloud/openfeign/PigxHystrixTargeter.java b/pigx-common/pigx-common-security/src/main/java/org/springframework/cloud/openfeign/PigxHystrixTargeter.java deleted file mode 100644 index 4246a9368..000000000 --- a/pigx-common/pigx-common-security/src/main/java/org/springframework/cloud/openfeign/PigxHystrixTargeter.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright (c) 2018-2025, lengleng All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * Neither the name of the pig4cloud.com developer nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * Author: lengleng (wangiegie@gmail.com) - */ - -package org.springframework.cloud.openfeign; - -import feign.Feign; -import feign.Target; -import feign.hystrix.FallbackFactory; -import feign.hystrix.HystrixFeign; -import feign.hystrix.SetterFactory; -import org.springframework.lang.Nullable; - -/** - * @author lengleng - *

- * 注入默认的 fallbackFactory - */ -@SuppressWarnings("unchecked") -class PigxHystrixTargeter implements Targeter { - - @Override - public T target(FeignClientFactoryBean factory, Feign.Builder feign, FeignContext context, - Target.HardCodedTarget target) { - if (!(feign instanceof HystrixFeign.Builder)) { - return feign.target(target); - } - HystrixFeign.Builder builder = (HystrixFeign.Builder) feign; - SetterFactory setterFactory = getOptional(factory.getName(), context, SetterFactory.class); - if (setterFactory != null) { - builder.setterFactory(setterFactory); - } - Class fallback = factory.getFallback(); - if (fallback != void.class) { - return targetWithFallback(factory.getName(), context, target, builder, fallback); - } - Class fallbackFactory = factory.getFallbackFactory(); - if (fallbackFactory != void.class) { - return targetWithFallbackFactory(factory.getName(), context, target, builder, fallbackFactory); - } - - PigxFallbackFactory pigxFallbackFactory = new PigxFallbackFactory(target); - return (T) builder.target(target, pigxFallbackFactory); - } - - private T targetWithFallbackFactory(String feignClientName, FeignContext context, - Target.HardCodedTarget target, - HystrixFeign.Builder builder, - Class fallbackFactoryClass) { - FallbackFactory fallbackFactory = (FallbackFactory) - getFromContext("fallbackFactory", feignClientName, context, fallbackFactoryClass, FallbackFactory.class); - return builder.target(target, fallbackFactory); - } - - - private T targetWithFallback(String feignClientName, FeignContext context, - Target.HardCodedTarget target, - HystrixFeign.Builder builder, Class fallback) { - T fallbackInstance = getFromContext("fallback", feignClientName, context, fallback, target.type()); - return builder.target(target, fallbackInstance); - } - - private T getFromContext(String fallbackMechanism, String feignClientName, FeignContext context, Class beanType, - Class targetType) { - Object fallbackInstance = context.getInstance(feignClientName, beanType); - if (fallbackInstance == null) { - throw new IllegalStateException(String.format("No " + fallbackMechanism + - " instance of type %s found for feign client %s", beanType, feignClientName)); - } - - if (!targetType.isAssignableFrom(beanType)) { - throw new IllegalStateException(String.format( - "Incompatible " + fallbackMechanism + " instance. Fallback/fallbackFactory of " + - "type %s is not assignable to %s for feign client %s", beanType, targetType, feignClientName)); - } - return (T) fallbackInstance; - } - - @Nullable - private T getOptional(String feignClientName, FeignContext context, Class beanType) { - return context.getInstance(feignClientName, beanType); - } -} diff --git a/pigx-common/pigx-common-security/src/main/resources/META-INF/spring.factories b/pigx-common/pigx-common-security/src/main/resources/META-INF/spring.factories index aaa6823bd..7908386a5 100644 --- a/pigx-common/pigx-common-security/src/main/resources/META-INF/spring.factories +++ b/pigx-common/pigx-common-security/src/main/resources/META-INF/spring.factories @@ -1,5 +1,4 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ - org.springframework.cloud.openfeign.PigxHystrixFeignTargeterConfiguration,\ com.pig4cloud.pigx.common.security.component.GlobalExceptionHandlerResolver,\ com.pig4cloud.pigx.common.security.component.PigxTokenEnhancer,\ com.pig4cloud.pigx.common.security.service.PigxUserDetailsServiceImpl diff --git a/pigx-common/pigx-common-sentinel/pom.xml b/pigx-common/pigx-common-sentinel/pom.xml new file mode 100755 index 000000000..213c65540 --- /dev/null +++ b/pigx-common/pigx-common-sentinel/pom.xml @@ -0,0 +1,30 @@ + + + + com.pig4cloud + pigx-common + 3.4.0 + + + 4.0.0 + jar + pigx-common-sentinel + sentinel服务降级熔断、限流组件 + + + com.pig4cloud + pigx-common-core + + + com.alibaba.cloud + spring-cloud-starter-alibaba-sentinel + + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + diff --git a/pigx-common/pigx-common-security/src/main/java/org/springframework/cloud/openfeign/PigxHystrixFeignTargeterConfiguration.java b/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/SentinelAutoConfigure.java old mode 100644 new mode 100755 similarity index 60% rename from pigx-common/pigx-common-security/src/main/java/org/springframework/cloud/openfeign/PigxHystrixFeignTargeterConfiguration.java rename to pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/SentinelAutoConfigure.java index 23430c455..29a4919e9 --- a/pigx-common/pigx-common-security/src/main/java/org/springframework/cloud/openfeign/PigxHystrixFeignTargeterConfiguration.java +++ b/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/SentinelAutoConfigure.java @@ -15,28 +15,27 @@ * Author: lengleng (wangiegie@gmail.com) */ -package org.springframework.cloud.openfeign; +package com.pig4cloud.pigx.common.sentinel; -import feign.hystrix.HystrixFeign; -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.context.annotation.Bean; +import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager; +import com.pig4cloud.pigx.common.sentinel.handle.PigxUrlBlockHandler; +import com.pig4cloud.pigx.common.sentinel.parser.PigxHeaderRequestOriginParser; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Primary; + +import javax.annotation.PostConstruct; /** * @author lengleng *

- * HystrixFeignTargeter 配置 + * sentinel 配置 */ @Configuration -@ConditionalOnClass(HystrixFeign.class) -@ConditionalOnProperty("feign.hystrix.enabled") -public class PigxHystrixFeignTargeterConfiguration { +public class SentinelAutoConfigure { - @Bean - @Primary - public Targeter pigxFeignTargeter() { - return new PigxHystrixTargeter(); + @PostConstruct + public void initWebCallbackManager() { + WebCallbackManager.setUrlBlockHandler(new PigxUrlBlockHandler()); + WebCallbackManager.setRequestOriginParser(new PigxHeaderRequestOriginParser()); } + } diff --git a/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/handle/PigxUrlBlockHandler.java b/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/handle/PigxUrlBlockHandler.java new file mode 100644 index 000000000..0cdd8115c --- /dev/null +++ b/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/handle/PigxUrlBlockHandler.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.common.sentinel.handle; + +import cn.hutool.http.ContentType; +import cn.hutool.json.JSONUtil; +import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler; +import com.alibaba.csp.sentinel.slots.block.BlockException; +import com.pig4cloud.pigx.common.core.util.R; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * @author lengleng + * @date 2019-10-11 + *

+ * 降级 限流策略 + */ +@Slf4j +public class PigxUrlBlockHandler implements UrlBlockHandler { + @Override + public void blocked(HttpServletRequest request, HttpServletResponse response, BlockException e) throws IOException { + + log.error("sentinel 降级 资源名称{}", e.getRule().getResource(), e); + + response.setContentType(ContentType.JSON.toString()); + response.setStatus(HttpStatus.TOO_MANY_REQUESTS.value()); + response.getWriter().print( + JSONUtil.toJsonStr(R.failed(e.getMessage()))); + } +} diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/util/package-info.java b/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/parser/PigxHeaderRequestOriginParser.java similarity index 56% rename from pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/util/package-info.java rename to pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/parser/PigxHeaderRequestOriginParser.java index 62490d11e..efcc91c60 100644 --- a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/util/package-info.java +++ b/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/parser/PigxHeaderRequestOriginParser.java @@ -15,16 +15,32 @@ * Author: lengleng (wangiegie@gmail.com) */ -/** - * Hystrix 默认 fallback - *

- * DefaultProperties: https://github.com/Netflix/Hystrix/issues/1446 - * - * @author L.cm - */ -@NonNullApi -@NonNullFields -package com.pig4cloud.pigx.common.security.util; +package com.pig4cloud.pigx.common.sentinel.parser; -import org.springframework.lang.NonNullApi; -import org.springframework.lang.NonNullFields; +import com.alibaba.csp.sentinel.adapter.servlet.callback.RequestOriginParser; + +import javax.servlet.http.HttpServletRequest; + +/** + * @author lengleng + * @date 2019-10-11 + *

+ * sentinel 请求头解析判断 + */ +public class PigxHeaderRequestOriginParser implements RequestOriginParser { + /** + * 请求头获取allow + */ + private static final String ALLOW = "Allow"; + + /** + * Parse the origin from given HTTP request. + * + * @param request HTTP request + * @return parsed origin + */ + @Override + public String parseOrigin(HttpServletRequest request) { + return request.getHeader(ALLOW); + } +} diff --git a/pigx-common/pigx-common-sentinel/src/main/resources/META-INF/spring.factories b/pigx-common/pigx-common-sentinel/src/main/resources/META-INF/spring.factories new file mode 100755 index 000000000..c396f293f --- /dev/null +++ b/pigx-common/pigx-common-sentinel/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + com.pig4cloud.pigx.common.sentinel.SentinelAutoConfigure diff --git a/pigx-common/pom.xml b/pigx-common/pom.xml index c2a49742d..fbbb12ef3 100644 --- a/pigx-common/pom.xml +++ b/pigx-common/pom.xml @@ -42,6 +42,7 @@ pigx-common-log pigx-common-minio pigx-common-security + pigx-common-sentinel pigx-common-sequence pigx-common-swagger pigx-common-transaction diff --git a/pigx-gateway/pom.xml b/pigx-gateway/pom.xml index f6a4a9d89..0d7a119b2 100644 --- a/pigx-gateway/pom.xml +++ b/pigx-gateway/pom.xml @@ -73,6 +73,11 @@ org.springframework.boot spring-boot-starter-data-redis + + + com.pig4cloud + pigx-common-sentinel + io.springfox diff --git a/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/config/RouterFunctionConfiguration.java b/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/config/RouterFunctionConfiguration.java index ca3e51f1f..2b46c6c55 100644 --- a/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/config/RouterFunctionConfiguration.java +++ b/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/config/RouterFunctionConfiguration.java @@ -17,7 +17,10 @@ package com.pig4cloud.pigx.gateway.config; -import com.pig4cloud.pigx.gateway.handler.*; +import com.pig4cloud.pigx.gateway.handler.ImageCodeHandler; +import com.pig4cloud.pigx.gateway.handler.SwaggerResourceHandler; +import com.pig4cloud.pigx.gateway.handler.SwaggerSecurityHandler; +import com.pig4cloud.pigx.gateway.handler.SwaggerUiHandler; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Bean; @@ -36,7 +39,6 @@ import org.springframework.web.reactive.function.server.RouterFunctions; @Configuration @AllArgsConstructor public class RouterFunctionConfiguration { - private final HystrixFallbackHandler hystrixFallbackHandler; private final SwaggerResourceHandler swaggerResourceHandler; private final SwaggerSecurityHandler swaggerSecurityHandler; private final SwaggerUiHandler swaggerUiHandler; @@ -45,16 +47,14 @@ public class RouterFunctionConfiguration { @Bean public RouterFunction routerFunction() { return RouterFunctions.route( - RequestPredicates.path("/fallback") - .and(RequestPredicates.accept(MediaType.APPLICATION_JSON_UTF8)), hystrixFallbackHandler) - .andRoute(RequestPredicates.GET("/code") - .and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), imageCodeHandler) - .andRoute(RequestPredicates.GET("/swagger-resources") - .and(RequestPredicates.accept(MediaType.ALL)), swaggerResourceHandler) - .andRoute(RequestPredicates.GET("/swagger-resources/configuration/ui") - .and(RequestPredicates.accept(MediaType.ALL)), swaggerUiHandler) - .andRoute(RequestPredicates.GET("/swagger-resources/configuration/security") - .and(RequestPredicates.accept(MediaType.ALL)), swaggerSecurityHandler); + RequestPredicates.path("/code") + .and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), imageCodeHandler) + .andRoute(RequestPredicates.GET("/swagger-resources") + .and(RequestPredicates.accept(MediaType.ALL)), swaggerResourceHandler) + .andRoute(RequestPredicates.GET("/swagger-resources/configuration/ui") + .and(RequestPredicates.accept(MediaType.ALL)), swaggerUiHandler) + .andRoute(RequestPredicates.GET("/swagger-resources/configuration/security") + .and(RequestPredicates.accept(MediaType.ALL)), swaggerSecurityHandler); } diff --git a/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/handler/HystrixFallbackHandler.java b/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/handler/HystrixFallbackHandler.java deleted file mode 100644 index d50872180..000000000 --- a/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/handler/HystrixFallbackHandler.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (c) 2018-2025, lengleng All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * Neither the name of the pig4cloud.com developer nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * Author: lengleng (wangiegie@gmail.com) - */ - -package com.pig4cloud.pigx.gateway.handler; - -import com.pig4cloud.pigx.common.core.util.R; -import lombok.extern.slf4j.Slf4j; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; -import org.springframework.stereotype.Component; -import org.springframework.web.reactive.function.BodyInserters; -import org.springframework.web.reactive.function.server.HandlerFunction; -import org.springframework.web.reactive.function.server.ServerRequest; -import org.springframework.web.reactive.function.server.ServerResponse; -import reactor.core.publisher.Mono; - -import java.util.Optional; - -import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR; - -/** - * @author lengleng - * @date 2018/7/5 - * Hystrix 降级处理 - */ -@Slf4j -@Component -public class HystrixFallbackHandler implements HandlerFunction { - @Override - public Mono handle(ServerRequest serverRequest) { - Optional originalUris = serverRequest.attribute(GATEWAY_ORIGINAL_REQUEST_URL_ATTR); - - originalUris.ifPresent(originalUri -> log.error("网关执行请求:{}失败,hystrix服务降级处理", originalUri)); - - return ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR.value()) - .contentType(MediaType.APPLICATION_JSON_UTF8) - .body(BodyInserters.fromObject(R.failed("服务异常"))); - } -} diff --git a/pigx-register/pom.xml b/pigx-register/pom.xml index 14bed8e7c..1a51727b8 100755 --- a/pigx-register/pom.xml +++ b/pigx-register/pom.xml @@ -59,6 +59,10 @@ 0.10.5 runtime + + org.springframework.cloud + spring-cloud-starter + diff --git a/pigx-upms/pigx-upms-api/pom.xml b/pigx-upms/pigx-upms-api/pom.xml index 3f8d07199..9686b35e9 100644 --- a/pigx-upms/pigx-upms-api/pom.xml +++ b/pigx-upms/pigx-upms-api/pom.xml @@ -44,5 +44,10 @@ com.baomidou mybatis-plus-extension + + + com.pig4cloud + pigx-common-sentinel + diff --git a/pigx-upms/pigx-upms-biz/pom.xml b/pigx-upms/pigx-upms-biz/pom.xml index 46a603f43..a65915308 100644 --- a/pigx-upms/pigx-upms-biz/pom.xml +++ b/pigx-upms/pigx-upms-biz/pom.xml @@ -77,6 +77,11 @@ com.pig4cloud pigx-common-gateway + + + com.pig4cloud + pigx-common-sentinel + com.baomidou diff --git a/pom.xml b/pom.xml index e1ec77a90..412e34458 100644 --- a/pom.xml +++ b/pom.xml @@ -94,11 +94,6 @@ spring-boot-admin-starter-client ${spring-boot-admin.version} - - - org.springframework.cloud - spring-cloud-starter-netflix-hystrix - org.projectlombok