resilience4j熔断器针对某些类型的异常跳过回退方法

huangapple go评论65阅读模式
英文:

resilience4j bulkhead skipping fallback method for certain type of exceptions

问题

我使用resilience4j bulkhead来限制我的服务方法中活动线程的数量。当线程限制超过配置时,它应该进入一个预期的回退方法。

但是,在我的方法中,当验证失败时,作为业务逻辑的一部分,我会抛出一个带有自定义消息的BadRequestException给我的消费者。在这种情况下,当发生checked异常时,控制权也会传递到回退方法,这实际上不应该发生。

那么,我们是否有配置可以在某些类型的异常发生时跳过控制转到回退方法,类似于Hystrix中的配置?

@Bulkhead(name="bhName", fallbackMethod="fallbackMethod")
public void doSomething(){

    // 验证
    // 如果验证成功
         // 执行一些业务逻辑
    // 否则,如果验证失败
        throw BadRequestException("错误消息");
}

public void fallbackMethod(Exception ex){
   log.info("并发限制超过了最大值");
   return null;
}
英文:

I use reslience4j bulkhead to limit number of active threads to one of my service method. When the thread limit exceeds the configuration it should go to a fallback method which is happening as expected.

But as part of business logic in my method when the validation fails I throw BadRequestException with a custom message to my consumer. Here the control goes to a fallback method when the checkedException occurs too Ideally it should not be the case.

So do we have any configuration to skip the control goes to fallback method on certain type of Exceptions similar to how do we have in Hystrix?

@Bulkhead(name="bhName" fallbackMethod="fallbackMethod")
public void doSomething(){

    //validatiion
    // if validation succeeds
         //do some business logic
    // else if validation fails
        throw BadRequestException("Error Message")
}

public void fallbackMethod(Exception ex){
   log.info("The number of concurrency limit exceeded");
   return null;
} 

答案1

得分: 1

只需使用不同的fallbackMethod签名:

public void fallbackMethod(BulkheadFullException ex){
   log.info("并发限制超过数量");
   return null;
} 
英文:

Just use a different fallbackMethod signature:

public void fallbackMethod(BulkheadFullException ex){
   log.info("The number of concurrency limit exceeded");
   return null;
} 

答案2

得分: 0

I think there is an issue with the latest Spring Boot and Spring Cloud version. I tested the old ones and the Bulkhead pattern worked for me.

Please, check my Maven POM.

Add the following Spring Boot version

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.3.RELEASE</version>
    <relativePath/>
</parent>

Add the following Spring Cloud and Resilience4j library version

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
    <resilience4j.version>1.5.0</resilience4j.version>
</properties>
<dependency>
    <groupId>io.github.resilience4j</groupId>
    <artifactId>resilience4j-spring-boot2</artifactId>
    <version>${resilience4j.version}</version>
</dependency>

Add the following dependencies

<dependency>
    <groupId>io.github.resilience4j</groupId>
    <artifactId>resilience4j-circuitbreaker</artifactId>
    <version>${resilience4j.version}</version>
</dependency>
<dependency>
    <groupId>io.github.resilience4j</groupId>
    <artifactId>resilience4j-timelimiter</artifactId>
    <version>${resilience4j.version}</version>
</dependency>
<dependency>
    <groupId>io.github.resilience4j</groupId>
    <artifactId>resilience4j-bulkhead</artifactId>
    <version>${resilience4j.version}</version>
</dependency>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
英文:

I think there is an issue with the latest Spring Boot and Spring Cloud version. I tested the old ones and the Bulkhead pattern worked for me.

Please, check my Maven POM.

Add the following Spring Boot version

&lt;parent&gt;
	&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
	&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
	&lt;version&gt;2.2.3.RELEASE&lt;/version&gt;
	&lt;relativePath/&gt;
&lt;/parent&gt;

Add the following Spring Cloud and Resilience4j library version

&lt;properties&gt;
	&lt;java.version&gt;1.8&lt;/java.version&gt;
	&lt;spring-cloud.version&gt;Hoxton.SR1&lt;/spring-cloud.version&gt;
	&lt;resilience4j.version&gt;1.5.0&lt;/resilience4j.version&gt;
&lt;/properties&gt;

   &lt;dependency&gt;
		&lt;groupId&gt;io.github.resilience4j&lt;/groupId&gt;
		&lt;artifactId&gt;resilience4j-spring-boot2&lt;/artifactId&gt;
		&lt;version&gt;${resilience4j.version}&lt;/version&gt;
	&lt;/dependency&gt;

Add the following dependencies

	&lt;dependency&gt;
		&lt;groupId&gt;io.github.resilience4j&lt;/groupId&gt;
		&lt;artifactId&gt;resilience4j-circuitbreaker&lt;/artifactId&gt;
		&lt;version&gt;${resilience4j.version}&lt;/version&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;io.github.resilience4j&lt;/groupId&gt;
		&lt;artifactId&gt;resilience4j-timelimiter&lt;/artifactId&gt;
		&lt;version&gt;${resilience4j.version}&lt;/version&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;io.github.resilience4j&lt;/groupId&gt;
		&lt;artifactId&gt;resilience4j-bulkhead&lt;/artifactId&gt;
		&lt;version&gt;${resilience4j.version}&lt;/version&gt;
	&lt;/dependency&gt;

&lt;dependencyManagement&gt;
	&lt;dependencies&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
			&lt;artifactId&gt;spring-cloud-dependencies&lt;/artifactId&gt;
			&lt;version&gt;${spring-cloud.version}&lt;/version&gt;
			&lt;type&gt;pom&lt;/type&gt;
			&lt;scope&gt;import&lt;/scope&gt;
		&lt;/dependency&gt;
	&lt;/dependencies&gt;
&lt;/dependencyManagement&gt;

huangapple
  • 本文由 发表于 2020年10月9日 21:09:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/64280768.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定