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

发布时间:2021-06-28 16:22:26 作者:chen
来源:亿速云 阅读:301

本篇内容介绍了“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配置本地存储 官方文档在此

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的主要说明如下

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

推荐阅读:
  1. spring cloud config
  2. Spring cloud config集成的示例分析

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

spring native

上一篇:Android中Content Provider的作用是什么

下一篇:Android中怎么调用摄像头拍照

相关阅读

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

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