【问题标题】:spring boot app actually running on port 0, instead of randomspring boot 应用程序实际上在端口 0 上运行,而不是随机运行
【发布时间】:2019-07-23 20:17:09
【问题描述】:

我的 application.properties 文件包含

server.port=0

应该由spring boot捕获并将其设置为随机端口。

相反,它实际上是在端口 0 上启动它,甚至在 spring 日志中也是如此:

01/Mar/2019 12:50:43,600- TomcatEmbeddedServletContainer: Tomcat initialized with port(s): 0 (http)

Eureka 将其视为“up”服务,并提供该服务的链接 (localhost:0/info),单击它会显示我的浏览器“ERR_ADDRESS_INVALID”,猜测是因为它不是有效端口..

App正在运行向Eureka发送心跳,但是为什么spring没有给它设置一个随机的端口号?

有没有可以防止随机的设置?如果是,如何取消设置?

编辑: server.port=0 的任何新启动应用程序都是随机的,它只是不适用于存在大量依赖项的现有 Spring Boot 应用程序

【问题讨论】:

  • 你的 spring boot 版本是什么,它不起作用?
  • @Karol Spring boot 2.1.6.RELEASE with Greenwich.SR1
  • 应用程序在正确的动态端口上启动,但 Eureka 仅将端口视为 0
  • @GauravVarma 看看这个:stackoverflow.com/questions/53491970/…

标签: spring spring-boot port


【解决方案1】:

它实际上并没有在端口 0 上启动它,而是在一个随机端口上启动它。 在您的 eureka 服务器中,您会看到它位于端口 0 中,但如果您将自己置于顶部而不单击,您将在浏览器栏中看到端口不同。

在日志中显示:

INFO  o.s.b.w.e.tomcat.TomcatWebServer - Tomcat initialized with port(s): 0 (http)

但后来改了:

INFO  o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port(s): 64039 (http) with context path ''
INFO  o.s.c.n.e.s.EurekaAutoServiceRegistration - Updating port to 64039

因此,如果您在相互通信时遇到问题,这是因为在每个以随机端口开头的微服务中,您必须在 application.ymlpreferIpAddress 中配置以通过 ip 而不是通过主机名找到它:

eureka:
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://localhost:portServer/eureka/
  instance:
    preferIpAddress: true

【讨论】:

  • 我已将preferIpAddress 设置为true,但问题仍然存在
  • 我猜你有 spring-boot-starter-actuator 依赖来访问 url host:port/actuator/info,如果你遇到同样的错误,你试过火狐浏览器吗?
【解决方案2】:

尝试以编程方式设置端口:

@Configuration
public class ServletConfig {

    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {
        return (container -> {
            container.setPort(new Random().nextInt(65_535) + 1_000);
        });
    }
}

另外,这可能会有所帮助:Eureka not able to find port when running microservices on random port

【讨论】:

  • 这确实在随机端口上启动了应用程序,尽管可能为时已晚,因为尤里卡仍在查看端口 0
  • 您使用的是哪个版本的 Spring Boot 和 Spring Cloud?
猜你喜欢
  • 1970-01-01
  • 2016-02-15
  • 2022-01-02
  • 1970-01-01
  • 2018-04-21
  • 2021-01-29
  • 1970-01-01
  • 2019-10-14
  • 2018-05-13
相关资源
最近更新 更多