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

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和服务,就可以在此对应的服务下面,首先引入

    @Autowired
    private 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:

 

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,spring,依微,config,cloud
来源: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/115457802