Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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. And I do not understand how can I use profiles with .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

Or I need write all in one application.yaml file? 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

like image 842
ip696 Avatar asked Oct 03 '18 12:10

ip696


People also ask

How do I get YAML properties in Spring Boot?

Accessing the YAML Properties To access the YAML properties, we'll create an object of the YAMLConfig class, and access the properties using that object. In the properties file, we'll set the spring. profiles. active environment variable to prod.

How does YAML file work in Spring Boot?

YAML is a data serialization language that is often used for writing configuration files. So YAML configuration file in Spring Boot provides a very convenient syntax for storing logging configurations in a hierarchical format. The application. properties file is not that readable.


2 Answers

Yes, you can create multiple profiles even with single file Profile are separated with 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
like image 198
Ramesh Avatar answered Oct 14 '22 01:10

Ramesh


  1. Create application.yaml and define all default properties there.
  2. Create application-local.yaml and override properties needed for the local profile.
  3. Create application-test.yaml and override properties needed for the test profile.
  4. Set spring.profiles.active by either passing it as a system property (-D for java) or defining it within application.yaml.

When you are running an app with a {PROFILE}, Spring will parse application-{PROFILE}.yaml after application.yaml.

like image 40
Andrew Tobilko Avatar answered Oct 14 '22 02:10

Andrew Tobilko