日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringCloud(若依微服务版)读取Nacos中的配置以及多个服务共享Nacos配置的使用

發布時間:2025/3/19 javascript 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringCloud(若依微服务版)读取Nacos中的配置以及多个服务共享Nacos配置的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

場景

若依微服務版手把手教你本地搭建環境并運行前后端項目:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/109363303

在上面介紹了使用SpringCloud(若依微服務版)搭建項目的基礎上,在業務開發中需要將某些配置

存放在配置文件中,比如上傳文件的地址、第三方接口的地址端口等。然后在使用的地方動態配置。這樣在配置需要進行

修改時就只需要修改配置文件即可。就不需要修改代碼了。

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。

實現

那么如果使用了Nacos作為配置中心,又是怎樣獲取Nacos中的配置文件的內容那。

首先在需要用到動態配置的服務下所對應的Nacos中的配置文件中,添加配置,比如這里需要在system這個服務下添加動態配置。

找到Nacos中對應的配置文件,將需要添加的配置在后面追加上

?

然后在對應的服務下面的包路徑下新建config目錄,在config目錄下新建讀取配置文件的配置類

import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.context.annotation.Configuration;/*** 讀取服務器上傳文件磁盤路徑* @author badao*/ @Configuration @RefreshScope @ConfigurationProperties(prefix = "file") public class FilePathProperties {/*** 傳文件磁盤路徑*/private String path;public String getPath(){return path;}public void setPath(String path){this.path = path;} }

注意這里的prefix前綴,上面的配置文件是從file開頭寫的,file下面對應的是path節點。

所以這里的配置類的前綴和屬性也要對應。

然后重啟Nacos和服務,就可以在此對應的服務下面,首先引入

??? @Autowiredprivate FilePathProperties filePathProperties;

然后通過

filePathProperties.getPath()

去獲取動態配置了

那么問題又來了,如果這是在SpringBoot單個服務的系統中這樣使用,就可以在任何需要用到配置

的地方動態配置引用。

但是如果要是在微服務架構下,在多個服務下都需要動態配置這個路徑的話,豈不是需要在每個服務下面

都要配置一遍。

那么可以通過Maven依賴的方式在一個公共的子模塊中讀取配置文件,然后在運行和打包時會將此公共模塊下的子模塊添加到各個服務下面,然后再在每個服務的配置文件中設置共享配置文件,那么就可以在一個共享配置文件中配置一次,然后在每個服務下都能讀取了。

Nacos共享配置實現

首先Nacos是支持共享配置文件的,具體可以參照其官方文檔

https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html#_more_information_about_nacos_config_starter_configurations

搜索shared-dataids

?

官方文檔原文描述

To share the data id among multiple applications in a clearer manner, you can also use the following method:

spring.cloud.nacos.config.shared-dataids=bootstrap-common.properties,all-common.properties spring.cloud.nacos.config.refreshable-dataids=bootstrap-common.properties

We can see that:

  • Multiple shared data ids can be configured using?spring.cloud.nacos.config.shared-dataids?, and the data ids are separted by commas.
  • spring.cloud.nacos.config.refreshable-dataids?is used to control which data ids will be refreshed dynamically when configurations are updated, and that the latest configuration values can be retrieved by applications. Data ids are separated with commas. If not specified, all shared data ids will not be dynamically refreshed.
?

When using?spring.cloud.nacos.config.shared-dataids?to configure multiple shared data ids, we agree on the following priority between the shared configurations: Priorities are decided based on the order in which the configurations appear. The one that occurs later is higher in priority than the one that appears first.

?

When using?spring.cloud.nacos.config.shared-dataids, the data Id must have a file extension, and it could be properties or yaml/yml. And the configuration in?spring.cloud.nacos.config.file-extension?does not have any impact on the customized Data Id file extension.

?

?

When?spring.cloud.nacos.config.refreshable-dataids?specifies the data ids that support dynamic refresh, the corresponding values of the data ids should also specify file extensions.

?

我們打開system服務下的resource下的bootstrap.yml,這里的配置文件默認是加了共享文件配置的

?

只不過這里的配置文件的完整名是使用的上面的變量

?

所以在system服務下配置的共享文件就是application-dev.yml

?

所以我們在application-dev.yml中添加配置內容,配置IM服務器地址

?

若依微服務的架構將ruoyi-common下的ruoyi-common-core會作為每個服務下的子模塊,所以在此子模塊添加讀取配置文件的配置類

在運行或打包時就會將此配置類添加到每個服務下。然后在每個服務下的bootstrap.yml中都添加了共享配置的配置,就都能獲取到

該配置內容了。

?

在common下新建配置類,添加注解來讀取配置文件

?

注意這里使用的是@Component注解,然后在此公共模塊下配置好了之后,在需要用到此配置的服務下的啟動類上

添加注解來定義掃描路徑

@ComponentScan(basePackages = {"com.ruoyi"})

?

因為上面的那個common-core中的配置類中直接使用了配置文件中獲取到的配置,所以在各個服務下都能使用這個工具類

?

總結

以上是生活随笔為你收集整理的SpringCloud(若依微服务版)读取Nacos中的配置以及多个服务共享Nacos配置的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。