Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Integrating the Spring Cloud Netflix Framework Into Your Existing API

DZone's Guide to

Integrating the Spring Cloud Netflix Framework Into Your Existing API

As API usage increases, analysis should consider scalability and fault tolerance. Netflix OSS Spring Cloud framework provides a starting point for enhancing scalability.

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

This article is featured in the new DZone Guide to Integration, Volume III. Get your free copy for more insightful articles, industry statistics, and more.

By this point, your RESTful API development has likely reached a level of success within your organization. As more and more microservices or APIs emerge into your landscape, the dependency on your APIs likely grows as well. An increase in demand for your service correlates to a rising desire to make your API or microservice as robust as possible. Often, this approach translates to multiple instances running, with some form of load balancing put into place to keep up with demand.

DZone_GuideToIntegration2016

Netflix (OSS) Spring Cloud Project

With the popularity and success of Netflix's (OSS) Spring Cloud project, it might be time to consider integrating the framework into your mission-critical APIs. The Netflix project builds upon the Spring Boot framework and introduces the following components:

  • Eureka is used for service discovery of instances running Spring-managed beans.
  • Zuul handles routing services and is viewed as the gatekeeper for requests.
  • Ribbon is used for dynamic routing and load balancing
  • Hystrix offers circuit-breaker functionality to handle unresponsive API calls.
  • Turbine provides Hystrix with information regarding all available circuit-breakers.

The use of Spring Cloud Config is recommended as well, as it allows for application configurations to be centralized in a Git-based repository.

First Challenge: Getting to Spring Boot

If your existing API or microservice is built on Spring Boot, you are in a very good position and can proceed to the next section. Most likely, your service is not running on Spring Boot, which means some effort will need to take place to get you in position to where you can take advantage of the OSS tools Net ix has offered the software community.

For starters, review the Convert an Existing Project to Spring Boot section in the current Spring Boot reference guide. The section provides some considerations and suggestions on how to perform the conversion. The section notes that non-web applications, like API services, are less difficult in making the transition to becoming a Spring Boot application. In this scenario, the code that creates an ApplicationContext is replaced with calls to SpringApplication. In that instance, the SpringBootServletInitializer can be extended for the Application class and Spring Boot auto-configuration enabled:

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application extends
SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(
    SpringApplicationBuilder application) {
        return application;
    }
}

The application can be made executable by adding the following main() method:

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}
DZone_GuideToIntegration2016

For existing Servlet applications, if they are utilizing version 3.0+ applications, the translation should migrate pretty easily — especially if using Spring Servlet initializer support classes.

For more complex applications, analysis should be completed to determine if the best approach is to start with a fresh Spring Boot application instance and migrate your classes and methods into the application.

Once you have your Spring Boot application running and APIs validated, the Netflix components can be introduced.

Create the Eureka and Zuul Servers

The first server that will be added will be the Eureka server, which will handle the service discovery. The Eureka server is a standard Spring Boot application with a simple main() method:

@SpringBootApplication
@EnableEurekaServer 
@EnableDiscoveryClient
public class EurekaApplication {
    public static void main(String[] args) { 
        SpringApplication.run(
           EurekaApplication.class, args);
    } 
 }
DZone_GuideToIntegration2016

The Zuul server will act as the gatekeeper, or the primary server ultimately being contacted for your microservice. Here, another standard Spring Boot application instance is created with the following main() method:

@SpringBootApplication 
@Controller
@EnableZuulProxy
public class ZuulApplication {
    public static void main(String[] args) { 
        new SpringApplicationBuilder(
            ZuulApplication.class) 
            .web(true).run(args);
    }
}
DZone_GuideToIntegration2016

At this point the Zuul server is up, running, and ready to handle the requests that are registered with Eureka. By default, the Ribbon service will be running as well, which will act as a load balancer for the clients accessing the services through Zuul.

Updating Your Microservice/API

In order for your service to register with Eureka, the @EnableDiscoveryClient annotation will need to be added to your Application class, as shown in this simple example:

@SpringBootApplication
@EnableDiscoveryClient
public class Application {
    public static void main(String[] args) { 
        SpringApplication.run(Application.class, args);
    } 
}
DZone_GuideToIntegration2016

