This sample application implements a service discovery client using Netflix open source Ribbon project.
It's a Spring based application. It uses Spring Boot to start Spring context and run the application and Spring Cloud Netflix to integrate Netflix Ribbon implementation into Spring.
##Version
- Spring Boot 1.4.0
- Spring Cloud 1.1.5
In order to use service discovery pattern in a common Spring Boot application only three steps are needed:
-
Add Spring Cloud dependencies:
org.springframework.cloud spring-cloud-starter-eureka 1.1.5.RELEASE org.springframework.cloud spring-cloud-starter-hystrix 1.1.5.RELEASE
These dependencies add Ribbon client classes to the project as well as Neflix circuit breaker implementation: Hystrix.
-
Enable Service discovery during Spring Boot startup using the annotation
@EnableEurekaClient
and circuit breaker using annotation@EnableCircuitBreaker
on the main class:@SpringBootApplication @EnableEurekaClient @EnableCircuitBreaker @RestController public class SngDemoConsumerApplication {
... } -
Add some configuration. Two configuration files are needed:
bootstrap.yml
spring:
application:
name: consumer
application.yml
server:
port: 8002
eureka:
client:
serviceUrl:
defaultZone: http://eureka-server:8000/eureka/
These parameters tell the application where the service registry is available. The first time the application tries to call another service, Ribbon will try to resolve service name to a list of URLs calling any Eureka Server configured in these parameters.
The application starts as a normal Spring Boot application:
- Run
mvn install
inside the proeject - Go to
target
folder - Run
java -jar sng-demo-service-consumer-1.0.0-SNAPSHOT.jar
- Optional: If you're using STS IDE you can use a view called
Boot Dashboard
to start/stop any Spring Boot project you have in your workspace