千家信息网

Spring Cloud Config配置中心的native模式有什么用

发表于:2025-02-05 作者:千家信息网编辑
千家信息网最后更新 2025年02月05日,本篇内容介绍了"Spring Cloud Config配置中心的native模式有什么用"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧
千家信息网最后更新 2025年02月05日Spring Cloud Config配置中心的native模式有什么用

本篇内容介绍了"Spring Cloud Config配置中心的native模式有什么用"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

背景说明

由于很多时候,生产环境没有git,也没有svn等等,所以需要使用native模式,鉴于网上缺少相关的资料,因此以此为切入点,记录一下native模式下Spring Cloud Config一些常用的功能

config-server配置

  1. 首先,老套路引入pom

                    org.springframework.cloud            spring-cloud-config-server        
  1. bootstrap.yml配置本地存储 官方文档在此

    • spring.profiles.active=composite,他是一个list,我们这里面因为是本地,添加一条type=native,并配上search-locations,这个属性可以用class-path,也可以用绝对路径,但是正常情况下,建议用绝对路径,因为配置文件会有改动,如果放在项目里面,改动起来比较麻烦。

    • 还要设置pring.cloud.config.server.bootstrap=true

    • config文件夹下,我放了三个文件config-info-dev.yml,config-info-test.yml,config-info-prod.yml,里面存放了不通内容,主要就为了测试

server:    port: 8001spring:  application:    name: config-server-1  profiles:    active: composite  cloud:    config:      server:        composite:          - type: native              # 文件存放的绝对路径,源码里面我用绝对路径的方式放在了resources里面,这里需要改成自己的路径            search-locations: file:///Users/sunhan/Downloads/config          bootstrap: true
  1. 启动类

@SpringBootApplication@EnableConfigServerpublic class EurekaConfigApp {    public static void main(String[] args) {        SpringApplication.run(EurekaConfigApp.class, args);    }}

至此,config-server暂告一段路

config-client配置

  1. 继续,引入pom

                   org.springframework.cloud            spring-cloud-config-client                            org.springframework.boot            spring-boot-starter-web        
  1. client添加了两个配置文件一个bootstrap.yml, 官方文档也说了If you use the bootstrap flag, the config server needs to have its name and repository URI configured in bootstrap.yml.。config的主要说明如下

    • uri: 就是config-server的请求路径

    • profile、name:这里面的profile和spring.profiles.active没有半毛钱关系,他仅仅是区分引用那个文件,比如配置文件config-info-dev.yml,name就是config-info,profile就是dev,还有一个label属性,官方解释是The label name to use to pull remote configuration properties,我的理解就是应该在git等远程仓库中会用的上,至少在文件匹配,profile和name够用了

spring:  cloud:    config:      uri: http://localhost:8001      profile: prod      name: config-info

还有一个就是spring-boot的application.yml文件,其中spring.cloud.config.name默认值就是spring.application.name

server:  port: 8002spring:  application:    name: config-info  profiles:      # 这里主要是为了掩饰和config.profile的区分    active: dev

最后,贴出来启动类

@SpringBootApplicationpublic class ConfigClientApp1 implements CommandLineRunner {    public static void main(String[] args) {        SpringApplication.run(ConfigClientApp1.class, args);    }    @Value("${env}")    private String env;    @Value("${hello}")    private String hello;    public void run(String... args) {        System.out.println("================");        System.out.println(env);        System.out.println(hello);        System.out.println("================");    }}

输入结果

================prod1dev================

接下来,就要考虑刷新的问题了。手动刷新,亦或是整合Spring Cloud Bus热刷新,下次侃

"Spring Cloud Config配置中心的native模式有什么用"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

0