spring-cloud / spring-cloud-stream

Framework for building Event-Driven Microservices
http://cloud.spring.io/spring-cloud-stream
Apache License 2.0
1.01k stars 614 forks source link

RabbitMQ: spring.cloud.stream.rabbit.bindings.* properties not recognized in Native/AOT builds #2938

Closed mjbingen closed 5 months ago

mjbingen commented 7 months ago

Describe the issue When building a Spring Boot app with Spring Cloud Stream as a native image with GraalVM, the properties scoped under spring.cloud.stream.rabbit.bindings.* do not appear to be recognized in the native image and are defaulted.

This is problematic as properties like exchangeType, routingKeyExpression, and bindingRoutingKey produce defaults that lead to unexpected behaviors. For example, the exchangeType will always be topic and the routing keys default to #.

It is important to note that properties scoped under spring.cloud.function.definition, spring.cloud.stream.output-bindings, and spring.cloud.stream.bindings seem to function as desired.

To Reproduce A simple reproducible example can be found in full here: https://github.com/mjbingen/spring-cloud-stream-sandbox/tree/native

Example:

application.yml

spring:
  cloud:
    function:
      definition: onFoo;onBar
    stream:
      output-bindings: the-exchange
      bindings:
        onFoo-in-0:
          destination: the-exchange
        onBar-in-0:
          destination: the-exchange
      rabbit:
        bindings:
          the-exchange:
            producer:
              exchangeType: direct
              routingKeyExpression: '''the.foo.key'''
          onFoo-in-0:
            consumer:
              exchangeType: direct
              bindingRoutingKey: the.foo.key
          onBar-in-0:
            consumer:
              exchangeType: direct
              bindingRoutingKey: the.bar.key

Code - expect to self-consume messages when REST endpoint is hit only from onFoo method that is bound to the exchange with matching routing key. However, when application is run as a native image, all bounding keys default to #, so the YAML-specified routing key is ignored and both methods consume the message.

package com.mjb.spring.cloud;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import java.time.OffsetDateTime;
import java.util.function.Consumer;

@SpringBootApplication
@RestController
public class Main
{
    public static void main(String... args)
    {
        SpringApplication.run(Main.class, args);
    }

    @Autowired
    private StreamBridge streamBridge;

    @RequestMapping
    @ResponseStatus(HttpStatus.ACCEPTED)
    public String sendToBridge()
    {
        // Publish a timestamp
        final var now = OffsetDateTime.now().toString();
        streamBridge.send("the-exchange", now);
        return now;
    }

    @Bean
    public Consumer<String> onFoo()
    {
        // Receive from exchange with key 'the.foo.key'
        return received -> System.out.println("Received from FOO: " + received);
    }

    @Bean
    public Consumer<String> onBar()
    {
        // Not expected to receive from exchange with key 'the.bar.key'
        return received -> System.out.println("ERROR: Received from BAR: " + received);
    }
}

Version of the framework Spring Boot 3.2.5 Spring Cloud Stream 4.1.1 Oracle GraalVM 21

Expected behavior Properties defined in application.yml and present at compile time should be reflected in the native image and behave just like the JVM version.

Screenshots

Additional context Similar to prior issue with Kafka streams. However, RabbitMQ binder documentation suggests properties must be defined in the prefix above.

sobychacko commented 6 months ago

@mjbingen We are investigating this issue. Can you please add instructions for how exactly to reproduce the issue? Your sample is a great start, and I see a native branch there. How do you build, run, and test the app? Also, how do you verify that things don't work as expected? Once you clarify these things, we will continue looking at this issue. Thanks!

sobychacko commented 6 months ago

@mjbingen Any updates?

mjbingen commented 6 months ago

Hi @sobychacko - sorry for the delay. Here is the full set of instructions to build the sample application to highlight the issue.

Let me know if this helps or you need any other details/tests to help with the investigation.

sobychacko commented 5 months ago

@mjbingen Sorry for the delay. This turned out to be a bug in the binder. We just addressed it via the above-referenced commit. It should be available as part of the 4.1.2 release.

mjbingen commented 5 months ago

Many thanks @sobychacko ! I confirmed the 4.1.2 release fixes the issue, the native image is working as expected. Thanks for the great work in Spring Cloud Stream and its compatibility with Spring Native!