首页

解决spring cloud基于RestTemplate访问注册Eureka服务接口报"Whitelabel Error Page..Internal Server Error 500..IllegalStateException: No instances available.."错误情况

标签:spring cloud,RestTemplate,IllegalStateException,available,500,RibbonLoadBalancerClient,getAllServers,getLoadBalancer,getServer     发布时间:2022-04-05   

一、错误描述

通过org.springframework.web.client.RestTemplate方式,实现EurekaClient客户端(配置@LoadBalanced注解后,以服务名经过Ribbon负载访问-如http://EurekaClient1/,否则通过具体ip+端口地址http://127.0.0.1:9991/)调用注册到springcloud的Eureka服务,调用后页面报"Whitelabel Error Page ..This application has no explicit mapping for /error, so you are seeing this as a fallback..There was an unexpected error (type=Internal Server Error, status=500)..No instances available for EurekaClient1",后台日志报"java.lang.IllegalStateException: No instances available for EurekaClient1.."异常(详细日志如下)

3_20220405165455副本.jpg

java.lang.IllegalStateException: No instances available for EurekaClient1@b@	at org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.execute(RibbonLoadBalancerClient.java:119) ~[spring-cloud-netflix-ribbon-2.1.3.RELEASE.jar:2.1.3.RELEASE]@b@	at org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.execute(RibbonLoadBalancerClient.java:99) ~[spring-cloud-netflix-ribbon-2.1.3.RELEASE.jar:2.1.3.RELEASE]@b@	at org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor.intercept(LoadBalancerInterceptor.java:58) ~[spring-cloud-commons-2.1.3.RELEASE.jar:2.1.3.RELEASE]@b@	at org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:92) ~[spring-web-5.1.11.RELEASE.jar:5.1.11.RELEASE]@b@	at org.springframework.http.client.InterceptingClientHttpRequest.executeInternal(InterceptingClientHttpRequest.java:76) ~[spring-web-5.1.11.RELEASE.jar:5.1.11.RELEASE]@b@	at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) ~[spring-web-5.1.11.RELEASE.jar:5.1.11.RELEASE]@b@	at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53) ~[spring-web-5.1.11.RELEASE.jar:5.1.11.RELEASE]@b@	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:735) ~[spring-web-5.1.11.RELEASE.jar:5.1.11.RELEASE]@b@	at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:670) ~[spring-web-5.1.11.RELEASE.jar:5.1.11.RELEASE]@b@	at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:338) ~[spring-web-5.1.11.RELEASE.jar:5.1.11.RELEASE]@b@	at com.xwood.cloud.eureka.client.controller.RestApiClientController.restApiTest(RestApiClientController.java:36) ~[classes/:na]@b@	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_221]@b@	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_221]@b@	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_221]@b@	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_221]@b@	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.1.11.RELEASE.jar:5.1.11.RELEASE]@b@	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.1.11.RELEASE.jar:5.1.11.RELEASE]@b@	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.1.11.RELEASE.jar:5.1.11.RELEASE]@b@	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:893) ~[spring-webmvc-5.1.11.RELEASE.jar:5.1.11.RELEASE]@b@	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:798) ~[spring-webmvc-5.1.11.RELEASE.jar:5.1.11.RELEASE]@b@	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.1.11.RELEASE.jar:5.1.11.RELEASE]@b@	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.1.11.RELEASE.jar:5.1.11.RELEASE]@b@	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.1.11.RELEASE.jar:5.1.11.RELEASE]@b@	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.1.11.RELEASE.jar:5.1.11.RELEASE]@b@	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.1.11.RELEASE.jar:5.1.11.RELEASE]@b@	at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) ~[tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.1.11.RELEASE.jar:5.1.11.RELEASE]@b@	at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.27.jar:9.0.27]@b@	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at org.springframework.boot.actuate.web.trace.servlet.HttpTraceFilter.doFilterInternal(HttpTraceFilter.java:88) ~[spring-boot-actuator-2.1.10.RELEASE.jar:2.1.10.RELEASE]@b@	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.1.11.RELEASE.jar:5.1.11.RELEASE]@b@	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.1.11.RELEASE.jar:5.1.11.RELEASE]@b@	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.1.11.RELEASE.jar:5.1.11.RELEASE]@b@	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.1.11.RELEASE.jar:5.1.11.RELEASE]@b@	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.1.11.RELEASE.jar:5.1.11.RELEASE]@b@	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:94) ~[spring-web-5.1.11.RELEASE.jar:5.1.11.RELEASE]@b@	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.1.11.RELEASE.jar:5.1.11.RELEASE]@b@	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:114) ~[spring-boot-actuator-2.1.10.RELEASE.jar:2.1.10.RELEASE]@b@	at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:104) ~[spring-boot-actuator-2.1.10.RELEASE.jar:2.1.10.RELEASE]@b@	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.1.11.RELEASE.jar:5.1.11.RELEASE]@b@	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.1.11.RELEASE.jar:5.1.11.RELEASE]@b@	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.1.11.RELEASE.jar:5.1.11.RELEASE]@b@	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526) [tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) [tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861) [tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1579) [tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_221]@b@	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_221]@b@	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.27.jar:9.0.27]@b@	at java.lang.Thread.run(Thread.java:748) [na:1.8.0_221]

二、解决方法

1. eurekaCient客户端配置如下,将eureka.client.fetch-registry=false改为true即可解决问题(完整gradle项目代码demo示例)

server.port=9991@b@server.servlet.context-path=/eureka-cient@b@#server.hostname=${spring.cloud.client.hostname}@b@spring.application.name=eureka-cient@b@spring.profiles.active=local@b@@b@# eureka客户端相关配置@b@# 客户端拉取服务注册信息的时间间隔,默认30秒,如果网关服务,配置小点@b@eureka.client.registry-fetch-interval-seconds=30@b@###因为自己是注册服务中心所以不需要检索服务信息@b@eureka.client.fetch-registry=false@b@###因为自己是注册中心,是否需要将自己注册给自己的服务中心(集群的时候需要将该值设置为true)@b@eureka.client.register-with-eureka=true@b@###注册地址@b@eureka.client.serviceUrl.defaultZone=http://localhost:9080/eureka/@b@@b@@b@eureka.services.rest.url=http://eureka-services/

修改为

server.port=9991@b@server.servlet.context-path=/eureka-cient@b@#server.hostname=${spring.cloud.client.hostname}@b@spring.application.name=eureka-cient@b@spring.profiles.active=local@b@@b@# eureka客户端相关配置@b@# 客户端拉取服务注册信息的时间间隔,默认30秒,如果网关服务,配置小点@b@eureka.client.registry-fetch-interval-seconds=30@b@###因为自己是注册服务中心所以不需要检索服务信息@b@eureka.client.fetch-registry=true@b@###因为自己是注册中心,是否需要将自己注册给自己的服务中心(集群的时候需要将该值设置为true)@b@eureka.client.register-with-eureka=true@b@###注册地址@b@eureka.client.serviceUrl.defaultZone=http://localhost:9080/eureka/@b@@b@@b@eureka.services.rest.url=http://eureka-services/
<<热门下载>>