16

I am working on Spring Boot Eureka Client Application with Ribbon Load Balancer.

I have two instances of the server registered with Eureka with the name "TEST". On the client side, I have the following code to fetch the server from Eureka.

@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableEurekaClient
@RestController
public class EurekaConsumerApplication {

    @Autowired
    DiscoveryClient discoveryClient;

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping(value = "/",method = RequestMethod.GET)
    String consumer(){
        InstanceInfo instance = discoveryClient.getNextServerFromEureka("TEST",        false);

        URI uri = UriComponentsBuilder.fromUriString(instance.getHomePageUrl() + "baseDir")
            .build()
            .toUri();
        String baseDir = restTemplate.getForObject(uri, String.class);

        return baseDir;

    }

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

application.yml

spring:
  application:
    name: consumer
info:
  component: Consumer to fetch configuration
server:
  port: 8090
eureka:
  instance:
   leaseRenewalIntervalInSeconds: 3
  metadataMap:
    instanceId:   ${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${random.value}}}
client:
# Default values comes from   org.springframework.cloud.netflix.eurek.EurekaClientConfigBean
  region: default
  registryFetchIntervalSeconds: 5
  instanceInfoReplicationIntervalSeconds: 5
  initialInstanceInfoReplicationIntervalSeconds: 5
  serviceUrl:
    defaultZone: http://localhost:8761/eureka/
    availabilityZones:
    default: ${APPLICATION_DOMAIN:${DOMAIN:defaultZone}}

However, when I hit the restful endpoint by using the following command, it gives an error:

curl http://localhost:8090/

This is the error:

{"exception":"java.lang.IllegalStateException","message":"org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: No instances available for Samarths-MacBook-Pro.local","path":"/"}

Stacktrace:

2015-07-22 14:37:35.005 INFO 13841 --- [tp1334391583-19] c.netflix.loadbalancer.BaseLoadBalancer : Client:Samarths-MacBook-Pro.local instantiated a LoadBalancer:DynamicServerListLoadBalancer:{NFLoadBalancer:name=Samarths-MacBook-Pro.local,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
2015-07-22 14:37:35.009 INFO 13841 --- [tp1334391583-19] c.n.l.DynamicServerListLoadBalancer : DynamicServerListLoadBalancer for client Samarths-MacBook-Pro.local initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=Samarths-MacBook-Pro.local,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@681eda37
2015-07-22 14:37:35.029 WARN 13841 --- [tp1334391583-19] o.eclipse.jetty.servlet.ServletHandler :

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: No instances available for Samarths-MacBook-Pro.local
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:808)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1669)
at org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration$ApplicationContextHeaderFilter.doFilterInternal(EndpointWebMvcAutoConfiguration.java:295)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:102)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.eclipse.jetty..ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:68)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
at org.eclipse.jetty.server.Server.handle(Server.java:499)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:310)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257)
at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: No instances available for Samarths-MacBook-Pro.local
at org.springframework.cloud.netflix.ribbon.RibbonClientHttpRequestFactory.createRequest(RibbonClientHttpRequestFactory.java:64)
at org.springframework.http.client.support.HttpAccessor.createRequest(HttpAccessor.java:76)
at org.springframework.web.client.Rlate.doExecute(RestTemplate.java:565)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:545)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:253)
at com.securityscorecard.eureka.consumer.EurekaConsumerApplication.consumer(EurekaConsumerApplication.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
... 38 common frames omitted

Looks like my server list is empty.

g00glen00b
  • 41,995
  • 13
  • 95
  • 133
Samarth
  • 233
  • 1
  • 2
  • 6

7 Answers7

25

The RestTemplate you autowired is already connected to Ribbon. So you do a lookup by hand and then RestTemplate is trying to lookup the hostname passed in to ribbon. You have two options: 1) Don't use the netflix DiscoveryClient and pass the serviceId as a logical hostname to ribbon (http://TEST/myservice), 2) Don't use the autowired RestTemplate, create a new one for your class. My choice would be #1.

