yellaiah
yellaiah

Reputation: 31

How to fix no instance available for micro service using ribbon client

I am doing client side load balancing using ribbon client. i am implementing micro services spring boot 2.2.6.RELEASE. I have CHAT-BOOK and USER-APP two micro services. I have added required dependencies (spring-boot-starter-web) in CHAT-BOOK (spring-boot-starter-web, spring-cloud-starter-netflix-ribbon) USER-APP micro service in pom.xml .

CHAT-BOOK microservice:

@RestController
@RequestMapping("/chatbook")
public class ChatbookController {

    @Value("${server.port}")
    private String port;

    @GetMapping("/chatnow")
    public String chat() {
        return "application up on port: " + port;
    }
}

USER-APP microservice:

I have added below properties in application.properties.

CHAT-BOOK.ribbon.listOfServers=localhost:8081,localhost:8082,localhost:8083

CHAT-BOOK.ribbon.eureka.enabled=false
server.port=9090

UserAppApplication class:

@SpringBootApplication

@RibbonClient(name = "CHAT-BOOK", configuration = RibbonConfiguration.class)

public class UserAppApplication {


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

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

}

RibbonConfiguration.java

public class RibbonConfiguration {

    @Autowired
    IClientConfig ribbonClient;

    @Bean
    public IPing ping(IClientConfig ribbonClient) {
        return new PingUrl();
    }

    @Bean
    public IRule rule(IClientConfig ribbonClient) {
        return new AvailabilityFilteringRule();
    }
}


UserContoller.java 

@RestController
public class UserContoller {

    @Autowired
    private RestTemplate template;

    @GetMapping("/invoke")
    public String invokeChatbook() {
        String url = "http://CHAT-BOOK/chatbook/chatnow";
        return template.getForObject(url, String.class);
    }

}

Reference URL:

https://www.youtube.com/watch?v=ueyVjOnDHYQ&list=PLVz2XdJiJQxz3L2Onpxbel6r72IDdWrJh&index=3

When i run above application i am getting below errors.

2020-04-14 11:27:50.013  WARN 9996 --- [nio-9090-exec-5] com.netflix.loadbalancer.RoundRobinRule  : No up servers available from load balancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=CHAT-BOOK,current list of Servers=[localhost:8081, localhost:8082, localhost:8083],Load balancer stats=Zone stats: {unknown=[Zone:unknown;   Instance count:3;   Active connections count: 0;    Circuit breaker tripped count: 0;   Active connections per server: 0.0;]
},Server stats: [[Server:localhost:8081;    Zone:UNKNOWN;   Total Requests:0;   Successive connection failure:0;    Total blackout seconds:0;   Last connection made:Thu Jan 01 05:30:00 IST 1970;  First connection made: Thu Jan 01 05:30:00 IST 1970;    Active Connections:0;   total failure count in last (1000) msecs:0; average resp time:0.0;  90 percentile resp time:0.0;    95 percentile resp time:0.0;    min resp time:0.0;  max resp time:0.0;  stddev resp time:0.0]
, [Server:localhost:8082;   Zone:UNKNOWN;   Total Requests:0;   Successive connection failure:0;    Total blackout seconds:0;   Last connection made:Thu Jan 01 05:30:00 IST 1970;  First connection made: Thu Jan 01 05:30:00 IST 1970;    Active Connections:0;   total failure count in last (1000) msecs:0; average resp time:0.0;  90 percentile resp time:0.0;    95 percentile resp time:0.0;    min resp time:0.0;  max resp time:0.0;  stddev resp time:0.0]
, [Server:localhost:8083;   Zone:UNKNOWN;   Total Requests:0;   Successive connection failure:0;    Total blackout seconds:0;   Last connection made:Thu Jan 01 05:30:00 IST 1970;  First connection made: Thu Jan 01 05:30:00 IST 1970;    Active Connections:0;   total failure count in last (1000) msecs:0; average resp time:0.0;  90 percentile resp time:0.0;    95 percentile resp time:0.0;    min resp time:0.0;  max resp time:0.0;  stddev resp time:0.0]
]}ServerList:com.netflix.loadbalancer.ConfigurationBasedServerList@387e7238
2020-04-14 11:27:50.016  WARN 9996 --- [nio-9090-exec-5] c.netflix.loadbalancer.BaseLoadBalancer  : LoadBalancer [CHAT-BOOK]:  Error choosing server for key default

