Spring Boot获取启动端口2种方式

我这里的版本是2.1.8

package com.ysy.config;

import com.ysy.HelloDemo;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;

/**
 * Created by Administrator on 2020/1/28 19:34
 */
@Configuration
public class webServerConfig {

   //方式1
    @EventListener(WebServerInitializedEvent.class)
    public void onWebServerReady(WebServerInitializedEvent event) {
        System.out.println("1.当前WebServer实现类为:"+event.getWebServer().getClass().getName());
        WebServerApplicationContext applicationContext = event.getApplicationContext();
        HelloDemo bean = applicationContext.getBean(HelloDemo.class);
        int port = applicationContext.getWebServer().getPort();
        String name = applicationContext.getWebServer().getClass().getName();
        System.out.println("获取bean对象:"+bean);
        System.out.println("1.容器端口"+port+",实现类-"+name);
    }
    /**
     * 在spring boot应用启动后回调
     * @param context
     * @return
     */
//方式2
    @Bean
    public ApplicationRunner runner(WebServerApplicationContext context) {
        return args -> {
            System.out.println("2.当前WebServer实现类为:"+context.getWebServer().getClass().getName());
        };
    }


    for 1.5.x  Spring Boot
//public class ServerConfig implements ApplicationListener<EmbeddedServletContainerInitializedEvent> {
//    private int serverPort;;
//
//    public int getPort() {
//        return this.serverPort;
//    }
//
//    @Override
//    public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) {
//        this.serverPort = event.getEmbeddedServletContainer().getPort();
//    }
//}
}

