本文整理汇总了Java中io.vertx.circuitbreaker.CircuitBreaker类的典型用法代码示例。如果您正苦于以下问题:Java CircuitBreaker类的具体用法?Java CircuitBreaker怎么用?Java CircuitBreaker使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CircuitBreaker类属于io.vertx.circuitbreaker包,在下文中一共展示了CircuitBreaker类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: example2
import io.vertx.circuitbreaker.CircuitBreaker; //导入依赖的package包/类
public void example2(Vertx vertx) {
CircuitBreaker breaker = CircuitBreaker.create("my-circuit-breaker", vertx,
new CircuitBreakerOptions().setMaxFailures(5).setTimeout(2000)
);
breaker.<String>execute(future -> {
vertx.createHttpClient().getNow(8080, "localhost", "/", response -> {
if (response.statusCode() != 200) {
future.fail("HTTP error");
} else {
response
.exceptionHandler(future::fail)
.bodyHandler(buffer -> {
future.complete(buffer.toString());
});
}
});
}).setHandler(ar -> {
// Do something with the result
});
}
示例2: buildRouter
import io.vertx.circuitbreaker.CircuitBreaker; //导入依赖的package包/类
private Router buildRouter(DfxConfig config, Map<String, Accessible> accessibleMap) {
Router router = Router.router(vertx);
try {
addCorsHandler(router, config.getCors());
}catch (Exception e) {
logger.error("Adding CORS handler failed, cause: {}", e);
vertx.close();
System.exit(-1);
}
config.getMappings().forEach((key, value) -> {
CircuitBreaker circuitBreaker = CircuitBreaker.create(key, vertx
, config.getCircuitBreakerOptions());
if (accessibleMap.get(value) != null) {
router.route(key).handler(new AccessibleHandler(key, accessibleMap.get(value)
, circuitBreaker, vertx));
}
}
);
return router;
}
示例3: withCircuitBreaker
import io.vertx.circuitbreaker.CircuitBreaker; //导入依赖的package包/类
public static void withCircuitBreaker(Vertx vertx, CircuitBreaker circuitBreaker
, Accessible accessible, Map params, Handler<Map> successHandler, Handler<Throwable> failureHandler) {
circuitBreaker.execute(future ->
vertx.executeBlocking(f -> {
try {
f.complete(accessible.invoke(params));
} catch (Throwable throwable) {
f.fail(throwable);
}
}
, result -> {
if (result.succeeded()) {
future.complete(result.result());
} else {
future.fail(result.cause());
}
})
).setHandler(result -> {
if (result.succeeded()) {
successHandler.handle((Map) result.result());
} else {
logger.error("CB[{}] execution failed, cause: ", circuitBreaker.name(), result.cause());
failureHandler.handle(result.cause());
}
});
}
示例4: example1
import io.vertx.circuitbreaker.CircuitBreaker; //导入依赖的package包/类
public void example1(Vertx vertx) {
CircuitBreaker breaker = CircuitBreaker.create("my-circuit-breaker", vertx,
new CircuitBreakerOptions()
.setMaxFailures(5) // number of failure before opening the circuit
.setTimeout(2000) // consider a failure if the operation does not succeed in time
.setFallbackOnFailure(true) // do we call the fallback on failure
.setResetTimeout(10000) // time spent in open state before attempting to re-try
);
breaker.execute(future -> {
// some code executing with the breaker
// the code reports failures or success on the given future.
// if this future is marked as failed, the breaker increased the
// number of failures
}).setHandler(ar -> {
// Get the operation result.
});
}
示例5: example3
import io.vertx.circuitbreaker.CircuitBreaker; //导入依赖的package包/类
public void example3(Vertx vertx) {
CircuitBreaker breaker = CircuitBreaker.create("my-circuit-breaker", vertx,
new CircuitBreakerOptions().setMaxFailures(5).setTimeout(2000)
);
breaker.executeWithFallback(
future -> {
vertx.createHttpClient().getNow(8080, "localhost", "/", response -> {
if (response.statusCode() != 200) {
future.fail("HTTP error");
} else {
response
.exceptionHandler(future::fail)
.bodyHandler(buffer -> {
future.complete(buffer.toString());
});
}
});
}, v -> {
// Executed when the circuit is opened
return "Hello";
})
.setHandler(ar -> {
// Do something with the result
});
}
示例6: example4
import io.vertx.circuitbreaker.CircuitBreaker; //导入依赖的package包/类
public void example4(Vertx vertx) {
CircuitBreaker breaker = CircuitBreaker.create("my-circuit-breaker", vertx,
new CircuitBreakerOptions().setMaxFailures(5).setTimeout(2000)
).fallback(v -> {
// Executed when the circuit is opened.
return "hello";
});
breaker.execute(
future -> {
vertx.createHttpClient().getNow(8080, "localhost", "/", response -> {
if (response.statusCode() != 200) {
future.fail("HTTP error");
} else {
response
.exceptionHandler(future::fail)
.bodyHandler(buffer -> {
future.complete(buffer.toString());
});
}
});
});
}
示例7: example5
import io.vertx.circuitbreaker.CircuitBreaker; //导入依赖的package包/类
public void example5(Vertx vertx) {
CircuitBreaker breaker = CircuitBreaker.create("my-circuit-breaker", vertx,
new CircuitBreakerOptions().setMaxFailures(5).setTimeout(2000)
).openHandler(v -> {
System.out.println("Circuit opened");
}).closeHandler(v -> {
System.out.println("Circuit closed");
});
breaker.execute(
future -> {
vertx.createHttpClient().getNow(8080, "localhost", "/", response -> {
if (response.statusCode() != 200) {
future.fail("HTTP error");
} else {
// Do something with the response
future.complete();
}
});
});
}
示例8: example7
import io.vertx.circuitbreaker.CircuitBreaker; //导入依赖的package包/类
public void example7(Vertx vertx) {
// Create the circuit breaker as usual.
CircuitBreaker breaker = CircuitBreaker.create("my-circuit-breaker", vertx);
CircuitBreaker breaker2 = CircuitBreaker.create("my-second-circuit-breaker", vertx);
// Create a Vert.x Web router
Router router = Router.router(vertx);
// Register the metric handler
router.get("/hystrix-metrics").handler(HystrixMetricHandler.create(vertx));
// Create the HTTP server using the router to dispatch the requests
vertx.createHttpServer()
.requestHandler(router::accept)
.listen(8080);
}
示例9: reset
import io.vertx.circuitbreaker.CircuitBreaker; //导入依赖的package包/类
/**
* A version of reset that can force the the state to `close` even if the circuit breaker is open. This is an
* internal API.
*
* @param force whether or not we force the state and allow an illegal transition
* @return the current circuit breaker.
*/
public synchronized CircuitBreaker reset(boolean force) {
failures = 0;
if (state == CircuitBreakerState.CLOSED) {
// Do nothing else.
return this;
}
if (!force && state == CircuitBreakerState.OPEN) {
// Resetting the circuit breaker while we are in the open state is an illegal transition
return this;
}
state = CircuitBreakerState.CLOSED;
closeHandler.handle(null);
sendUpdateOnEventBus();
return this;
}
示例10: main
import io.vertx.circuitbreaker.CircuitBreaker; //导入依赖的package包/类
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
CircuitBreakerOptions options = new CircuitBreakerOptions()
.setFallbackOnFailure(true)
.setMaxFailures(10)
.setResetTimeout(5000)
.setTimeout(1000)
.setMetricsRollingWindow(10000);
CircuitBreaker cba = CircuitBreaker.create("A", vertx, options);
CircuitBreaker cbb = CircuitBreaker.create("B", vertx, options);
CircuitBreaker cbc = CircuitBreaker.create("C", vertx, options);
Router router = Router.router(vertx);
router.get("/metrics").handler(HystrixMetricHandler.create(vertx));
router.get("/A").handler(rc -> a(rc, cba));
router.get("/B").handler(rc -> b(rc, cbb));
router.get("/C").handler(rc -> c(rc, cbc));
vertx.createHttpServer()
.requestHandler(router::accept)
.listen(8080);
}
示例11: b
import io.vertx.circuitbreaker.CircuitBreaker; //导入依赖的package包/类
private static void b(RoutingContext rc, CircuitBreaker cb) {
int choice = random.nextInt(10);
if (choice < 5) {
cb.executeWithFallback(
commandThatWorks(rc.vertx()),
(t) -> "OK (fallback)")
.setHandler(s -> rc.response().end(s.result()));
} else if (choice < 7) {
cb.executeWithFallback(
commandThatCrashes(rc.vertx()),
(t) -> "OK (fallback)")
.setHandler(s -> rc.response().end(s.result()));
} else {
cb.executeWithFallback(
commandThatFails(rc.vertx()),
(t) -> "OK (fallback)")
.setHandler(s -> rc.response().end(s.result()));
}
}
示例12: c
import io.vertx.circuitbreaker.CircuitBreaker; //导入依赖的package包/类
private static void c(RoutingContext rc, CircuitBreaker cb) {
int choice = random.nextInt(10);
if (choice < 5) {
cb.executeWithFallback(
commandThatWorks(rc.vertx()),
(t) -> "OK (fallback)")
.setHandler(s -> rc.response().end(s.result()));
} else if (choice < 7) {
cb.executeWithFallback(
commandThatTimeout(rc.vertx(), 15000),
(t) -> "OK (fallback)")
.setHandler(s -> rc.response().end(s.result()));
} else {
cb.executeWithFallback(
commandThatFails(rc.vertx()),
(t) -> "OK (fallback)")
.setHandler(s -> rc.response().end(s.result()));
}
}
示例13: testOk
import io.vertx.circuitbreaker.CircuitBreaker; //导入依赖的package包/类
@Test
@Repeat(5)
public void testOk() {
breaker = CircuitBreaker.create("test", vertx, new CircuitBreakerOptions());
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.CLOSED);
AtomicBoolean operationCalled = new AtomicBoolean();
AtomicReference<String> completionCalled = new AtomicReference<>();
breaker.<String>execute(fut -> {
operationCalled.set(true);
fut.complete("hello");
}).setHandler(ar -> completionCalled.set(ar.result()));
await().until(operationCalled::get);
await().until(() -> completionCalled.get().equalsIgnoreCase("hello"));
}
示例14: testWithUserFutureOk
import io.vertx.circuitbreaker.CircuitBreaker; //导入依赖的package包/类
@Test
@Repeat(5)
public void testWithUserFutureOk() {
breaker = CircuitBreaker.create("test", vertx, new CircuitBreakerOptions());
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.CLOSED);
AtomicBoolean operationCalled = new AtomicBoolean();
AtomicReference<String> completionCalled = new AtomicReference<>();
Future<String> userFuture = Future.future();
userFuture.setHandler(ar ->
completionCalled.set(ar.result()));
breaker.executeAndReport(userFuture, fut -> {
operationCalled.set(true);
fut.complete("hello");
});
await().until(operationCalled::get);
await().until(() -> completionCalled.get().equalsIgnoreCase("hello"));
}
示例15: testAsynchronousOk
import io.vertx.circuitbreaker.CircuitBreaker; //导入依赖的package包/类
@Test
public void testAsynchronousOk() {
breaker = CircuitBreaker.create("test", vertx, new CircuitBreakerOptions());
assertThat(breaker.state()).isEqualTo(CircuitBreakerState.CLOSED);
AtomicBoolean called = new AtomicBoolean();
AtomicReference<String> result = new AtomicReference<>();
breaker.<String>execute(future ->
vertx.setTimer(100, l -> {
called.set(true);
future.complete("hello");
})
).setHandler(ar -> result.set(ar.result()));
await().until(called::get);
await().untilAtomic(result, is("hello"));
}