SpringBoot項(xiàng)目War包部署無(wú)法注冊(cè)到Nacos中的解決
SpringBoot項(xiàng)目War包部署無(wú)法注冊(cè)到Nacos中
注: 其實(shí)標(biāo)題的描述不是很準(zhǔn)確,準(zhǔn)確的說(shuō)是已經(jīng)注冊(cè)成功并且可以正常訪問(wèn),但是在服務(wù)列表卻看不到。
問(wèn)題
最近在進(jìn)行Eureka遷移Nacos架構(gòu)升級(jí)的時(shí)候,發(fā)現(xiàn)有兩個(gè)之前的舊項(xiàng)目,雖然也是SpringBoot項(xiàng)目,但是啟動(dòng)方式是通過(guò)外置Tomcat啟動(dòng)的。就在項(xiàng)目改造完成后,發(fā)現(xiàn)啟動(dòng)正常,訪問(wèn)也正常,但就是在nacos客戶端服務(wù)列表中看不到這個(gè)服務(wù)。
解決方案
我們從Nacos的注冊(cè)類(lèi)NacosAutoServiceRegistration 進(jìn)去之后可以發(fā)現(xiàn)它繼承了SpringCloud的AbstractAutoServiceRegistration 注冊(cè)類(lèi),在AbstractAutoServiceRegistration 中有一個(gè)綁定監(jiān)聽(tīng)事件,他的作用就是監(jiān)聽(tīng)到內(nèi)置容器啟動(dòng)完成之后獲取容器端口向注冊(cè)中心注冊(cè),如下圖:
因?yàn)檫@個(gè)接口只可以監(jiān)聽(tīng)內(nèi)置容器,所以我們就可以得出結(jié)論:之所以會(huì)出現(xiàn)上面的問(wèn)題,就是因?yàn)槭褂猛獠咳萜鲿r(shí),不會(huì)觸發(fā)監(jiān)聽(tīng)事件,所以也就注冊(cè)不到nacos中。
因此我們可以借助SpringBoot提供的ApplicationRunner接口,這個(gè)接口的作用就是在應(yīng)用啟動(dòng)完成之后執(zhí)行一些定義好的初始化操作。所以我們可以在服務(wù)啟動(dòng)成功之后,通過(guò)這個(gè)接口將我們的項(xiàng)目注冊(cè)到Nacos中,下面看代碼
/** * @author shy * @date 2021/11/29 16:23 */ @Component public class NacosConfig implements ApplicationRunner { @Autowired(required = false) private NacosAutoServiceRegistration registration; @Value("${server.port}") Integer port; @Override public void run(ApplicationArguments args) { if (registration != null && port != null) { //如果getTomcatPort()端口獲取異常,就采用配置文件中配置的端口 Integer tomcatPort = port; try { tomcatPort = new Integer(getTomcatPort()); } catch (Exception e) { e.printStackTrace(); } registration.setPort(tomcatPort); registration.start(); } } /** * 獲取外置tomcat端口 */ public String getTomcatPort() 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"); return port; } }
加上這個(gè)配置類(lèi)啟動(dòng)之后,我們就可以在Nacos客戶端服務(wù)列表中看到相應(yīng)的服務(wù)
SpringBoot項(xiàng)目war包部署及出現(xiàn)的問(wèn)題
Failed to bind properties under 'mybatis.configuration.mapped-statements[0].
1.修改pom文件
修改打包方式 為war;
添加tomcat使用范圍,provided的意思即在發(fā)布的時(shí)候有外部提供,內(nèi)置的tomcat就不會(huì)打包進(jìn)去
?<groupId>com.school</groupId> ? ? <artifactId>daniel</artifactId> ? ? <version>0.0.1-SNAPSHOT</version> ? ? <name>daniel</name> ? ? <description>student information project for Spring Boot</description> ? ? <!--打包方式,發(fā)布時(shí)使用此項(xiàng)--> ? ? <packaging>war</packaging> ? ? <properties> ? ? ? ? <java.version>1.8</java.version> ? ? </properties> ? ? <dependencies> ? ? ? ? <!--需要發(fā)布發(fā)war包時(shí)使用--> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? <artifactId>spring-boot-starter-tomcat</artifactId> ? ? ? ? ? ? <scope>provided</scope> ? ? ? ? </dependency>
2.在啟動(dòng)類(lèi)或者配置類(lèi)中繼承SpringBootServletInitializer
如果需要打war包部署,需要繼承此類(lèi),重寫(xiě)configure方法。
@SpringBootApplication public class DanielApplication extends SpringBootServletInitializer { ? ? protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { ? ? ? ? return builder.sources(DanielApplication.class); ? ? } ? ? public static void main(String[] args) { ? ? ? ? SpringApplication.run(DanielApplication.class, args); ? ? } }
注意:
如果使用的springboot最新的版本則會(huì)報(bào),我當(dāng)時(shí)用的是
spring-boot-starter-parent
:2.2.0.RELEASEmybatis-spring-boot-starter
:2.1.1
修改為下面的版本問(wèn)題解決,通過(guò)查詢(xún)資料發(fā)現(xiàn)現(xiàn)在的最新版本springboot與mybatis兼容性存在問(wèn)題,到后面升級(jí)后應(yīng)該就沒(méi)有問(wèn)題了。
Failed to bind properties under 'mybatis.configuration.mapped-statements[0].parameter-map.parameter-…
?<parent> ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? <artifactId>spring-boot-starter-parent</artifactId> ? ? ? ? <!--<version>2.2.0.RELEASE</version>--> ? ? ? ? <version>2.1.5.RELEASE</version> ? ? ? ? <relativePath/> <!-- lookup parent from repository --> ? ? </parent> <dependency> ? ? ? ? <groupId>org.mybatis.spring.boot</groupId> ? ? ? ? <artifactId>mybatis-spring-boot-starter</artifactId> ? ? ? ? <!-- <version>2.1.1</version>--> ? ? ? ? <version>2.0.1</version> </dependency>
附:以上的問(wèn)題都已解決,但是我的項(xiàng)目中使用了shiro框架,在外置tomcat部署時(shí),無(wú)法使用shiro。沒(méi)有改版本的時(shí)候使用idea是正常的…
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java多線程加鎖以及Condition類(lèi)的使用實(shí)例解析
這篇文章主要介紹了java多線程加鎖以及Condition類(lèi)的使用實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11如何使用IntelliJ IDEA搭建MyBatis-Plus框架并連接MySQL數(shù)據(jù)庫(kù)
這篇文章主要介紹了如何使用IntelliJ IDEA搭建MyBatis-Plus框架并連接MySQL數(shù)據(jù)庫(kù),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-11-11Java實(shí)現(xiàn)簡(jiǎn)單的斗地主游戲
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單的斗地主游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04Java中的靜態(tài)內(nèi)部類(lèi)詳解及代碼示例
這篇文章主要介紹了Java中的靜態(tài)內(nèi)部類(lèi)詳解及代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10SpringBoot注冊(cè)第三方Bean的方法總結(jié)
眾所周知,SpringBoot默認(rèn)會(huì)掃描啟動(dòng)類(lèi)所在的包及其子包,一般我們都是在需要的類(lèi)上通過(guò)注解的方式去將Bean注冊(cè)交給IOC進(jìn)行管理,但是注冊(cè)第三方Bean的方案卻不支持,所以本文給大家介紹了SpringBoot注冊(cè)第三方Bean的方法,需要的朋友可以參考下2024-01-01