How to concat strings in application.yaml?

246 Views Asked by At

I want to add two lines in a yaml file, or rather, I want the instance-id field to contain the result of adding the values of the port and name fields I tried using +, tried through various tags that I saw in similar questions, like !join, !concat (java says they don't exist). But nothing worked in the end. Is there a solution?

Example yaml (Sorry, i dont know how to load yaml here):

server:
  port: &port 62000
spring:
  application:
    name: &appname 'appname'
eureka:
  client:
    service-url:
      defaultZone: 'http://localhost:8081/eureka/'
  instance:
    instance-id: here should be *appname + ":" + *port (result appname:62000)

3

There are 3 best solutions below

1
GeertPt On BEST ANSWER

This won't work for YAML in general, but it will work in Spring Boot, since you can use Property Placeholders, and thus reference other properties from within a property value:

server:
  port: 62000
spring:
  application:
    name: 'appname'
eureka:
  client:
    service-url:
      defaultZone: 'http://localhost:8081/eureka/'
  instance:
    instance-id: ${spring.application.name}:${server.port}

This also works with environment variables, btw.

0
джедай On

i can provide my application.yml file config for flyway, i think that, based on this code, you can achieve what you want

  flyway:
    url: jdbc:postgresql://${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_NAME}
    user: ${DATABASE_USER}
    password: ${DATABASE_PASSWORD}
    driver-class-name: org.postgresql.Driver
    baseline-on-migrate: true
    enabled: true
    validate-on-migrate: true
    baseline-version: 0
    default-schema: ${DATABASE_SCHEMA}
and you should remember, that this values must been provided by you, while running application, put them in a environment variables see a link!, or if you run integration tests, you can use @DynamicPropertySource public static void setUpDynamicPropertyValues(DynamicPropertyRegistry registry) { // registry.add("eureka.instance.instance-id", () -> "your instance-id here");

0
inspironman On

typically we don't have direct string concatenation or arithmetic operations like in some other languages. However, you can use YAML anchors and aliases to achieve something similar.