Click here for understanding sample Microservice – Forex Service (Currency Exchange)

Click here for understanding sample Currency Conversion Service that makes a rest call internally to Currency Conversion Microservice and also we can see on how to use Feign Client for calling microservice.

Click here for understanding Ribbon load balancing

Click here for understanding Eureka Naming server 

Spring Cloud Config :

Configuration for Multiple Environments in Git Repository
/git-localconfig-repo/limits-service-dev.properties New
limits-service.minimum=1
limits-service.maximum=111
/git-localconfig-repo/limits-service-qa.properties New
limits-service.minimum=2 limits-service.maximum=222
/git-localconfig-repo/limits-service.properties New
limits-service.minimum=8
limits-service.maximum=888

Configuring SpringConfigServer :

@EnableConfigServer
@SpringBootApplication
public class SpringCloudConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudConfigServerApplication.cl
ass, args);

Configuring Application.properties (/spring-cloud-configserver/src/main/resources/application.properties )

spring.application.name=spring-cloud-config-server
server.port=8888
spring.cloud.config.server.git.uri=file:///spring-micro-services/microservices/git-localconfig-repo

Connecting MicroService to Spring Cloud Config Server 

application.properties of microservice:

spring.application.name=limits-service
spring.cloud.config.uri=http://localhost:8888

bootsrap.properties of microservice:

spring.profiles.active=qa

Spring Config Client :

Microservices that invoke SpringConfigServer are the clients. We need to add dependency of config client .

Feign Client :

How to configure:

  1. Add Dependency -> Add Feign as Dependency
  2. Enabling Feign
    @SpringBootApplication
    @EnableFeignClients
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    

3. Configuring proxy.

@FeignClient(name=”forex-service” url=”localhost:8000″)
public interface CurrencyExchangeServiceProxy {
@GetMapping(“/currency-exchange/from/{from}/to/{to}”)
public CurrencyConversionBean retrieveExchangeValue
(@PathVariable(“from”) String from, @PathVariable(“to”) String to);

Ribbon :

How to configure:

  1. Add Dependency -> Add Ribbon as Dependency
  2. Enabling Ribbon :
    // @FeignClient(name="forex-service" url="localhost:8000")
    @FeignClient(name="forex-service")
    @RibbonClient(name="forex-service")
    public interface CurrencyExchangeServiceProxy {
    

Here using Feign we are calling forex-service from CurrencyExchangeService. The commented line with Feign client has the hardcoded URL . Ribbon solves this problem so that load can be distributed incase of multiple instances of Microservices.

3.  Configure the instances in application.properties

forex-service.ribbon.listOfServers=localhost:8000,localhost:8001

 

Eureka Naming Server:

How to configure:

  1. Add Dependency -> Add Eureka Sever as Dependency
  2. Enabling Naming Server :                                                                          @SpringBootApplication                                                                      @EnableEurekaServer
    public class NetflixEurekaNamingServerApplication {
    public static void main(String[] args) {
    SpringApplication.run(NetflixEurekaNamingServerApplication.
    class, args);
    }
  3. Configuring application.properties
    spring.application.name=netflix-eureka-naming-server
    server.port=8761
    eureka.client.register-with-eureka=false
    eureka.client.fetch-registry=false

Connecting Microservice to Eureka : 

  1. Dependency in pom.xml                                                                                    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eurekaclient</
    artifactId>
    </dependency>
  2. Configuring application.properties                                                            eureka.client.service-url.defaultzone= http://localhost:8761/eureka
    #currency-exchangeservice.ribbon.listOfServers=http://localhost:8000,http://l
    ocalhost:8001  (See the commented section that were hardcoded and naming server solves this proplem)
  3. Enabling Discovery Client on the Microservice                                                       @SpringBootApplication
    @EnableDiscoveryClient
    public class CurrencyConversionServiceApplication {

Spring Cloud Sleuth :

Following are the steps needed to configure Spring Cloud Sleuth

  1. Adding dependency

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

2. Defining sampler bean @Bean
public Sampler
defaultSampler(){
return Sampler.ALWAYS_SAMPLE;
}

Fault Tolerance with Hystrix

  1. Creating Dependency .

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

  2.  Implementing Fall back feature

    @RestController
    public class LimitsConfigurationController {
    @GetMapping(“/fault-tolerance-example”)
    @HystrixCommand(fallbackMethod=”fallbackRetrieveConfiguration”)
    public LimitConfiguration retrieveConfiguration() {
    throw new RuntimeException(“Not available”);
    }
    public LimitConfiguration fallbackRetrieveConfiguration()
    {
    return new LimitConfiguration(999, 9);
    }
    }

  3. Enabling Hystrix

    @SpringBootApplication
    @EnableHystrix
    public class LimitsServiceApplication {