From 9568372a6381f8c18fd796622cb4996e81d512a0 Mon Sep 17 00:00:00 2001 From: "Johnny Miller (ZA)" Date: Thu, 3 Feb 2022 17:51:20 +0800 Subject: [PATCH] perf($OpenFeign): add support for Spring Cloud CircuitBreaker Resilience4J --- .../src/main/resources/application.yml | 2 ++ .../authcenter/remote/OssCenterRemoteApi.java | 20 +++++++++-- .../osscenter/OssCenterRemoteApi.java | 20 +++++++++-- maf-mis/src/main/resources/application.yml | 3 +- oss-center/src/main/resources/application.yml | 3 +- spring-cloud-starter/pom.xml | 4 +++ .../MafAutoConfiguration.java | 4 ++- .../configuration/OpenFeignConfiguration.java | 33 +++++++++++++++++++ .../FeignClientConfigurationProperties.java | 2 +- 9 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/configuration/OpenFeignConfiguration.java rename spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/{configuration => property}/FeignClientConfigurationProperties.java (93%) diff --git a/auth-center/auth-center-bootstrap/src/main/resources/application.yml b/auth-center/auth-center-bootstrap/src/main/resources/application.yml index ec0b000c..c87c311d 100644 --- a/auth-center/auth-center-bootstrap/src/main/resources/application.yml +++ b/auth-center/auth-center-bootstrap/src/main/resources/application.yml @@ -128,6 +128,8 @@ spring: useProperties: false feign: + circuitbreaker: + enabled: true httpclient: enabled: false okhttp: diff --git a/auth-center/auth-center-infra/src/main/java/com/jmsoftware/maf/authcenter/remote/OssCenterRemoteApi.java b/auth-center/auth-center-infra/src/main/java/com/jmsoftware/maf/authcenter/remote/OssCenterRemoteApi.java index 5ae76ab1..a7460390 100644 --- a/auth-center/auth-center-infra/src/main/java/com/jmsoftware/maf/authcenter/remote/OssCenterRemoteApi.java +++ b/auth-center/auth-center-infra/src/main/java/com/jmsoftware/maf/authcenter/remote/OssCenterRemoteApi.java @@ -2,19 +2,25 @@ import com.jmsoftware.maf.common.bean.ResponseBodyBean; import com.jmsoftware.maf.common.domain.osscenter.write.ObjectResponse; +import lombok.extern.slf4j.Slf4j; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.http.MediaType; +import org.springframework.stereotype.Component; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.multipart.MultipartFile; +import javax.validation.constraints.NotNull; + /** * Description: OssCenterRemoteApi, change description here. * * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 9/15/2021 11:10 AM * @see Spring Cloud OpenFeign **/ -@FeignClient(value = OssCenterRemoteApi.SERVICE_NAME) +@Validated +@FeignClient(value = OssCenterRemoteApi.SERVICE_NAME, fallback = OssCenterRemoteApi.OssCenterRemoteApiFallback.class) public interface OssCenterRemoteApi { String SERVICE_NAME = "oss-center"; @@ -25,5 +31,15 @@ public interface OssCenterRemoteApi { * @return the response body bean */ @PostMapping(value = "/upload/single", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) - ResponseBodyBean uploadSingleResource(@RequestPart("file") MultipartFile multipartFile); + ResponseBodyBean uploadSingleResource(@NotNull @RequestPart("file") MultipartFile multipartFile); + + @Slf4j + @Component + class OssCenterRemoteApiFallback implements OssCenterRemoteApi { + @Override + public ResponseBodyBean uploadSingleResource(@NotNull MultipartFile multipartFile) { + log.error("Fallback -> OssCenterRemoteApi#uploadSingleResource()"); + return null; + } + } } diff --git a/maf-mis/src/main/java/com/jmsoftware/maf/mafmis/remoteapi/osscenter/OssCenterRemoteApi.java b/maf-mis/src/main/java/com/jmsoftware/maf/mafmis/remoteapi/osscenter/OssCenterRemoteApi.java index 6366ff37..aa508416 100644 --- a/maf-mis/src/main/java/com/jmsoftware/maf/mafmis/remoteapi/osscenter/OssCenterRemoteApi.java +++ b/maf-mis/src/main/java/com/jmsoftware/maf/mafmis/remoteapi/osscenter/OssCenterRemoteApi.java @@ -2,19 +2,25 @@ import com.jmsoftware.maf.common.bean.ResponseBodyBean; import com.jmsoftware.maf.common.domain.osscenter.write.ObjectResponse; +import lombok.extern.slf4j.Slf4j; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.http.MediaType; +import org.springframework.stereotype.Component; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.multipart.MultipartFile; +import javax.validation.constraints.NotNull; + /** * Description: OssCenterRemoteApi, change description here. * * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 9/15/2021 11:10 AM * @see Spring Cloud OpenFeign **/ -@FeignClient(value = OssCenterRemoteApi.SERVICE_NAME) +@Validated +@FeignClient(value = OssCenterRemoteApi.SERVICE_NAME, fallback = OssCenterRemoteApi.OssCenterRemoteApiFallback.class) public interface OssCenterRemoteApi { String SERVICE_NAME = "oss-center"; @@ -25,5 +31,15 @@ public interface OssCenterRemoteApi { * @return the response body bean */ @PostMapping(value = "/upload/single", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) - ResponseBodyBean uploadSingleResource(@RequestPart("file") MultipartFile multipartFile); + ResponseBodyBean uploadSingleResource(@NotNull @RequestPart("file") MultipartFile multipartFile); + + @Slf4j + @Component + class OssCenterRemoteApiFallback implements OssCenterRemoteApi { + @Override + public ResponseBodyBean uploadSingleResource(@NotNull MultipartFile multipartFile) { + log.error("Fallback -> OssCenterRemoteApi#uploadSingleResource()"); + return null; + } + } } diff --git a/maf-mis/src/main/resources/application.yml b/maf-mis/src/main/resources/application.yml index 0f76bb47..6f29f125 100644 --- a/maf-mis/src/main/resources/application.yml +++ b/maf-mis/src/main/resources/application.yml @@ -112,6 +112,8 @@ spring: useProperties: false feign: + circuitbreaker: + enabled: true httpclient: enabled: false okhttp: @@ -127,7 +129,6 @@ feign: enabled: true response: enabled: true - useGzipDecoder: true management: endpoints: diff --git a/oss-center/src/main/resources/application.yml b/oss-center/src/main/resources/application.yml index 3d60cd0f..f194e85e 100644 --- a/oss-center/src/main/resources/application.yml +++ b/oss-center/src/main/resources/application.yml @@ -51,6 +51,8 @@ spring: min-idle: 0 feign: + circuitbreaker: + enabled: true httpclient: enabled: false okhttp: @@ -66,7 +68,6 @@ feign: enabled: true response: enabled: true - useGzipDecoder: true management: endpoints: diff --git a/spring-cloud-starter/pom.xml b/spring-cloud-starter/pom.xml index 794241c9..facffbd8 100644 --- a/spring-cloud-starter/pom.xml +++ b/spring-cloud-starter/pom.xml @@ -98,6 +98,10 @@ org.springframework.cloud spring-cloud-starter-openfeign + + org.springframework.cloud + spring-cloud-starter-circuitbreaker-resilience4j + io.github.openfeign feign-okhttp diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/MafAutoConfiguration.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/MafAutoConfiguration.java index afde8a54..035af8a4 100644 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/MafAutoConfiguration.java +++ b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/MafAutoConfiguration.java @@ -17,6 +17,7 @@ import com.jmsoftware.maf.springcloudstarter.helper.SpringBootStartupHelper; import com.jmsoftware.maf.springcloudstarter.minio.MinioConfiguration; import com.jmsoftware.maf.springcloudstarter.poi.ExcelImportConfigurationProperties; +import com.jmsoftware.maf.springcloudstarter.property.FeignClientConfigurationProperties; import com.jmsoftware.maf.springcloudstarter.property.JwtConfigurationProperties; import com.jmsoftware.maf.springcloudstarter.property.MafConfigurationProperties; import com.jmsoftware.maf.springcloudstarter.property.MafProjectProperties; @@ -75,7 +76,8 @@ TypeConversionConfiguration.class, QuartzConfiguration.class, WebSocketConfiguration.class, - OpenApiConfiguration.class + OpenApiConfiguration.class, + OpenFeignConfiguration.class }) public class MafAutoConfiguration { private static final String INITIAL_MESSAGE = "Initial bean: '{}'"; diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/configuration/OpenFeignConfiguration.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/configuration/OpenFeignConfiguration.java new file mode 100644 index 00000000..2b1cd96c --- /dev/null +++ b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/configuration/OpenFeignConfiguration.java @@ -0,0 +1,33 @@ +/* + * Copyright By ZATI + * Copyright By 3a3c88295d37870dfd3b25056092d1a9209824b256c341f2cdc296437f671617 + * All rights reserved. + * + * If you are not the intended user, you are hereby notified that any use, disclosure, copying, printing, forwarding or + * dissemination of this property is strictly prohibited. If you have got this file in error, delete it from your + * system. + */ +package com.jmsoftware.maf.springcloudstarter.configuration; + +import feign.Target; +import lombok.extern.slf4j.Slf4j; +import org.springframework.cloud.openfeign.CircuitBreakerNameResolver; +import org.springframework.context.annotation.Bean; + +import java.lang.reflect.Method; + +/** + * Description: OpenFeignConfiguration, change description here. + * + * @author 钟俊 (za-zhongjun), email: jun.zhong@zatech.com, date: 2/3/2022 3:52 PM + * @see + * Feign Spring Cloud CircuitBreaker Support + **/ +@Slf4j +@SuppressWarnings("AlibabaClassNamingShouldBeCamel") +public class OpenFeignConfiguration { + @Bean + public CircuitBreakerNameResolver circuitBreakerNameResolver() { + return (String feignClientName, Target target, Method method) -> feignClientName + "_" + method.getName(); + } +} diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/configuration/FeignClientConfigurationProperties.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/property/FeignClientConfigurationProperties.java similarity index 93% rename from spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/configuration/FeignClientConfigurationProperties.java rename to spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/property/FeignClientConfigurationProperties.java index 17cb5ed6..4c84ea08 100644 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/configuration/FeignClientConfigurationProperties.java +++ b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/property/FeignClientConfigurationProperties.java @@ -1,4 +1,4 @@ -package com.jmsoftware.maf.springcloudstarter.configuration; +package com.jmsoftware.maf.springcloudstarter.property; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties;