简体   繁体   English

如何使用 spring boot + .yaml 创建配置文件?

[英]How can I create profiles with spring boot + .yaml?

I have spring boot server with 2 property files: application-local.properties and application-test.properties我有带有 2 个属性文件的 Spring Boot 服务器: application-local.propertiesapplication-test.properties

In each file I have configs for dev machine and for test.在每个文件中,我都有用于开发机器和测试的配置。 Start it like this:像这样开始:

-Dspring.profiles.active=local

But in new spring boot project I use .yaml config file.但是在新的 Spring Boot 项目中,我使用了.yaml配置文件。 And I do not understand how can I use profiles with .yaml .而且我不明白如何将profiles.yaml一起.yaml I tried read documentation but understood nothing.我尝试阅读文档,但什么也不懂。 Can you explain what to do, step by step?你能一步一步解释该怎么做吗?

I need have two files?我需要两个文件?

application-local.yaml and application-test.yaml application-local.yamlapplication-test.yaml

Or I need write all in one application.yaml file?或者我需要在一个application.yaml文件中写入所有内容? If in one file how can I separate configs?如果在一个文件中,我如何分离配置? It is my config:这是我的配置:

server:
  path: ***
  port: ***

cxf:
  path: ***

spring.datasource:
  type: com.zaxxer.hikari.HikariDataSource
  driver-class-name: oracle.jdbc.OracleDriver
  url: ***
  username: ***
  password: ***
  hikari:
    minimumIdle: 5
    maximumPoolSize: 20
    idleTimeout: 30000
    poolName: SpringBootJPAHikariCP
    maxLifetime: 2000000
    connectionTimeout: 30000
    connection-test-query: SELECT 1 FROM DUAL

spring.jpa:
  show-sql: false
  database-platform: org.hibernate.dialect.Oracle10gDialect
  properties.hibernate.jdbc.batch_size: 30
  properties.hibernate.cache.use_second_level_cache: false
  hibernate:
    ddl-auto: validate


spring.cache:
  ehcache:
    config: classpath:ehcache.xml

#app configs
my:
  messages-max-count: 5
  messages-delay: 100
  schedulers-charge-delay: 100
  client:
    first-server-address: ***
    second-server-address: ***
    last-server-address: ***
  enabled-client: FirstClient

I want create test profile and change database url (or change to postgreSql), change maximumPoolSize property我想创建测试配置文件并更改数据库 url(或更改为 postgreSql),更改maximumPoolSize属性

  1. Create application.yaml and define all default properties there.创建application.yaml并在那里定义所有默认属性。
  2. Create application-local.yaml and override properties needed for the local profile.创建application-local.yaml并覆盖local配置文件所需的属性。
  3. Create application-test.yaml and override properties needed for the test profile.创建application-test.yaml并覆盖test配置文件所需的属性。
  4. Set spring.profiles.active by either passing it as a system property ( -D for java ) or defining it within application.yaml .通过将spring.profiles.active作为系统属性( -D用于java )传递或在application.yaml定义它来设置spring.profiles.active

When you are running an app with a {PROFILE} , Spring will parse application-{PROFILE}.yaml after application.yaml .当您运行带有{PROFILE}的应用程序时,Spring 将在application.yaml之后解析application-{PROFILE}.yaml

Yes, you can create multiple profiles even with single file Profile are separated with 3 DASH ( --- )是的,即使使用单个文件,您也可以创建多个配置文件配置文件用 3 个 DASH ( --- ) 分隔

logging:
  level:
    .: error
    org.springframework: ERROR

spring:
  profiles:
    active: "dev"
  main:
    banner-mode: "off"

server:
  port: 8085

---

spring:
  profiles: dev

---

spring:
  profiles: prod

I have spring boot server with 2 property files: application-local.properties and application-test.properties我有带有2个属性文件的spring boot服务器: application-local.propertiesapplication-test.properties

In each file I have configs for dev machine and for test.在每个文件中,我都有用于开发机和测试的配置。 Start it like this:像这样启动它:

-Dspring.profiles.active=local

But in new spring boot project I use .yaml config file.但是在新的春季启动项目中,我使用.yaml配置文件。 And I do not understand how can I use profiles with .yaml .而且我不明白如何将.yamlprofiles一起.yaml I tried read documentation but understood nothing.我尝试阅读文档,但一无所知。 Can you explain what to do, step by step?您能一步一步解释什么吗?

I need have two files?我需要两个文件吗?

application-local.yaml and application-test.yaml application-local.yamlapplication-test.yaml

Or I need write all in one application.yaml file?还是我需要将所有内容都写在一个application.yaml文件中? If in one file how can I separate configs?如果在一个文件中,如何分隔配置? It is my config:这是我的配置:

server:
  path: ***
  port: ***

cxf:
  path: ***

spring.datasource:
  type: com.zaxxer.hikari.HikariDataSource
  driver-class-name: oracle.jdbc.OracleDriver
  url: ***
  username: ***
  password: ***
  hikari:
    minimumIdle: 5
    maximumPoolSize: 20
    idleTimeout: 30000
    poolName: SpringBootJPAHikariCP
    maxLifetime: 2000000
    connectionTimeout: 30000
    connection-test-query: SELECT 1 FROM DUAL

spring.jpa:
  show-sql: false
  database-platform: org.hibernate.dialect.Oracle10gDialect
  properties.hibernate.jdbc.batch_size: 30
  properties.hibernate.cache.use_second_level_cache: false
  hibernate:
    ddl-auto: validate


spring.cache:
  ehcache:
    config: classpath:ehcache.xml

#app configs
my:
  messages-max-count: 5
  messages-delay: 100
  schedulers-charge-delay: 100
  client:
    first-server-address: ***
    second-server-address: ***
    last-server-address: ***
  enabled-client: FirstClient

I want create test profile and change database url (or change to postgreSql), change maximumPoolSize property我想创建测试配置文件并更改数据库URL(或更改为postgreSql),更改maximumPoolSize属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Spring Boot和Maven设置配置文件? - How can I set profiles with spring boot and maven? Spring启动YAML配置用于配置文件的组合 - Spring boot YAML config for combinations of profiles 如何在Spring Boot中将YAML属性与构造函数注入配合使用? - How can I use YAML properties with constructor injection in Spring Boot? 如何在运行时为 Spring Boot 中的不同配置文件创建多个 bean? - How to create multiple beans at runtime for different profiles in Spring Boot? 如何使用 Spring Boot 配置文件 - how to use Spring Boot profiles 如何在不使用配置文件的情况下覆盖Spring Boot应用程序属性进行测试? - How can I override Spring Boot application properties for tests without using profiles? 如何在没有Spring Boot的情况下创建Spring Sleuth应用程序 - How can I create a Spring sleuth application without Spring boot 如何在 Spring Boot 中从 YAML 配置创建地图? - How to create a Map from YAML configuation in spring boot? 如何使用 Spring Boot 和 Liquibase 变更集 yaml 文件访问系统属性 - how can I access system properties using spring boot and Liquibase changeset yaml file 如何在运行时使用 spring 引导应用程序中的 java 代码对 application.yaml 进行读写操作? - How can I write and read to and from a application.yaml at runtime using java code in a spring boot application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM