Using Netflix Eureka with Spring Cloud / Spring Boot microservices (part 2)

Several months back I started to look at setting up a simple example app with Spring Boot microservices using Netflix Eureka as a service registry. I got distracted by other shiny things for a few months, but just went back to finish this off.

The example app comprises of 3 Spring Boot apps:

  • SpringCloudEureka: registers the Eureka server using @EnableEurekaServer
  • SpringBootService1 with endpoint POST /service1/example1/address
    • registers with Eureka server with @EnableDiscoveryClient
    • uses Ribbon load balancer aware RestTemplate to call Service2 to validate a zipcode
  • SpringBootService2 provides endpoint GET /service2/zip/{zipcode} which is called by Service1
    • also registers with Eureka server with @EnableDiscoveryClient so it can be looked up by Service1

SpringBootService1 and SpringBootService2 both register with the Eureka server using the annotation @EnableDiscoveryClient. Using some magic with @EnableFeignClients, SpringBootService1 is able to call SpringBootService2 using a regular Spring RestTemplate, but it is Eureka aware and able to lookup SpringBootService2 by service name inplace of an absolute ip and port.

This allows the services to be truly decoupled. Service1 needs to know it needs to call Service2 to perform some purpose (in this case validate a zip code), but it doesn’t need to know where Service2 is deployed or what ip address/port it is available on.

Example code is available on github here.

Using Netflix Eureka with Spring Cloud / Spring Boot microservices

I’ve been taking a look at this article on using Spring Cloud‘s integration/support for Netflix Eureka. I’ve started to put together a simple example using Eureka as a service registry for a couple of Spring Boot services, and what this would look like if deployed in Docker containers.

So far I’ve created the initial service that uses Spring Cloud’s @EnableEurekaServer annotation to start up the Eureka service.

Jumping ahead of the instructions, by default if you run this app it will attempt to reach out and find a local running Eureka server and register with it, but since this app is the Eureka server, you need to add config to tell the app not to do this by default. Otherwise you’ll see errors like:

com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused
at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:187) ~[jersey-apache-client4-1.19.1.jar:1.19.1]
at com.sun.jersey.api.client.filter.GZIPContentEncodingFilter.handle(GZIPContentEncodingFilter.java:123) ~[jersey-client-1.19.1.jar:1.19.1]
at com.netflix.discovery.EurekaIdentityHeaderFilter.handle(EurekaIdentityHeaderFilter.java:27) ~[eureka-client-1.6.2.jar:1.6.2]

Adding the recommended config per the article:

server:

  port: ${PORT:8761}

eureka:

  client:

    registerWithEureka: false

    fetchRegistry: false

    server: waitTimeInMsWhenSyncEmpty: 0

Now when I start up I see this:

2017-04-11 22:23:17.040  INFO 37607 - o.s.c.n.eureka.InstanceInfoFactory       : Setting initial instance status as: STARTING
2017-04-11 22:23:17.100  INFO 37607 - com.netflix.discovery.DiscoveryClient    : Initializing Eureka in region us-east-1
2017-04-11 22:23:17.101  INFO 37607 - com.netflix.discovery.DiscoveryClient    : Client configured to neither register nor query for data.
2017-04-11 22:23:17.110  INFO 37607 -com.netflix.discovery.DiscoveryClient    : Discovery Client initialized at timestamp 1491974597110 with initial instances count: 0
2017-04-11 22:23:17.192  INFO 37607 - c.n.eureka.DefaultEurekaServerContext    : Initializing ...
2017-04-11 22:23:17.195  INFO 37607 - c.n.eureka.cluster.PeerEurekaNodes       : Adding new peer nodes [http://localhost:8761/eureka/]
2017-04-11 22:23:17.359  INFO 37607 - c.n.d.provider.DiscoveryJerseyProvider   : Using JSON encoding codec LegacyJacksonJson
2017-04-11 22:23:17.359  INFO 37607 - c.n.d.provider.DiscoveryJerseyProvider   : Using JSON decoding codec LegacyJacksonJson
2017-04-11 22:23:17.359  INFO 37607 - c.n.d.provider.DiscoveryJerseyProvider   : Using XML encoding codec XStreamXml
2017-04-11 22:23:17.359  INFO 37607 - c.n.d.provider.DiscoveryJerseyProvider   : Using XML decoding codec XStreamXml
2017-04-11 22:23:22.479  INFO 37607 - c.n.eureka.cluster.PeerEurekaNodes       : Replica node URL:  http://localhost:8761/eureka/
2017-04-11 22:23:22.486  INFO 37607 - c.n.e.registry.AbstractInstanceRegistry  : Finished initializing remote region registries. All known remote regions: []
2017-04-11 22:23:22.486  INFO 37607 - c.n.eureka.DefaultEurekaServerContext    : Initialized

Hitting http://localhost:8761 I get the fancy Eureka dashboard:

Looks good so far! More to come later.

Github repo for the code so far is here.