简体   繁体   English

spring-cloud 随机端口未注册到 Eureka

[英]spring-cloud random port not registered to Eureka

I'm trying to get my microservices to come up on random ports.我正在尝试让我的微服务出现在随机端口上。 If I specify server.port: 0 in the service bootstrap.yml as suggested in Spring Cloud app doesn't register correct port to Eureka I get the following NPE:如果我按照Spring 中的建议在服务 bootstrap.yml 中指定 server.port: 0 Cloud app doesn't register correct port to Eureka我得到以下 NPE:

    2015-04-14 16:24:34,357  INFO          com.fisc.restmocker.Application:  47 - Starting Application on JSR-IAAS-20 with PID 4820 (C:\iaas2.0\git\FII-RestMocker\build\classes\main started by jsr in C:\iaas2.0\git\FII-RestMocker)
    2015-04-14 16:24:34,482  INFO ation.AnnotationConfigApplicationContext: 510 - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@4fb61f4a: startup date [Tue Apr 14 16:24:34 EDT 2015]; root of context hierarchy
    2015-04-14 16:24:34,748  INFO ion.AutowiredAnnotationBeanPostProcessor: 153 - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
    2015-04-14 16:24:35,217  WARN ix.config.sources.URLConfigurationSource: 120 - No URLs will be polled as dynamic configuration sources.
    2015-04-14 16:24:35,560  INFO ontext.support.DefaultLifecycleProcessor: 341 - Starting beans in phase 0
    2015-04-14 16:24:35,560  INFO lientConfigServiceBootstrapConfiguration:  92 - Locating configserver via discovery
    2015-04-14 16:24:35,592  WARN lientConfigServiceBootstrapConfiguration: 113 - Could not locate configserver via discovery
    java.lang.NullPointerException: null
        at com.netflix.discovery.DiscoveryClient$$FastClassBySpringCGLIB$$a84c8cb4.invoke(<generated>) ~[spring-core-4.1.5.RELEASE.jar:1.1.147]
        at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.1.5.RELEASE.jar:4.1.5.RELEASE]
        at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717) ~[spring-aop-4.1.5.RELEASE.jar:4.1.5.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.1.5.RELEASE.jar:4.1.5.RELEASE]
        at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:133) ~[spring-aop-4.1.5.RELEASE.jar:4.1.5.RELEASE]
        at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:121) ~[spring-aop-4.1.5.RELEASE.jar:4.1.5.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.1.5.RELEASE.jar:4.1.5.RELEASE]
        at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653) ~[spring-aop-4.1.5.RELEASE.jar:4.1.5.RELEASE]
        at com.netflix.discovery.DiscoveryClient$$EnhancerBySpringCGLIB$$96b726d7.getNextServerFromEureka(<generated>) ~[spring-core-4.1.5.RELEASE.jar:1.1.147]
        at org.springframework.cloud.netflix.config.DiscoveryClientConfigServiceBootstrapConfiguration.refresh(DiscoveryClientConfigServiceBootstrapConfiguration.java:93) [spring-cloud-netflix-core-1.0.0.RELEASE.jar:1.0.0.RELEASE]
        at org.springframework.cloud.netflix.config.DiscoveryClientConfigServiceBootstrapConfiguration.onApplicationEvent(DiscoveryClientConfigServiceBootstrapConfiguration.java:65) [spring-cloud-netflix-core-1.0.0.RELEASE.jar:1.0.0.RELEASE]
        at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:151) [spring-context-4.1.5.RELEASE.jar:4.1.5.RELEASE]
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:128) [spring-context-4.1.5.RELEASE.jar:4.1.5.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:331) [spring-context-4.1.5.RELEASE.jar:4.1.5.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:773) [spring-context-4.1.5.RELEASE.jar:4.1.5.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:483) [spring-context-4.1.5.RELEASE.jar:4.1.5.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686) [spring-boot-1.2.2.RELEASE.jar:1.2.2.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:320) [spring-boot-1.2.2.RELEASE.jar:1.2.2.RELEASE]
        at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:139) [spring-boot-1.2.2.RELEASE.jar:1.2.2.RELEASE]
        at org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.java:125) [spring-cloud-config-client-1.0.0.RELEASE.jar:1.0.0.RELEASE]
        at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:75) [spring-cloud-config-client-1.0.0.RELEASE.jar:1.0.0.RELEASE]
        at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:1) [spring-cloud-config-client-1.0.0.RELEASE.jar:1.0.0.RELEASE]
        at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:151) [spring-context-4.1.5.RELEASE.jar:4.1.5.RELEASE]
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:128) [spring-context-4.1.5.RELEASE.jar:4.1.5.RELEASE]
        at org.springframework.boot.context.event.EventPublishingRunListener.publishEvent(EventPublishingRunListener.java:100) [spring-boot-1.2.2.RELEASE.jar:1.2.2.RELEASE]
        at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:59) [spring-boot-1.2.2.RELEASE.jar:1.2.2.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:285) [spring-boot-1.2.2.RELEASE.jar:1.2.2.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:957) [spring-boot-1.2.2.RELEASE.jar:1.2.2.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:946) [spring-boot-1.2.2.RELEASE.jar:1.2.2.RELEASE]
        at com.fisc.restmocker.Application.main(Application.java:18) [main/:na]
    2015-04-14 16:24:35,592  INFO          com.fisc.restmocker.Application:  56 - Started Application in 1.641 seconds (JVM running for 2.978)

Here is the bootstrap.yml:这是 bootstrap.yml:

    info:
      description: FII user interface mock service

    spring:
      application:
        name: FII-rest-mocker
      cloud:
        config:
          failFast: true
          discovery:
            enabled: true
            serviceId: config-server
          username: user
          password: password

    logging:
      level:
        com.netflix.discovery: 'OFF'
        org.springframework.cloud: 'DEBUG'

    eureka:
      instance:
        leaseRenewalIntervalInSeconds: 10
        metadataMap:
          instanceId: ${spring.application.name}:${spring.application.instance_id:${random.value}}
      client:
        serviceUrl:
          defaultZone: ${vcap.services.${PREFIX:}eureka.credentials.uri:http://user:password@localhost:8761}/eureka/

    server:
      port: 0

If I remove the server.port from the bootstrap.yml and place it in fii-rest-mocker.yml inside the configuration server, then my service starts up on a random port, but is registered on 8080 in Eureka.如果我从 bootstrap.yml 中删除 server.port 并将其放在配置服务器内的 fii-rest-mocker.yml 中,那么我的服务将在一个随机端口上启动,但在 Eureka 中注册到 8080。

I am only running one Eureka server and one Config server.我只运行一台 Eureka 服务器和一台配置服务器。 All services are running on the same machine.所有服务都在同一台机器上运行。

So, how does one get a service to boot and register in Eureka on a random port?那么,如何在 Eureka 的随机端口上启动和注册服务呢?

I'm using the 1.0.0.RELEASE version of spring-cloud and 1.2.2.RELEASE of spring-boot.我正在使用 1.0.0.RELEASE 版本的 spring-cloud 和 1.2.2.RELEASE spring-boot。

随机端口目前与spring.cloud.config.discovery.enabled: true不兼容。

@spencergibb: Strange you say it is not supposed to work at the moment. @spencergibb:奇怪的是你说它目前不应该起作用。

I manage to get it work (apparently) by forcing the property eureka.instance.non-secure-port to 0 instead of the default 80.我设法通过强制属性eureka.instance.non-secure-port为 0 而不是默认的 80 使其工作(显然)。

Setting this value to ZERO triggers the execution of EurekaDiscoveryClientConfiguration.containerPortInitializer() that will retrieve the actual port number from the embedded servlet container.将此值设置EurekaDiscoveryClientConfiguration.containerPortInitializer()会触发EurekaDiscoveryClientConfiguration.containerPortInitializer()的执行, EurekaDiscoveryClientConfiguration.containerPortInitializer()从嵌入式 servlet 容器中检索实际端口号。

Did I miss something?我错过了什么?

Right now (Spring boot 1.5.10 and Edgeware.SR2), you can just use a randomized port, but you can't set a management port.现在(Spring boot 1.5.10 和 Edgeware.SR2),您可以只使用随机端口,但不能设置管理端口。 This will trigger a bug where the application is registered with the management port which doesn't work)这将触发一个错误,即应用程序注册到管理端口不起作用)

Try registering a unique instance name for each running instance.尝试为每个正在运行的实例注册一个唯一的实例名称。 Default (non-unique) instance names will just replace the running instance.默认(非唯一)实例名称将仅替换正在运行的实例。

This can be accomplished by setting this property:这可以通过设置此属性来完成:

eureka.instance.instance-id=${spring.application.name}:${spring.application.instance_id:${random.value}}

According to the documentation Doc Spring Cloud , insert the following configuration in the spring configuration file application.yml :根据文档Doc Spring Cloud ,在 spring 配置文件application.yml中插入如下配置:

eureka:
  instance:
    instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM