springcloud 无法注册到服务,显示No instances availabletime

解决Ribbon with Spring Cloud and Eureka: java.lang.IllegalStateException: No instances available for Samarths-MacBook-Pro.local - spring-spring-cloud-ribbon - ITkeyowrd
解决Ribbon with Spring Cloud and Eureka: java.lang.IllegalStateException: No instances available for Samarths-MacBook-Pro.local
领取地址:
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 discoveryC
@Autowired
RestTemplate restT
@RequestMapping(value = &/&,method = RequestMethod.GET)
String consumer(){
InstanceInfo instance = discoveryClient.getNextServerFromEureka(&TEST&,
URI uri = UriComponentsBuilder.fromUriString(instance.getHomePageUrl() + &baseDir&)
String baseDir = restTemplate.getForObject(uri, String.class);
return baseD
public static void main(String[] args) {
SpringApplication.run(EurekaConsumerApplication.class, args);
application.yml
application:
name: consumer
component: Consumer to fetch configuration
port: 8090
leaseRenewalIntervalInSeconds: 3
metadataMap:
instanceId:
${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${random.value}}}
# 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: curl , it gives me the following error: &exception&:&java.lang.IllegalStateException&,&message&:&org.springframework.web.util.NestedServletException: Reque nested exception is java.lang.IllegalStateException: No instances available for Samarths-MacBook-Pro.local&,&path&:&/&}
Stacktrace:
14:37:35.005 INFO 13841 --- [tp-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
14:37:35.009 INFO 13841 --- [tp-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
14:37:35.029 WARN 13841 --- [tp-19] o.eclipse.jetty.servlet.ServletHandler :
org.springframework.web.util.NestedServletException: Reque 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.answer 1 >>解决方法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.
相关阅读排行
相关内容推荐
请激活账号
为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。
您的注册邮箱:
如果您没有收到激活邮件,请注意检查垃圾箱。spring cloud中服务注册成功,但不能通过服务名来调用IP地址
[问题点数:50分]
本版专家分:0
CSDN今日推荐
本版专家分:0
本版专家分:0
本版专家分:30
匿名用户不能发表回复!
其他相关推荐学习微服务的断路器监控——Hystrix Dashboard丶一个站在Java后端设计之路的男青年个人博客网站玩转JavaEE(gh_d1ca11234a30)原文发表时间:本文参与,欢迎正在阅读的你也加入,一起分享。分享分享文章到朋友圈分享文章到 QQ分享文章到 QQ 空间分享文章到微博复制文章链接到剪贴板扫描二维码扫描关注云+社区112 篇文章41 人订阅相关文章来自专栏406来自专栏429来自专栏69来自专栏181来自专栏272来自专栏360扫描二维码扫描关注云+社区Spring Cloud 入门教程(一):
服务注册 - 大象O无形 - 博客园
随笔 - 38, 文章 - 2, 评论 - 17, 引用 - 0
1. &什么是Spring Cloud?
Spring提供了一系列工具,可以帮助开发人员迅速搭建分布式系统中的公共组件(比如:配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁,主节点选举, 分布式session, 集群状态)。协调分布式环境中各个系统,为各类服务提供模板性配置。使用Spring Cloud, 开发人员可以搭建实现了这些样板的应用,并且在任何分布式环境下都能工作得非常好,小到笔记本电脑, 大到数据中心和云平台。
Spring Cloud官网的定义比较抽象,我们可以从简单的东西开始。Spring Cloud是基于Spring Boot的, 最适合用于管理Spring Boot创建的各个微服务应用。要管理分布式环境下的各个Spring Boot微服务,必然存在服务的注册问题。所以我们先从服务的注册谈起。既然是注册,必然有个管理注册中心的服务器,各个在Spring Cloud管理下的Spring Boot应用就是需要注册的client
Spring Cloud使用erureka server, &然后所有需要访问配置文件的应用都作为一个erureka client注册上去。eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳,在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。
2. &创建Eureka Server
1).创建一个Maven工程helloworld.eureka.server, pom.xml内容如下:
1 &?xml version="1.0" encoding="UTF-8"?&
2 &project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&
&modelVersion&4.0.0&/modelVersion&
&groupId&com.chry&/groupId&
&artifactId&springcloud.helloworld.eureka.server&/artifactId&
&version&0.0.1-SNAPSHOT&/version&
&packaging&jar&/packaging&
&name&springcloud.helloworld.Eureka.server&/name&
<span style="color: #
&description&Demo Spring Eureka Server&/description&
<span style="color: #
<span style="color: #
<span style="color: #
&groupId&org.springframework.boot&/groupId&
<span style="color: #
&artifactId&spring-boot-starter-parent&/artifactId&
<span style="color: #
&version&1.5.3.RELEASE&/version&
<span style="color: #
&relativePath/& &!-- lookup parent from repository --&
<span style="color: #
<span style="color: #
<span style="color: #
&properties&
<span style="color: #
&project.build.sourceEncoding&UTF-8&/project.build.sourceEncoding&
<span style="color: #
&project.reporting.outputEncoding&UTF-8&/project.reporting.outputEncoding&
<span style="color: #
&java.version&1.8&/java.version&
<span style="color: #
&/properties&
<span style="color: #
<span style="color: #
&dependencies&
<span style="color: #
&!--eureka server --&
<span style="color: #
&dependency&
<span style="color: #
&groupId&org.springframework.cloud&/groupId&
<span style="color: #
&artifactId&spring-cloud-starter-eureka&/artifactId&
<span style="color: #
&/dependency&
<span style="color: #
&dependency&
<span style="color: #
&groupId&org.springframework.cloud&/groupId&
<span style="color: #
&artifactId&spring-cloud-starter-eureka-server&/artifactId&
<span style="color: #
&/dependency&
<span style="color: #
&dependency&
<span style="color: #
&groupId&org.springframework.cloud&/groupId&
<span style="color: #
&artifactId&spring-cloud-starter-config&/artifactId&
<span style="color: #
&/dependency&
<span style="color: #
&!-- spring boot test--&
<span style="color: #
&dependency&
<span style="color: #
&groupId&org.springframework.boot&/groupId&
<span style="color: #
&artifactId&spring-boot-starter-test&/artifactId&
<span style="color: #
&scope&test&/scope&
<span style="color: #
&/dependency&
<span style="color: #
&/dependencies&
<span style="color: #
<span style="color: #
&dependencyManagement&
<span style="color: #
&dependencies&
<span style="color: #
&dependency&
<span style="color: #
&groupId&org.springframework.cloud&/groupId&
<span style="color: #
&artifactId&spring-cloud-dependencies&/artifactId&
<span style="color: #
&version&Dalston.RC1&/version&
<span style="color: #
&type&pom&/type&
<span style="color: #
&scope&import&/scope&
<span style="color: #
&/dependency&
<span style="color: #
&/dependencies&
<span style="color: #
&/dependencyManagement&
<span style="color: #
<span style="color: #
<span style="color: #
<span style="color: #
<span style="color: #
&groupId&org.springframework.boot&/groupId&
<span style="color: #
&artifactId&spring-boot-maven-plugin&/artifactId&
<span style="color: #
<span style="color: #
&/plugins&
<span style="color: #
<span style="color: #
<span style="color: #
&repositories&
<span style="color: #
&repository&
<span style="color: #
&id&spring-milestones&/id&
<span style="color: #
&name&Spring Milestones&/name&
<span style="color: #
&url&https://repo.spring.io/milestone&/url&
<span style="color: #
&snapshots&
<span style="color: #
&enabled&false&/enabled&
<span style="color: #
&/snapshots&
<span style="color: #
&/repository&
<span style="color: #
&/repositories&
<span style="color: #
<span style="color: # &/project&
&2). 用Spring Boot创建一个服务类EurekaServerApplication,需要一个注解@EnableEurekaServer加在springboot工程的启动类上
1 package springcloud.helloworld.eureka.
3 import org.springframework.boot.SpringA
4 import org.springframework.boot.autoconfigure.SpringBootA
5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaS
7 @EnableEurekaServer
8 @SpringBootApplication
9 public class EurekaServerApplication {
<span style="color: #
<span style="color: #
public static void main(String[] args) {
<span style="color: #
SpringApplication.run(EurekaServerApplication.class, args);
<span style="color: #
<span style="color: # }
3).eureka server的配置文件appication.yml,其中registerWithEureka:false和fetchRegistry:false表明自己是一个eureka server
port: 8761
hostname: localhost
registerWithEureka: false
fetchRegistry: false
<span style="color: #
serviceUrl:
<span style="color: #
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
4) eureka server的工程结构如下
5)启动eureka server,然后访问http://localhost:8761, 界面如下, "No instances available" 表示无client注册
3. &创建Eureka Client
1).&创建一个Maven工程helloworld.eureka.client, pom.xml内容如下:
1 &?xml version="1.0" encoding="UTF-8"?&
2 &project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&
&modelVersion&4.0.0&/modelVersion&
&groupId&com.chry&/groupId&
&artifactId&springcloud.helloworld.eureka.client&/artifactId&
&version&0.0.1-SNAPSHOT&/version&
&name&springcloud.helloworld.eureka.client&/name&
&packaging&jar&/packaging&
&description&Demo Spring Boot Client&/description&
<span style="color: #
<span style="color: #
<span style="color: #
&groupId&org.springframework.boot&/groupId&
<span style="color: #
&artifactId&spring-boot-starter-parent&/artifactId&
<span style="color: #
&version&1.5.3.RELEASE&/version&
<span style="color: #
&relativePath/& &!-- lookup parent from repository --&
<span style="color: #
<span style="color: #
<span style="color: #
&properties&
<span style="color: #
&project.build.sourceEncoding&UTF-8&/project.build.sourceEncoding&
<span style="color: #
&project.reporting.outputEncoding&UTF-8&/project.reporting.outputEncoding&
<span style="color: #
&java.version&1.8&/java.version&
<span style="color: #
&/properties&
<span style="color: #
<span style="color: #
&dependencies&
<span style="color: #
&dependency&
<span style="color: #
&groupId&org.springframework.cloud&/groupId&
<span style="color: #
&artifactId&spring-cloud-starter-eureka&/artifactId&
<span style="color: #
&/dependency&
<span style="color: #
&dependency&
<span style="color: #
&groupId&org.springframework.boot&/groupId&
<span style="color: #
&artifactId&spring-boot-starter-web&/artifactId&
<span style="color: #
&/dependency&
<span style="color: #
<span style="color: #
&dependency&
<span style="color: #
&groupId&org.springframework.boot&/groupId&
<span style="color: #
&artifactId&spring-boot-starter-test&/artifactId&
<span style="color: #
&scope&test&/scope&
<span style="color: #
&/dependency&
<span style="color: #
&/dependencies&
<span style="color: #
<span style="color: #
&dependencyManagement&
<span style="color: #
&dependencies&
<span style="color: #
&dependency&
<span style="color: #
&groupId&org.springframework.cloud&/groupId&
<span style="color: #
&artifactId&spring-cloud-dependencies&/artifactId&
<span style="color: #
&version&Dalston.RC1&/version&
<span style="color: #
&type&pom&/type&
<span style="color: #
&scope&import&/scope&
<span style="color: #
&/dependency&
<span style="color: #
&/dependencies&
<span style="color: #
&/dependencyManagement&
<span style="color: #
<span style="color: #
<span style="color: #
<span style="color: #
<span style="color: #
&groupId&org.springframework.boot&/groupId&
<span style="color: #
&artifactId&spring-boot-maven-plugin&/artifactId&
<span style="color: #
<span style="color: #
&/plugins&
<span style="color: #
<span style="color: #
<span style="color: #
&repositories&
<span style="color: #
&repository&
<span style="color: #
&id&spring-milestones&/id&
<span style="color: #
&name&Spring Milestones&/name&
<span style="color: #
&url&https://repo.spring.io/milestone&/url&
<span style="color: #
&snapshots&
<span style="color: #
&enabled&false&/enabled&
<span style="color: #
&/snapshots&
<span style="color: #
&/repository&
<span style="color: #
&/repositories&
<span style="color: #
<span style="color: #
<span style="color: # &/project&
2). &创建主类EurekaClientApplication
使用@EnableEurekaClient注解表明是client
1 package springcloud.helloworld.eureka.
3 import org.springframework.beans.factory.annotation.V
4 import org.springframework.boot.SpringA
5 import org.springframework.boot.autoconfigure.SpringBootA
6 import org.springframework.cloud.netflix.eureka.EnableEurekaC
7 import org.springframework.web.bind.annotation.RequestM
8 import org.springframework.web.bind.annotation.RequestP
9 import org.springframework.web.bind.annotation.RestC
<span style="color: #
<span style="color: # @SpringBootApplication
<span style="color: # @EnableEurekaClient
<span style="color: # @RestController
<span style="color: #
<span style="color: # public class EurekaClientApplication {
<span style="color: #
<span style="color: #
public static void main(String[] args) {
<span style="color: #
SpringApplication.run(EurekaClientApplication.class, args);
<span style="color: #
<span style="color: #
<span style="color: #
@Value("${server.port}")
<span style="color: #
<span style="color: #
@RequestMapping("/")
<span style="color: #
public String home() {
<span style="color: #
return "hello world from port " +<span style="color: #
<span style="color: #
<span style="color: # }
3) eureka client的配置文件appication.yml
<span style="color: # eureka:
<span style="color: #
<span style="color: #
serviceUrl:
<span style="color: #
defaultZone: http://localhost:8761/eureka/
<span style="color: # server:
<span style="color: #
port: 8762
<span style="color: # spring:
<span style="color: #
application:
<span style="color: #
name: service-helloworld
4). Client启动后, 可以访问http://localhost:8762
5). 再次访问服务器端口, 可以看到Service Helloworld已经自动注册到之前的server中
参考资料:
Spring Cloud 官网
博客: http://blog.csdn.net/forezp/article/details/

我要回帖

更多关于 availabletime 的文章

 

随机推荐