0
votes

I have Below Simple Setup. Spring Cloud Eureka Server- and Two Services PricingService and DiscountService. Pricing Service calls-> DiscountService.

Server Setup

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {    
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }    
}

properties

spring.application.name=eureka-server
server.port=8761
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

enter image description here

DiscountService

@SpringBootApplication
@EnableDiscoveryClient
@Slf4j
public class DiscountServiceApplication {

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

}
@RestController
@RequestMapping("/discount/{product}")
@Slf4j
public class DiscountController{

    @GetMapping
    public int getDiscountPercentage(@PathVariable("product") String product){
        log.info("Getting Discount for Product {}",product);
        return  50;
    }

}

spring.application.name=discount-service 
eureka.client.serviceUrl.defaultZone= http://localhost:8761/eureka/
server.port=8081

Pricing Service

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class PricingServiceApplication {

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


}
@RestController
@Slf4j
class ServiceInstanceRestController {

    @Autowired
    private DiscountServiceClient discountServiceClient;

    @GetMapping("/test/")
    public String getPriceForProduct() {
        log.info("getPriceForProduct");
      int dscount = discountServiceClient.getDiscountPercentage("Test");
        log.info("Discount is {}",dscount);
        return "Price";
    }
}

@FeignClient("discount-service")
interface DiscountServiceClient {
    @RequestMapping(method = RequestMethod.GET, value = "/discount/{product}")
    int getDiscountPercentage(@PathVariable("product") String product);
}

spring.application.name=pricing-service 
eureka.client.serviceUrl.defaultZone= http://localhost:8761/eureka/
server.port=8080
eureka.client.fetchRegistry=true

While Calling Discount Service i am getting exception

com.netflix.client.ClientException: Load balancer does not have available server for client: discount-service
    at com.netflix.loadbalancer.LoadBalancerContext.getServerFromLoadBalancer(LoadBalancerContext.java:483) ~[ribbon-loadbalancer-2.3.0.jar:2.3.0]

I am using Spring Boot 2.1.4.RELEASE dependency used

spring-cloud-starter-netflix-eureka-client
spring-cloud-starter-openfeign
spring-cloud-starter-netflix-eureka-server-server (Fo Server)

I have already checked the answers for Load balancer does not have available server for client But did not work for me...

What I am missing here ?

1
What does /eureka/apps say on the eureka server?spencergibb
Both the services are registeredNiraj Sonawane
Do you have ribbon?spencergibb
I changed the application.properties to application.yml and it worked. Not able to trace the issueNiraj Sonawane

1 Answers

0
votes

It also requires actuator dependency

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

For more details, refer the following links

  1. https://github.com/M-Thirumal/eureka-server
  2. https://github.com/M-Thirumal/eureka-client-1
  3. https://github.com/M-Thirumal/eureka-client-2