Springboot项目中获取当前服务端口号

1. 启动时获取

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class AdminApplication {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(AdminApplication.class);
        Environment env = app.run( args).getEnvironment();
        System.out.println(env.getProperty("server.port"));
    }
}

2. ApplicationListener接口

import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Configuration
public class ServiceConfig implements ApplicationListener<WebServerInitializedEvent> {
    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        // 项目启动获取启动的端口号
        ServiceConfig.serverPort = event.getWebServer().getPort();
    }

    /**
     * 当前服务使用的端口号
     */
    private static int serverPort;

    public static int getServerPort() {
        return serverPort;
    }

    public void setServerPort(int serverPort) {
        ServiceConfig.serverPort = serverPort;
    }

    /**
     * 获取请求基地址
     *
     * @return String 请求基地址
     * @date 2021/11/15
     **/
    public static String getBaseUrl() {
        InetAddress address = null;
        try {
            address = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return "http://"+address.getHostAddress() +":"+ getServerPort();
    }
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot,可以通过定义多个WebSocketHandler来实现多个WebSocket服务端。下面是一个简单的示例: 1. 定义多个WebSocketHandler ```java @Component public class WebSocketHandler1 extends TextWebSocketHandler { // ... } @Component public class WebSocketHandler2 extends TextWebSocketHandler { // ... } ``` 2. 配置多个WebSocket服务端 ```java @Configuration @EnableWebSocket public class WebSocketConfig implements WebSocketConfigurer { @Autowired private WebSocketHandler1 webSocketHandler1; @Autowired private WebSocketHandler2 webSocketHandler2; @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { // 注册WebSocket服务端1 registry.addHandler(webSocketHandler1, "/ws1") .setAllowedOrigins("*") .addInterceptors(new WebSocketInterceptor()); // 注册WebSocket服务端2 registry.addHandler(webSocketHandler2, "/ws2") .setAllowedOrigins("*") .addInterceptors(new WebSocketInterceptor()); } // ... } ``` 在上面的代码,我们定义了两个WebSocketHandler,即WebSocketHandler1和WebSocketHandler2,然后在WebSocketConfig通过@Autowired将它们注入进来,并分别注册到不同的路径(/ws1和/ws2)上。 需要注意的是,如果多个WebSocket服务端都使用了相同的路径,那么只有第一个能够正常工作,因为WebSocket的路径是唯一的。 另外,我们在注册WebSocket服务端时,还可以添加一些拦截器(如上面代码的WebSocketInterceptor)来拦截WebSocket请求,进行一些特殊的处理。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值