Skip to content
Wen Hao edited this page Aug 16, 2018 · 1 revision

Build Status Coverage Status Apache 2.0 License

Mushrooms

Mushrooms

Mushrooms is an easy setup failover and stub framework. To ensure high levels of efficiency for remote service integration.

Why

Remote service integration, especially based on HTTP protocol, e.g. web service, REST etc, highly unstable when developing.

Features

Failover
  • RestTemplate Request Cache.
  • Okhttp Request Cache with @FeignClient.
Stub
  • Stub REST API via @FeignClient.
  • Stub Soap API via @FeignClient.

Gradle

repositories {
    jcenter()
}

dependencies {
    compile 'com.github.wenhao:mushrooms:2.1.7'
}

Maven

<dependency>
    <groupId>com.github.wenhao</groupId>
    <artifactId>mushrooms</artifactId>
    <version>2.1.7</version>
</dependency>

Build

./gradlew clean build

Get Started

Failover Configuration

application.yml

Enabled mushrooms failover and set included headers, don't inlcude any frequent changeable header.

mushrooms:
  failover:
    okhttp:
      enabled: true
    resttemplate:
      enabled: true
    headers:
      - application-specific
      - content-type

Stub Configuration

Enabled mushrooms stub and set stub request and response.

Stub REST API

mushrooms:
  stub:
    okhttp:
      enabled: true
      stubs:
        - uri: "${READL_HOST:http://localhost:8080}/stub/book"
          method: POST
          body: /stubs/stub_rest_request.json
          response: /stubs/stub_rest_response.json

Stub Soap API

mushrooms:
  stub:
    okhttp:
      enabled: true
      stubs:
        - uri: "${READL_HOST:http://localhost:8080}/stub/get_book"
          method: POST
          body: /stubs/stub_soap_request.xml
          response: /stubs/stub_soap_response.xml

Generic Configuration

If enabled okhttp failover or stub, enabling feign okhttp client.

feign:
  okhttp:
    enabled: true
  client:
    config:
      default:
        connectTimeout: 5000
        readTimeout: 5000
        loggerLevel: full

Failover will use redis, if RedisTemplate bean is not configured, add follow configuration:

spring:
  redis:
    host: localhost
    port: 6379
    password:
    lettuce:
      pool:
        max-active: 8
        max-idle: 8
        max-wait: -1ms
        min-idle: 1

Logging

logging:
  level:
    com.github.wenhao: DEBUG

Customization

As default, failover only applys if httpstatus not equals to 200.

Custom RestTemplate Health Check

As default, HttpStatusRestTemplateHealthCheck.java added, customize health checks if need:

@Component
public class CustomRestTemplateHealthCheck implements RestTemplateHealthCheck {

    @Override
    public boolean health(final ClientHttpResponseWrapper response) {
        try {
            final JSONObject jsonObject = new JSONObject(response.getBodyAsString());
            return jsonObject.getBoolean("success");
        } catch (JSONException e) {
            return false;
        }
    }
}
Custom OkHttp Health Check

As default, HttpStatusOkHttpClientHealthCheck.java added, customize health checks if need:

@Component
public class CustomOkHttpClientHealthCheck implements OkHttpClientHealthCheck {

    @Override
    public boolean health(final Response response) {
        final String body = getResponseBody(response);
        try {
            final JSONObject jsonObject = new JSONObject(body);
            return jsonObject.getBoolean("success");
        } catch (Exception e) {
            return false;
        }
    }
}

Attentions

  1. Stub okhttp interceptor prior to failover okhttp interceptor.
  2. Exclude from failover okhttp configuration if don't need it when stub.

Copyright and license

Copyright © 2018 Wen Hao

Licensed under Apache License