我正在使用 Resilience4J 的断路器,我需要忽略一些自定义异常,因此我需要更改默认配置。我正在使用微服务,因此我有一个连接到数据库的微服务,该数据库有一些基本请求,例如通过 id 获取,并且我还有一个使用这些请求的边缘服务。例如,我需要,如果 id 不存在,则微服务会引发自定义异常,并且在这种情况下断路器不会打开。
带有数据库的微服务:
- 获取请求
@GetMapping("/sales-rep/{id}")
@ResponseStatus(HttpStatus.OK)
public SalesRepDTO getSalesRep(@PathVariable Integer id) {
return salesRepService.getSalesRep(id);
}
服务
公共 SalesRepDTO getSalesRep(整数 id){
if(salesRepRepository.existsById(id)) { SalesRep salesRep = salesRepRepository.findById(id).get(); return new SalesRepDTO(salesRep.getId(), salesRep.getName()); } else { throw new SalesRepNotFoundException("Sales rep not found"); }
}
边缘服务:
- 服务
import com.ironhack.manageAllservice.client.AccountClient;
import com.ironhack.manageAllservice.client.LeadClient;
import com.ironhack.manageAllservice.client.SalesRepClient;
import com.ironhack.manageAllservice.controller.dtos.*;
import com.ironhack.manageAllservice.controller.dtos.report.OpportunityBySalesRepDTO;
import com.ironhack.manageAllservice.controller.dtos.report.ReportDTO;
import com.ironhack.manageAllservice.service.exceptions.SalesRepNotFoundException;
import com.ironhack.manageAllservice.controller.dtos.report.*;
import com.ironhack.manageAllservice.service.interfaces.IManageAllService;
import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig;
import io.github.resilience4j.timelimiter.TimeLimiterConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.circuitbreaker.resilience4j.Resilience4JCircuitBreakerFactory;
import org.springframework.cloud.circuitbreaker.resilience4j.Resilience4JConfigBuilder;
import org.springframework.cloud.client.circuitbreaker.CircuitBreaker;
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;
import org.springframework.cloud.client.circuitbreaker.Customizer;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@Service
public class ManageAllService implements IManageAllService {
@Autowired
private CircuitBreakerFactory circuitBreakerFactory;
@Bean
public Customizer<Resilience4JCircuitBreakerFactory> globalCustomConfiguration() {
CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom()
.failureRateThreshold(50)
.waitDurationInOpenState(Duration.ofMillis(1000))
.slidingWindowSize(2)
.ignoreExceptions(SalesRepNotFoundException.class)
.build();
TimeLimiterConfig timeLimiterConfig = TimeLimiterConfig.custom()
.timeoutDuration(Duration.ofSeconds(4))
.build();
return factory -> factory.configureDefault(id -> new Resilience4JConfigBuilder(id)
.circuitBreakerConfig(circuitBreakerConfig)
.timeLimiterConfig(timeLimiterConfig)
.build());
}
public SalesRepDTO getSalesRepById(Integer id) {
CircuitBreaker circuitBreaker = circuitBreakerFactory.create("salesRep-service");
SalesRepDTO salesRepDTO = circuitBreaker.run(()->salesRepClient.getSalesRep(id),
throwable -> postSalesRepFallBack());
return salesRepDTO;
}
SalesRepNotFoundException.class 是我想忽略的异常,但断路器并未更改配置。有什么建议吗?