Skip to content

Instantly share code, notes, and snippets.

@NiteshKant
Last active August 29, 2015 14:02
Show Gist options
  • Save NiteshKant/c7a909e53030c76473b0 to your computer and use it in GitHub Desktop.
Save NiteshKant/c7a909e53030c76473b0 to your computer and use it in GitHub Desktop.
Ribbon proposed interaction model
public @interface Hystrix {
String name();
Class<? extends FallbackProvider<?>> fallback();
// How do we represent the config?
interface FallbackProvider<T> extends Func1<HystrixCommand<T>, Observable<? extends T>> {
}
}
public class MyApplication {
public static void main(String[] args) {
Movie movie = Ribbon.createClient(MyService.class).getMovie("id1")
.flatMap(new Func1<Response<Movie>, Observable<? extends Movie>>() {
@Override
public Observable<? extends Movie> call(
Response<Movie> movieResponse) {
return movieResponse.value();
}
}).toBlockingObservable().single();
}
}
public interface MyService {
@Get(uri = "/api/v1/movie/{id}")
@EVCache(name = "api-movie-cache", appName = "api", cacheKeyTemplate = "{custId}:{id1}",
enableZoneFallback = true, ttl = 36000)
@Hystrix(name = "movie-fallback", fallback = MovieFallback.class)
Observable<Response<Movie>> getMovie(@Var("id") String movieId);
@Get(uri = "/api/v1/mylist/")
@EVCache(name = "api-mylist-cache", appName = "api", cacheKeyTemplate = "{custId}",
enableZoneFallback = true, ttl = 36000)
@Hystrix(name = "movie-list-fallback", fallback = MyListFallback.class)
Observable<Response<List<Movie>>> getMyList();
class MovieFallback implements Hystrix.FallbackProvider<Movie> {
@Override
public Observable<Movie> call(HystrixCommand<Movie> movieHystrixCommand) {
// TODO: Auto-generated method stub
return null;
}
}
class MyListFallback implements Hystrix.FallbackProvider<List<Movie>> {
@Override
public Observable<List<Movie>> call(HystrixCommand<List<Movie>> movieHystrixCommand) {
// TODO: Auto-generated method stub
return null;
}
}
}
public interface Response<T> {
/*TODO: In order to provide some type safety of value, we may think about providing a Dictionary construcs as opposed to Map here.*/
Observable<Map<String, Object>> asDictionary();
Observable<T> value();
// Any other generic method we would want to put here?
}
public final class Ribbon {
private Ribbon() {
}
@SuppressWarnings("unchecked")
public static <T> T createClient(Class<T> contract) {
return (T) Proxy.newProxyInstance(Ribbon.class.getClassLoader(),
new Class[] {contract}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// TODO: Auto-generated method stub
return null;
}
});
}
}
@NiteshKant
Copy link
Author

Review Comments:

  • Fallback should return Observable and not T.
  • Instead of Observable<Response<T>> from the service methods, should we be returning RibbonResponse where the RibbonResponse can look like:
public interface RibbonResponse<T> {

    T execute();

    Future<T> queue();

    Observable<T> observe();

    /*TODO: In order to provide some type safety of value, we may think about providing Dictionary construcs as opposed to Map here.*/
    Map<String, Object> asDictionary();

    /*TODO: In order to provide some type safety of value, we may think about providing Dictionary construcs as opposed to Map here.*/
    Future<Map<String, Object>> queueAsDictionary();

    /*TODO: In order to provide some type safety of value, we may think about providing Dictionary construcs as opposed to Map here.*/
    Observable<Map<String, Object>> observeAsDictionary();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment