Thursday, December 26, 2019

Microservices - Part 8 | Building a Microservice using Spring Cloud Config Server + Spring Cloud Bus + Feign Rest Client + Ribbon + Zuul + Spring Cloud Sleuth and Zipkin + Eureka + Hysterix

Ports

ApplicationPort
Limits Service8080, 8081, ...
Spring Cloud Config Server8888
Currency Exchange Service8000, 8001, 8002, ..
Currency Conversion Service8100, 8101, 8102, ...
Netflix Eureka Naming Server8761
Netflix Zuul API Gateway Server8765
Zipkin Distributed Tracing Server9411

URLs

ApplicationURL
Limits Servicehttp://localhost:8080/limits POST -> http://localhost:8080/actuator/refresh
Spring Cloud Config Serverhttp://localhost:8888/limits-service/default http://localhost:8888/limits-service/dev
Currency Converter Service - Direct Callhttp://localhost:8100/currency-converter/from/USD/to/INR/quantity/10
Currency Converter Service - Feignhttp://localhost:8100/currency-converter-feign/from/EUR/to/INR/quantity/10000
Currency Exchange Servicehttp://localhost:8000/currency-exchange/from/EUR/to/INR
http://localhost:8001/currency-exchange/from/USD/to/INR
Eurekahttp://localhost:8761/
Zuul - Currency Exchange & Exchange Serviceshttp://localhost:8765/currency-exchange-service/currency-exchange/from/EUR/to/INR
 http://localhost:8765/currency-conversion-service/
currency-converter-feign/from/USD/to/INR/quantity/10
Zipkinhttp://localhost:9411/zipkin/
Spring Cloud Bus Refreshhttp://localhost:8080/bus/refresh
Git :

/03.microservices/git-localconfig-repo/limits-service.properties
limits-service.minimum=8
limits-service.maximum=888
management.endpoints.web.exposure.include=*


Steps to Create a Microservice :

Step 1 : Create a Centralized Microservice Configuration with Spring Cloud Config Server
Dependency : spring-cloud-config-server
Annotation : @EnableConfigServer
application.properties :
spring.application.name=spring-cloud-config-server
server.port=8888
spring.cloud.config.server.git.uri=file:///in28Minutes/git/spring-micro-services/03.microservices/git-localconfig-repo

For Fetching data from this config server add the following in bootstrap.properties for the respective service :
spring.application.name=limits-service
spring.cloud.config.uri=http://localhost:8888
spring.profiles.active=qa
management.endpoints.web.exposure.include=*


Step 2 : Using Spring Cloud Bus to exchange messages about Configuration updates
Implementing a refresh URL to dynamically load properties without restarting respective services.
Dependency : spring-cloud-starter-bus-amqp (to be included in all projects which needs to be interconnected.)
URL :  http://localhost:8080/bus/refresh

Refresh one url, rest all will receive refresh message from rabbitmq or Spring Cloud bus.

Step 3 : Implementing Eureka Server
Dependency : spring-cloud-starter-netflix-eureka-server
Annotation : @EnableEurekaServer
application.properties :
spring.application.name=netflix-eureka-naming-server
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

Step 4: Implementing API Gateway with Zuul
Dependency : spring-cloud-starter-netflix-zuul
Annotation : 
@EnableZuulProxy
@EnableDiscoveryClient
application.properties :
spring.application.name=netflix-zuul-api-gateway-server
server.port=8765
eureka.client.service-url.default-zone=http://localhost:8761/eureka


Step 5 : Implementing Services : Conversion Service -> Exchange Service
Annotation: @EnableDiscoveryClient , @EnableFeignClients("com.in28minutes.microservices.currencyconversionservice")

PS: Create a proxy for calling exchange service from conversion service with automatic detection :
@FeignClient(name="netflix-zuul-api-gateway-server")
@RibbonClient(name="currency-exchange-service")

Step 6 : Enable Distributed Tracing via Zipking and Sleuth
Add Following Bean in gateway + all services :
@Bean
public Sampler defaultSampler(){
return Sampler.ALWAYS_SAMPLE;
}

Plus the following dependency :
                <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

Step 6.1 : Install rabbit mq and zipkin server.

Step 6.2 : Start a zipkin server :

Annotation : @EnableZipkinServer
dependency : 
                <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin-stream</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>

<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
<scope>runtime</scope>
</dependency>

 application.properties :

spring.application.name=zipkin-distributed-tracing-server
server.port=9411

Step 7: Implementing Fault Tolerance with Hysterix
Annotation : @EnableHystrix
@HystrixCommand(fallbackMethod="fallbackRetrieveConfiguration")
dependency :
                 <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>


Ref : https://github.com/in28minutes/spring-microservices/tree/master/03.microservices

No comments:

Post a Comment