Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Introduction to the management of Web Services with Spring Cloud and Netflix OSS

In this tutorial we will see an introduction to Spring Cloud and Netflix OSS as tools to implement some of the most common patterns used in distributed systems.

1. Introduction

Given distributed models, it is very common to run into applications that, faced with an increasing number of services, face new challenges in terms of their administration and synchronization. In this regard, some of the most common doubts that we can encounter are usually:

  • How can I ensure that all my services are configured correctly?
  • How can I know what services are deployed and where?
  • How can I keep the routing information of all the services updated?
  • How can I prevent failures due to dropped services?
  • How can I verify that all services are working?
  • How can I ensure which services are exposed externally?

To provide a solution to these scenarios  Spring.io  offers us a set of components that integrated with Netflix OSS tools allows us to easily and quickly develop applications that implement some of the most commonly used patterns in distributed systems.

Some of these patterns are usually:

  • The distributed configuration.
  • Registration and self-recognition of services.
  • Routing
  • Calls service to service.
  • Load balancing.
  • Control of communication break with services.
  • Clusterization
  • Distributed messaging

Given that there are many possibilities that Spring and Netflix offer, in this tutorial we will focus on an example with the following components:

  • Spring Cloud Config: To centrally manage the configuration of our services using a git repository.
  • Netflix Eureka: To register services at runtime in a shared repository.
  • Netflix Zuul: As a server for routing and filtering external requests. Zuul uses Ribbon to search for available services and routes external requests to an appropriate instance of the services.

To visualize how these components are linked, we can look at the following image:

To build a simple example that involves all these components, we will configure a Server with each one of them and a pair of “client” services that verify its operation.

2. Environment

The tutorial is written using the following environment:

  • Hardware : Portable MacBook Pro Retina 15 ‘(2.5 Ghz Intel Core I7, 16GB DDR3).
  • Operating System : Mac OS Sierra 10.12.3
  • Maven – Version: 3.3.9
  • Java – Version: 1.8.0_112
  • Spring Boot – Version: 1.5.1.RELEASE
  • Spring Cloud – Version: 1.3.0.M1

3. Spring Cloud Config

The first thing we will configure is our Spring Cloud Config server, from which our services will obtain a property file. To create our server we will take advantage of the benefits of maven to import the necessary dependencies and those of Spring Boot to have a self-contained server.

In this order of ideas, the first thing we will do is create our maven project where our pom.xml should have the following form:


4.0.0
com.autentia.spring.cloud
SpringCloudConfig-Server
0.0.1-SNAPSHOT


org.springframework.boot
spring-boot-starter-parent
1.5.1.RELEASE


1.8




org.springframework.cloud
spring-cloud-config
1.3.0.M1
pom
import




org.springframework.cloud
spring-cloud-config-server



spring-milestones
Spring Milestones
https://repo.spring.io/libs-milestone

false


Since our Configuration Server is a Spring Boot application we will need a Main and in it we will include the annotation @EnableConfigServer  that will enable it as a Spring Cloud Config server.

Once started our server we can prove that everything is working correctly with our browser http: // localhost: 8888 / health which should report the current status of our server.

4. Spring Cloud Netflix Eureka

The second server that we will incorporate for the support of our services will be  Eureka , with the incorporation of the capacity to automatically discover the services and instances that we are incorporating in execution time. For this and like the previous server we will use Maven and Spring Boot.

In this sense, our pom.xml should be as follows:


4.0.0
com.autentia.spring.cloud
SpringCloudNetflix-EurekaServer
0.0.1-SNAPSHOT


org.springframework.boot
spring-boot-starter-parent
1.5.1.RELEASE


1.8




org.springframework.cloud
spring-cloud-netflix
1.3.0.M1
pom
import




org.springframework.cloud
spring-cloud-starter-eureka-server


org.springframework.boot
spring-boot-starter-web


org.springframework.boot
spring-boot-starter-actuator


org.springframework.boot
spring-boot-starter-test



spring-milestones
Spring Milestones
https://repo.spring.io/libs-milestone

false



Next step to the inclusion of the necessary dependencies to enable our Eureka server, we must create our ApplicationEurekaServer.java and include the annotation @EnableEurekaServer  .

