use of com.alibaba.csp.sentinel.adapter.jaxrs.fallback.SentinelJaxRsFallback in project Sentinel by alibaba.
the class ClientFilterTest method testClientCustomFallback.
@Test
public void testClientCustomFallback() {
final String url = "/test/hello";
final String resourceName = "GET:" + url;
configureRulesFor(resourceName, 0);
SentinelJaxRsConfig.setJaxRsFallback(new SentinelJaxRsFallback() {
@Override
public javax.ws.rs.core.Response fallbackResponse(String route, Throwable cause) {
return javax.ws.rs.core.Response.status(javax.ws.rs.core.Response.Status.OK).entity("Blocked by Sentinel (flow limiting)").type(MediaType.APPLICATION_JSON_TYPE).build();
}
@Override
public Future<Response> fallbackFutureResponse(final String route, final Throwable cause) {
return new FutureTask<>(new Callable<Response>() {
@Override
public Response call() throws Exception {
return fallbackResponse(route, cause);
}
});
}
});
Response response = SentinelJaxRsClientTemplate.execute(resourceName, new Supplier<Response>() {
@Override
public Response get() {
return client.target(host).path(url).request().get();
}
});
assertEquals(javax.ws.rs.core.Response.Status.OK.getStatusCode(), response.getStatus());
assertEquals("Blocked by Sentinel (flow limiting)", response.readEntity(String.class));
ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
assertEquals(0, cn.passQps(), 0.01);
}
use of com.alibaba.csp.sentinel.adapter.jaxrs.fallback.SentinelJaxRsFallback in project Sentinel by alibaba.
the class SentinelJaxRsQuarkusAdapterTest method testCustomFallback.
@Test
public void testCustomFallback() {
String url = "/test/hello";
String resourceName = "GET:" + url;
SentinelJaxRsConfig.setJaxRsFallback(new SentinelJaxRsFallback() {
@Override
public javax.ws.rs.core.Response fallbackResponse(String route, Throwable cause) {
return javax.ws.rs.core.Response.status(javax.ws.rs.core.Response.Status.OK).entity("Blocked by Sentinel (flow limiting)").type(MediaType.APPLICATION_JSON_TYPE).build();
}
@Override
public Future<javax.ws.rs.core.Response> fallbackFutureResponse(final String route, final Throwable cause) {
return new FutureTask<>(new Callable<javax.ws.rs.core.Response>() {
@Override
public javax.ws.rs.core.Response call() throws Exception {
return fallbackResponse(route, cause);
}
});
}
});
configureRulesFor(resourceName, 0);
Response response = given().get(url);
response.then().statusCode(javax.ws.rs.core.Response.Status.OK.getStatusCode()).body(equalTo("Blocked by Sentinel (flow limiting)"));
ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
assertEquals(0, cn.passQps(), 0.01);
}
use of com.alibaba.csp.sentinel.adapter.jaxrs.fallback.SentinelJaxRsFallback in project Sentinel by alibaba.
the class ProviderFilterTest method testCustomFallback.
@Test
public void testCustomFallback() {
String url = "/test/hello";
String resourceName = "GET:" + url;
SentinelJaxRsConfig.setJaxRsFallback(new SentinelJaxRsFallback() {
@Override
public javax.ws.rs.core.Response fallbackResponse(String route, Throwable cause) {
return javax.ws.rs.core.Response.status(javax.ws.rs.core.Response.Status.OK).entity("Blocked by Sentinel (flow limiting)").type(MediaType.APPLICATION_JSON_TYPE).build();
}
@Override
public Future<javax.ws.rs.core.Response> fallbackFutureResponse(final String route, final Throwable cause) {
return new FutureTask<>(new Callable<javax.ws.rs.core.Response>() {
@Override
public javax.ws.rs.core.Response call() throws Exception {
return fallbackResponse(route, cause);
}
});
}
});
configureRulesFor(resourceName, 0);
Response response = given().get(url);
response.then().statusCode(javax.ws.rs.core.Response.Status.OK.getStatusCode()).body(equalTo("Blocked by Sentinel (flow limiting)"));
ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
assertEquals(0, cn.passQps(), 0.01);
}
Aggregations