GH-335 Add initial support for attaching user application to a binder at startup time

This commit is contained in:
Oleg Zhurakousky
2021-08-17 10:01:10 +02:00
parent d95a1c1a95
commit 8045ff4df9
7 changed files with 58 additions and 55 deletions

View File

@@ -13,6 +13,7 @@
<properties>
<spring-cloud-stream.version>3.2.0-SNAPSHOT</spring-cloud-stream.version>
<java.version>1.8</java.version>
<spring-cloud-function.version>3.2.0-SNAPSHOT</spring-cloud-function.version>
<maven-checkstyle-plugin.failsOnError>true</maven-checkstyle-plugin.failsOnError>
<maven-checkstyle-plugin.failsOnViolation>true
</maven-checkstyle-plugin.failsOnViolation>

View File

@@ -12,6 +12,10 @@
<artifactId>spring-cloud-stream-binder-rabbit-parent</artifactId>
<version>3.2.0-SNAPSHOT</version>
</parent>
<properties>
<disable.checks>true</disable.checks>
</properties>
<dependencies>
<dependency>
@@ -42,4 +46,5 @@
</plugin>
</plugins>
</build>
</project>

View File

@@ -16,22 +16,34 @@
package org.springframework.cloud.stream.binder.rabbit.deployer;
import java.util.function.Consumer;
import java.util.function.Function;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.function.context.FunctionProperties;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.messaging.Message;
import org.springframework.messaging.rsocket.RSocketRequester;
import org.springframework.messaging.support.MessageBuilder;
/**
* Main application class to trigger the deployment of Java Functions based
* user application with RabitMQ bindings.
* <br>
* <br>
* It is based on Spring Cloud Function
* <a href="https://docs.spring.io/spring-cloud-function/docs/3.1.3/reference/html/spring-cloud-function.html#_deploying_a_packaged_function">deploy feature</a>
*
* <br>
* All you need to do is to provide few additional properties to assist Spring Cloud Function deployer.
* <br>
* For example:
* <br>
* <pre class="code">
*
* java -jar spring-cloud-stream-binder-rabbit-deployer/target/spring-cloud-stream-binder-rabbit-deployer-3.2.0-SNAPSHOT.jar\
* --spring.cloud.function.location=/foo/bar/simplestjar-1.0.0.RELEASE.jar
* --spring.cloud.function.function-class=function.example.UpperCaseFunction
*
* </pre>
* The only required property is 'spring.cloud.function.location'. Every other property is optional and is based on the style of user application.
* For more details on the supported styles of user application please refer to
* <a href="https://docs.spring.io/spring-cloud-function/docs/3.1.3/reference/html/spring-cloud-function.html#_deploying_a_packaged_function">
* Deploying a Packaged Function</a> section of Spring Cloud Function.
*
* @author Oleg Zhurakousky
* @since 3.1.2
@@ -40,49 +52,8 @@ import org.springframework.messaging.support.MessageBuilder;
@SpringBootApplication
public class RabbitDeployer {
/*
* SpringApplication.run(RabbitDeployer.class,
"--spring.cloud.function.definition=reverseFunction",
"--spring.cloud.function.location=/bootjar-1.0.0.RELEASE-exec.jar",
"--spring.cloud.function.function-class=function.example.ReverseFunction"
);
*/
public static void main(String[] args) {
SpringApplication.run(RabbitDeployer.class);
SpringApplication.run(RabbitDeployer.class, args);
}
@Bean
@ConditionalOnProperty(name = FunctionProperties.PREFIX + ".rsocket.enabled", matchIfMissing = true)
public Function<Message<byte[]>, Message<byte[]>> gateway(StreamBridge bridge) {
return message -> {
String destinationName = (String) message.getHeaders().get("target_destination");
bridge.send(destinationName, message);
return MessageBuilder.withPayload("Successfully sent to reverseFunction-in-0".getBytes()).build();
};
}
/*
* Just like any other stream bean. This one will subscribe to broker destination (using regular stream mechanisms)
* and using some configuration provided by the user will propagate message to remote (rsocket) subscriber
*/
@Bean
@ConditionalOnProperty(name = FunctionProperties.PREFIX + ".rsocket.enabled", matchIfMissing = true)
public Consumer<Message<byte[]>> delegatingConsumer(RSocketRequester.Builder rsocketRequesterBuilder, Environment environment) {
String host = environment.getProperty("spring.cloud.function.rsocket.subscriber.host");
String port = environment.getProperty("spring.cloud.function.rsocket.subscriber.port");
return message -> {
// rsocketRequesterBuilder.tcp("host", Integer.valueOf(port))
// .route("pojoToString")
// .data(message)
// .retrieveMono(String.class);
};
}
// Step-1 - rabbit-bundle.jar(time) | rabbit-bundle.jar(log) - Step One - local
// Step-2 - polyglot
// Step-3 - SCDF
// Step-4 - Kubernetes
//http | rabbit-rsocket-bundle.jar(producer) | python | rabbit-rsocket-bundle.jar(consumer) | rabbit-bundle.jar(log)
}

View File

@@ -35,7 +35,9 @@ class RabbitDeployerEnvironmentPostProcessor implements EnvironmentPostProcessor
if (!environment.containsProperty("spring.cloud.function.rsocket.enabled")) {
environment.getSystemProperties().putIfAbsent("spring.cloud.function.rsocket.enabled", false);
}
if (!environment.containsProperty("spring.cloud.function.deployer.enabled")) {
environment.getSystemProperties().putIfAbsent("spring.cloud.function.deployer.enabled", false);
}
}
}

View File

@@ -14,6 +14,11 @@
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-deployer</artifactId>
<version>${spring-cloud-function.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit-core</artifactId>
@@ -92,4 +97,12 @@
<version>4.5.13</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@@ -1 +1,4 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.springframework.cloud.stream.binder.rabbit.config.ExtendedBindingHandlerMappingsProviderConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration:\
org.springframework.cloud.stream.binder.rabbit.config.ExtendedBindingHandlerMappingsProviderConfiguration
org.springframework.boot.env.EnvironmentPostProcessor:\
org.springframework.cloud.stream.binder.rabbit.deployer.RabbitDeployerEnvironmentPostProcessor

View File

@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
"-//Puppy Crawl//DTD Suppressions 1.1//EN"
"https://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
<suppressions>
<suppress files=".*RabbitDeployer\.java" checks="HideUtilityClassConstructor"/>
<suppress files=".*RabbitDeployer\.java" checks="FinalClass"/>
</suppressions>