Skip to content

Commit

Permalink
updated to use ribbon support in restTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
spencergibb committed Sep 29, 2014
1 parent 4621cb7 commit 6a48a5b
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 62 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<eureka.version>1.1.134</eureka.version>
<feign.version>6.1.2</feign.version>
<hystrix.version>1.4.0-RC4</hystrix.version>
<ribbon.version>0.3.12</ribbon.version>
<ribbon.version>0.3.13</ribbon.version>
<zuul.version>1.0.24</zuul.version>
<spring-data-rest.version>2.2.0.BUILD-SNAPSHOT</spring-data-rest.version>
<spring-cloud.version>${project.version}</spring-cloud.version>
Expand Down Expand Up @@ -177,7 +177,7 @@
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-java-dsl</artifactId>
<version>1.0.0.M2</version>
<version>1.0.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
Expand Down
9 changes: 9 additions & 0 deletions spring-cloud-sandbox-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,19 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon-core</artifactId>
</dependency>
<dependency>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.12.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

This file was deleted.

9 changes: 9 additions & 0 deletions spring-cloud-sandbox-sample-frontend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon-core</artifactId>
</dependency>
<dependency>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon-httpclient</artifactId>
</dependency>
<dependency>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon-eureka</artifactId>
Expand All @@ -128,5 +132,10 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>14.0.1</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package org.springframework.cloud.sample.frontend;

import com.netflix.client.ClientFactory;
import com.netflix.http4.NFHttpClient;
import com.netflix.http4.NFHttpClientFactory;
import com.netflix.niws.client.http.RestClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.FeignConfigurer;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.cloud.logging.LoggingListener;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.FeignConfigurer;
import org.springframework.cloud.netflix.hystrix.annotations.EnableHystrix;
import org.springframework.web.client.RestTemplate;

//import feign.slf4j.Slf4jLogger;

Expand All @@ -21,18 +23,20 @@
public class Application extends FeignConfigurer {

@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
public NFHttpClient nfHttpClient() {
return NFHttpClientFactory.getDefaultClient();
}

@Bean
public HelloClient helloClient() {
return loadBalance(HelloClient.class, "http://samplebackendservice");
public RestClient restClient() {
return (RestClient) ClientFactory.getNamedClient("default");
}

@Bean
public LoggingListener loggingListener() {
return new LoggingListener();
public HelloClient helloClient() {
//return feign().target(HelloClient.class, "http://samplebackendservice");
//TODO: create proxy for interface if annotated with a marker
return loadBalance(HelloClient.class, "http://samplebackendservice");
}

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package org.springframework.cloud.sample.frontend;

import com.netflix.client.http.HttpRequest;
import com.netflix.client.http.HttpResponse;
import com.netflix.http4.NFHttpClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.command.AsyncResult;
import com.netflix.hystrix.contrib.javanica.command.ObservableResult;
import com.netflix.niws.client.http.RestClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sample.backend.Message;
import org.springframework.stereotype.Service;
Expand All @@ -22,12 +27,32 @@ public class HelloService {
@Autowired
HelloClient helloClient;

@Autowired
//NFHttpClient httpClient;
RestClient restClient;

//@CircuitBreaker
@HystrixCommand(fallbackMethod = "getDefaultMessage")
public String getMessage() {
return getMessageImpl();
}

//@HystrixCommand(fallbackMethod = "getDefaultMessage")
public String getRibbonMessage() throws Exception {
HttpRequest request = HttpRequest.newBuilder()
.uri("http://localhost:7080/hello")
.verb(HttpRequest.Verb.GET)
.build();
//TODO: wire spring message decoders to jersey client in RestClient
HttpResponse response = restClient.execute(request);
return response.getEntity(Message.class).getBody();
}

public String getRestTemplateMessage() {
Message message = restTemplate.getForObject("http://samplebackendservice/hello", Message.class);
return message.getBody();
}

@HystrixCommand(fallbackMethod = "getDefaultMessage")
public String sendMessage() {
return helloClient.hello(new Message("World via POST")).getBody();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ public String home(Map<String, Object> model, HttpServletRequest req) {
return "home";
}

@RequestMapping("/ribbon")
public String ribbon(Map<String, Object> model, HttpServletRequest req) throws Exception {
String message = helloService.getRibbonMessage();
model.put("message", "Hello Ribbon "+ message);
values(model, req);
return "home";
}

@RequestMapping("/rt")
public String restTemplate(Map<String, Object> model, HttpServletRequest req) throws Exception {
String message = helloService.getRestTemplateMessage();
model.put("message", "Hello Rest Template "+ message);
values(model, req);
return "home";
}

@RequestMapping("/future")
public String future(Map<String, Object> model, HttpServletRequest req) throws ExecutionException, InterruptedException {
model.put("message", "Hello Future "+helloService.getMessageFuture().get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ endpoints:
server:
port: 9080

spring:
cloud:
client:
serviceIds:
- samplebackendservice

#TODO: set all this as defaults during autoconfigure
samplebackendservice:
ribbon:
Expand All @@ -20,22 +26,22 @@ samplebackendservice:
#DeploymentContextBasedVipAddresses: samplebackendservice

# Max number of retries on the same server (excluding the first try)
MaxAutoRetries: 1
# MaxAutoRetries: 1

# Max number of next servers to retry (excluding the first server)
MaxAutoRetriesNextServer: 1
# MaxAutoRetriesNextServer: 1

# Whether all operations can be retried for this client
OkToRetryOnAllOperations: true
# OkToRetryOnAllOperations: true

# Interval to refresh the server list from the source
ServerListRefreshInterval: 2000

# Connect timeout used by Apache HttpClient
ConnectTimeout: 3000
# ConnectTimeout: 3000

# Read timeout used by Apache HttpClient
ReadTimeout: 3000
# ReadTimeout: 3000

eureka:
client:
Expand Down

1 comment on commit 6a48a5b

@rubesMN
Copy link

@rubesMN rubesMN commented on 6a48a5b Oct 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question, can you expand on what you're changing here? I'm looking to use an instance of RestTemplate and have it use a default loadBalancer within a 'spring-cloud-starters' application configured with Hystrix and Eureka?

Please sign in to comment.