Spring Cloud - Eureka
Let’s learn Service Discovery and use Eureka as a service registry.
Service Discovery
Nowadays services are usually running on the cloud and have dynamic network locations. Network locations changes when service is restarted or upgraded. Locating such service with a configuration file just doesn’t work. Service Discovery can solve this problem
There are two main service discovery patterns:
- client‑side discovery - application service register its location to central registry. The client queries service registry to get the available service location and then make a request to one of the available service. The Biggest drawback is clients has to know how to talk to the service registry and this creates tight coupling between service registry and clients. Example Client-side discovery registry are Zookeeper, Etcd
- server‑side discovery - The client makes a request to a service via load balancer. The load balancer queries the service registry and routes each request to an available service instance. AWS Elastic Load Balancer(ELB) is an example of server-side discovery router.
Eureka
Eureka is 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.
Eureka is an example of client-side discovery service registry.
Remember that Eureka 1.x is still active. The work on Eureka 2.0 is discontinued.
Eureka Comes with Two Components
- Eureka Server
- Eureka Client
There are two types of applications that uses Eureka
- Application Client - use Eureka Client to make requests to the Application Service.
- Application Service - receives requests from Application Client and sends a response back.
Eureka Server
pom.xml
1 | <properties> |
main application - @EnableEurekaServer
annotation shows it is a Eureka Server
1 | import org.springframework.boot.SpringApplication; |
application.properties file
1 | server.port=8761 |
Properties explained
eureka.client.register-with-eureka
Register itself as a Client, default is trueeureka.client.fetch-registry
fetch registry from eureka server, default is trueeureka.client.serviceUrl.defaultZone
comma separated list of peers
Now Eureka Server is running at http://localhost:8761/
Eureka Clients
All clients need to have eureka-client dependency in pom.xml
1 | <dependency> |
ClientAController.java - A very simple Controller
1 |
|
ClientA application.properties file
1 | spring.application.name=eureka-client-a |
@EnableEurekaClient
can be added to the main class, but this is optional.
ClientBController.java - note that it can refer to clientA host using the registered application name.
1 |
|
ClientB application.properties file
1 | spring.application.name=eureka-client-b |
Configuration for RestTemplate dependency. It has @LoadBalancered annotation so that it can resolve application host name and do load balance.
1 |
|
output for URL http://localhost:8001/getMsgFromClientA
1 | Message From ClientA: message from server1. |
Feign Rest Client
OpenFeign is a declarative REST client for Spring Boot apps. Feign makes writing java http clients easier. You can use Feign to replace RestTemplate.
Maven Dependency
1 | <dependency> |
EurekaClientBApplication.java - add @EnableFeignClients
annotation to top level Spring boot Application class.
1 |
|
Create FeignClient Interface. Feign Client has @FeignClient
annotation.
1 |
|
ClientBController class - replace RestTemplate with Feign client.
1 |
|
Feign supports Hystrix. To enable Hystrix, add feign.hystrix.enabled=true
to application.properties file. You can also implement fallback method when the service call fails. For more information on OpenFeign, see Spring Cloud OpenFeign by Baeldung
Eureka Server Cluster
In production, we usually need to have 3 or more instances of registration service for High Availability. Each eureka service knows each other.
application.yml file with 3 different profiles.
1 |
|
Add the following to /etc/hosts file when you are running the cluster in local machine.
1 | 127.0.0.1 peer1 |
Spring Cloud Eureka Docs
Reference