spring 云应用程序中定义的端口 8080 在哪里?

where is port 8080 defined in spring cloud app?

我一直在逐行研究 this spring cloud app on github 中的代码,然后将其下载并安装到我的开发箱中。当您在网络浏览器中键入 http://localhost:8080 时,来自 ui 应用程序的内容将加载,并且能够与来自 authserverresource 应用程序的内容进行交互。

Port 9999 是为 authserver 应用程序定义的,在 ui 应用程序的 zuul route 定义中,也在 authserver 应用程序的 [=20] 中=].

同样,port 9000 是为 resource 应用程序定义的,在 ui 应用程序的路由定义中,也在 resource 应用程序的 application.properties 中.

但我找不到任何对 port 8080 的引用,即使在检查了三个应用程序中的每一行代码并且还做了 Ctrl-H 对整个 eclipse 工作区进行全文搜索以查找单词 8080

那么 ui 应用程序如何知道在 port 8080 上提供其客户端内容?

您可以使用.properties 或.yml 文件来存储应用程序的所有配置。在那里你可以定义服务器端口 属性 来监听你想要的任何端口。

如果您使用 .properties 文件,它将是这样的:

server.port=8082

或者在 .yml 文件中它会是这样的:

server:
  port: 8082

有关 spring 中的属性和配置的更多信息,请查看 this

UI 模块具有以下 Spring 启动依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

该依赖项有其自己的依赖项,如下所示:

Spring Boot Starter Web POM.xml

这包括 Spring Boot 的 Tomcat Starter:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>

这会引入实际的 Tomcat 嵌入式 jar

Spring Boot Starter Tomcat POM.xml

因为 UI 应用程序是一个 Spring 引导应用程序(例如,它使用 Spring 引导父 POM 并已将其主要 class 注释为 @SpringBootApplication) 它会根据类路径为我们自动配置很多东西。

请注意此 class 中的 @ConditionalOnClass 注释:

EmbeddedServletContainerAutoConfiguration

这最终会导致这个 class:

TomcatEmbeddedServletContainer

触发嵌入式 Tomcat 服务器的启动。

默认端口 8080 设置实际上来自嵌入式 Tomcat 依赖项,如下所示:

org.apache.catalina.startup.Tomcat

protected int port = 8080;

有所有 spring 引导默认属​​性的参考,一旦您遇到一些未知的默认值,就应该检查这些属性:)

https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html