Исключение не перехватывается CircuitBreakerJAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Исключение не перехватывается CircuitBreaker

Сообщение Anonymous »

У меня два сервиса, и при отправке запроса от одного к другому он проходит через FeignClient

Код: Выделить всё

package org.example.projectservice.client;

import org.example.projectservice.config.FeignClientConfiguration;
import org.example.projectservice.dto.response.CompanyResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(value = "${service.company.name}",
path = "${service.company.path}",
configuration = FeignClientConfiguration.class)
public interface CompanyFeignClient {

@GetMapping("/{companyId}")
CompanyResponse getCompanyById(@PathVariable String companyId);
}
Есть еще такой декодер

Код: Выделить всё

package org.example.projectservice.config;

import feign.FeignException;
import feign.Response;
import feign.RetryableException;
import feign.codec.ErrorDecoder;
import org.example.projectservice.exception.FeignClientException;
import org.example.projectservice.exception.NotFoundException;

public class MyDecoder implements ErrorDecoder {
@Override
public Exception decode(String s, Response response) {
FeignException exception = FeignException.errorStatus(s, response);
int status = response.status();
if (status == 503) {
return new RetryableException(
status,
"Service unavailable",
response.request().httpMethod(),
(Long) null,
response.request()
);
}
String[] responseMessageSplit = exception.getMessage().split("\"message\"");
String[] exMessageSplit = responseMessageSplit[responseMessageSplit.length - 1].split("\"");
System.out.println(exMessageSplit.toString());
String exMessage = exMessageSplit[exMessageSplit.length - 2];
if (status == 400) {
return new FeignClientException(exMessage);
}
if (status == 404) {
return new NotFoundException(exMessage);
}

return exception;
}
}
Существует также сервис, вызывающий этот метод

Код: Выделить всё

package org.example.projectservice.service.impl;

import feign.FeignException;
import feign.RetryableException;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import lombok.RequiredArgsConstructor;
import org.example.projectservice.client.CompanyFeignClient;
import org.example.projectservice.dto.response.CompanyResponse;
import org.example.projectservice.exception.ServiceUnAvailableException;
import org.example.projectservice.service.CompanyService;
import org.example.projectservice.utill.ExceptionMessages;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class CompanyServiceImpl implements CompanyService {
private final CompanyFeignClient companyFeignClient;

@Override
@CircuitBreaker(name = "company", fallbackMethod = "fallBackMethodCompanyService")
public CompanyResponse getCompanyById(String companyId) {
return companyFeignClient.getCompanyById(companyId);
}

public CompanyResponse fallBackMethodCompanyService(String companyId, RetryableException e) {
System.out.println("From RetryableException");
throw new ServiceUnAvailableException(String.format(ExceptionMessages.COMPANY_SERVICE_NOT_AVAILABLE_WITH_MESSAGE, e.getMessage()));
}

public CompanyResponse fallBackMethodCompanyService(String companyId, Exception e) {
System.out.println("From Exception");
throw new ServiceUnAvailableException(String.format(ExceptionMessages.COMPANY_SERVICE_NOT_AVAILABLE_WITH_MESSAGE, e.getMessage()));
}

}
Итак, вопрос в том, почему CircuitBreaker не перехватывает это исключение? При вызове летает

Код: Выделить всё

feign.RetryableException: Service unavailable
2024-09-27T20:07:23.654552662Z  at org.example.projectservice.config.MyDecoder.decode(MyDecoder.java:21) ~[classes/:0.0.1-SNAPSHOT]
2024-09-27T20:07:23.654555808Z  at feign.InvocationContext.decodeError(InvocationContext.java:126) ~[feign-core-13.3.jar:na]
2024-09-27T20:07:23.654558383Z
Вылетает исключение RetryableException, а не перехват. А иногда вылетает следующее

Код: Выделить всё

java.net.UnknownHostException: fc1fce880bb4
2024-09-27T20:25:15.791819608Z  at java.base/sun.nio.ch.NioSocketImpl.connect(Unknown Source) ~[na:na]
2024-09-27T20:25:15.791824077Z  at java.base/java.net.Socket.connect(Unknown Source) ~[na:na]
Я попробовал все варианты, может быть кто-то сталкивался с такой проблемой

Подробнее здесь: https://stackoverflow.com/questions/790 ... uitbreaker
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»