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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Component Tests for Spring Cloud Microservices
  • A Robust Distributed Payment Network With Enchanted Audit Functionality - Part 2: Spring Boot, Axon, and Implementation
  • 7 Microservices Best Practices for Developers

Trending

  • Scalable, Resilient Data Orchestration: The Power of Intelligent Systems
  • Accelerating AI Inference With TensorRT
  • Unlocking AI Coding Assistants Part 1: Real-World Use Cases
  • Rethinking Recruitment: A Journey Through Hiring Practices
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Using Netflix OSS + Spring Cloud Netflix: Part 1

Using Netflix OSS + Spring Cloud Netflix: Part 1

Learn how to use Netflix's open source components on its Spring Cloud to build a scalable distributed system of microservices.

By 
Ramzi Yassine user avatar
Ramzi Yassine
·
Aug. 07, 17 · Tutorial
Likes (12)
Comment
Save
Tweet
Share
22.5K Views

Join the DZone community and get the full member experience.

Join For Free

Netflix OSS and Spring Cloud Netflix

Netflix was one of the first companies to adopt microservices architecture, and they have built very interesting blocks for managing and implementing a microservices platform. To repay the open source community for their help getting started, they open sourced all the components used to make Netflix a leader in Internet television.

Spring, as always and to keep up with the crowd, made Spring Cloud Netflix, to make it easy for Spring Boot projects to integrate the Netflix OSS. With a few simple annotations, you can integrate all the patterns used in Netflix OSS and build a scalable performant distributed system.

Example of a Microservices Product and Details

For this example, we will use a traditional example, managing products ordering and their details, in the functional diagram:

Image title

  • Product: our entry point to the Rest APIs.
  • Details: details about the product (stock, price, name).
  • Orders: make an order about a product (need to verify the stock before making the order).

Architecture and Netflix OSS

technical architecture

In this part, we will discuss the technical architecture that we will use in this example, and the Netflix OSS blocks that we will base our architecture on.

  • Eureka Service Discovery: services registry.
  • Zuul Intelligent routing (proxy for our micro services): the Product API that we see in the functional diagram.
  • Feign: to make one service consume another, we will use the Feign java to Http Client, we will talk later about this client.

These are the components that we will use for this first article, to not submerge the reader with too much information.

Eureka Server

On https://start.spring.io/, create a Eureka server. For this article, we will be using Maven:

  • Search for the Eureka Server dependency:

Image title

Click on Generate Project, and the Zip of the project will be downloaded. Extract the project and import it using your IDE (I am using Eclipse).

Usually, when starting an instance of a Eureka Service, it is launched as a server to register services, as well as a client to be registered in other Peers. For this example, we will use a Standalone Eureka Server.

For this reason, we will stop the client behavior on it, so it doesn’t keep looking for its peers and failing to reach them.

Communication Between Eureka's Client and Server

The Client that is, in our case, a microservice, is sending heartbeats to the server every 30 seconds to tell it that it's reachable.

The Client has a cache of all the other Clients, so if the server is down, the client can use its cache to retrieve another client.

Server Configuration

First of all, import your project in your IDE.

Modify the application.properties:

spring.application.name=EurekaServer

server.port=8761

# the hostname
eureka.instance.hostname=localhost

# Eureka server time to sync with other peers, 
# usually it's 3 min and it will retry if it doesnt
# get any response but for developpement we will put it to 0
eureka.numberRegistrySyncRetries=0

#############################################
# EUREKA CLIENT CONFIGURATION#
#############################################
# the Eureka server has the same host as ours 
# so we do this to stop the server from registring as it's own peer
eureka.client.service-url.default-zone=http://${eureka.instance.hostname}:${server.port}/eureka/

# To stop it from trying to register to an Eureka Server
eureka.client.register-with-eureka=false

# To stop fetching registry information about other services from other peers
eureka.client.fetch-registry=false


Modify the bootstrap class of your class by adding @EnableEurekaServer above @SpringBootApplication:

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

@EnableEurekaServer
@SpringBootApplication
public class EurekaServiceApplication {

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

Now you have just to launch your project : 

# windows
mvnw spring-boot:run

# linux and mac
./mvnw spring-boot:run

Your Eureka server will be available at http://localhost:8761/.

Image title

Order Service

Using start.spring.io, create an Order Service with the following dependencies :

  • Eureka Discovery: to discover all the microservices in the hood (Eureka Client).
  • JPA: to manage data.
  • H2: an in-memory database.
  • Feign: REST Client to call other REST services.
  • Rest Repositories: to expose JPA repositories as REST Endpoints.
  • Web: Spring MVC and embedded Tomcat.
  • Lombok: to generate setters and getters, constructors, and other boilerplate code.

Image title

This is the code:

The model Order.java:

package com.orders.models;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Entity
public class Order {
    @Id
    @GeneratedValue
    private Long id;

    private int quantity;

    private double price;
}

The Spring data Repository:

package com.orders.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import com.orders.models.Order;
@RepositoryRestResource
public interface OrderRepository  extends JpaRepository<Order , Long> {

}

The bootstrapping class of the application:

package com.orders.ordersservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
@SpringBootApplication
public class OrdersServiceApplication {

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

The @EnableEurekaClient is the Eureka client annotation that will add the client behavior to our microservice.

And last but not least, the application.properties file:

server.port=8081
spring.application.name=orders-service
#we tell the Eureka Client the sever url
eureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka/
# the eureka client will send heart beats every 10 seconds, default is 30 seconds
eureka.instance.leaseRenewalIntervalInSeconds: 10
# An Eureka instance is indentified by eureka.instance.instanceId 
# if not used eureka.instance.metadataMap.instanceId will be it should be unique
eureka.instance.metadataMap.instanceId: ${spring.application.name}:${spring.application.instance_id:${server.port}:${random.value}}

Now you should just start your microservice and it will be registered in the Eureka Server:

Start your microservice using

# windows
mvnw spring-boot:run

# linux and mac
./mvnw spring-boot:run

And this the final result:

Image title

Our Orders-service is registered!

Conclusion

In this first article, we showed how to create our Eureka server and how to register a microservice to it. In the next article, we will show how to consume other microservices, how to make Zuul our entry point, and how to load some dynamic filters from Cassandra for our Zuul.

This repo will contain all the source code and configurations of this example.

Spring Framework Spring Cloud microservice

Opinions expressed by DZone contributors are their own.

Related

  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Component Tests for Spring Cloud Microservices
  • A Robust Distributed Payment Network With Enchanted Audit Functionality - Part 2: Spring Boot, Axon, and Implementation
  • 7 Microservices Best Practices for Developers

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!