4

We have a Spring boot project using the deprecated @StreamListener and now we are switching to Spring Cloud Stream functional kafka binders.

The problem is that this service connects to multiple kafka topics and our single line spring.cloud.function.definition: topicA;topicB;topicC;...;topicN is becoming very long

I would like to know how to use on Spring's application.yaml the yaml capabilities such as multi line values (such as | or > operators) but I haven't found something similar on documentation.

My goal would be something such as

spring.cloud.function.definition: | topicA;
                                    topicB;
                                    topicC;
                                    ...;
                                    topicN

Thanks

Alex
  • 4,987
  • 1
  • 8
  • 26
marionmaiden
  • 3,250
  • 8
  • 33
  • 50

1 Answers1

5

There are multiple ways to represent multiline values in YAML. Details could be found in How do I break a string in YAML over multiple lines? and most of them are supported in Spring and could be used in application.yml.

Using multiline for function definitions in Spring Cloud Stream

All above approaches works in Spring but at the end it really depends how the result value is used. In some cases new line character is preserved and in some cases replaced with space.

From what I see spring.cloud.function.definition is parsed in org.springframework.cloud.stream.function.FunctionConfiguration and logic doesn't expect any white spaces.

The only approach that would work in this case is double-quoted block with escaped the newline

spring.cloud.function.definition: "topicA;topicB;topicC;\
  ...;topicN"
Alex
  • 4,987
  • 1
  • 8
  • 26
  • 1
    Hi Alex, Unfortunately none of these alternatives worked. I know they are valid solutions for yaml (I used couple of them when writing OpenApi yaml files) but somehow on Spring can't interpret the value when they are used in application.yaml files – marionmaiden Jun 17 '22 at 15:00
  • 1
    What Spring version you are using? I validated all examples on spring boot `2.6.4`. – Alex Jun 17 '22 at 15:12
  • 1
    I validated pure configuration loading. Probably there are some restrictions in Spring Cloud Stream functional – Alex Jun 17 '22 at 15:18
  • 1
    @marionmaiden are you sure you could use `;` as a separator. Looking at Spring Cloud Stream examples `|` is used. – Alex Jun 17 '22 at 15:27
  • 2
    Hello @Alex, we are using Spring boot 2.6.6 and Spring cloud stream 2021.0.1. There is a difference between `;` and `|`separators on Spring Cloud Stream. The first only lists unrelated bindings and the second allows bindings composition (just like we have with pipes on linux terminal). The issue with the long strings is exactly with this specific Spring property so I imagine the limitation comes from Spring and not from yaml capabilities. – marionmaiden Jun 20 '22 at 07:05
  • 1
    the question was about "multiline values on application.yaml" and all suggested approaches works in spring framework. Updated the answer for `spring.cloud.function.definition` and I would suggest to change the question title to be more specific – Alex Jun 21 '22 at 04:37