How to Use Ribbon 2.0 HttpClient Template with Eureka Registry

141 views
Skip to first unread message

prasenjit majumder

unread,
Aug 5, 2015, 2:06:34 AM8/5/15
to eureka_netflix
Hi,

I am using Ribbon Client to look up the Services from Eureka Registry, So in my application using Ribbon Client discovering the service instances. e.g In Eureka registry two Instances are registered (service1(host1:port1), service2(host2:port2)).

In my Ribbon Client Part i am discovering the services by VipAddress like:

IRule rule = new AvailabilityFilteringRule();
        ServerList<DiscoveryEnabledServer> list = new DiscoveryEnabledNIWSServerList("MyVIP:7001");
        ServerListFilter<DiscoveryEnabledServer> filter = new ZoneAffinityServerListFilter<DiscoveryEnabledServer>();
        ZoneAwareLoadBalancer<DiscoveryEnabledServer> lb = LoadBalancerBuilder.<DiscoveryEnabledServer>newBuilder()
                .withDynamicServerList(list)
                .withRule(rule)
                .withServerListFilter(filter)
                .buildDynamicServerListLoadBalancer();   
        DiscoveryEnabledServer server = lb.chooseServer();

The above mentioned approach provides the loadbalancing and 1st request goes to (service1) and another to (service2).

But there is another available option in Ribbon 2 using Ribbon HttpClient template. And it takes the ServerLists hard coded in the withConfigurationBasedServerList("localhost:8080,localhost:8088")). like:

HttpResourceGroup httpResourceGroup = Ribbon.createHttpResourceGroup("movieServiceClient",
            ClientOptions.create()
                    .withMaxAutoRetriesNextServer(3)
                    .withConfigurationBasedServerList("localhost:8080,localhost:8088"));
HttpRequestTemplate<ByteBuf> recommendationsByUserIdTemplate = httpResourceGroup.newTemplateBuilder("recommendationsByUserId", ByteBuf.class)
            .withMethod("GET")
            .withUriTemplate("/users/{userId}/recommendations")
            .withFallbackProvider(new RecommendationServiceFallbackHandler())
            .withResponseValidator(new RecommendationServiceResponseValidator())
            .build();
Observable<ByteBuf> result = recommendationsByUserIdTemplate.requestBuilder()
                        .withRequestProperty("userId", “user1")
                        .build()
                        .observe();
So my Question is: If i want to use The HttpClient Template along with the eureka registry, what is the better approach.
Reply all
Reply to author
Forward
0 new messages