I run three servers (8081, 8082, 8083). but it is showing No up servers available from load balancer.

Error1:

java.lang.NullPointerException: null
    at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:890) ~[guava-28.2-android.jar:na]
    at com.google.common.cache.LocalCache.get(LocalCache.java:3848) ~[guava-28.2-android.jar:na]
    at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3873) ~[guava-28.2-android.jar:na]
    at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4798) ~[guava-28.2-android.jar:na]
    at com.netflix.loadbalancer.LoadBalancerStats.getServerStats(LoadBalancerStats.java:185) ~[ribbon-loadbalancer-2.3.0.jar:2.3.0]
    at com.netflix.loadbalancer.LoadBalancerStats.getSingleServerStat(LoadBalancerStats.java:372) ~[ribbon-loadbalancer-2.3.0.jar:2.3.0]
    at com.netflix.loadbalancer.AvailabilityPredicate.apply(AvailabilityPredicate.java:73) ~[ribbon-loadbalancer-2.3.0.jar:2.3.0]
    at com.netflix.loadbalancer.AvailabilityPredicate.apply(AvailabilityPredicate.java:35) ~[ribbon-loadbalancer-2.3.0.jar:2.3.0]
    at com.netflix.loadbalancer.CompositePredicate.apply(CompositePredicate.java:52) ~[ribbon-loadbalancer-2.3.0.jar:2.3.0]
    at com.netflix.loadbalancer.CompositePredicate.apply(CompositePredicate.java:40) ~[ribbon-loadbalancer-2.3.0.jar:2.3.0]
    at com.netflix.loadbalancer.AvailabilityFilteringRule.choose(AvailabilityFilteringRule.java:86) ~[ribbon-loadbalancer-2.3.0.jar:2.3.0]
    at com.netflix.loadbalancer.BaseLoadBalancer.chooseServer(BaseLoadBalancer.java:755) ~[ribbon-loadbalancer-2.3.0.jar:2.3.0]
    at com.netflix.loadbalancer.ZoneAwareLoadBalancer.chooseServer(ZoneAwareLoadBalancer.java:113) [ribbon-loadbalancer-2.3.0.jar:2.3.0]
    at org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.getServer(RibbonLoadBalancerClient.java:189) [spring-cloud-netflix-ribbon-2.2.2.RELEASE.jar:2.2.2.RELEASE]....

Error2:

java.lang.IllegalStateException: No instances available for CHAT-BOOK
    at org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.execute(RibbonLoadBalancerClient.java:119) ~[spring-cloud-netflix-ribbon-2.2.2.RELEASE.jar:2.2.2.RELEASE]
    at org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.execute(RibbonLoadBalancerClient.java:99) ~[spring-cloud-netflix-ribbon-2.2.2.RELEASE.jar:2.2.2.RELEASE]
    at org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor.intercept(LoadBalancerInterceptor.java:58) ~[spring-cloud-commons-2.2.2.RELEASE.jar:2.2.2.RELEASE]
    at org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:93) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.http.client.InterceptingClientHttpRequest.executeInternal(InterceptingClientHttpRequest.java:77) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:739) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:674) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:315) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at com.loadbalanced.api.UserContoller.invokeChatbook(UserContoller.java:17) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_231]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_231]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_231]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_231]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) ~[tomcat-embed-core-9.0.33.jar:9.0.33].....

Could any one suggest me how to resolve this is above issues.

Thanks in advance

Upvotes: 2

Views: 5592

Answers (3)

Sumit Bhawsar
Sumit Bhawsar

Reputation: 21

looks like the services not resolved through eureka. please check if those are registerd in eureka, and also check if eureka is not disabled by below properties

CHAT-BOOK.ribbon.eureka.enabled=false

if you are not using eureka then set below properties

    CHAT-BOOK.ribbon.eureka.enabled=false
    CHAT-BOOK.ribbon.NIWSServerListClassName=com.netflix.loadbalancer.ConfigurationBasedServerList
    CHAT-BOOK.ribbon.listOfServers=http://localhost:9999/

Upvotes: 0

Diego Ramos
Diego Ramos

Reputation: 1059

Since you have specified the strategy as PingUrl you need to add a @RequestMapping for "/" in your service, so that Ribbon can ping to know the availability of the instances:

@RequestMapping(value = "/")
      public String home() {
        return "home";
      }

Upvotes: 2

Related Questions