Netflix OSS: A beginner's guide [pt1]

Netflix OSS: A beginner's guide [pt1]

In this series of posts, I'll try to get you inside the Netflix stack: understand how it works and get your feet wet in the microservices world.

So, to begin, we need to know the first component of the Netflix stack: Eureka

The Eureka is described by Netflix as

a REST based service that is primarily used in the AWS cloud for locating services for the purpose of load balancing and failover of middle-tier servers. We call this service, the Eureka Server. Eureka also comes with a Java-based client component,the Eureka Client, which makes interactions with the service much easier. The client also has a built-in load balancer that does basic round-robin load balancing.

So basically, Eureka is a register, that will know where which one of our services lives, how many instances of they are up (or down) and how to access them.

Thanks to Spring, all the complexity to get a Eureka server up and running was wrapped inside useful libraries that we'll be using in this series.

So, enough with chit chat, let's get to work.

Creating an Eureka Server using Spring

Get a Eureka Server to run is unbelievable easy. First, visit http://start.spring.io and fill the form as follow:

Start

Pay attention to "Eureka Server" dependency listed.
Now, download the project clicking "Generate Project" and import it to your IDE (if you are using Eclipse, you can "mavenize" it running mvn eclipse:eclipse on root).

Now, if you open it, you'll see an empty Spring Boot project, then, go to your application.yml (or application.properties if you prefer) and change as follow:

server:  
  port: 8761
eureka:  
  numberRegistrySyncRetries: 1
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  server:
    enable-self-preservation: true

Explaining the most important lines:

server:  
  port: 8761

Here, we are configuring the Eureka Server to run on port 8761. This is the default port and you can change, but you need to give this port to the clients later on.

eureka:  
  numberRegistrySyncRetries: 1

If you are running locally, there is a 2 to 3 minutes wait until fulling boot up. This happens because Eureka will be looking for peers. To disable this, set to 0 (although you should never do this in production)

  client:
    registerWithEureka: false

As this is the Eureka Server, we do not want it to register itself. Will always be set to false on server and true on the clients.

serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

the defaultZone is the fallback URL for every client that doesn't specify a preference for a server.

Now, the only thing we should do is enable the Eureka Server.
To do so, go to the main class (at this point, you should only have one class in the project, though) and annotate it with @EnableEurekaServer, as follow

@EnableAutoConfiguration
@EnableEurekaServer
public class EurekaServerApplication {

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

and start the server with mvn spring-boot:run

the latest log lines would be something like this

2016-06-07 21:34:38.624  INFO 723 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
2016-06-07 21:34:38.627  INFO 723 --- [           main] .p.EurekaConfigBasedInstanceInfoProvider : Setting initial instance status as: STARTING
2016-06-07 21:34:39.120  INFO 723 --- [           main] com.netflix.discovery.DiscoveryClient    : Not registering with Eureka server per configuration
2016-06-07 21:34:39.127  INFO 723 --- [           main] c.n.e.EurekaDiscoveryClientConfiguration : Registering application bootstrap with eureka with status UP
2016-06-07 21:34:39.202  WARN 723 --- [       Thread-2] c.n.eureka.PeerAwareInstanceRegistry     : The replica size seems to be empty. Check the route 53 DNS Registry
2016-06-07 21:34:39.362  INFO 723 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8761 (http)
2016-06-07 21:34:39.366  INFO 723 --- [           main] com.inkdrop.EurekaServerApplication      : Started EurekaServerApplication in 12.2 seconds (JVM running for 18.242)

Access http://127.0.0.1:8761 and you'll see this:

Eureka

and that's it! Your Eureka Server is up and running.
In the next post, we'll create a microservice that will register itself on Eureka.

If you want to see the code already, go to the final github project

🍻 🍻

Icons created by Freepik - Flaticon