Spring Boot的外围配置文件用于配置Spring Boot程序,文件名字必须以application开始。这个既是底层源码的强制要求,也是SpringBoot的一种代码规约,有助于在开发层面利于代码标准治理。

阐明:以下内容接着i后面的SpringBootCase我的项目进行演示。

1、application. properties外围文件

格局:以键值对的形式进行配置,key=value 示例:name=xintu 咱们批改application.properties配置文件。比方批改默认tomcat端口号及我的项目高低文件根。

设置内嵌Tomcat端口号

server.port=8888

设置我的项目上下文根门路,这个在申请的时候须要用到

server.servlet.context-path=/springbootcase

配置结束之后,启动测试。

浏览器输出地址:http://localhost:8888/springbootcase/hello?name=lw , 页面验证后果如下。

2、 application.yml配置文件(举荐配置格调) yml 是一种 yaml 格局的配置文件,次要采纳肯定的空格、换行等格局排版进行配置。格局如下,

key: 空格+value, 示意一对键值对。

配置YNML时,须要留神以下4点:

1)以空格的缩进来管制层级关系。 2)只有是左对齐的一列数据,都是属于同一层级。 3)空格必须有。 4)属性和值对大小写敏感。
server:  port: 8888 # 设置内嵌Tomcat端口号  servlet:    context-path: /SpringBootCase # 设置我的项目上下文根门路,这个在申请拜访的时候须要用到

特点:与 application. properties 相比,yaml更能直观地被计算机辨认,而且容易被人类浏览。 yaml 相似于 xml,然而语法比xml 简洁很多,值与后面的冒号配置项必须要有一个空格, yml 后缀也能够应用 yaml 后缀。当两种格局配置文件同时存在,应用的是 .properties 配置文件,为了演示yml,能够先将其改名,从新运行SpringbootApplication,查看启动的端口及上下文根。

咱们在前面的学习过程中,均应用 .yml格局 。如果想改.properties模式也能够,依照本人喜爱的格调 或 团队约定即可。

3、SpringBoot多环境配置

在理论开发的过程中,咱们的我的项目会经验很多的阶段,开发、测试、上线, 尤其时很大厂,在进行一些重要需要迭代时,还会包含预发、灰度等。每个阶段的配置会因利用所依赖的环境不同而不同,例如:数据库配置、缓存配置、依赖第三方配置等,那么这个时候为了不便配置在不同的环境之间切换,SpringBoot提供了多环境profile配置性能。

命名格局:application-环境标识.yml(或 .properties)。

上面咱们为每个环境创立3个配置文件,别离命名为:application-dev.yml(开发环境)、application-dev.yml(测试环境)、application-dev.yml(生产环境)。

各配置文件内容如下,

  1. application-dev.yml

开发环境

server:  port: 8881 # 设置内嵌Tomcat端口号  servlet:    context-path: /springbootcase1 # 设置我的项目上下文根门路,这个在申请拜访的时候须要用到
  1. application-test.yml
#测试环境server:  port: 8883 # 设置内嵌Tomcat端口号  servlet:    context-path: /springbootcase3 # 设置我的项目上下文根门路,这个在申请拜访的时候须要用到
  1. application-prod.yml
#生产环境server:  port: 8882 # 设置内嵌Tomcat端口号  servlet:    context-path: /springbootcase2 # 设置我的项目上下文根门路,这个在申请拜访的时候须要用到

4)总配置文件application.yml进行环境的激活

spring:  profiles:      active: test #激活对应环境配置,以测试环境为例

阐明:除此之外,还能够应用maven编译打包的形式配置profile。通常这种状况比拟少见,须要依据各公司环境状况而定。

启动并测试验证如下,

浏览器输出: http://localhost:8883/springbootcase3/hello?name=lw, 后果如下,

4、SpringBoot自定义配置

在SpringBoot外围配置文件中,除以上应用内置的配置项之外,咱们还能够在自定义配置,而后采纳注解形式去读取配置。

1)@Value注解

步骤1:在外围配置文件applicatin.yml中,增加两个自定义配置项test.site和test.user。在IDEA中能够看到这两个属性不能被SpringBoot辨认,背景是红色的。

