Spring-cloud-netflix: Eureka doesn't work with restTemplate anymore, was this fixed or not?

Created on 21 Jun 2016  路  12Comments  路  Source: spring-cloud/spring-cloud-netflix

As per Josh Long's webinar "Building Bootiful Microservices with Spring", I have implemented the restClient bean with an autowired RestTemplate. But it keeps giving me IO error: unknown host, or autowire failed error.

@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
public class TemplateApplication {

    @Autowired
    private RestTemplate template;

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

    @Bean
    CommandLineRunner restClient() {
        return args -> {
            ParameterizedTypeReference<List<Friend>> reference = new ParameterizedTypeReference<List<Friend>>() { };

            ResponseEntity<List<Friend>> response = this.template.exchange("http://reservation-service/reservations", HttpMethod.GET, null, reference);

            for (Friend f : response.getBody()) {
                System.out.println(f.toString());
            }

        };
    }
}

I did some searching around and found these resources:

https://spring.io/blog/2015/01/20/microservice-registration-and-discovery-with-spring-cloud-and-netflix-s-eureka (Blog post by Josh, with similar code)

https://github.com/spring-cloud/spring-cloud-netflix/issues/1031

This document is suggested as a remedy for the issue, but I am not getting what they're doing differently.

https://github.com/spring-cloud/spring-cloud-commons/blob/master/docs/src/main/asciidoc/spring-cloud-commons.adoc#spring-resttemplate-as-a-load-balancer-client

Could anyone clarify this?

question

Most helpful comment

In Angel, a RestTemplate with an @LoadBalanced qualifier was created for you automatically. This was removed (and documented) in Brixton. You need to create the RestTemplate bean somewhere in your application's configuration, annotated with @LoadBalanced in order to use it.

All 12 comments

BTW, I get unknown host error when I use a new template:

RestTemplate template = new RestTemplate();

In Angel, a RestTemplate with an @LoadBalanced qualifier was created for you automatically. This was removed (and documented) in Brixton. You need to create the RestTemplate bean somewhere in your application's configuration, annotated with @LoadBalanced in order to use it.

I'll give that annotation a try.

Update: The LoadBalanced bean fixed the issue.

Created RestTemplate Bean and annotated with @LoadBalanced but still getting unknown host error

Here goes the stack trace

java.net.UnknownHostException: db-service
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184) ~[na:1.8.0_171]
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_171]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_171]
at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_171]
at java.net.Socket.connect(Socket.java:538) ~[na:1.8.0_171]
at sun.net.NetworkClient.doConnect(NetworkClient.java:180) ~[na:1.8.0_171]
at sun.net.www.http.HttpClient.openServer(HttpClient.java:463) ~[na:1.8.0_171]
at sun.net.www.http.HttpClient.openServer(HttpClient.java:558) ~[na:1.8.0_171]
at sun.net.www.http.HttpClient.(HttpClient.java:242) ~[na:1.8.0_171]
at sun.net.www.http.HttpClient.New(HttpClient.java:339) ~[na:1.8.0_171]
at sun.net.www.http.HttpClient.New(HttpClient.java:357) ~[na:1.8.0_171]
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1220) ~[na:1.8.0_171]
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1156) ~[na:1.8.0_171]
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1050) ~[na:1.8.0_171]
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:984) ~[na:1.8.0_171]
at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:76) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:108) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.cloud.client.loadbalancer.LoadBalancerRequestFactory.lambda$createRequest$0(LoadBalancerRequestFactory.java:59) ~[spring-cloud-commons-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.execute(RibbonLoadBalancerClient.java:112) ~[spring-cloud-netflix-ribbon-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.execute(RibbonLoadBalancerClient.java:94) ~[spring-cloud-netflix-ribbon-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor.intercept(LoadBalancerInterceptor.java:55) ~[spring-cloud-commons-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:92) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.http.client.InterceptingClientHttpRequest.executeInternal(InterceptingClientHttpRequest.java:76) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:723) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:680) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:629) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at com.techprimers.stock.stockservice.resource.StockResource.getQuote(StockResource.java:31) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_171]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_171]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_171]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_171]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.31.jar:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1468) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.31.jar:8.5.31]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_171]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_171]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.31.jar:8.5.31]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_171]

Though i can see db-service is already registered with the netflix eureka server which can be observed in the last line of the following log.

: No active profile set, falling back to default profiles: default
: Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@24f43aa3: startup date [Tue Jul 10 14:09:30 IST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@
: BeanFactory id=3cf42302-bc65-38be-a83c-602471f93145
: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
: Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$17b41d65] is not eligible
: Tomcat initialized with port(s): 8301 (http)
: Starting service [Tomcat]
: Starting Servlet Engine: Apache Tomcat/8.5.31
: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [D:\SoftwaresInstalled\Java\jdk1.8.0_171\bin;C:\windows\Sun\Java\bin;C:\windows\system32;C:\windows;D
: Initializing Spring embedded WebApplicationContext
: Root WebApplicationContext: initialization completed in 1340 ms
: Servlet dispatcherServlet mapped to [/]
: Mapping filter: 'characterEncodingFilter' to: [/]
: Mapping filter: 'hiddenHttpMethodFilter' to: [/
]
: Mapping filter: 'httpPutFormContentFilter' to: [/]
: Mapping filter: 'requestContextFilter' to: [/
]
: No URLs will be polled as dynamic configuration sources.
: To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
: No URLs will be polled as dynamic configuration sources.
: To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
: Mapped URL path [//favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
: Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@24f43aa3: startup date [Tue Jul 10 14:09:30 IST 2018]; parent: org.springframework.context.annotation.AnnotationConfi
: Mapped "{[/rest/stock/{username}],methods=[GET]}" onto public java.util.List com.techprimers.stock.stockservice.resource.StockResource.getQuote(java.lang.String)
: Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest
: Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.H
: Mapped URL path [/webjars/
] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
: Mapped URL path [/] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
: Registering beans for JMX exposure on startup
: Bean with name 'configurationPropertiesRebinder' has been autodetected for JMX exposure
: Bean with name 'environmentManager' has been autodetected for JMX exposure
: Bean with name 'refreshScope' has been autodetected for JMX exposure
: Located managed bean 'environmentManager': registering with JMX server as MBean [org.springframework.cloud.context.environment:name=environmentManager,type=EnvironmentManager]
: Located managed bean 'refreshScope': registering with JMX server as MBean [org.springframework.cloud.context.scope.refresh:name=refreshScope,type=RefreshScope]
: Located managed bean 'configurationPropertiesRebinder': registering with JMX server as MBean [org.springframework.cloud.context.properties:name=configurationPropertiesRebinder,context=24f43aa3,type=ConfigurationPropertiesRebinder]
: Starting beans in phase 0
: Setting initial instance status as: STARTING
: Initializing Eureka in region us-east-1
: Using JSON encoding codec LegacyJacksonJson
: Using JSON decoding codec LegacyJacksonJson
: Using XML encoding codec XStreamXml
: Using XML decoding codec XStreamXml
: Resolving eureka endpoints via configuration
: Disable delta property : false
: Single vip registry refresh property : null
: Force full registry fetch : false
: Application is null : false
: Registered Applications size is zero : true
: Application version is -1: true
: Getting all instance registry info from the eureka server
: The response status is 200
: Starting heartbeat executor: renew interval is: 30
: InstanceInfoReplicator onDemand update allowed rate per min is 4
: Discovery Client initialized at timestamp 1531211975770 with initial instances count: 1
: Registering application stock-service with eureka with status UP
: Saw local status change event StatusChangeEvent [timestamp=1531211975776, current=UP, previous=STARTING]
: DiscoveryClient_STOCK-SERVICE/del1-lhp-n02547.synapse.com:stock-service:8301: registering service...
: Tomcat started on port(s): 8301 (http) with context path ''
: Updating port to 8301
: Started StockServiceApplication in 7.124 seconds (JVM running for 8.338)
: **DiscoveryClient_STOCK-SERVICE/:stock-service:8301 - registration status: 204

Please open a separate issue

has a seperate issue been opened for this? im having similiar trouble.

@gewure go ahead and open a separate issue providing the details of the problem as well as a sample to reproduce the issue.

Created RestTemplate with @Bean and @LoadBalanced. But get the following error:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception
[Request processing failed; nested exception is java.lang.IllegalStateException: No instances available for PRODUCT]
with root cause

To everyone replying with "I'm having the same issue", please open a new issue
and provide a complete, minimal, verifiable sample that reproduces the problem. It should be available as a GitHub (or similar) project or attached to this issue as a zip file.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hotblac picture hotblac  路  3Comments

mekhaba picture mekhaba  路  4Comments

Ikki-Dai picture Ikki-Dai  路  3Comments

jpuigsegur picture jpuigsegur  路  4Comments

jjparsons picture jjparsons  路  3Comments