Skip to content

johanhaleby/kystrix

Repository files navigation

Logo

Maven Central

Kystrix is a small DSL that makes working with Hystrix easier from Kotlin.

For example:

val greeting = hystrixCommand<Greeting> {
    groupKey("GreetingService")
    commandKey("Greeting")
    command {
        // This is what you want Hystrix to wrap
        get("/greeting?firstName=John&lastName=Doe").asJson()
    }
    commandProperties {
        withExecutionTimeoutInMilliseconds(10000)
        withExecutionIsolationStrategy(THREAD)
        withFallbackEnabled(false)
    }
    threadPoolProperties {
        withQueueSizeRejectionThreshold(5)
        withMaxQueueSize(10)
    }
}

Contents

  1. Getting Started
  2. Spring Support
  3. More Examples

Getting Started

The project contains two modules, kystrix-core and kystrix-spring. You only need kystrix-spring if you're using components from Spring's reactive stack such as spring-webflux. See Kystrix Spring for more info.

The project is available in Maven central as well as JCenter.

Maven

<dependency>
    <groupId>se.haleby.kystrix</groupId>
    <artifactId>kystrix-core</artifactId>
    <version>0.1.16</version>
</dependency>

Gradle

compile 'se.haleby.kystrix:kystrix-core:0.1.16'

Kobalt

dependencies {
    compile("se.haleby.kystrix:kystrix-core:0.1.16")
}

Imports

Once the kystrix-core DSL is included in the build there are two different entry points, hystrixCommand and hystrixObservableCommand, and both can be imported like this:

import se.haleby.kystrix.hystrixCommand
import se.haleby.kystrix.hystrixObservableCommand

Use the hystrixCommand for blocking IO and hystrixObservableCommand for non-blocking IO using RxJava Observables.

See this blog post for more information.

Spring Support

Kystrix provides a module named kystrix-spring makes it easier to integrate and work with Mono and Flux and Hystrix.

Maven

<dependency>
    <groupId>se.haleby.kystrix</groupId>
    <artifactId>kystrix-spring</artifactId>
    <version>0.1.16</version>
</dependency>

Gradle

compile 'se.haleby.kystrix:kystrix-spring:0.1.16'

Kobalt

dependencies {
    compile("se.haleby.kystrix:kystrix-spring:0.1.16")
}

Imports

Once the kystrix-spring DSL is included in the classpath you can start using the extension functions in the se.haleby.kystrix package:

import se.haleby.kystrix.monoCommand
import se.haleby.kystrix.toMono
import se.haleby.kystrix.fluxCommand
import se.haleby.kystrix.toFlux

Mono Example

val greeting = hystrixObservableCommand<Greeting> {
    groupKey("Test")
    commandKey("Test-Command")
    monoCommand {
        webClient.get().uri("/greetings/1").retrieve().bodyToMono()
    }
    commandProperties {
        withExecutionTimeoutInMilliseconds(10000)
        withFallbackEnabled(false)
    }
}.toMono()

Using the monoCommand extension function makes it easy to integrate a Mono response with Hystrix. Also note the call to toMono() at the end, this will convert the Observable returned by Hystrix back to a Mono instance.

Flux Example

val greeting = hystrixObservableCommand<Greeting> {
    groupKey("Test")
    commandKey("Test-Command")
    fluxCommand {
        webClient.get().uri("/greetings").retrieve().bodyToFlux()
    }
    commandProperties {
        withExecutionTimeoutInMilliseconds(10000)
        withFallbackEnabled(false)
    }
}.toFlux()

Here Kystrix returns a non-blocking stream of Greeting wrapped in a Flux.

More Examples

Have a look in the test package (see here for spring examples).

Support my work

Buy Me A Coffee

About

Kystrix is a small Kotlin DSL over Hystrix

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published