Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry and Circuit Breaker in HttpClientSupport #35

Merged
merged 7 commits into from
Jan 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 41 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,47 @@ When the parameter value is invalid, Resty responds the following response with
}
```

## HTTP client

`HttpClientSupport` trait offers methods to send HTTP request. You can call other Web APIs easily using these methods.

```scala
class HelloController extends HttpClientSupport {
@Action(method = "GET", path = "/hello/{id}")
def hello(id: Int): Message = {
// Call other API using methods provided by HttpClientSupport
val user: User = httpGet[User](s"http://localhost:8080/user/${id}")
Message(s"Hello ${user.name}!")
}

@Action(method = "GET", path = "/hello-async/{id}")
def helloAsync(id: Int): Future[Message] = {
// HttpClientSupport also supports asynchronous communication
val future: Future[Either[ErrorModel, User]] = httpGetAsync[User](s"http://localhost:8080/user/${id}")
future.map {
case Right(user) => Message(s"Hello ${user.name}!")
case Left(error) => throw new ActionResultException(InternalServerError(error))
}
}
}
```

These methods have retrying ability and circuit breaker. You can configure these behavior by defining `HttpClientConfig` as an implicit value.

```scala
class HelloController extends HttpClientSupport {

implicit override val httpClientConfig = HttpClientConfig(
maxRetry = 5, // max number of retry. default is 0 (no retry)
retryInterval = 500, // interval of retry (msec). default is 0 (retry immediately)
maxFailure = 3, // max number until open circuit breaker. default is 0 (disabling circuit breaker)
resetInterval = 60000 // interval to reset closed circuit breaker (msec). default is 60000
)

...
}
```

## Swagger integration

Resty provides [Swagger](http://swagger.io/) integration in default. Swagger JSON is provided at `http://localhost:8080/swagger.json` and also Swagger UI is available at `http://localhost:8080/swagger-ui/`.
Expand Down Expand Up @@ -209,27 +250,6 @@ Add following parameter to `web.xml` to enable Hystrix integration:

Furthermore, Resty supports [Zipkin](http://zipkin.io/) as well. You can send execution results to the Zipkin server by enabling Zipkin support and using `HttpClientSupport` for calling other APIs.

```scala
class HelloController extends HttpClientSupport {
@Action(method = "GET", path = "/hello/{id}")
def hello(id: Int): Message = {
// Call other API using methods provided by HttpClientSupport
val user: User = httpGet[User](s"http://localhost:8080/user/${id}")
Message(s"Hello ${user.name}!")
}

@Action(method = "GET", path = "/hello-async/{id}")
def helloAsync(id: Int): Future[Message] = {
// HttpClientSupport also supports asynchronous communication
val future: Future[Either[ErrorModel, User]] = httpGetAsync[User](s"http://localhost:8080/user/${id}")
future.map {
case Right(user) => Message(s"Hello ${user.name}!")
case Left(error) => throw new ActionResultException(InternalServerError(error))
}
}
}
```

Add following parameters to `web.xml` to enable Zipkin integration:

```xml
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name := "resty"

organization := "com.github.takezoe"

version := "0.0.15"
version := "0.0.16-SNAPSHOT"

scalaVersion := "2.12.4"

Expand Down
Loading