步骤2:在SpringbootApplication中定义属性,并应用@Value注解或者自定义配置值,并对其办法进行测试 。

package com.xintu.demo;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestController@SpringBootApplicationpublic class SpringbootApplication {    @Value("${test.site}") //获取自定义属性test.site    private String site;    @Value("${test.user}")  //获取自定义属性test.user    private String user;    public static void main(String[] args) {        SpringApplication.run(SpringbootApplication.class, args);    }    @GetMapping("/hello")    public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {        return String.format("欢送 %s 来到<a href=\"http://www.35xintu.com\">35新途</a>!", name);    }    @GetMapping("/value")    public String testValue() { //测试@Value注解        return String.format("欢送 %s 来到<a href=\"http://www.35xintu.com\">%s</a>!" , user,site);    }}

步骤3: 重新启动后,在浏览器中进行测试验证: http://localhost:8888/springbootcase/value。

2) @ConfigurationProperties

将整个文件映射成一个对象,用于自定义配置项比拟多的状况。

步骤1:在 com.xintu.demo.config 包下创立 XinTuConfigInfo 类,并为该类加上@Component和@ConfigurationProperties注解,prefix能够不指定,如果不指定,那么会去配置文件中寻找与该类的属性名统一的配置,prefix的作用能够辨别同名配置。

package com.xintu.demo.config;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;@Component@ConfigurationProperties(prefix = "test")public class XinTuConfigInfo {    private String site;    private String user;    public String getSite() {        return site;    }    public void setSite(String site) {        this.site = site;    }    public String getUser() {        return user;    }    public void setUser(String user) {        this.user = user;    }}

步骤2: 在SpringbootApplication中注入XinTuConfigInfo 配置类。

package com.xintu.demo;import com.xintu.demo.config.XinTuConfigInfo;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestController@SpringBootApplicationpublic class SpringbootApplication {    @Autowired    private XinTuConfigInfo configInfo; //测试@ConfigurationProperties    @Value("${test.site}")    private String site;    @Value("${test.user}")    private String user;    public static void main(String[] args) {        SpringApplication.run(SpringbootApplication.class, args);    }    @GetMapping("/hello")    public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {        return String.format("欢送 %s 来到<a href=\"http://www.35xintu.com\">35新途</a>!", name);    }    @GetMapping("/value")    public String testValue() { //测试 @Value 注解        return String.format("欢送 %s 来到<a href=\"http://www.35xintu.com\">%s</a>!" , user,site);    }    @GetMapping("/config")    public String config() { //测试 @ConfigurationProperties 注解        return String.format("欢送 %s 来到<a href=\"http://www.35xintu.com\">%s</a>!" , configInfo.getUser(),configInfo.getSite());    }}

步骤3:重新启动并在浏览器中进行测试。

5. 近程配置核心(目前生产罕用的形式)

1)配置核心流程图

次要蕴含两大核心内容:

① 近程配置核心负责属性键值的新增、变更、公布等操作。

② 业务利用端,则通过配置近程配置核心地址获取本人锁须要的配置数据。 另外通过客户端监听的形式,近实时地获取远端配置的批改或删除。

2)开源配置核心比照(按开源工夫程序)

目前市面上用的比拟多的配置核心有:Disconf、Spring Cloud Config、Apollo、Nacos。

① Disconf

2014年7月百度开源的配置管理核心,同样具备配置的治理能力,不过目前曾经不保护了。

② Spring Cloud Config

2014年9月开源,Spring Cloud 生态组件,能够和Spring Cloud体系无缝整合。

③ Apollo

2016年5月,携程开源的配置管理核心,具备标准的权限、流程治理等个性。

④ Nacos

2018年6月,阿里开源的配置核心,也能够做DNS和RPC的服务发现。

以上各配置服务在配置管理畛域的概念和原理基本相同,然而也存在一些不同的点。另外也有一些外部公有配置核心,比方京东的DUCC,美团的MCC等,设计思路根本大同小异,感兴趣的能够本人深入研究。

以上!