温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2021-06-28 16:22:26 来源:亿速云 阅读:355 作者:chen 栏目:大数据

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

背景说明

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

config-server配置

  1. 首先,老套路引入pom

        <dependency>             <groupId>org.springframework.cloud</groupId>             <artifactId>spring-cloud-config-server</artifactId>         </dependency>
  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: 8001 spring:   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 @EnableConfigServer public class EurekaConfigApp {     public static void main(String[] args) {         SpringApplication.run(EurekaConfigApp.class, args);     } }

至此,config-server暂告一段路

config-client配置

  1. 继续,引入pom

    	<dependency>             <groupId>org.springframework.cloud</groupId>             <artifactId>spring-cloud-config-client</artifactId>         </dependency>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-web</artifactId>         </dependency>
  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: 8002 spring:   application:     name: config-info   profiles:   	# 这里主要是为了掩饰和config.profile的区分     active: dev

最后,贴出来启动类

@SpringBootApplication public 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("================");     } }

输入结果

================ prod1 dev ================

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

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI