2

Actually I am not even sure if it is possible to do what I want to: I am consuming an event stream (Event.class) from a Broker (MQTT) using Spring Integration and want to forward the stream to another microservice. Both services are supposed to be horizontally scalable. Thus, in the other service I have a POST endpoint that excepts a Flux<Event> as body (using Spring Cloud Functions: spring-cloud-starter-function-webflux). In the forwarding service I want to use Ribbon and WebClient.

What I have so far (simplified):

@Configuration
public class EventFlowConfiguration{ 

@Autowired
private Webclient webClient;

@Bean
public MessageProducerSupport inboundAdapter() {
    final MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter(MqttAsyncClient.generateClientId(), this.mqttClientFactory, "/topic");
    adapter.setConverter(new DefaultPahoMessageConverter());
    return adapter;
}

@Bean
Publisher<Message<Event>> eventFlow() {
    return IntegrationFlows
            .from(inboundAdapter())
            .handle((payload, header) -> this.eventHandler((String) payload))
            .toReactivePublisher();
}

private Event eventHandler(String payload) {
    // parsing, store in database ...
    return event;
}

@PostConstruct
public void setTrigger() {
    buildTrigger(webClient, "/receiveEventStream", eventFlow(), Event.class);
}

private <T, E extends Event> void buildTrigger(WebClient webClient, String uri, Publisher<Message<T>> publisher, Class<E> eventClass) {
    webClient
            .post()
            .uri(uri)
            .contentType(MediaType.APPLICATION_STREAM_JSON)
            .body(
                    Flux.from(publisher)
                            .retry()
                            .map(Message::getPayload)
                    ,
                    eventClass
            )
            .retrieve()
            .bodyToMono(Void.class)
            .retry()
            .subscribe();
}
}

My questions:

(1) Is their any chance to test this in general? How would I do that? I that about OkHttp, WireMock, an internal spring boot application in the test, that has the /eventReceiverEndpoint but just does asserts. However, didn't really get anywhere. Actually atm I don't even get an error. I would have expected a 4xx since no in memeory server is around at start-up.

(2) Do the two retry() calls really retry connection issues on both sides? Do I loose messages in case of disconnections? How can I test that?

(3) Is Ribbon in combination with this long living Flux still able to do load balancing, after the connection was established (like when the load of the final consumer gets to high or so). I suppose it would have to automatically reconnect the Flux to another instance?

PeMa
  • 1,559
  • 18
  • 44

0 Answers0