Skip to content

Commit

Permalink
perf($OpenFeign): add support for Spring Cloud CircuitBreaker Resilie…
Browse files Browse the repository at this point in the history
…nce4J
  • Loading branch information
johnnymillergh committed Feb 3, 2022
1 parent ee9065e commit 9568372
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ spring:
useProperties: false

feign:
circuitbreaker:
enabled: true
httpclient:
enabled: false
okhttp:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href='https://docs.spring.io/spring-cloud-openfeign/docs/current/reference/html/'>Spring Cloud OpenFeign</a>
**/
@FeignClient(value = OssCenterRemoteApi.SERVICE_NAME)
@Validated
@FeignClient(value = OssCenterRemoteApi.SERVICE_NAME, fallback = OssCenterRemoteApi.OssCenterRemoteApiFallback.class)
public interface OssCenterRemoteApi {
String SERVICE_NAME = "oss-center";

Expand All @@ -25,5 +31,15 @@ public interface OssCenterRemoteApi {
* @return the response body bean
*/
@PostMapping(value = "/upload/single", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
ResponseBodyBean<ObjectResponse> uploadSingleResource(@RequestPart("file") MultipartFile multipartFile);
ResponseBodyBean<ObjectResponse> uploadSingleResource(@NotNull @RequestPart("file") MultipartFile multipartFile);

@Slf4j
@Component
class OssCenterRemoteApiFallback implements OssCenterRemoteApi {
@Override
public ResponseBodyBean<ObjectResponse> uploadSingleResource(@NotNull MultipartFile multipartFile) {
log.error("Fallback -> OssCenterRemoteApi#uploadSingleResource()");
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href='https://docs.spring.io/spring-cloud-openfeign/docs/current/reference/html/'>Spring Cloud OpenFeign</a>
**/
@FeignClient(value = OssCenterRemoteApi.SERVICE_NAME)
@Validated
@FeignClient(value = OssCenterRemoteApi.SERVICE_NAME, fallback = OssCenterRemoteApi.OssCenterRemoteApiFallback.class)
public interface OssCenterRemoteApi {
String SERVICE_NAME = "oss-center";

Expand All @@ -25,5 +31,15 @@ public interface OssCenterRemoteApi {
* @return the response body bean
*/
@PostMapping(value = "/upload/single", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
ResponseBodyBean<ObjectResponse> uploadSingleResource(@RequestPart("file") MultipartFile multipartFile);
ResponseBodyBean<ObjectResponse> uploadSingleResource(@NotNull @RequestPart("file") MultipartFile multipartFile);

@Slf4j
@Component
class OssCenterRemoteApiFallback implements OssCenterRemoteApi {
@Override
public ResponseBodyBean<ObjectResponse> uploadSingleResource(@NotNull MultipartFile multipartFile) {
log.error("Fallback -> OssCenterRemoteApi#uploadSingleResource()");
return null;
}
}
}
3 changes: 2 additions & 1 deletion maf-mis/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ spring:
useProperties: false

feign:
circuitbreaker:
enabled: true
httpclient:
enabled: false
okhttp:
Expand All @@ -127,7 +129,6 @@ feign:
enabled: true
response:
enabled: true
useGzipDecoder: true

management:
endpoints:
Expand Down
3 changes: 2 additions & 1 deletion oss-center/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ spring:
min-idle: 0

feign:
circuitbreaker:
enabled: true
httpclient:
enabled: false
okhttp:
Expand All @@ -66,7 +68,6 @@ feign:
enabled: true
response:
enabled: true
useGzipDecoder: true

management:
endpoints:
Expand Down
4 changes: 4 additions & 0 deletions spring-cloud-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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: '{}'";
Expand Down
Original file line number Diff line number Diff line change
@@ -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
* <a href='https://docs.spring.io/spring-cloud-openfeign/docs/current/reference/html/#spring-cloud-feign-circuitbreaker'>Feign Spring Cloud CircuitBreaker Support</a>
**/
@Slf4j
@SuppressWarnings("AlibabaClassNamingShouldBeCamel")
public class OpenFeignConfiguration {
@Bean
public CircuitBreakerNameResolver circuitBreakerNameResolver() {
return (String feignClientName, Target<?> target, Method method) -> feignClientName + "_" + method.getName();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 9568372

Please sign in to comment.