SpringCloudConfig配置中心的native模式有什么用

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

创新互联客户idc服务中心,提供德阳机房服务器托管、成都服务器、成都主机托管、成都双线服务器等业务的一站式服务。通过各地的服务中心,我们向成都用户提供优质廉价的产品以及开放、透明、稳定、高性价比的服务,资深网络工程师在机房提供7*24小时标准级技术保障。

背景说明

由于很多时候,生产环境没有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: 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

    	
            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: 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模式有什么用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


网页题目:SpringCloudConfig配置中心的native模式有什么用
文章分享:http://bzwzjz.com/article/ghhsdd.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 重庆手机网站建设 成都网站建设 成都网站建设 成都网站制作 网站建设费用 成都网站设计 重庆电商网站建设 成都网站制作 网站建设方案 企业网站设计 成都企业网站制作 成都网站制作 营销网站建设 成都网站设计 企业网站设计 外贸网站建设 手机网站制作 成都网站制作 高端网站设计 响应式网站设计方案 定制网站建设 成都企业网站建设