2
votes

I am setting up a micro service based application, where Aggregation layer / API gateway makes calls to micro services. Eureka is used for service discovery and Ribbon for providing a load balancing RestTemplate.

Postman calls Aggregation--> Aggregation calls Micro service using Eureka/Ribbon/RestTemplate.

I have 4 instances of one micro services type running on my machine on 4 different ports. Hitting the same REST endpoint repeatedly Postman causes the requests to get load balanced properly in a round robin fashion.

When I stop one of the micro service instances the service is deregistered from Eureka, but the LoadBalancer still sends requests to the dead service and the call fails.

Below is my code:

Aggregation:

 @Configuration
    @ComponentScan(basePackages = {"com.mycompany.aggregator"})
    @EnableAutoConfiguration
    @EnableEurekaClient
    public class AggregatorApplication {

        public static void main(String[] args) {
            SpringApplication.run(AggregatorApplication.class, args);
        }
    }

**Configuration:**
@Configuration
public class AggregatorConfig {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

@Configuration
@RibbonClient(name="microservice", configuration = FooConfig.class)
public class TestConfig {
}

//FooConfig is excluded from component-scan

@Configuration
public class FooConfig {

    @Bean
    public IPing ribbonPing(IClientConfig config) {
        return new NIWSDiscoveryPing();
    }

    @Bean
    public IRule ribbonRule(IClientConfig config) {
        return new AvailabilityFilteringRule();
    }
}

restTemplate call:

ResponseEntity<Object> responseEntity = restTemplate.getForEntity(myUrl, Object.class);

Properties:

spring.application.name=aggregator
server.contextPath=/ott
server.port = 8090
my.url=http://microservice
eureka.instance.leaseRenewalIntervalInSeconds=1
eureka.instance.leaseExpirationDurationInSeconds=2

MicroService Code:

@SpringBootApplication
@EnableEurekaClient
public class MicroServiceApplication

Properties:

spring.application.name=microservice
server.contextPath=/ott
server.port = 9000
eureka.instance.leaseRenewalIntervalInSeconds=1
eureka.instance.leaseExpirationDurationInSeconds=2

Eureka Server:

@SpringBootApplication
@EnableEurekaServer
public class EurekaserverApplication {

Properties:

server.port=8761

eureka.server.enableSelfPreservation=false
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false

logging.level.com.netflix.eureka=OFF
logging.level.com.netflix.discovery=OFF
1

1 Answers

0
votes

The issue causing this is that Eureka intellisense gives the shutdown instance more time to come up. The intellisense works this way for multiple microservices with the same name running on the same box.

When the services are deployed on different boxes then this issue doesn't come up.