????????根據(jù)公司要求,需要將項(xiàng)目集成到nacos中,當(dāng)前項(xiàng)目是基于若依前后端分離版開發(fā)的,若依的版本為3.8.3,若依框架中整合的springBoot版本為2.5.14。Nacos核心提供兩個(gè)功能:服務(wù)注冊與發(fā)現(xiàn),動態(tài)配置管理。
一、服務(wù)注冊與發(fā)現(xiàn)
1、引入pom依賴
</dependency>-->
<!-- 在SpringBoot 2.4.x的版本之后,對配置文件加載方式進(jìn)行了重構(gòu),需要導(dǎo)入如下的依賴;
詳情見官網(wǎng):https://docs.spring.io/spring-cloud-config/docs/current/reference/html/#config-first-bootstrap -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.0.1</version>
</dependency>
<!-- 服務(wù)注冊與發(fā)現(xiàn)依賴 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2021.1</version>
</dependency>
2、bootstrap.yml配置
spring:
# nacos配置
cloud:
nacos:
discovery:
server-addr: 10.2.XX.XX:8848
compatibility-verifier:
enabled: false
3、添加注解
在項(xiàng)目啟動類上添加 @EnableDiscoveryClient 注解:
@EnableDiscoveryClient
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class RiskExamineApplication
{
public static void main(String[] args)
{
// System.setProperty("spring.devtools.restart.enabled", "false");
SpringApplication.run(RiskExamineApplication.class, args);
System.out.println("(????)?? 若依啟動成功 ?(′?`?)? \n" +
" .-------. ____ __ \n" +
" | _ _ \\ \\ \\ / / \n" +
" | ( ' ) | \\ _. / ' \n" +
" |(_ o _) / _( )_ .' \n" +
" | (_,_).' __ ___(_ o _)' \n" +
" | |\\ \\ | || |(_,_)' \n" +
" | | \\ `' /| `-' / \n" +
" | | \\ / \\ / \n" +
" ''-' `'-' `-..-' ");
}
}
4、驗(yàn)證
????????項(xiàng)目啟動后,登錄nacos,在服務(wù)列表中,點(diǎn)擊查詢即可看到注冊到nacos的服務(wù),如下圖:
服務(wù)名默認(rèn)為yml配置里spring.application.name的名稱,也可通過spring.cloud.nacos.discovery.service配置指定。
遇到問題:Your project setup is incompatible with our requirements due to following reasons:
- Spring Boot [2.5.14] is not compatible with this Spring Cloud release train
Change Spring Boot version to one of the following versions [2.3.x, 2.4.x]?
***************************
APPLICATION FAILED TO START
***************************
Description:
Your project setup is incompatible with our requirements due to following reasons:
- Spring Boot [2.5.14] is not compatible with this Spring Cloud release train
Action:
Consider applying the following actions:
- Change Spring Boot version to one of the following versions [2.3.x, 2.4.x] .
You can find the latest Spring Boot versions here [https://spring.io/projects/spring-boot#learn].
If you want to learn more about the Spring Cloud Release train compatibility, you can visit this page [https://spring.io/projects/spring-cloud#overview] and check the [Release Trains] section.
If you want to disable this check, just set the property [spring.cloud.compatibility-verifier.enabled=false]
解決辦法:出現(xiàn)這個(gè)問題是由于springBoot和springCloud版本不一致導(dǎo)致的,如果是自己手動搭建的框架,可以根據(jù)提示將springBoot版本降到[2.3.x, 2.4.x];但是由于我用的是若依的框架,擔(dān)心將springBoot版本降低后會引起其它問題,所以根據(jù)提示設(shè)置spring.cloud.compatibility-verifier.enabled=false這個(gè)屬性,但是引入后發(fā)現(xiàn)配置不生效,最后根據(jù)研究一番之后,根據(jù)網(wǎng)上的提示需將配置文件名稱application.yml改為bootstrap.yml,然后再引入依賴spring-cloud-starter-bootstrap 即可解決。
注意:如果只需要nacos的服務(wù)注冊與發(fā)現(xiàn)功能,不用服務(wù)配置功能的話,一定要將服務(wù)配置的pom依賴(spring-cloud-starter-alibaba-nacos-config)去掉,否則啟動時(shí)會報(bào)錯(cuò):currentServerAddr:http://localhost:8848, err : connect timed out 。
二、服務(wù)動態(tài)配置與服務(wù)注冊發(fā)現(xiàn)
1、引入pom依賴
<!-- nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2021.1</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2021.1</version>
</dependency>
2、bootstrap.yml配置
spring:
cloud:
nacos:
discovery:
server-addr: 124.70.XX.XX:32389
namespace: zs-smart
group: dev
config:
server-addr: 124.70.XX.XX:32389
namespace: zs-smart
group: dev
name: zs-smart
file-extension: yaml
compatibility-verifier:
enabled: false
3、nacos配置
1)新建命名空間
?2)?新建配置
打開配置列表,選擇剛剛創(chuàng)建的zs-smart,點(diǎn)擊右上角的“+”號,如下圖:
3)填寫配置
然后啟動項(xiàng)目,如果項(xiàng)目啟動成功,即說明配置沒問題!?文章來源:http://www.zghlxwxcb.cn/news/detail-410928.html
參考文章:Springboot集成Nacos2「服務(wù)注冊與發(fā)現(xiàn)」 - 知乎文章來源地址http://www.zghlxwxcb.cn/news/detail-410928.html
到了這里,關(guān)于若依前后端分離版集成nacos的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!