spencergibb
  • 24,471
  • 6
  • 69
  • 75
  • Thanks for the suggestion. I used a variation of your first choice to fix my problem. – Samarth Jul 23 '15 at 14:27
  • A link to the documentation:http://projects.spring.io/spring-cloud/spring-cloud.html#_spring_resttemplate_as_a_load_balancer_client – Rudy Vissers Dec 08 '15 at 10:22
  • Could you please guide here https://stackoverflow.com/questions/49791825/java-lang-illegalstateexception-no-instances-available-for-student-service-at-o ? – Jeff Cook Apr 12 '18 at 08:43
7

I got this working. The only change I had to make was in the way I was using RestTemplate api.

Error Code:

@Autowired
RestTemplate restTemplate;

@RequestMapping(value = "/",method = RequestMethod.GET)
String consumer(){
    String baseDir = restTemplate.getForObject("TEST", String.class);

    return baseDir;
}

Working Code:

@Autowired
RestTemplate restTemplate;

@RequestMapping(value = "/",method = RequestMethod.GET)
String consumer(){
    String baseDir = restTemplate.getForObject("http://TEST", String.class);

    return baseDir;
}

Solution:

The first parameter to restTemplate.getForObject should have the format of a URL. And the domain name should be the name of the service you want to discover.

Ex: http://TEST. Here, TEST is the name of my server registered to eureka registry

Samarth
  • 233
  • 1
  • 2
  • 6
3

The question is already answered, but I found a workaround that seems neat and fixed our problem.

First declare a new @Component class and in it create a method that returns RestTemplate:

@Component
public class RestTemplateComponentFix{

 @Autowired
 SomeConfigurationYouNeed someConfiguration;

 @LoadBalanced
 public RestTemplate getRestTemplate() {
       // TODO set up your restTemplate
        rt.setRequestFactory( new HttpComponentsClientHttpRequestFactory() );
        return rt;
    }

}

After that just Autowire the restTemplateComponentFix in your class and when when you need the rest template call the restTemplate() method. Something like this:

@Service
public class someClass{

    @Autowired
    RestTemplateComponentFix restTemplateComponentFix;

    public void methodUsingRestTemplate(){
        // Some code...
        RestTemplate rt = restTemplateComponentFix.getRestTemplate();
        // Some code...
    }
}

After that you can unit test with something like:

RestTemplate rt = Mockito.mock(RestTemplate.class) 
when(restTemplateComponentFix.getRestTemplate()).thenReturn(rt);
when(rt.someMethod()).thenReturn(something);
pstoyanov
  • 61
  • 3
1

Removing @LoadBalanced Annotation from RestTemplate works for me.

P.S: @LoadAnnotation is supposed to be applied only to Eureka Server which is your discovery server.

M.K
  • 1,464
  • 2
  • 24
  • 46
1

The question is already answered by @spencergibb, after trying @spencergibb way if you are still struggling with no instance avialable for ..MS... make sure that in pom.xml if you are using Netflix Eureka then avoid adding dependency for Netflix ribbon as Eureka itself internally uses ribbon. This was causing problems for me.

0

RestTemplateBuilder works for me. As below-

@Service
public class someClass{

    @Autowired
    RestTemplateBuilder restTemplateBuilder;

    public void methodUsingRestTemplate(){
        restTemplateBuilder.build().getForObject("http://TEST", String.class);
    }
}
m4n0
  • 29,823
  • 27
  • 76
  • 89
0

Can be due to outdated release used in dependencies.

I was using a project from GitHub blog to understand the working of Microservices registry and discovery.

The dependency of netflix in that project was outdated, spring-cloud-starter-netflix-eureka-server', version: '2.0.1.RELEASE'

After removing version, issue was solved as latest version would be used.

Janakar
  • 1
  • 2
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 26 '22 at 20:22