Skip to content

Instantly share code, notes, and snippets.

@kennedyoliveira
Created April 10, 2017 15:15
Show Gist options
  • Save kennedyoliveira/24e5827edbc2185b396c08faa67a5269 to your computer and use it in GitHub Desktop.
Save kennedyoliveira/24e5827edbc2185b396c08faa67a5269 to your computer and use it in GitHub Desktop.
Example showing difference in metrics for vertx-circuit-breaker and hystrix
import com.github.kennedyoliveira.hystrix.contrib.vertx.metricsstream.EventMetricsStreamHandler
import com.netflix.hystrix.HystrixCommandGroupKey
import com.netflix.hystrix.HystrixObservableCommand
import io.vertx.circuitbreaker.CircuitBreaker
import io.vertx.circuitbreaker.HystrixMetricHandler
import io.vertx.core.AbstractVerticle
import io.vertx.core.Future
import io.vertx.core.Vertx
import io.vertx.ext.web.Router
import rx.Observable
@Grapes([
@Grab(group = 'io.vertx', module = 'vertx-circuit-breaker', version = '3.4.1'),
@Grab(group = 'io.vertx', module = 'vertx-web', version = '3.4.1'),
// grapes wasn't able to fetch the web module without oauth2
@Grab(group = 'io.vertx', module = 'vertx-auth-oauth2', version = '3.4.1'),
@Grab(group = 'io.vertx', module = 'vertx-lang-groovy', version = '3.4.1'),
@Grab(group = 'com.netflix.hystrix', module = 'hystrix-core', version = '1.5.6'),
@Grab(group = 'com.github.kennedyoliveira', module = 'hystrix-vertx-metrics-stream', version = '1.5.6')
])
def libs
def vertx = Vertx.vertx()
class CircuitBreakerVerticle extends AbstractVerticle {
@Override
void start(Future<Void> startFuture) throws Exception {
def cb = CircuitBreaker.create("sample", vertx)
// vertx circuit breaker
vertx.setPeriodic(250L, {
// sample
cb.execute({ f -> f.complete() })
})
// hystrix
vertx.setPeriodic(250L, {
def hystrixSampleCommnand = new HystrixObservableCommand<Void>(HystrixObservableCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("hystrix"))) {
@Override
protected Observable construct() {
// sample complete
return Observable.empty()
}
}
hystrixSampleCommnand.observe()
})
def router = Router.router(vertx)
router.get('/vertx-metrics')
.handler(HystrixMetricHandler.create(vertx))
router.get()
.handler(EventMetricsStreamHandler.createHandler(vertx))
vertx.createHttpServer()
.requestHandler(router.&accept)
.listen(8080)
}
}
vertx.deployVerticle(CircuitBreakerVerticle.name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment