Spring?Boot應(yīng)用打WAR包后無法注冊到Nacos的問題及解決方法
在微服務(wù)架構(gòu)中,服務(wù)注冊與發(fā)現(xiàn)是至關(guān)重要的一環(huán)。Nacos 作為阿里巴巴開源的注冊中心,能夠很好地滿足這一需求。然而,在將 Spring Boot 應(yīng)用打包成 WAR 部署到外部服務(wù)器時,可能會遇到服務(wù)無法注冊到 Nacos 的問題。本文將詳細講解這一問題的解決方案。
問題描述
在開發(fā)過程中,通常使用 JAR 包運行 Spring Boot 應(yīng)用,這種方式下,服務(wù)注冊到 Nacos 通常沒有問題。然而,當(dāng)我們將 Spring Boot 應(yīng)用打包成 WAR 并部署到外部 Tomcat 服務(wù)器時,可能會遇到服務(wù)無法注冊到 Nacos 的情況。其原因主要是應(yīng)用獲取不到正確的服務(wù)器端口。
解決方案
為了在 WAR 包部署時正確地注冊服務(wù)到 Nacos,我們需要動態(tài)地獲取實際使用的服務(wù)器端口,并將其設(shè)置到 Nacos 的服務(wù)注冊中。以下是一個具體的實現(xiàn)代碼:
import com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.Query;
import java.lang.management.ManagementFactory;
import java.util.Set;
/**
* 解決 Spring Boot 應(yīng)用打 WAR 包后無法注冊到 Nacos 的問題
* 通過動態(tài)獲取服務(wù)器端口并注冊到 Nacos
*/
@Slf4j
@ConditionalOnProperty(prefix = "project.deploy", name = "mode", havingValue = "war")
@Component
public class NacosConfig implements ApplicationRunner {
@Autowired
private NacosAutoServiceRegistration registration;
@Value("${server.port:8080}")
Integer port;
@Override
public void run(ApplicationArguments args) {
if (registration != null && port != null) {
Integer serverPort = port;
try {
serverPort = new Integer(getServerPort());
} catch (Exception e) {
log.warn("getServerPort warn", e);
log.info("getServerPort fail, use the config-file's port {}", port);
}
registration.setPort(serverPort);
registration.start();
}
}
/**
* 獲取實際使用的服務(wù)器端口
*/
public String getServerPort() throws Exception {
MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
Set<ObjectName> objectNames = beanServer.queryNames(new ObjectName("*:type=Connector,*"), Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
String port = objectNames.iterator().next().getKeyProperty("port");
log.debug("getServerPort {}", port);
return port;
}
}代碼解析
1. 類與注解
@Slf4j
@ConditionalOnProperty(prefix = "project.deploy", name = "mode", havingValue = "war")
@Component
public class NacosConfig implements ApplicationRunner {@Slf4j:Lombok 提供的注解,用于生成日志記錄器。@ConditionalOnProperty:只有在project.deploy.mode屬性值為war時,才會創(chuàng)建這個NacosConfigbean。@Component:標(biāo)記為 Spring 的組件,使其能夠被 Spring 掃描和管理。ApplicationRunner:實現(xiàn)該接口的run方法將在應(yīng)用啟動時運行。
2. 自動注入與配置
@Autowired
private NacosAutoServiceRegistration registration;
@Value("${server.port:8080}")
Integer port;@Autowired:注入 Nacos 的服務(wù)注冊類NacosAutoServiceRegistration。@Value:注入配置文件中的端口號,如果未配置則默認(rèn)使用 6888 端口。
3. 啟動時設(shè)置端口
@Override
public void run(ApplicationArguments args) {
if (registration != null && port != null) {
Integer serverPort = port;
try {
serverPort = new Integer(getServerPort());
} catch (Exception e) {
log.warn("getServerPort warn", e);
log.info("getServerPort fail, use the config-file's port {}", port);
}
registration.setPort(serverPort);
registration.start();
}
}- 在應(yīng)用啟動時嘗試獲取實際使用的服務(wù)器端口,如果獲取失敗則使用配置文件中的端口。
- 將端口設(shè)置到 Nacos 的服務(wù)注冊中,并啟動服務(wù)注冊。
4. 獲取實際端口
public String getServerPort() throws Exception {
MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
Set<ObjectName> objectNames = beanServer.queryNames(new ObjectName("*:type=Connector,*"), Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
String port = objectNames.iterator().next().getKeyProperty("port");
log.debug("getServerPort {}", port);
return port;
}通過 JMX 管理接口獲取實際使用的 HTTP 端口。
總結(jié)
通過上述代碼,我們可以實現(xiàn)在 Spring Boot 應(yīng)用啟動時,動態(tài)獲取并設(shè)置 Nacos 注冊的端口號。這對于在不同環(huán)境中部署應(yīng)用非常有用,可以避免硬編碼端口號帶來的問題。同時,結(jié)合 Nacos 的服務(wù)注冊與發(fā)現(xiàn)功能,可以更加靈活地管理微服務(wù)架構(gòu)中的各個服務(wù)。
到此這篇關(guān)于Spring Boot 應(yīng)用打 WAR 包后無法注冊到 Nacos怎么辦的文章就介紹到這了,更多相關(guān)Spring Boot打 WAR 包無法注冊到 Nacos內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Java將Excel科學(xué)計數(shù)法解析成數(shù)字
這篇文章主要介紹了基于Java將Excel科學(xué)計數(shù)法解析成數(shù)字,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09
spring Boot打包部署到遠程服務(wù)器的tomcat中
這篇文章主要給大家介紹了關(guān)于spring Boot打包部署到遠程服務(wù)器的tomcat中的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
springboot+vue實現(xiàn)websocket配置過程解析
這篇文章主要介紹了springboot+vue實現(xiàn)websocket配置過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04
關(guān)于Spring 中 StringUtils.isEmpty 被棄用如何正確使用
SpringBoot/SpringFramework升級后,StringUtils.isEmpty()方法已被棄用,推薦使用hasLength和hasText替代,hasLength判斷是否有長度,hasText判斷是否包含至少一個非空白字符,本文給大家介紹關(guān)于Spring 中 StringUtils.isEmpty 被棄用如何正確使用,感興趣的朋友一起看看吧2026-01-01

