根据入门指南(https://Resilience4j.readme.io/docs/get-started-3)和演示项目(https://github.com/Resilience4j/Resilience4j-spring-boot2-demo),我想自己测试它,创建一个更简单的测试应用程序。
代码如下:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ServiceConfiguration {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
@RestController
public class ServiceController {
@Autowired
private AlbumService albumService;
@GetMapping("/albums")
ResponseEntity<String> getAlbums() {
return albumService.getAlbums();
}
}
@Slf4j
@Service
public class AlbumService {
@Autowired
private RestTemplate restTemplate;
@CircuitBreaker(name = "albumService", fallbackMethod = "getDefaultAlbumList")
public ResponseEntity<String> getAlbums() {
String url = MockNeat.secure().probabilites(String.class)
.add(0.7, "https://wrong-url.com")
.add(0.3, "https://jsonplaceholder.typicode.com/albums").val();
return new ResponseEntity<>(restTemplate.getForObject(url, String.class), HttpStatus.OK);
}
private ResponseEntity<String> getDefaultAlbumList(Exception ex) throws URISyntaxException, IOException {
log.info("Recovered: " + ex.getMessage());
return new ResponseEntity<>(new String(Files.readAllBytes(Paths.get(getClass().getClassLoader().getResource("fallback-album-list.json").toURI()))), HttpStatus.OK);
}
}
resilience4j:
circuitbreaker:
configs:
default:
registerHealthIndicator: true
slidingWindowSize: 10
minimumNumberOfCalls: 5
permittedNumberOfCallsInHalfOpenState: 3
automaticTransitionFromOpenToHalfOpenEnabled: true
waitDurationInOpenState: 5s
failureRateThreshold: 50
eventConsumerBufferSize: 10
instances:
albumService:
baseConfig: default
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
metrics:
distribution:
percentiles-histogram:
http:
server:
request: true
resielence4j:
circuitbreaker:
calls: true
发现问题了!我忘了把spring-aop作为依赖项!下面是我的pom.xml,以防有人遇到同样的问题:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>r4j</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>r4j</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<repositories>
<repository>
<id>jcenter</id>
<url>https://jcenter.bintray.com/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>net.andreinc.mockneat</groupId>
<artifactId>mockneat</artifactId>
<version>0.3.9</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot2</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
</dependencies>
</project>
断路器将处于闭合或半断开状态无限时间,直到达到最小的呼叫次数,对吗?有什么办法我可以设置什么时候没有调用在数量的时间,它将转为关闭状态?另外,在半开状态下,是否有可能使最小呼叫数大于允许的呼叫数?谢谢。
我想用Resilience4j来处理容错,我用的是断路器和定时器限制。 我想分离业务逻辑的容错行为,不要“弄脏”我的业务代码。 2-我如何有这个应用程序的许多实例,断路器为每个实例单独工作?我是对的?
我可以看到,我可以使用以下代码以编程方式将状态设置为强制打开:CircuitBreaker强制打开状态 但是有没有一种方法可以设置一个属性,在应用程序启动时立即将状态设置为此,以便可以与测试一起使用呢?
我有一个关于带Resilience4J(不仅仅是Resilience4J)的Spring Cloud断路器的快速问题。 这两个项目都非常棒。然而,目前,我们经常以撤退告终。这意味着,当第三方服务真的很好时,我们最终还是会选择后退。 这可能是我自己的问题,因此,我想问一个特定配置的问题。 我想告诉当前配置做以下操作:断路器。(我将使用好的,坏的,和一半好/一半坏的状态。 > 当状态是一半好/一半坏:
我正面临使用Spring Cloud Resilience 4j的断路器实现的问题。 在一些教程之后,我尝试在项目中添加必要的依赖项。此外,尝试添加配置,但电路仍然没有打开,并且没有调用回退方法。 对于用例,我正在从我的服务调用外部 API,如果该外部 API 关闭,那么在几次调用后,我需要启用断路器。 请从不同的文件中找到代码片段。 我是断路器模式的新手。我们将非常感谢您的帮助。 pom.xml
我想将我的断路器配置从application.yml文件移到某个配置java文件作为bean声明信标,这使得application.yml文件变得很大,我是否可以从applciation.yml中删除配置并使用配置注释来定义断路器配置。我有配置java文件,代码如下: 在我的服务文件中,我正在注释断路器的方法,如 在目前的实现中,我的断路器无法达到开路状态。请给我一些建议