com.netflix.hystrix.HystrixCircuitBreaker Java Examples

The following examples show how to use com.netflix.hystrix.HystrixCircuitBreaker. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HystrixKafkaCircuitBreaker.java    From nakadi with MIT License 6 votes vote down vote up
public HystrixKafkaCircuitBreaker(final String brokerId) {
    commandKey = HystrixCommandKey.Factory.asKey(brokerId);
    commandProperties = HystrixPropertiesFactory.getCommandProperties(commandKey, null);
    threadPoolKey = HystrixThreadPoolKey.Factory.asKey(brokerId);
    hystrixCommandMetrics = HystrixCommandMetrics.getInstance(
            commandKey,
            HYSTRIX_CMD_GROUP_KEY,
            threadPoolKey,
            commandProperties);
    circuitBreaker = HystrixCircuitBreaker.Factory.getInstance(
            commandKey,
            HYSTRIX_CMD_GROUP_KEY,
            commandProperties,
            hystrixCommandMetrics);
    concurrentExecutionCount = new AtomicInteger();
}
 
Example #2
Source File: CircuitBreaker.java    From tenacity with Apache License 2.0 6 votes vote down vote up
public static Optional<CircuitBreaker> usingHystrix(TenacityPropertyKey id) {
    final HystrixCircuitBreaker circuitBreaker = TenacityCommand.getCircuitBreaker(id);

    if (circuitBreaker == null) {
        return Optional.empty();
    }

    final HystrixCommandProperties commandProperties = TenacityCommand.getCommandProperties(id);

    if (commandProperties.circuitBreakerForceOpen().get()) {
        return Optional.of(CircuitBreaker.forcedOpen(id));
    } else if (commandProperties.circuitBreakerForceClosed().get()) {
        return Optional.of(CircuitBreaker.forcedClosed(id));
    } else if (circuitBreaker.allowRequest()) {
        return Optional.of(CircuitBreaker.closed(id));
    } else {
        return Optional.of(CircuitBreaker.open(id));
    }
}
 
Example #3
Source File: HystrixServlet.java    From s2g-zuul with MIT License 4 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	PrintWriter writer = resp.getWriter();
	try {

		// make a writable copy of the immutable System.getenv() map
		Map<String, String> breakerMap = new TreeMap<String, String>();

		for (HystrixCommandMetrics commandMetrics : HystrixCommandMetrics.getInstances()) {
			HystrixCommandKey key = commandMetrics.getCommandKey();
			HystrixCircuitBreaker circuitBreaker = HystrixCircuitBreaker.Factory.getInstance(key);

			if (circuitBreaker != null) {
				if (circuitBreaker.isOpen()) {
					breakerMap.put(key.name(), DynamicPropertyFactory.getInstance().getStringProperty("mail."+key.name(), "[email protected]").get());
				}else{
					if(DynamicPropertyFactory.getInstance().getBooleanProperty("hystrix.command."+key.name()+".circuitBreaker.forceOpen",false).get()){
						breakerMap.put(key.name(), DynamicPropertyFactory.getInstance().getStringProperty("mail."+key.name(), "[email protected]").get());
					}else if(DynamicPropertyFactory.getInstance().getBooleanProperty("hystrix.command.default.circuitBreaker.forceOpen",false).get()){
						breakerMap.put(key.name(), DynamicPropertyFactory.getInstance().getStringProperty("mail."+key.name(), "[email protected]").get());							
					}
				}
			}
		}

		String jsonStr = JSON.toJSONString(breakerMap);

		resp.addHeader("Access-Control-Allow-Origin", "*");
		resp.addHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
		resp.setContentType("application/json; charset=UTF-8");

		writer.write(jsonStr);
		resp.setStatus(Response.SC_OK);
	}catch(Throwable t){
		writer.write(t.getMessage());
		resp.setStatus(Response.SC_INTERNAL_SERVER_ERROR);
	}finally {
	
		if (writer != null) {
			writer.close();
		}
	}
}
 
Example #4
Source File: HystrixMetricsProcessingJob.java    From cerberus with Apache License 2.0 4 votes vote down vote up
public void processHystrixCommandMetrics() {
  for (HystrixCommandMetrics metrics : HystrixCommandMetrics.getInstances()) {
    Map<String, String> dimensions =
        ImmutableMap.of(
            "key", metrics.getCommandKey().name(),
            "group", metrics.getCommandGroup().name());
    boolean isCircuitOpen =
        HystrixCircuitBreaker.Factory.getInstance(metrics.getCommandKey()).isOpen();

    log.debug(
        "group:{}, commandKey:{}, CircuitOpen:{}, Mean:{}, 95%:{}, 99%:{}, 99.5%:{}, {}",
        metrics.getCommandGroup().name(),
        metrics.getCommandKey().name(),
        isCircuitOpen,
        metrics.getExecutionTimeMean(),
        metrics.getExecutionTimePercentile(95.0),
        metrics.getExecutionTimePercentile(99.0),
        metrics.getExecutionTimePercentile(99.5),
        metrics.getHealthCounts());

    metricsService.getOrCreateCallbackGauge(
        "hystrix.command.circuit_open", () -> isCircuitOpen ? 1 : 0, dimensions);
    metricsService.getOrCreateCallbackGauge(
        "hystrix.command.exec_time.mean", metrics::getExecutionTimeMean, dimensions);
    metricsService.getOrCreateCallbackGauge(
        "hystrix.command.exec_time.95th",
        () -> metrics.getExecutionTimePercentile(95.0),
        dimensions);
    metricsService.getOrCreateCallbackGauge(
        "hystrix.command.exec_time.99th",
        () -> metrics.getExecutionTimePercentile(99.0),
        dimensions);
    metricsService.getOrCreateCallbackGauge(
        "hystrix.command.rolling.max_concurrent_execs",
        metrics::getRollingMaxConcurrentExecutions,
        dimensions);
    metricsService.getOrCreateCallbackGauge(
        "hystrix.command.total_count",
        () -> metrics.getHealthCounts().getTotalRequests(),
        dimensions);
    metricsService.getOrCreateCallbackGauge(
        "hystrix.command.error_count",
        () -> metrics.getHealthCounts().getErrorCount(),
        dimensions);
  }
}