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
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
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Retrying HTTP Requests in Spring Cloud Netflix

Retrying HTTP Requests in Spring Cloud Netflix

Cloud-native apps need to be resilient enough to handle network problems so HTTP requests don't just stop happening. Spring Cloud Netflix has those tools.

Ryan Baxter user avatar by
Ryan Baxter
·
Mar. 17, 17 · Tutorial
Like (6)
Save
Tweet
Share
11.61K Views

Join the DZone community and get the full member experience.

Join For Free

As I have talked about before, one of the key aspects of cloud native applications is resiliency. In order to be robust and avoid all the potential problems that can come from running in such a volatile environment like the cloud, your application needs to be able to handle anything that can be thrown at it. One of the more common operations a cloud native application will do is make HTTP requests to other applications. These applications may be under your control as well, or they could be third-party apps, but in the end it doesn’t really matter who controls them, there is always the potential for failure when making any kind of network request.

Cloud native apps should be able to deal with any kind of failure in a graceful manor. Circuit breakers, which I have talked about before, are one way of dealing with failures. Before we get to the point of tripping a circuit breaker, we can try to be a smarter about the network requests we make. Just because a network request fails once doesn’t mean we should give up and all hope is lost.

Network requests can fail for a number of reasons. Yes, the service you may be making a request to may be down, which is probably the worst case scenario, but there are other reasons as well. Maybe it is just the network itself having issues. Maybe the service you are trying to make a request to is actually up but is temporarily unavailable. Whatever the reason may be for the failure, it could be a short-term issue. If you were to make the same request shortly after the failure occurs it may succeed. So in order to make our applications more resilient we can retry failed requests in the hope that subsequent requests might succeed.

If you are familiar with Spring Cloud Netflix, there are a number ways to make HTTP requests. The three main ways are using a load balanced RestTemplate, Feign, and Zuul. In Spring Cloud Brixton all three of these scenarios leveraged an HTTP client from Netflix that has built-in logic to retry failed requests. However, this HTTP client was deprecated and we needed to come up with our own HTTP client to use. Unfortunately when implementing the new HTTP client we did not originally include logic to retry failed requests. As of Spring Cloud Camden.SR6 all three use cases now once again have logic to retry failed requests.

To achieve this, we leveraged Spring Retry as an easy way of implementing the retry logic. Because we leverage Spring Retry, you must make sure Spring Retry is on the classpath of your application in order to enable the retry logic. Without Spring Retry on the classpath you will find that failed requests will not be retried.

You can control how many times a request is retried using standard Ribbon properties. To control how many times a request is retried on the same server you can adjust sample-client.ribbon.MaxAutoRetries. To control how many servers to try you can adjust sample-client.ribbon.MaxAutoRetriesNextServer. The total number of retries that are made will be equal to:

sample-client.ribbon.MaxAutoRetries *

sample-client.ribbon.MaxAutoRetriesNextServer.

You can also control whether just GET requests will be retried or whether all requests will be retried by adjusting sample-client.ribbon.OkToRetryOnAllOperations. The above properties apply whether you are using a load balanced RestTemplate, Feign, or Zuul.

When using Zuul, there are some additional properties you can configure to control retry functionality. Setting zuul.retryable to true or false will control whether all or none of the proxied requests will be retried. By default it is set to true. You can also control retry functionality on a route by route basis by setting zuul.routes.routename.retryable.

If you have been sticking to using the deprecated Netflix RestClient due to the lack of retry logic in the newer HTTP clients hopefully this will now allow you to switch to using the newer clients. The retry logic we have added should work for both the Apache based HTTP clients of the OK HTTP clients, so you have your choice as to which one you want to use.

Spring Cloud Requests 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

  • Better Performance and Security by Monitoring Logs, Metrics, and More
  • Kubernetes vs Docker: Differences Explained
  • How to Create a Real-Time Scalable Streaming App Using Apache NiFi, Apache Pulsar, and Apache Flink SQL
  • Top 10 Secure Coding Practices Every Developer Should Know

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: