Microservice Resiliency

Eureka

https://github.com/jeffgbutler/microservice-workshop-kotlin/

https://jeffgbutler.github.io/microservice-workshop-kotlin/

What is Eureka?

  • Eureka is a service discovery system
  • Eureka originated at Netflix
  • Spring Cloud Netflix has integrated Eureka into Spring
  • Web services register themselves with a Eureka Server
  • Web service clients look up service addresses from the Eureka server
  • This solves the problem of hard-coded URLs in the clients

Configuring a Eureka Server

  • "Eureka Server" is a dependency in the Spring Initializr
  • Build a new project with only the "Eureka Server" dependency
  • Add @EnableEurekaServer to the main Spring Boot class
  • That's, basically, it
  • Eureka server is also built into Pivotal Cloud Foundry with the Spring Cloud Services tile

Configuring Eureka Clients

  • "Eureka Discovery" is the dependency in the Spring Initializr
  • Web services are automatically registered with a Eureka server
  • Feign clients will lookup endpoints from Eureka if we simply remove the url attribute of @FeignClient
  • REST Template clients will lookup endpoints from Eureka through a >DiscoveryClient
  • That's, basically, it

Use of Discovery Client with REST Template

          
            @Service
            class MovieCastService(private val template: RestTemplate, private val discoveryClient: DiscoveryClient) {
            
                fun findCastMembers(movieId: Int): List<CastMember> {
                    val url = discoveryClient.getInstances("movie-cast-service")
                            .firstOrNull()?.uri?.toString()
                            ?: throw IllegalStateException("movie-cast-service not available")
              
                    val uri = UriComponentsBuilder.fromHttpUrl(url)
                            .pathSegment("cast")
                            .pathSegment("search")
                            .queryParam("movieId", movieId)
                            .toUriString()
            
                    val ent = template.exchange(uri, HttpMethod.GET, null, object : ParameterizedTypeReference<List<CastMember>>() {})
                    return ent.body ?: emptyList()
                }
            }
          
        

Service Names and Granularity

  • The name of the service is taken from the spring.application.name property. This property is specified in bootstrap.yml by convention
  • The name attribute of a @FeignClient annotation specifies which service to lookup
  • This is a lookup for a base URL for all services in an application - it is not a granular service catalog

What is Ribbon?

  • Ribbon, another Netflix project, is a client side load balancer
  • Ribbon is a transitive dependency of Eureka Discovery (it comes along for free)
  • Multiple service instances can register themselves in Eureka with the same name
  • When there are multiple service instances, Ribbon will load balance between them
  • If you have another load balancing strategy (like PCF scaling), then you can disable Ribbon on the clients
  • Ribbon is in maintenance mode currently and will be replaced by Spring Cloud Loadbalancer. This should be transparent to most users

Exercise

  • Create a Eureka Server
  • Alter the services so they will register with the Eureka server
  • Alter the aggregator service so it will lookup endpoints from the Eureka server