When your service starts, it will register with Eureka, which can then be accessed via the Zuul gateway server. At this point, you will be able to introduce multiple instances of your service, which will be automatically registered and load balanced via Ribbon, which is running as part of the Zuul server.

Adding Hystrix and Turbine Into the Mix

At this point, clients can access your service through the Zuul service. From a monitoring perspective, Hystrix can be added just as easily as the Eureka and Zuul servers were introduced — by starting with a base Spring Boot image and using the @EnableHystrixDashboard annotation.

@SpringBootApplication
@Controller
@EnableHystrixDashboard
public class HystrixDashboardApplication extends 
SpringBootServletInitializer {
   public static void main(String[] args) { 
       new SpringgApplicationBuilder(
           HystrixDashboardApplication.class) 
           .web(true).run(args);
   } 
}
DZone_GuideToIntegration2016

To set up Turbine, add the @EnableTurbineAmqp annotation to another base Spring Boot image:

@SpringBootApplication 
@EnableTurbineAmqp 
@EnableDiscoveryClient
public class TurbineApplication {
    public static void main(String[] args) { 
        new SpringApplicationBuilder(
            TurbineApplication.class).run(args);
    } 
}
DZone_GuideToIntegration2016

Adding Circuit-Breaker Functionality

The Netflix Hystrix service provides circuit-breaker functionality to your service consumer. If the server stops responding, Hystrix can redirect the API call to an internal method in the service itself. This way, your application can handle the scenario when a service is not responding.

DZone_GuideToIntegration2016

Hystrix has the ability to open the circuit and "fast fail" (bypass the service call and simply use the internal method) on every future call until the service becomes available again.

The Hystrix product provides a dashboard, which provides a dynamic list of the services being monitored.

In the image above from the Hystrix dashboard, the getMovie API call is currently not responding, causing the circuit to remain in an Open state, which would trigger calling any con gured fallback methods.

To add the circuit-breaker functionality to your application, the @EnableCircuitBreaker annotation is added to the Spring Boot application for your service. From there, the @HystrixCommand() annotation can be used, similar to the example below:

@HystrixCommand(fallbackMethod = "baseResults")
public ResponseEntity<List<Result>> getResults(Long id) {
    ...
}

public ResponseEntity<List<Result>> getBaseResults(Long 
id) {
    ... 
}
DZone_GuideToIntegration2016

In the example above, when the service call performed in the getResults() method encounters a service/time- out error, the getBaseResults() method will be used until the circuit is closed—which happens once the failed service issue is back online.

Challenges

By this point, your core application or API is running as a Spring Boot application with one or more instances. Those instances are registered with the Eureka service and are being served by requests to the Zuul gateway, which includes Ribbon for load balancing. When a service stops responding, due to network and service timeouts, the Hystrix circuit breaker opens and allows for a fallback method to be utilized. The power and functionality of the Netflix's Spring Cloud project are quite impressive, but it is not without some challenges.

DZone_GuideToIntegration2016

The biggest challenge is the impact on the developer, who has likely become accustomed to working with a single application service instance running. Now, the developer has to make sure at least two additional services (Eureka and Zuul) are running — which adds to the complexity of doing local development work.

Of course, things can get complicated when depending on another service also served by Zuul and Eureka — which may not have the option of running locally. In this case, the developer may need to register the local server instance with Eureka under another name—which can present challenges when used with a centralized configuration service.

Looking Ahead

This article is intended to be a high-level review of the components involved with the Net ix's Spring Cloud project. Discussion around multiple Zuul and Eureka instances were omitted intentionally but would be considerations for Production environments. Additionally, I didn't get deep into the details of Spring Configuration, the use of RabbitMQ, and more functionality of Hystrix and Turbine for the very same reasons.

If your API or microservice has reached the point to where it is a strong candidate for the features and functionality that Netflix's Spring Cloud project provide, I strongly recommend taking time to evaluate the usage of OSS into your solution. Even if the project requires time and effort to convert your service to Spring Boot, the long-term gains should outweigh the short-term costs.

More EI Goodness

For more insight on application integration architectures, REST APIs, microservices, and more, get your free copy of the new DZone Guide to Integration, Volume III!

If you want to see other articles in the guide, check out:

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
integration ,netflix ,spring cloud ,rest api

Published at DZone with permission of John Vester, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}