E:\software\JDK1.8\jdk8\bin\java.exe -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:E:\software\IntelliJ IDEA 2019.1.3\lib\idea_rt.jar=51066:E:\software\IntelliJ IDEA 2019.1.3\bin" -Dfile.encoding=UTF-8 -classpath E:\software\JDK1.8\jdk8\jre\lib\charsets.jar;E:\software\JDK1.8\jdk8\jre\lib\deploy.jar;E:\software\JDK1.8\jdk8\jre\lib\ext\access-bridge-64.jar;E:\software\JDK1.8\jdk8\jre\lib\ext\cldrdata.jar;E:\software\JDK1.8\jdk8\jre\lib\ext\dnsns.jar;E:\software\JDK1.8\jdk8\jre\lib\ext\jaccess.jar;E:\software\JDK1.8\jdk8\jre\lib\ext\jfxrt.jar;E:\software\JDK1.8\jdk8\jre\lib\ext\localedata.jar;E:\software\JDK1.8\jdk8\jre\lib\ext\nashorn.jar;E:\software\JDK1.8\jdk8\jre\lib\ext\sunec.jar;E:\software\JDK1.8\jdk8\jre\lib\ext\sunjce_provider.jar;E:\software\JDK1.8\jdk8\jre\lib\ext\sunmscapi.jar;E:\software\JDK1.8\jdk8\jre\lib\ext\sunpkcs11.jar;E:\software\JDK1.8\jdk8\jre\lib\ext\zipfs.jar;E:\software\JDK1.8\jdk8\jre\lib\javaws.jar;E:\software\JDK1.8\jdk8\jre\lib\jce.jar;E:\software\JDK1.8\jdk8\jre\lib\jfr.jar;E:\software\JDK1.8\jdk8\jre\lib\jfxswt.jar;E:\software\JDK1.8\jdk8\jre\lib\jsse.jar;E:\software\JDK1.8\jdk8\jre\lib\management-agent.jar;E:\software\JDK1.8\jdk8\jre\lib\plugin.jar;E:\software\JDK1.8\jdk8\jre\lib\resources.jar;E:\software\JDK1.8\jdk8\jre\lib\rt.jar;E:\project\springboot_learning\springboot01\target\classes;E:\response\org\springframework\boot\spring-boot-starter-undertow\2.1.8.RELEASE\spring-boot-starter-undertow-2.1.8.RELEASE.jar;E:\response\io\undertow\undertow-core\2.0.26.Final\undertow-core-2.0.26.Final.jar;E:\response\org\jboss\logging\jboss-logging\3.3.3.Final\jboss-logging-3.3.3.Final.jar;E:\response\org\jboss\xnio\xnio-api\3.3.8.Final\xnio-api-3.3.8.Final.jar;E:\response\org\jboss\xnio\xnio-nio\3.3.8.Final\xnio-nio-3.3.8.Final.jar;E:\response\io\undertow\undertow-servlet\2.0.26.Final\undertow-servlet-2.0.26.Final.jar;E:\response\org\jboss\spec\javax\annotation\jboss-annotations-api_1.2_spec\1.0.2.Final\jboss-annotations-api_1.2_spec-1.0.2.Final.jar;E:\response\io\undertow\undertow-websockets-jsr\2.0.26.Final\undertow-websockets-jsr-2.0.26.Final.jar;E:\response\org\jboss\spec\javax\websocket\jboss-websocket-api_1.1_spec\1.1.4.Final\jboss-websocket-api_1.1_spec-1.1.4.Final.jar;E:\response\javax\servlet\javax.servlet-api\4.0.1\javax.servlet-api-4.0.1.jar;E:\response\org\glassfish\javax.el\3.0.0\javax.el-3.0.0.jar;E:\response\org\springframework\boot\spring-boot-starter\2.1.8.RELEASE\spring-boot-starter-2.1.8.RELEASE.jar;E:\response\org\springframework\boot\spring-boot\2.1.8.RELEASE\spring-boot-2.1.8.RELEASE.jar;E:\response\org\springframework\spring-context\5.1.9.RELEASE\spring-context-5.1.9.RELEASE.jar;E:\response\org\springframework\spring-aop\5.1.9.RELEASE\spring-aop-5.1.9.RELEASE.jar;E:\response\org\springframework\spring-expression\5.1.9.RELEASE\spring-expression-5.1.9.RELEASE.jar;E:\response\org\springframework\boot\spring-boot-autoconfigure\2.1.8.RELEASE\spring-boot-autoconfigure-2.1.8.RELEASE.jar;E:\response\org\springframework\boot\spring-boot-starter-logging\2.1.8.RELEASE\spring-boot-starter-logging-2.1.8.RELEASE.jar;E:\response\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;E:\response\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;E:\response\org\apache\logging\log4j\log4j-to-slf4j\2.11.2\log4j-to-slf4j-2.11.2.jar;E:\response\org\apache\logging\log4j\log4j-api\2.11.2\log4j-api-2.11.2.jar;E:\response\org\slf4j\jul-to-slf4j\1.7.28\jul-to-slf4j-1.7.28.jar;E:\response\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;E:\response\org\yaml\snakeyaml\1.23\snakeyaml-1.23.jar;E:\response\org\slf4j\slf4j-api\1.7.28\slf4j-api-1.7.28.jar;E:\response\org\springframework\spring-core\5.1.9.RELEASE\spring-core-5.1.9.RELEASE.jar;E:\response\org\springframework\spring-jcl\5.1.9.RELEASE\spring-jcl-5.1.9.RELEASE.jar;E:\response\org\springframework\boot\spring-boot-starter-webflux\2.1.8.RELEASE\spring-boot-starter-webflux-2.1.8.RELEASE.jar;E:\response\org\springframework\boot\spring-boot-starter-json\2.1.8.RELEASE\spring-boot-starter-json-2.1.8.RELEASE.jar;E:\response\com\fasterxml\jackson\core\jackson-databind\2.9.9.3\jackson-databind-2.9.9.3.jar;E:\response\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;E:\response\com\fasterxml\jackson\core\jackson-core\2.9.9\jackson-core-2.9.9.jar;E:\response\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.9\jackson-datatype-jdk8-2.9.9.jar;E:\response\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.9\jackson-datatype-jsr310-2.9.9.jar;E:\response\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.9\jackson-module-parameter-names-2.9.9.jar;E:\response\org\springframework\boot\spring-boot-starter-reactor-netty\2.1.8.RELEASE\spring-boot-starter-reactor-netty-2.1.8.RELEASE.jar;E:\response\io\projectreactor\netty\reactor-netty\0.8.11.RELEASE\reactor-netty-0.8.11.RELEASE.jar;E:\response\io\netty\netty-codec-http\4.1.39.Final\netty-codec-http-4.1.39.Final.jar;E:\response\io\netty\netty-common\4.1.39.Final\netty-common-4.1.39.Final.jar;E:\response\io\netty\netty-buffer\4.1.39.Final\netty-buffer-4.1.39.Final.jar;E:\response\io\netty\netty-transport\4.1.39.Final\netty-transport-4.1.39.Final.jar;E:\response\io\netty\netty-resolver\4.1.39.Final\netty-resolver-4.1.39.Final.jar;E:\response\io\netty\netty-codec\4.1.39.Final\netty-codec-4.1.39.Final.jar;E:\response\io\netty\netty-codec-http2\4.1.39.Final\netty-codec-http2-4.1.39.Final.jar;E:\response\io\netty\netty-handler\4.1.39.Final\netty-handler-4.1.39.Final.jar;E:\response\io\netty\netty-handler-proxy\4.1.39.Final\netty-handler-proxy-4.1.39.Final.jar;E:\response\io\netty\netty-codec-socks\4.1.39.Final\netty-codec-socks-4.1.39.Final.jar;E:\response\io\netty\netty-transport-native-epoll\4.1.39.Final\netty-transport-native-epoll-4.1.39.Final-linux-x86_64.jar;E:\response\io\netty\netty-transport-native-unix-common\4.1.39.Final\netty-transport-native-unix-common-4.1.39.Final.jar;E:\response\org\hibernate\validator\hibernate-validator\6.0.17.Final\hibernate-validator-6.0.17.Final.jar;E:\response\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;E:\response\com\fasterxml\classmate\1.4.0\classmate-1.4.0.jar;E:\response\org\springframework\spring-web\5.1.9.RELEASE\spring-web-5.1.9.RELEASE.jar;E:\response\org\springframework\spring-beans\5.1.9.RELEASE\spring-beans-5.1.9.RELEASE.jar;E:\response\org\springframework\spring-webflux\5.1.9.RELEASE\spring-webflux-5.1.9.RELEASE.jar;E:\response\io\projectreactor\reactor-core\3.2.12.RELEASE\reactor-core-3.2.12.RELEASE.jar;E:\response\org\reactivestreams\reactive-streams\1.0.3\reactive-streams-1.0.3.jar;E:\response\org\synchronoss\cloud\nio-multipart-parser\1.1.0\nio-multipart-parser-1.1.0.jar;E:\response\org\synchronoss\cloud\nio-stream-storage\1.1.3\nio-stream-storage-1.1.3.jar com.ysy.HelloDemo

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.8.RELEASE)

2020-01-28 19:46:54.103  INFO 5276 --- [           main] com.ysy.HelloDemo                        : Starting HelloDemo on Win7-2020USJWDB with PID 5276 (E:\project\springboot_learning\springboot01\target\classes started by Administrator in E:\project\springboot_learning\springboot01)
2020-01-28 19:46:54.109  INFO 5276 --- [           main] com.ysy.HelloDemo                        : No active profile set, falling back to default profiles: default
2020-01-28 19:46:57.356  INFO 5276 --- [           main] org.xnio                                 : XNIO version 3.3.8.Final
2020-01-28 19:46:57.377  INFO 5276 --- [           main] org.xnio.nio                             : XNIO NIO Implementation Version 3.3.8.Final
2020-01-28 19:46:57.489  INFO 5276 --- [           main] o.s.b.w.e.undertow.UndertowWebServer     : Undertow started on port(s) 8080 (http)
1.当前WebServer实现类为:org.springframework.boot.web.embedded.undertow.UndertowWebServer
获取bean对象:com.ysy.HelloDemo$$EnhancerBySpringCGLIB$$d8c48f55@3fabf088
1.容器端口8080,实现类-org.springframework.boot.web.embedded.undertow.UndertowWebServer
2020-01-28 19:46:57.497  INFO 5276 --- [           main] com.ysy.HelloDemo                        : Started HelloDemo in 5.056 seconds (JVM running for 6.906)
2.当前WebServer实现类为:org.springframework.boot.web.embedded.undertow.UndertowWebServer

Process finished with exit code -1

2个方法都可以兼容webservlet和Reactive;
其中WebServerInitializedEvent的兼容性更强(方便扩展非web)

各自继承关系如下:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Spring Cloud中获取IP和端口,可以使用以下步骤: 1. 首先,确保在项目的pom.xml文件中添加了spring-cloud-starter-loadbalancer依赖项。这可以通过以下方式完成: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency> ``` 这个依赖项将提供负载均衡功能,以便在获取IP和端口时使用。 2. 其次,在应用程序的配置文件中,可以使用以下属性来配置Eureka实例和IP地址: ```properties eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port} eureka.instance.prefer-ip-address=true ``` 这将配置Eureka实例的ID为IP地址和端口号的组合,并设置优先使用IP地址。 3. 最后,可以创建一个IpConfiguration类来获取本机端口号。这可以通过实现ApplicationListener接口并监听WebServerInitializedEvent事件来完成。以下是一个示例IpConfiguration类的代码: ```java import org.springframework.boot.web.context.WebServerInitializedEvent; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; @Component public class IpConfiguration implements ApplicationListener<WebServerInitializedEvent> { private int serverPort; @Override public void onApplicationEvent(WebServerInitializedEvent event) { this.serverPort = event.getWebServer().getPort(); } public int getPort() { return this.serverPort; } } ``` 这个类将在应用程序启动获取本机端口号,并提供一个getPort方法获取端口号。 请注意,以上步骤适用于Spring Boot版本2.1.3,并且假设您已经正确配置了Eureka和负载均衡器。 #### 引用[.reference_title] - *1* [Spring Cloud根据服务名获取服务的ip端口](https://blog.csdn.net/weixin_43888891/article/details/126755927)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [spring cloud配置注册中心显示服务的ip地址和端口](https://blog.csdn.net/web15687102624/article/details/126504257)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [SpringBoot / SpringCloud获取启动端口号](https://blog.csdn.net/weixin_42639445/article/details/89013405)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值