DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Retrying Requests with Spring Cloud Ribbon

Retrying Requests with Spring Cloud Ribbon

Se examples of how requests can be retried using Ribbon and the Spring Retry project.

Ryan Baxter user avatar by
Ryan Baxter
·
Nov. 18, 16 · Tutorial
Like (4)
Save
Tweet
Share
6.63K Views

Join the DZone community and get the full member experience.

Join For Free

Most applications today make HTTP requests to external services. When using Spring Cloud, one way to make these requests is to use what we call a “load balanced RestTemplate”. Creating a load balanced RestTemplate is pretty straightforward.

@Configuration 
public class MyConfiguration { 
  @LoadBalanced 
  @Bean 
  RestTemplate restTemplate() { 
    return new RestTemplate(); 
  } 
} 

public class MyClass { 
  @Autowired 
  private RestTemplate restTemplate; 
  public String doOtherStuff() { 
    String results = restTemplate.getForObject("http://stores/stores", String.class); 
    return results; 
  } 
} 

The added @LoadBalanced annotation allows you to take advantage of services registered with a discovery service, such as Eureka. As you can see in the doOtherStuff method, we are making a request to the URL http://stores/stores. The hostname, stores, is not actually a registered hostname. Instead, it is the name of the stores service registered with the discovery server. The RestTemplate, in this case, will replace the stores hostname with the hostname (or IP address) of the stores service.

As we are all aware, making requests like this can be problematic. For any number of reasons, something may go wrong and the request could fail. That is why, in robust applications, API requests like the one above may be retried when failures are encountered. It might be the case that the service is completely down and we are never going to get a response. However, it is equally as likely that the failure was due to some kind of fluke network issue and a subsequent request may succeed. It could even be the case that a given instance of a service may be experiencing problems, but there is another instance available that might be perfectly capable of handling our request.

In Spring Cloud, if you used a load balanced RestTemplate to make your API request and the request failed, then it was up to you, the developer, to retry the request. As of Camden.SR2, we have introduced some retry handling into load balanced RestTemplates. We now take advantage of the awesome Spring Retry project to provide the retry logic. You can use Ribbon properties to configure how many retry requests are made, and which requests are retried.

In the future, we will be using Spring Retry when making API requests with Feign as well as when requests are made through Zuul.

Requests Spring Cloud Spring Framework

Published at DZone with permission of Ryan Baxter, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Steel Threads Are a Technique That Will Make You a Better Engineer
  • Monolithic First
  • How To Choose the Right Streaming Database
  • DevOps for Developers: Continuous Integration, GitHub Actions, and Sonar Cloud

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: