日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Boot端口从默认更改为自定义或新端口

發布時間:2023/12/3 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot端口从默认更改为自定义或新端口 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

更改Spring Boot應用程序端口的快速指南。 application.properties文件和yml文件中的server.port屬性的示例。 以及從命令行參數@ SpringBootApplication,WebServerFactoryCustomizer

1.簡介

在本教程中,您將學習如何在Spring Boot應用程序中更改端口。

默認情況下,Spring Boot會執行許多自動配置,并提供了根據需要進行自定義的方法。

最常見的用例是將應用程序端口更改為新端口。 因為默認情況下, 嵌入式tomcat配置有8080端口。 如果您已經在同一端口上運行了另一個服務,則無法在該端口上啟動新服務。 因此,您必須在其他端口上運行該應用程序。

可以通過許多可能的方式更改端口。

讓我們看一看實用示例程序。

2.通過使用屬性和YML文件更改端口

我剛剛創建了一個新的Spring Boot應用程序,并在端口8080上的默認tomcat服務器上啟動。

application.properties

這是來自日志的消息,內容為“ Tomcat在端口8080上啟動”。

2020-05-06 20:16:17.003 INFO 19737 --- [ main] jsSSpringBootCofigurationsApplication : Starting SpringBootCofigurationsApplication on -MacBook-Pro-2.local with PID 19737 2020-05-06 20:16:17.006 INFO 19737 --- [ main] jsSSpringBootCofigurationsApplication : No active profile set, falling back to default profiles: default 2020-05-06 20:16:17.921 INFO 19737 --- [ main] osbwembedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2020-05-06 20:16:17.932 INFO 19737 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2020-05-06 20:16:17.933 INFO 19737 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.33] 2020-05-06 20:16:18.044 INFO 19737 --- [ main] oaccC[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2020-05-06 20:16:18.044 INFO 19737 --- [ main] osweb.context.ContextLoader : Root WebApplicationContext: initialization completed in 999 ms 2020-05-06 20:16:18.222 INFO 19737 --- [ main] ossconcurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2020-05-06 20:16:18.387 INFO 19737 --- [ main] osbwembedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2020-05-06 20:16:18.391 INFO 19737 --- [ main] jsSSpringBootCofigurationsApplication : Started SpringBootCofigurationsApplication in 1.689 seconds (JVM running for 2.651)

現在要更改端口,只需在application.properties文件中添加一個屬性,如下所示。


[server.port = 9000]

上面的屬性server.port會將tomcat端口更改為9000。屬性文件將位于resources文件夾下。

添加后,您需要重新啟動應用程序以使配置更改生效。

從應用程序控制臺僅顯示最后幾行。 現在默認端口8080更改為9000

2020-05-06 20:20:05.358信息初始化ExecutorService'applicationTaskExecutor'

2020-05-06 20:20:05.500信息Tomcat在具有上下文路徑“”的端口:9000 (http)上啟動 2020-05-06 20:20:05.504信息在1.369秒內啟動了SpringBootCofigurationsApplication(JVM運行2.007)

application.yml

除了application.properties文件之外,還有一個其他文件,該文件將由spring boot自動在src / main / resources文件夾下進行掃描。

application: name: spring-boot-configurations server: port: 9006

但是,如果兩個文件中都存在端口屬性,則application.properties文件端口將被認為具有最高優先級。

3. Spring Boot中的特定于環境的端口

與application.properties類似,對于每個環境(例如dev,sit,QA和prod),您可以具有不同的屬性文件。

application-dev.properties

server.port = 9008

application-qa.properties

server.port = 8008

這些文件對于在多個環境中部署應用程序而對每次更改或部署都沒有任何更改最有用。

4.以編程方式更改端口

如果您無權訪問屬性文件,則可以使用@SpringBootApplication批注類或自定義嵌入式tomcat服務器設置來實現 。

4.1 @SpringBootApplication類級別

使用相同的屬性“ server.port”設置自定義端口。 下面的程序設置為9009。

package com.javaprogramto.springboot.SpringBootCofigurations; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import java.util.HashMap; import java.util.Map; @SpringBootApplication public class SpringBootCofigurationsApplication { public static void main(String[] args) { // SpringApplication.run(SpringBootCofigurationsApplication.class, args); SpringApplication app = new SpringApplication(SpringBootCofigurationsApplication. class ); Map<String, Object> customConfig = new HashMap<>(); customConfig.put( "server.port" , "9009" ); app.setDefaultProperties(customConfig); app.run(args); } }

4.2使用WebServerFactoryCustomizer界面

使用Interface WebServerFactoryCustomizer <ConfigurableWebServerFactory>實現任何類,并實現customset()方法以使用setPort()方法設置端口。

package com.javaprogramto.springboot.SpringBootCofigurations; import org.springframework.boot.web.server.ConfigurableWebServerFactory; import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.stereotype.Component; @Componentpublic class CustomEmbededPortChange implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> { @Override public void customize(ConfigurableWebServerFactory factory) { factory.setPort(8086); } }

如果收到此錯誤,請確保沒有兩次調用SpringApplication.run()。

ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name defined in class path resource [org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfiguration. defined in ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springApplicationAdminRegistrar' ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name path resource [org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfiguration. class ]: Invocation of init method failed; nested exception is javax.management.InstanceAlreadyExistsException: org.springframework.boot:type=Admin,name=SpringApplication ]: Invocation of init method failed; nested exception is javax.management.InstanceAlreadyExistsException: org.springframework.boot:type=Admin,name=SpringApplication 2020-05-06 21:26:09.907 INFO 21555 --- [ main] ossconcurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor' 2020-05-06 21:26:09.908 INFO 21555 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2020-05-06 21:26:09.915 INFO 21555 --- [ main] ConditionEvaluationReportLoggingListener : Error starting ApplicationContext. To display the conditions report re-run your application with Error starting ApplicationContext. To display the conditions report re-run your application with enabled. 'debug' Error starting ApplicationContext. To display the conditions report re-run your application with enabled. 2020-05-06 21:26:09.923 ERROR 21555 --- [ main] osboot.SpringApplication : Application run failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name class path resource [org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfiguration. defined in org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springApplicationAdminRegistrar' org.springframework.beans.factory.BeanCreationException: Error creating bean with name path resource [org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfiguration. class ]: Invocation of init method failed; nested exception is javax.management.InstanceAlreadyExistsException: org.springframework.boot:type=Admin,name=SpringApplication ]: Invocation of init method failed; nested exception is javax.management.InstanceAlreadyExistsException: org.springframework.boot:type=Admin,name=SpringApplication at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:882) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE] at com.javaprogramto.springboot.SpringBootCofigurations.SpringBootCofigurationsApplication.main(SpringBootCofigurationsApplication.java:24) ~[classes/:na] Caused by: javax.management.InstanceAlreadyExistsException: org.springframework.boot:type=Admin,name=SpringApplication at java.management/com.sun.jmx.mbeanserver.Repository.addMBean(Repository.java:436) ~[na:na] at java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerWithRepository(DefaultMBeanServerInterceptor.java:1855) ~[na:na] at java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerDynamicMBean(DefaultMBeanServerInterceptor.java:955) ~[na:na] at java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerObject(DefaultMBeanServerInterceptor.java:890) ~[na:na] at java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:320) ~[na:na] at java.management/com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:522) ~[na:na] at org.springframework.boot.admin.SpringApplicationAdminMXBeanRegistrar.afterPropertiesSet(SpringApplicationAdminMXBeanRegistrar.java:129) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1855) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1792) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE] ... 14 common frames omitted

5.使用命令行參數

如果您不是開發人員,則只進行部署。 如果要獨立啟動應用程序,則可以通過指定–server,port標志如下運行java -jar命令。

java -jar?Spring-Boot-Cofigurations-0.0.1-SNAPSHOT.jar --server.port=9099

或者您可以將其用作eclipse或Intelleji或任何IDE的VM參數。

java -jar -Dserver.port=9099 Spring-Boot-Cofigurations-0.0.1-SNAPSHOT.jar

6.評估順序(優先級)

如果您在不知不覺中以多種方式進行配置,則應非常小心,因為它可能在不同的端口上運行,并且也很難發現。

這是從高優先級到低優先級的列表。

  • 嵌入式服務器配置– WebServerFactoryCustomizer接口
  • 命令行參數
  • 屬性文件
  • @SpringBootApplication主要配置

7,結論

在本文中,您已經了解了從Spring Boot應用程序中的默認端口將端口更改為自定義端口的方法。

在倉庫中,所有配置均已注釋。 請為您取消注釋所需的一個。

本文顯示的所有代碼都在GitHub上。

您可以直接下載該項目,并且可以在本地運行而沒有任何錯誤。

  • 在GitHub上查看
  • 下載

如果您有任何疑問,請在評論部分中發布。

翻譯自: https://www.javacodegeeks.com/2020/05/spring-boot-port-change-to-custom-or-new-port-from-default.html

總結

以上是生活随笔為你收集整理的Spring Boot端口从默认更改为自定义或新端口的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。