package com.autentia.spring.cloud.netflix.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class ApplicationEurekaServer {

public static void main(String[] args) throws Exception {
SpringApplication.run(ApplicationEurekaServer.class, args);
}

}

To configure our server we will include an application.yml file inside the folder / src / main / resources / , and in which we will configure the name of the application, the port where it will be executed and we will indicate that it should not be published on another eureka server. the registerWithEureka property in false.

Once started our server we can prove that everything is working correctly with our browser http: // localhost: 8761 / health which should report the current status of our server.

We can also navigate the graphical interface of Eureka ( http: // localhost: 8761 / ) and validate the services that are being incorporated.

5. Spring Cloud Netflix Zuul

As the last of the support servers for our web services, we will configure Zuul’s services as a gateway for the services we want to publish.

For this and as in the previous we will use Maven and Spring Boot where the pom.xml should look like this:


4.0.0
com.autentia.spring.cloud
SpringCloudNetflix-ZuulServer
0.0.1-SNAPSHOT


org.springframework.boot
spring-boot-starter-parent
1.5.1.RELEASE


1.8




org.springframework.cloud
spring-cloud-netflix
1.3.0.M1
pom
import




org.springframework.cloud
spring-cloud-starter-eureka


org.springframework.cloud
spring-cloud-starter-zuul


org.springframework.boot
spring-boot-starter-web


org.springframework.boot
spring-boot-starter-actuator




spring-milestones
Spring Milestones
https://repo.spring.io/libs-milestone

false



To enable the Zuul service in our Main class we will include the annotation @EnableZuulProxy .

package com.autentia.spring.cloud.netflix.zuul;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.stereotype.Controller;

@SpringBootApplication
@Controller
@EnableZuulProxy
public class ApplicationZuul {

public static void main(String[] args) {
new SpringApplicationBuilder(ApplicationZuul.class).web(true).run(args);
}
}

Finally and like the other servers we will include an application.yml to configure our server where we must highlight the routing for the requests to our example public service that we identify with its Service Id:   public-restservice.

Once our server is started, we can prove that everything is working by validating that it has been registered against our Eureka server previously started.

6. Test Rest Services with Spring Boot

Finally and to be able to test how our solution is synchronized we are going to create a couple of web services that take a random port and register automatically against our Eureka server.

As with the servers we will use Maven and Spring Boot to facilitate our implementation. Among the dependencies that we will include for the services will be those of the Eureka Starter as a client and the Starter Web to enable them as REST controllers.


4.0.0
com.autentia.spring
SpringCloudNetflix-Public-RestService
0.0.1-SNAPSHOT


org.springframework.boot
spring-boot-starter-parent
1.5.1.RELEASE


1.8




org.springframework.cloud
spring-cloud-netflix
1.3.0.M1
pom
import


org.springframework.cloud
spring-cloud-config
1.3.0.M1
pom
import




org.springframework.cloud
spring-cloud-starter-config


org.springframework.cloud
spring-cloud-starter-eureka


org.springframework.boot
spring-boot-starter-web


org.springframework.boot
spring-boot-starter-actuator


org.springframework.boot
spring-boot-starter-test



spring-milestones
Spring Milestones
https://repo.spring.io/libs-milestone

false


Since it is a Spring Boot application we will need a Main with the option to include the annotation @EnableDiscoveryClient that will allow our services to be subscribed automatically in the Eureka server.

package com.autentia.spring.cloud.netflix;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class ApplicationRestService {

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

To enable an example for our services we will include a class that responds to the mapping / example :

Once the services are started we can check that everything is working on our Eureka server, in it we can see how many instances of our services have been registered and under what port we can invoke them.

Note: To make our test more rigorous we can start a second instance of one of our services and check the number of instances that are available from the same service.

Finally, and to prove that the public service is accessible from outside through our Zuul server, we can visit  http: // localhost: 8765 / public / example  and check that our service configuration is responding correctly.

7. Conclusions

We have seen how the Spring Cloud and Netflix OSS components can be used to simplify the management and synchronization of web services.

The post Introduction to the management of Web Services with Spring Cloud and Netflix OSS appeared first on Target Veb.



This post first appeared on Targetveb, please read the originial post: here

Share the post

Introduction to the management of Web Services with Spring Cloud and Netflix OSS

×

Subscribe to Targetveb

Get updates delivered right to your inbox!

Thank you for your subscription

×