You can define the CircuitBreaker annotation from MicroProfile Fault Tolerance
in your generated classes by setting the desired configuration in application.properties
.
Let’s say you have the following OpenAPI definition:
{
"openapi": "3.0.3",
"info": {
"title": "Simple API",
"version": "1.0.0-SNAPSHOT"
},
"paths": {
"/hello": {
"get": {
"responses": {
"200": {
"description": "OK",
"content": {
"text/plain": {
"schema": {
"type": "string"
}
}
}
}
}
}
},
"/bye": {
"get": {
"responses": {
"200": {
"description": "OK",
"content": {
"text/plain": {
"schema": {
"type": "string"
}
}
}
}
}
}
}
}
}
And you want to configure Circuit Breaker for the /bye
endpoint, you can do it in the following way:
Add the SmallRye Fault Tolerance extension to your project’s pom.xml
file:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-fault-tolerance</artifactId>
</dependency>
Assuming your Open API spec file is in src/main/openapi/simple-openapi.json
, add the following configuration to your application.properties
file:
# Note that the file name must have only alphabetic characters or underscores (_).
quarkus.openapi-generator.codegen.spec.simple_openapi_json.base-package=org.acme.openapi.simple
# Enables the CircuitBreaker extension for the byeGet method from the DefaultApi class
org.acme.openapi.simple.api.DefaultApi/byeGet/CircuitBreaker/enabled=true
With the above configuration, your Rest Clients will be created with a code similar to the following:
package org.acme.openapi.simple.api;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.MediaType;
@Path("")
@RegisterRestClient(configKey="simple-openapi_json")
public interface DefaultApi {
@GET
@Path("/bye")
@Produces({ "text/plain" })
@org.eclipse.microprofile.faulttolerance.CircuitBreaker
public String byeGet();
@GET
@Path("/hello")
@Produces({ "text/plain" })
public String helloGet();
}
You can also override the default Circuit Breaker configuration by setting the properties
in application.properties
just as you would for a traditional MicroProfile application:
org.acme.openapi.simple.api.DefaultApi/byeGet/CircuitBreaker/failOn=java.lang.IllegalArgumentException,java.lang.NullPointerException
org.acme.openapi.simple.api.DefaultApi/byeGet/CircuitBreaker/skipOn=java.lang.NumberFormatException
org.acme.openapi.simple.api.DefaultApi/byeGet/CircuitBreaker/delay=33
org.acme.openapi.simple.api.DefaultApi/byeGet/CircuitBreaker/delayUnit=MILLIS
org.acme.openapi.simple.api.DefaultApi/byeGet/CircuitBreaker/requestVolumeThreshold=42
org.acme.openapi.simple.api.DefaultApi/byeGet/CircuitBreaker/failureRatio=3.14
org.acme.openapi.simple.api.DefaultApi/byeGet/CircuitBreaker/successThreshold=22
See the module circuit-breaker for an example of how to use this feature.