From 1399a2e458a72d3e91bb05e28df98de447fa9865 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Fri, 8 Sep 2023 15:45:30 +0200 Subject: [PATCH] Split files --- .../ROOT/pages/spring-cloud-function.adoc | 1057 ----------------- .../deploying-a-packaged.adoc | 183 +++ .../functional-bean-definitions.adoc | 5 + .../getting-started.adoc | 5 + .../spring-cloud-function/introduction.adoc | 7 + .../programming-model.adoc | 717 +++++++++++ .../serverless-platform-adapters.adoc | 14 + .../standalone-streaming-applications.adoc | 6 + .../standalone-web-applications.adoc | 120 ++ 9 files changed, 1057 insertions(+), 1057 deletions(-) create mode 100644 docs/modules/ROOT/pages/spring-cloud-function/deploying-a-packaged.adoc create mode 100644 docs/modules/ROOT/pages/spring-cloud-function/functional-bean-definitions.adoc create mode 100644 docs/modules/ROOT/pages/spring-cloud-function/getting-started.adoc create mode 100644 docs/modules/ROOT/pages/spring-cloud-function/introduction.adoc create mode 100644 docs/modules/ROOT/pages/spring-cloud-function/programming-model.adoc create mode 100644 docs/modules/ROOT/pages/spring-cloud-function/serverless-platform-adapters.adoc create mode 100644 docs/modules/ROOT/pages/spring-cloud-function/standalone-streaming-applications.adoc create mode 100644 docs/modules/ROOT/pages/spring-cloud-function/standalone-web-applications.adoc diff --git a/docs/modules/ROOT/pages/spring-cloud-function.adoc b/docs/modules/ROOT/pages/spring-cloud-function.adoc index 9cff8a176..f44e47630 100644 --- a/docs/modules/ROOT/pages/spring-cloud-function.adoc +++ b/docs/modules/ROOT/pages/spring-cloud-function.adoc @@ -13,1060 +13,3 @@ Mark Fisher, Dave Syer, Oleg Zhurakousky, Anshul Mehra, Dan Dobrin :nofooter: :branch: master -[[introduction]] -== Introduction - -include::_intro.adoc[] - -include::https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/docs/src/main/asciidoc/contributing-docs.adoc[] - -[[getting-started]] -== Getting Started - -include::getting-started.adoc[] - -[[programming-model]] -== Programming model - -[[function.catalog]] - -[[function-catalog-and-flexible-function-signatures]] -=== Function Catalog and Flexible Function Signatures - -One of the main features of Spring Cloud Function is to adapt and support a range of type signatures for user-defined functions, -while providing a consistent execution model. -That's why all user defined functions are transformed into a canonical representation by `FunctionCatalog`. - -While users don't normally have to care about the `FunctionCatalog` at all, it is useful to know what -kind of functions are supported in user code. - -It is also important to understand that Spring Cloud Function provides first class support for reactive API -provided by https://projectreactor.io/[Project Reactor] allowing reactive primitives such as `Mono` and `Flux` -to be used as types in user defined functions providing greater flexibility when choosing programming model for -your function implementation. -Reactive programming model also enables functional support for features that would be otherwise difficult to impossible to implement -using imperative programming style. For more on this please read <> section. - -[[java-8-function-support]] -=== Java 8 function support - -Spring Cloud Function embraces and builds on top of the 3 core functional interfaces defined by Java -and available to us since Java 8. - -- Supplier -- Function -- Consumer - -To avoid constantly mentioning `Supplier`, `Function` and `Consumer` we’ll refer to them a Functional beans for the rest of this manual where appropriate. - -In a nutshell, any bean in your Application Context that is Functional bean will lazily be registered with `FunctionCatalog`. -This means that it could benefit from all of the additional features described in this reference manual. - -In a simplest of application all you need to do is to declare `@Bean` of type `Supplier`, `Function` or `Consumer` in your application configuration. -Then you can access `FunctionCatalog` and lookup a particular function based on its name. - -For example: - - -[source, test] ----- -@Bean -public Function uppercase() { - return value -> value.toUpperCase(); -} - -. . . - -FunctionCatalog catalog = applicationContext.getBean(FunctionCatalog.class); -Function uppercase = catalog.lookup(“uppercase”); ----- - -Important to understand that given that `uppercase` is a bean, you can certainly get it form the `ApplicationContext` directly, but all you will get is just your bean as you declared it without any extra features provided by SCF. When you do lookup of a function via `FunctionCatalog`, the instance you will receive is wrapped (instrumented) with additional features (i.e., type conversion, composition etc.) described in this manual. Also, it is important to understand that a typical user does not use Spring Cloud Function directly. Instead a typical user implements Java `Function/Supplier/Consumer` with the idea of using it in different execution contexts without additional work. For example the same java function could be represented as _REST endpoint_ or _Streaming message handler_ or _AWS Lambda_ and more via Spring Cloud Function provided -adapters as well as other frameworks using Spring Cloud Function as the core programming model (e.g., https://spring.io/projects/spring-cloud-stream[Spring Cloud Stream]) -So in summary Spring Cloud Function instruments java functions with additional features to be utilised in variety of execution contexts. - - -[[function-definition]] -==== Function definition -While the previous example shows you how to lookup function in FunctionCatalog programmatically, in a typical integration case where Spring Cloud Function used as programming model by another framework (e.fg. Spring Cloud Stream), you declare which functions to use via `spring.cloud.function.definition` property. Knowing that it is important to understand some default behaviour when it comes to discovering functions in `FunctionCatalog`. For example, if you only have one Functional bean in your `ApplicationContext`, the `spring.cloud.function.definition` property typically will not be required, since a single function in `FunctionCatalog` can be looked up by an empty name or any name. For example, assuming that `uppercase` is the only function in your catalog, it can be looked up as `catalog.lookup(null)`, `catalog.lookup(“”)`, `catalog.lookup(“foo”)` -That said, for cases where you are using framework such as Spring Cloud Stream which uses `spring.cloud.function.definition` it is best practice and recommended to always use `spring.cloud.function.definition` property. - -For example, - -[source, test] ----- -spring.cloud.function.definition=uppercase ----- - -[[filtering-ineligible-functions]] -==== Filtering ineligible functions -A typical Application Context may include beans that are valid java functions, but not intended to be candidates to be registered with `FunctionCatalog`. -Such beans could be auto-configurations from other projects or any other beans that qualify to be Java functions. -The framework provides default filtering of known beans that should not be candidates for registration with function catalog. -You can also add to this list additional beans by providing coma delimited list of bean definition names using -`spring.cloud.function.ineligible-definitions` property. - -For example, - -[source, test] ----- -spring.cloud.function.ineligible-definitions=foo,bar ----- - -[[supplier]] -==== Supplier -Supplier can be _reactive_ - `Supplier>` -or _imperative_ - `Supplier`. From the invocation standpoint this should make no difference -to the implementor of such Supplier. However, when used within frameworks -(e.g., https://spring.io/projects/spring-cloud-stream[Spring Cloud Stream]), Suppliers, especially reactive, -often used to represent the source of the stream, therefore they are invoked once to get the stream (e.g., Flux) -to which consumers can subscribe to. In other words such suppliers represent an equivalent of an _infinite stream_. -However, the same reactive suppliers can also represent _finite_ stream(s) (e.g., result set on the polled JDBC data). -In those cases such reactive suppliers must be hooked up to some polling mechanism of the underlying framework. - -To assist with that Spring Cloud Function provides a marker annotation -`org.springframework.cloud.function.context.PollableBean` to signal that such supplier produces a -finite stream and may need to be polled again. That said, it is important to understand that Spring Cloud Function itself -provides no behavior for this annotation. - -In addition `PollableBean` annotation exposes a _splittable_ attribute to signal that produced stream -needs to be split (see https://www.enterpriseintegrationpatterns.com/patterns/messaging/Sequencer.html[Splitter EIP]) - -Here is the example: - -[source, java] ----- -@PollableBean(splittable = true) -public Supplier> someSupplier() { - return () -> { - String v1 = String.valueOf(System.nanoTime()); - String v2 = String.valueOf(System.nanoTime()); - String v3 = String.valueOf(System.nanoTime()); - return Flux.just(v1, v2, v3); - }; -} ----- - -[[function]] -==== Function -Function can also be written in imperative or reactive way, yet unlike Supplier and Consumer there are -no special considerations for the implementor other then understanding that when used within frameworks -such as https://spring.io/projects/spring-cloud-stream[Spring Cloud Stream] and others, reactive function is -invoked only once to pass a reference to the stream (Flux or Mono) and imperative is invoked once per event. - -[[consumer]] -==== Consumer -Consumer is a little bit special because it has a `void` return type, -which implies blocking, at least potentially. Most likely you will not -need to write `Consumer>`, but if you do need to do that, -remember to subscribe to the input flux. - -[[function-composition]] -=== Function Composition -Function Composition is a feature that allows one to compose several functions into one. -The core support is based on function composition feature available with https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html#andThen-java.util.function.Function-[Function.andThen(..)] -support available since Java 8. However on top of it, we provide few additional features. - -[[declarative-function-composition]] -==== Declarative Function Composition - -This feature allows you to provide composition instruction in a declarative way using `|` (pipe) or `,` (comma) delimiter -when providing `spring.cloud.function.definition` property. - -For example ----- ---spring.cloud.function.definition=uppercase|reverse ----- -Here we effectively provided a definition of a single function which itself is a composition of -function `uppercase` and function `reverse`. In fact that is one of the reasons why the property name is _definition_ and not _name_, -since the definition of a function can be a composition of several named functions. -And as mentioned you can use `,` instead of pipe (such as `...definition=uppercase,reverse`). - -[[composing-non-functions]] -==== Composing non-Functions -Spring Cloud Function also supports composing Supplier with `Consumer` or `Function` as well as `Function` with `Consumer`. -What's important here is to understand the end product of such definitions. -Composing Supplier with Function still results in Supplier while composing Supplier with Consumer will effectively render Runnable. -Following the same logic composing Function with Consumer will result in Consumer. - -And of course you can't compose uncomposable such as Consumer and Function, Consumer and Supplier etc. - - -[[function-routing-and-filtering]] -=== Function Routing and Filtering - -Since version 2.2 Spring Cloud Function provides routing feature allowing -you to invoke a single function which acts as a router to an actual function you wish to invoke -This feature is very useful in certain FAAS environments where maintaining configurations -for several functions could be cumbersome or exposing more than one function is not possible. - -The `RoutingFunction` is registered in _FunctionCatalog_ under the name `functionRouter`. For simplicity -and consistency you can also refer to `RoutingFunction.FUNCTION_NAME` constant. - -This function has the following signature: - -[source, java] ----- -public class RoutingFunction implements Function { -. . . -} ----- -The routing instructions could be communicated in several ways. We support providing instructions via Message headers, System -properties as well as pluggable strategy. So let's look at some of the details - -[[messageroutingcallback]] -==== MessageRoutingCallback - -The `MessageRoutingCallback` is a strategy to assist with determining the name of the route-to function definition. - -[source, java] ----- -public interface MessageRoutingCallback { - FunctionRoutingResult routingResult(Message message); - . . . -} ----- - -All you need to do is implement and register it as a bean to be picked up by the `RoutingFunction`. -For example: - -[source, java] ----- -@Bean -public MessageRoutingCallback customRouter() { - return new MessageRoutingCallback() { - @Override - public FunctionRoutingResult routingResult(Message message) { - return new FunctionRoutingResult((String) message.getHeaders().get("func_name")); - } - }; -} ----- - -In the preceding example you can see a very simple implementation of `MessageRoutingCallback` which determines the function definition from -`func_name` Message header of the incoming Message and returns the instance of `FunctionRoutingResult` containing the definition of function to invoke. - -*Message Headers* - -If the input argument is of type `Message`, you can communicate routing instruction by setting one of -`spring.cloud.function.definition` or `spring.cloud.function.routing-expression` Message headers. -As the name of the property suggests `spring.cloud.function.routing-expression` relies on Spring Expression Language (SpEL). -For more static cases you can use `spring.cloud.function.definition` header which allows you to provide -the name of a single function (e.g., `...definition=foo`) or a composition instruction (e.g., `...definition=foo|bar|baz`). -For more dynamic cases you can use `spring.cloud.function.routing-expression` header and provide SpEL expression that should resolve -into definition of a function (as described above). - -NOTE: SpEL evaluation context's root object is the -actual input argument, so in the case of `Message` you can construct expression that has access -to both `payload` and `headers` (e.g., `spring.cloud.function.routing-expression=headers.function_name`). - -IMPORTANT: SpEL allows user to provide string representation of Java code to be executed. Given that the `spring.cloud.function.routing-expression` could be provided via Message headers means that ability to set such expression could be exposed to the end user (i.e., HTTP Headers when using web module) which could result in some problems (e.g., malicious code). To manage that, all expressions coming via Message headers will only be evaluated against `SimpleEvaluationContext` which has limited functionality and designed to only evaluate the context object (Message in our case). On the other hand, all expressions that are set via property or system variable are evaluated against `StandardEvaluationContext`, which allows for full flexibility of Java language. -While setting expression via system/application property or environment variable is generally considered to be secure as it is not exposed to the end user in normal cases, there are cases where visibility as well as capability to update system, application and environment variables are indeed exposed to the end user via Spring Boot Actuator endpoints provided either by some of the Spring projects or third parties or custom implementation by the end user. Such endpoints must be secured using industry standard web security practices. -Spring Cloud Function does not expose any of such endpoints. - -In specific execution environments/models the adapters are responsible to translate and communicate -`spring.cloud.function.definition` and/or `spring.cloud.function.routing-expression` via Message header. -For example, when using _spring-cloud-function-web_ you can provide `spring.cloud.function.definition` as an HTTP -header and the framework will propagate it as well as other HTTP headers as Message headers. - -*Application Properties* - -Routing instruction can also be communicated via `spring.cloud.function.definition` -or `spring.cloud.function.routing-expression` as application properties. The rules described in the -previous section apply here as well. The only difference is you provide these instructions as -application properties (e.g., `--spring.cloud.function.definition=foo`). - -NOTE: It is important to understand that providing `spring.cloud.function.definition` -or `spring.cloud.function.routing-expression` as Message headers will only work for imperative functions (e.g., `Function`). -That is to say that we can _only_ route ***per-message*** with imperative functions. With reactive functions we can not route -***per-message***. Therefore you can only provide your routing instructions as Application Properties. -It's all about unit-of-work. In imperative function unit of work is Message so we can route based on such unit-of-work. -With reactive function unit-of-work is the entire stream, so we'll act only on the instruction provided via application -properties and route the entire stream. - -*Order of priority for routing instructions* - -Given that we have several mechanisms of providing routing instructions it is important to understand the priorities for -conflict resolutions in the event multiple mechanisms are used at the same time, so here is the order: - -1. `MessageRoutingCallback` (If function is imperative will take over regardless if anything else is defined) -2. Message Headers (If function is imperative and no `MessageRoutingCallback` provided) -3. Application Properties (Any function) - -*Unroutable Messages* - -In the event route-to function is not available in catalog you will get an exception stating that. - -There are cases when such behavior is not desired and you may want to have some "catch-all" type function which can handle such messages. -To accomplish that, framework provides `org.springframework.cloud.function.context.DefaultMessageRoutingHandler` strategy. All you need to do is register it as a bean. -Its default implementation will simply log the fact that the message is un-routable, but will allow message flow to proceed without the exception, effectively dropping the un-routable message. -If you want something more sophisticated all you need to do is provide your own implementation of this strategy and register it as a bean. - -[source, java] ----- -@Bean -public DefaultMessageRoutingHandler defaultRoutingHandler() { - return new DefaultMessageRoutingHandler() { - @Override - public void accept(Message message) { - // do something really cool - } - }; -} ----- - -[[function-filtering]] -==== Function Filtering -Filtering is the type of routing where there are only two paths - 'go' or 'discard'. In terms of functions it mean -you only want to invoke a certain function if some condition returns 'true', otherwise you want to discard input. -However, when it comes to discarding input there are many interpretation of what it could mean in the context of your application. -For example, you may want to log it, or you may want to maintain the counter of discarded messages. you may also want to do nothing at all. -Because of these different paths, we do not provide a general configuration option for how to deal with discarded messages. -Instead we simply recommend to define a simple Consumer which would signify the 'discard' path: - -[source, java] ----- -@Bean -public Consumer devNull() { - // log, count or whatever -} ----- -Now you can have routing expression that really only has two paths effectively becoming a filter. For example: - -[source, text] ----- ---spring.cloud.function.routing-expression=headers.contentType.toString().equals('text/plain') ? 'echo' : 'devNull' ----- -Every message that does not fit criteria to go to 'echo' function will go to 'devNull' where you can simply do nothing with it. -The signature `Consumer` will also ensure that no type conversion will be attempted resulting in almost no execution overhead. - - -IMPORTANT: When dealing with reactive inputs (e.g., Publisher), routing instructions must only be provided via Function properties. This is -due to the nature of the reactive functions which are invoked only once to pass a Publisher and the rest -is handled by the reactor, hence we can not access and/or rely on the routing instructions communicated via individual -values (e.g., Message). - -[[multiple-routers]] -==== Multiple Routers - -By default the framework will always have a single routing function configured as described in previous sections. However, there are times when you may need more than one routing function. -In that case you can create your own instance of the `RoutingFunction` bean in addition to the existing one as long as you give it a name other than `functionRouter`. - -You can pass `spring.cloud.function.routing-expression` or `spring.cloud.function.definition` to RoutinFunction as key/value pairs in the map. - -Here is a simple example - ----- -@Configuration -protected static class MultipleRouterConfiguration { - - @Bean - RoutingFunction mySpecialRouter(FunctionCatalog functionCatalog, BeanFactory beanFactory, @Nullable MessageRoutingCallback routingCallback) { - Map propertiesMap = new HashMap<>(); - propertiesMap.put(FunctionProperties.PREFIX + ".routing-expression", "'reverse'"); - return new RoutingFunction(functionCatalog, propertiesMap, new BeanFactoryResolver(beanFactory), routingCallback); - } - - @Bean - public Function reverse() { - return v -> new StringBuilder(v).reverse().toString(); - } - - @Bean - public Function uppercase() { - return String::toUpperCase; - } -} ----- - -and a test that demonstrates how it works - -` ----- -@Test -public void testMultipleRouters() { - System.setProperty(FunctionProperties.PREFIX + ".routing-expression", "'uppercase'"); - FunctionCatalog functionCatalog = this.configureCatalog(MultipleRouterConfiguration.class); - Function function = functionCatalog.lookup(RoutingFunction.FUNCTION_NAME); - assertThat(function).isNotNull(); - Message message = MessageBuilder.withPayload("hello").build(); - assertThat(function.apply(message)).isEqualTo("HELLO"); - - function = functionCatalog.lookup("mySpecialRouter"); - assertThat(function).isNotNull(); - message = MessageBuilder.withPayload("hello").build(); - assertThat(function.apply(message)).isEqualTo("olleh"); -} ----- - -[[input/output-enrichment]] -=== Input/Output Enrichment - -There are often times when you need to modify or refine an incoming or outgoing Message and to keep your code clean of non-functional concerns. You don’t want to do it inside of your business logic. - -You can always accomplish it via <>. Such approach provides several benefits: - -- It allows you to isolate this non-functional concern into a separate function which you can compose with the business function as function definition. -- It provides you with complete freedom (and danger) as to what you can modify before incoming message reaches the actual business function. - -[source, java] ----- -@Bean -public Function, Message> enrich() { - return message -> MessageBuilder.fromMessage(message).setHeader("foo", "bar").build(); -} - -@Bean -public Function, Message> myBusinessFunction() { - // do whatever -} ----- - -And then compose your function by providing the following function definition `enrich|myBusinessFunction`. - -While the described approach is the most flexible, it is also the most involved as it requires you to write some code, make it a bean or -manually register it as a function before you can compose it with the business function as you can see from the preceding example. - -But what if modifications (enrichments) you are trying to make are trivial as they are in the preceding example? Is there a simpler and more dynamic and configurable - mechanism to accomplish the same? - -Since version 3.1.3, the framework allows you to provide SpEL expression to enrich individual message headers for both input going into function and -and output coming out of it. Let’s look at one of the tests as the example. - -[source, java] ----- -@Test -public void testMixedInputOutputHeaderMapping() throws Exception { - try (ConfigurableApplicationContext context = new SpringApplicationBuilder( - SampleFunctionConfiguration.class).web(WebApplicationType.NONE).run( - "--logging.level.org.springframework.cloud.function=DEBUG", - "--spring.main.lazy-initialization=true", - "--spring.cloud.function.configuration.split.output-header-mapping-expression.keyOut1='hello1'", - "--spring.cloud.function.configuration.split.output-header-mapping-expression.keyOut2=headers.contentType", - "--spring.cloud.function.configuration.split.input-header-mapping-expression.key1=headers.path.split('/')[0]", - "--spring.cloud.function.configuration.split.input-header-mapping-expression.key2=headers.path.split('/')[1]", - "--spring.cloud.function.configuration.split.input-header-mapping-expression.key3=headers.path")) { - - FunctionCatalog functionCatalog = context.getBean(FunctionCatalog.class); - FunctionInvocationWrapper function = functionCatalog.lookup("split"); - Message result = (Message) function.apply(MessageBuilder.withPayload("helo") - .setHeader(MessageHeaders.CONTENT_TYPE, "application/json") - .setHeader("path", "foo/bar/baz").build()); - assertThat(result.getHeaders().containsKey("keyOut1")).isTrue(); - assertThat(result.getHeaders().get("keyOut1")).isEqualTo("hello1"); - assertThat(result.getHeaders().containsKey("keyOut2")).isTrue(); - assertThat(result.getHeaders().get("keyOut2")).isEqualTo("application/json"); - } -} ----- - -Here you see a properties called `input-header-mapping-expression` and `output-header-mapping-expression` preceded by the name of the function (i.e., `split`) and followed by the name of the message header key you want to set and the value as SpEL expression. The first expression (for 'keyOut1') is literal SpEL expressions enclosed in single quotes, effectively setting 'keyOut1' to value `hello1`. The `keyOut2` is set to the value of existing 'contentType' header. - -You can also observe some interesting features in the input header mapping where we actually splitting a value of the existing header 'path', setting individual values of key1 and key2 to the values of split elements based on the index. - -NOTE: if for whatever reason the provided expression evaluation fails, the execution of the function will proceed as if nothing ever happen. -However you will see the WARN message in your logs informing you about it - -[source, text] ----- -o.s.c.f.context.catalog.InputEnricher : Failed while evaluating expression "hello1" on incoming message. . . ----- - -In the event you are dealing with functions that have multiple inputs (next section), you can use index immediately after `input-header-mapping-expression` - -[source, text] ----- ---spring.cloud.function.configuration.echo.input-header-mapping-expression[0].key1=‘hello1' ---spring.cloud.function.configuration.echo.input-header-mapping-expression[1].key2='hello2' ----- - -[[function-arity]] -=== Function Arity - -There are times when a stream of data needs to be categorized and organized. For example, -consider a classic big-data use case of dealing with unorganized data containing, let’s say, -‘orders’ and ‘invoices’, and you want each to go into a separate data store. -This is where function arity (functions with multiple inputs and outputs) support -comes to play. - -Let’s look at an example of such a function (full implementation details are available -https://github.com/spring-cloud/spring-cloud-stream/blob/master/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/function/MultipleInputOutputFunctionTests.java#L342[here]), - -[source, java] ----- -@Bean -public Function, Tuple2, Flux>> organise() { - return flux -> ...; -} ----- - -Given that Project Reactor is a core dependency of SCF, we are using its Tuple library. -Tuples give us a unique advantage by communicating to us both _cardinality_ and _type_ information. -Both are extremely important in the context of SCSt. Cardinality lets us know -how many input and output bindings need to be created and bound to the corresponding -inputs and outputs of a function. Awareness of the type information ensures proper type -conversion. - -Also, this is where the ‘index’ part of the naming convention for binding -names comes into play, since, in this function, the two output binding -names are `organise-out-0` and `organise-out-1`. - -IMPORTANT: IMPORTANT: At the moment, function arity is *only* supported for reactive functions -(`Function...>, TupleN...>>`) centered on Complex event processing -where evaluation and computation on confluence of events typically requires view into a -stream of events rather than single event. - -[[input-header-propagation]] -=== Input Header propagation - -In a typical scenario input Message headers are not propagated to output and rightfully so, since the output of a function may be an input to something else requiring it's own set of Message headers. -However, there are times when such propagation may be necessary so Spring Cloud Function provides several mechanisms to accomplish this. - -First you can always copy headers manually. For example, if you have a Function with the signature that takes `Message` and returns `Message` (i.e., `Function`), you can simply and selectively copy headers yourselves. Remember, if your function returns Message, the framework will not do anything to it other then properly converting its payload. -However, such approach may prove to be a bit tedious, especially in cases when you simply want to copy all headers. -To assist with cases like this we provide a simple property that would allow you to set a boolean flag on a function where you want input headers to be propagated. -The property is `copy-input-headers`. - -For example, let's assume you have the following configuration: - -[source, java] ----- -@EnableAutoConfiguration -@Configuration -protected static class InputHeaderPropagationConfiguration { - - @Bean - public Function uppercase() { - return x -> x.toUpperCase(); - } -} ----- - -As you know you can still invoke this function by sending a Message to it (framework will take care of type conversion and payload extraction) - -By simply setting `spring.cloud.function.configuration.uppercase.copy-input-headers` to `true`, the following assertion will be true as well - ----- -Function, Message> uppercase = catalog.lookup("uppercase", "application/json"); -Message result = uppercase.apply(MessageBuilder.withPayload("bob").setHeader("foo", "bar").build()); -assertThat(result.getHeaders()).containsKey("foo"); ----- - -[[type-conversion-content-type-negotiation]] -=== Type conversion (Content-Type negotiation) - -Content-Type negotiation is one of the core features of Spring Cloud Function as it allows to not only transform the incoming data to the types declared -by the function signature, but to do the same transformation during function composition making otherwise un-composable (by type) functions composable. - -To better understand the mechanics and the necessity behind content-type negotiation, we take a look at a very simple use case by -using the following function as an example: - -[source, java] ----- -@Bean -public Function personFunction {..} ----- - -The function shown in the preceding example expects a `Person` object as an argument and produces a String type as an output. If such function is -invoked with the type `Person`, than all works fine. But typically function plays a role of a handler for the incoming data which most often comes -in the raw format such as `byte[]`, `JSON String` etc. In order for the framework to succeed in passing the incoming data as an argument to -this function, it has to somehow transform the incoming data to a `Person` type. - -Spring Cloud Function relies on two native to Spring mechanisms to accomplish that. - -. _MessageConverter_ - to convert from incoming Message data to a type declared by the function. -. _ConversionService_ - to convert from incoming non-Message data to a type declared by the function. - -This means that depending on the type of the raw data (Message or non-Message) Spring Cloud Function will apply one or the other mechanisms. - -For most cases when dealing with functions that are invoked as part of some other request (e.g., HTTP, Messaging etc) the framework relies on `MessageConverters`, -since such requests already converted to Spring `Message`. In other words, the framework locates and applies the appropriate `MessageConverter`. -To accomplish that, the framework needs some instructions from the user. One of these instructions is already provided by the signature of the function -itself (Person type). Consequently, in theory, that should be (and, in some cases, is) enough. However, for the majority of use cases, in order to -select the appropriate `MessageConverter`, the framework needs an additional piece of information. That missing piece is `contentType` header. - -Such header usually comes as part of the Message where it is injected by the corresponding adapter that created such Message in the first place. -For example, HTTP POST request will have its content-type HTTP header copied to `contentType` header of the Message. - -For cases when such header does not exist framework relies on the default content type as `application/json`. - - -[[content-type-versus-argument-type]] -==== Content Type versus Argument Type - -As mentioned earlier, for the framework to select the appropriate `MessageConverter`, it requires argument type and, optionally, content type information. -The logic for selecting the appropriate `MessageConverter` resides with the argument resolvers which trigger right before the invocation of the user-defined -function (which is when the actual argument type is known to the framework). -If the argument type does not match the type of the current payload, the framework delegates to the stack of the -pre-configured `MessageConverters` to see if any one of them can convert the payload. - -The combination of `contentType` and argument type is the mechanism by which framework determines if message can be converted to a target type by locating -the appropriate `MessageConverter`. -If no appropriate `MessageConverter` is found, an exception is thrown, which you can handle by adding a custom `MessageConverter` -(see `<>`). - -NOTE: Do not expect `Message` to be converted into some other type based only on the `contentType`. -Remember that the `contentType` is complementary to the target type. -It is a hint, which `MessageConverter` may or may not take into consideration. - -[[message-converters]] -==== Message Converters - -`MessageConverters` define two methods: - -[source, java] ----- -Object fromMessage(Message message, Class targetClass); - -Message toMessage(Object payload, @Nullable MessageHeaders headers); ----- - -It is important to understand the contract of these methods and their usage, specifically in the context of Spring Cloud Stream. - -The `fromMessage` method converts an incoming `Message` to an argument type. -The payload of the `Message` could be any type, and it is -up to the actual implementation of the `MessageConverter` to support multiple types. - - -[[provided-messageconverters]] -==== Provided MessageConverters - -As mentioned earlier, the framework already provides a stack of `MessageConverters` to handle most common use cases. -The following list describes the provided `MessageConverters`, in order of precedence (the first `MessageConverter` that works is used): - -. `JsonMessageConverter`: Supports conversion of the payload of the `Message` to/from POJO for cases when `contentType` is `application/json` using Jackson (DEFAULT) or Gson libraries. This message converter also aware of `type` parameter (e.g., _application/json;type=foo.bar.Person_). This is useful for cases where types may not be known at the time when function is developed, hence function signature may look like `Function` or `Function` or `Function`. In other words for type conversion we typically derive type from function signature. Having, mime-type parameter allows you to communicate type in a more dynamic way. -. `ByteArrayMessageConverter`: Supports conversion of the payload of the `Message` from `byte[]` to `byte[]` for cases when `contentType` is `application/octet-stream`. It is essentially a pass through and exists primarily for backward compatibility. -. `StringMessageConverter`: Supports conversion of any type to a `String` when `contentType` is `text/plain`. - -When no appropriate converter is found, the framework throws an exception. When that happens, you should check your code and configuration and ensure you did -not miss anything (that is, ensure that you provided a `contentType` by using a binding or a header). -However, most likely, you found some uncommon case (such as a custom `contentType` perhaps) and the current stack of provided `MessageConverters` -does not know how to convert. If that is the case, you can add custom `MessageConverter`. See <>. - -[[user-defined-message-converters]] -==== User-defined Message Converters - -Spring Cloud Function exposes a mechanism to define and register additional `MessageConverters`. -To use it, implement `org.springframework.messaging.converter.MessageConverter`, configure it as a `@Bean`. -It is then appended to the existing stack of `MessageConverter`s. - -NOTE: It is important to understand that custom `MessageConverter` implementations are added to the head of the existing stack. -Consequently, custom `MessageConverter` implementations take precedence over the existing ones, which lets you override as well as add to the existing converters. - -The following example shows how to create a message converter bean to support a new content type called `application/bar`: - -[source,java] ----- -@SpringBootApplication -public static class SinkApplication { - - ... - - @Bean - public MessageConverter customMessageConverter() { - return new MyCustomMessageConverter(); - } -} - -public class MyCustomMessageConverter extends AbstractMessageConverter { - - public MyCustomMessageConverter() { - super(new MimeType("application", "bar")); - } - - @Override - protected boolean supports(Class clazz) { - return (Bar.class.equals(clazz)); - } - - @Override - protected Object convertFromInternal(Message message, Class targetClass, Object conversionHint) { - Object payload = message.getPayload(); - return (payload instanceof Bar ? payload : new Bar((byte[]) payload)); - } -} ----- - -[[note-on-json-options]] -==== Note on JSON options - -In Spring Cloud Function we support Jackson and Gson mechanisms to deal with JSON. -And for your benefit have abstracted it under `org.springframework.cloud.function.json.JsonMapper` which itself is aware of two mechanisms and will use the one selected -by you or following the default rule. -The default rules are as follows: - -* Whichever library is on the classpath that is the mechanism that is going to be used. So if you have `com.fasterxml.jackson.*` to the classpath, Jackson is going to be used and if you have `com.google.code.gson`, then Gson will be used. -* If you have both, then Gson will be the default, or you can set `spring.cloud.function.preferred-json-mapper` property with either of two values: `gson` or `jackson`. - - -That said, the type conversion is usually transparent to the developer, however given that `org.springframework.cloud.function.json.JsonMapper` is also registered as a bean -you can easily inject it into your code if needed. - - -[[kotlin-lambda-support]] -=== Kotlin Lambda support - -We also provide support for Kotlin lambdas (since v2.0). -Consider the following: - -[source, java] ----- -@Bean -open fun kotlinSupplier(): () -> String { - return { "Hello from Kotlin" } -} - -@Bean -open fun kotlinFunction(): (String) -> String { - return { it.toUpperCase() } -} - -@Bean -open fun kotlinConsumer(): (String) -> Unit { - return { println(it) } -} - ----- -The above represents Kotlin lambdas configured as Spring beans. The signature of each maps to a Java equivalent of -`Supplier`, `Function` and `Consumer`, and thus supported/recognized signatures by the framework. -While mechanics of Kotlin-to-Java mapping are outside of the scope of this documentation, it is important to understand that the -same rules for signature transformation outlined in "Java 8 function support" section are applied here as well. - -To enable Kotlin support all you need is to add Kotlin SDK libraries on the classpath which will trigger appropriate -autoconfiguration and supporting classes. - -[[function-component-scan]] -=== Function Component Scan - -Spring Cloud Function will scan for implementations of `Function`, `Consumer` and `Supplier` in a package called `functions` if it exists. Using this -feature you can write functions that have no dependencies on Spring - not even the `@Component` annotation is needed. If you want to use a different -package, you can set `spring.cloud.function.scan.packages`. You can also use `spring.cloud.function.scan.enabled=false` to switch off the scan completely. - -[[standalone-web-applications]] -== Standalone Web Applications - -Functions could be automatically exported as HTTP endpoints. - -The `spring-cloud-function-web` module has autoconfiguration that -activates when it is included in a Spring Boot web application (with -MVC support). There is also a `spring-cloud-starter-function-web` to -collect all the optional dependencies in case you just want a simple -getting started experience. - -With the web configurations activated your app will have an MVC -endpoint (on "/" by default, but configurable with -`spring.cloud.function.web.path`) that can be used to access the -functions in the application context where function name becomes part of the URL path. The supported content types are -plain text and JSON. - -IMPORTANT: It is important to understand that while SCF provides ability to export Functional beans as REST endpoints it is NOT a replacement for Spring MVC/WebFlux etc. -It is primarily to accommodate _stateless serverless patterns_ where you simply want to have some stateless functionality to be exposed via HTTP. - -|=== -| Method | Path | Request | Response | Status - -| GET | /{supplier} | - | Items from the named supplier | 200 OK -| POST | /{consumer} | JSON object or text | Mirrors input and pushes request body into consumer | 202 Accepted -| PUT | /{consumer} | JSON object or text | Mirrors input and pushes request body into consumer | 202 Accepted -| DELETE | /{consumer} | JSON object or text | - | 204 NO CONTENT -| POST | /{function} | JSON object or text | The result of applying the named function | 200 OK -| PUT | /{function} | JSON object or text | The result of applying the named function | 200 OK -| GET | /{function}/{item} | - | Convert the item into an object and return the result of applying the function | 200 OK - -|=== - -As the table above shows the behavior of the endpoint depends on the method and also the type of incoming request data. When the incoming data is single valued, and the target function is declared as obviously single valued (i.e. not returning a collection or `Flux`), then the response will also contain a single value. -For multi-valued responses the client can ask for a server-sent event stream by sending `Accept: text/event-stream`. - -Functions and consumers that are declared with input and output in `Message` will see the request headers as _message headers_, and the output _message headers_ will be converted to HTTP headers. -The _payload_ of the Message will be a `body` or empty string if there is no `body` or it is null. - -When POSTing text the response format might be different with Spring Boot 2.0 and older versions, depending on the content negotiation (provide content type and accept headers for the best results). - -See <> to see the details and example on how to test such application. - -[[http-request-parameters]] -==== HTTP Request Parameters -As you have noticed from the previous table, you can pass an argument to a function as path variable (i.e., `/{function}/{item}`). -For example, `http://localhost:8080/uppercase/foo` will result in calling `uppercase` function with its input parameter being `foo`. - -While this is the recommended approach and the one that fits most use cases cases, there are times when you have to deal with HTTP request parameters (e.g., `http://localhost:8080/uppercase/foo?name=Bill`) -The framework will treat HTTP request parameters similar to the HTTP headers by storing them in the `Message` headers under the header key `http_request_param` -with its value being a `Map` of request parameters, so in order to access them your function input signature should accept `Message` type (e.g., `Function, String>`). For convenience we provide `HeaderUtils.HTTP_REQUEST_PARAM` constant. - -[[function-mapping-rules]] -=== Function Mapping rules - -If there is only a single function (consumer etc.) in the catalog, the name in the path is optional. -In other words, providing you only have `uppercase` function in catalog -`curl -H "Content-Type: text/plain" localhost:8080/uppercase -d hello` and `curl -H "Content-Type: text/plain" localhost:8080/ -d hello` calls are identical. - -Composite functions can be addressed using pipes or commas to separate function names (pipes are legal in URL paths, but a bit awkward to type on the command line). -For example, `curl -H "Content-Type: text/plain" localhost:8080/uppercase,reverse -d hello`. - -For cases where there is more than a single function in catalog, each function will be exported and mapped with function name being -part of the path (e.g., `localhost:8080/uppercase`). -In this scenario you can still map specific function or function composition to the root path by providing -`spring.cloud.function.definition` property - -For example, ----- ---spring.cloud.function.definition=foo|bar ----- - -The above property will compose 'foo' and 'bar' function and map the composed function to the "/" path. - -The same property will also work for cases where function can not be resolved via URL. For example, your URL may be `localhost:8080/uppercase`, but there is no `uppercase` function. -However there are function `foo` and `bar`. So, in this case `localhost:8080/uppercase` will resolve to `foo|bar`. -This could be useful especially for cases when URL is used to communicate certain information since there will be Message header called `uri` with the value -of the actual URL, giving user ability to use it for evaluation and computation. - -[[function-filtering-rules]] -=== Function Filtering rules - -In situations where there are more than one function in catalog there may be a need to only export certain functions or function compositions. In that case you can use -the same `spring.cloud.function.definition` property listing functions you intend to export delimited by `;`. -Note that in this case nothing will be mapped to the root path and functions that are not listed (including compositions) are not going to be exported - -For example, - ----- ---spring.cloud.function.definition=foo;bar ----- - -This will only export function `foo` and function `bar` regardless how many functions are available in catalog (e.g., `localhost:8080/foo`). - ----- ---spring.cloud.function.definition=foo|bar;baz ----- - -This will only export function composition `foo|bar` and function `baz` regardless how many functions are available in catalog (e.g., `localhost:8080/foo,bar`). - -[[crud-rest-with-spring-cloud-function]] -=== CRUD REST with Spring Cloud Function - -By now it should be clear that functions are exported as REST endpoints and can be invoked using various HTTP methods. In other words a single -function could be triggered via GET, POST, PUT etc. - -However, it is not always desirable and certainly does not fit the CRUD concept. And while SCF does not support and has no intention of supporting -all the features of Spring web stack, the framework does provide support for CRUD mappings where a single function could be mapped to a particular HTTP method(s). -It is done via spring.cloud.function.http. property. - -For example, - ----- -spring.cloud.function.http.GET=uppercase;reverse;foo|bar -spring.cloud.function.http.POST=reverse -spring.cloud.function.http.DELETE=deleteById ----- - -As you can see, here we’re mapping functions to various HTTP methods using the same rules as `spring.cloud.function.definition` property where “;” allows us to define several functions and “|” signifies function composition. - -[[standalone-streaming-applications]] -== Standalone Streaming Applications - -To send or receive messages from a broker (such as RabbitMQ or Kafka) you can leverage `spring-cloud-stream` project and it's integration with Spring Cloud Function. -Please refer to https://cloud.spring.io/spring-cloud-static/spring-cloud-stream/current/reference/html/spring-cloud-stream.html#spring_cloud_function[Spring Cloud Function] section of the https://spring.io/projects/spring-cloud-stream[Spring Cloud Stream] reference manual for more details and examples. - -[[deploying-a-packaged-function]] -== Deploying a Packaged Function - -Spring Cloud Function provides a "deployer" library that allows you to launch a jar file (or exploded archive, or set of jar files) with an isolated class loader and expose the functions defined in it. This is quite a powerful tool that would allow you to, for instance, adapt a function to a range of different input-output adapters without changing the target jar file. Serverless platforms often have this kind of feature built in, so you could see it as a building block for a function invoker in such a platform (indeed the https://projectriff.io[Riff] Java function invoker uses this library). - -The standard entry point is to add `spring-cloud-function-deployer` to the classpath, the deployer kicks in and looks for some configuration to tell it where to find the function jar. - -```xml - - org.springframework.cloud - spring-cloud-function-deployer - ${spring.cloud.function.version} - -``` - - -At a minimum the user has to provide a `spring.cloud.function.location` which is a URL or resource location for the archive containing -the functions. It can optionally use a `maven:` prefix to locate the artifact via a dependency lookup (see `FunctionProperties` -for complete details). A Spring Boot application is bootstrapped from the jar file, using the `MANIFEST.MF` to locate a start class, so -that a standard Spring Boot fat jar works well, for example. If the target jar can be launched successfully then the result is a function -registered in the main application's `FunctionCatalog`. The registered function can be applied by code in the main application, even though -it was created in an isolated class loader (by deault). - -Here is the example of deploying a JAR which contains an 'uppercase' function and invoking it . - -```java -@SpringBootApplication -public class DeployFunctionDemo { - - public static void main(String[] args) { - ApplicationContext context = SpringApplication.run(DeployFunctionDemo.class, - "--spring.cloud.function.location=..../target/uppercase-0.0.1-SNAPSHOT.jar", - "--spring.cloud.function.definition=uppercase"); - - FunctionCatalog catalog = context.getBean(FunctionCatalog.class); - Function function = catalog.lookup("uppercase"); - System.out.println(function.apply("hello")); - } -} -``` - -And here is the example using Maven URI (taken from one of the tests in `FunctionDeployerTests`): - -```java -@SpringBootApplication -public class DeployFunctionDemo { - - public static void main(String[] args) { - String[] args = new String[] { - "--spring.cloud.function.location=maven://oz.demo:demo-uppercase:0.0.1-SNAPSHOT", - "--spring.cloud.function.function-class=oz.demo.uppercase.MyFunction" }; - - ApplicationContext context = SpringApplication.run(DeployerApplication.class, args); - FunctionCatalog catalog = context.getBean(FunctionCatalog.class); - Function function = catalog.lookup("myFunction"); - - assertThat(function.apply("bob")).isEqualTo("BOB"); - } -} -``` - -Keep in mind that Maven resource such as local and remote repositories, user, password and more are resolved using default MavenProperties which -effectively use local defaults and will work for majority of cases. However if you need to customize you can simply provide a bean of type -`MavenProperties` where you can set additional properties (see example below). - -```java -@Bean -public MavenProperties mavenProperties() { - MavenProperties properties = new MavenProperties(); - properties.setLocalRepository("target/it/"); - return properties; -} -``` - -[[supported-packaging-scenarios]] -=== Supported Packaging Scenarios - -Currently Spring Cloud Function supports several packaging scenarios to give you the most flexibility when it comes to deploying functions. - -[[simple-jar]] -==== Simple JAR - -This packaging option implies no dependency on anything related to Spring. -For example; Consider that such JAR contains the following class: -```java -package function.example; -. . . -public class UpperCaseFunction implements Function { - @Override - public String apply(String value) { - return value.toUpperCase(); - } -} -``` -All you need to do is specify `location` and `function-class` properties when deploying such package: - -``` ---spring.cloud.function.location=target/it/simplestjar/target/simplestjar-1.0.0.RELEASE.jar ---spring.cloud.function.function-class=function.example.UpperCaseFunction -``` - -It's conceivable in some cases that you might want to package multiple functions together. For such scenarios you can use -`spring.cloud.function.function-class` property to list several classes delimiting them by `;`. - -For example, - -``` ---spring.cloud.function.function-class=function.example.UpperCaseFunction;function.example.ReverseFunction -``` - -Here we are identifying two functions to deploy, which we can now access in function catalog by name (e.g., `catalog.lookup("reverseFunction");`). - - -For more details please reference the complete sample available https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-deployer/src/it/simplestjar[here]. -You can also find a corresponding test in https://github.com/spring-cloud/spring-cloud-function/blob/master/spring-cloud-function-deployer/src/test/java/org/springframework/cloud/function/deployer/FunctionDeployerTests.java#L70[FunctionDeployerTests]. - -*** Component Scanning *** - -Since version 3.1.4 you can simplify your configuration thru component scanning feature described in <>. If you place your functional class in -package named `functions`, you can omit `spring.cloud.function.function-class` property as framework will auto-discover functional classes loading them in function catalog. -Keep in mind the naming convention to follow when doing function lookup. For example function class `functions.UpperCaseFunction` will be available in `FunctionCatalog` -under the name `upperCaseFunction`. - -[[spring-boot-jar]] -==== Spring Boot JAR - -This packaging option implies there is a dependency on Spring Boot and that the JAR was generated as Spring Boot JAR. That said, given that the deployed JAR -runs in the isolated class loader, there will not be any version conflict with the Spring Boot version used by the actual deployer. -For example; Consider that such JAR contains the following class (which could have some additional Spring dependencies providing Spring/Spring Boot is on the classpath): -```java -package function.example; -. . . -public class UpperCaseFunction implements Function { - @Override - public String apply(String value) { - return value.toUpperCase(); - } -} -``` -As before all you need to do is specify `location` and `function-class` properties when deploying such package: - -``` ---spring.cloud.function.location=target/it/simplestjar/target/simplestjar-1.0.0.RELEASE.jar ---spring.cloud.function.function-class=function.example.UpperCaseFunction -``` -For more details please reference the complete sample available https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-deployer/src/it/bootjar[here]. -You can also find a corresponding test in https://github.com/spring-cloud/spring-cloud-function/blob/master/spring-cloud-function-deployer/src/test/java/org/springframework/cloud/function/deployer/FunctionDeployerTests.java#L50[FunctionDeployerTests]. - -[[spring-boot-application]] -==== Spring Boot Application - -This packaging option implies your JAR is complete stand alone Spring Boot application with functions as managed Spring beans. -As before there is an obvious assumption that there is a dependency on Spring Boot and that the JAR was generated as Spring Boot JAR. That said, given that the deployed JAR -runs in the isolated class loader, there will not be any version conflict with the Spring Boot version used by the actual deployer. -For example; Consider that such JAR contains the following class: -```java -package function.example; -. . . -@SpringBootApplication -public class SimpleFunctionAppApplication { - - public static void main(String[] args) { - SpringApplication.run(SimpleFunctionAppApplication.class, args); - } - - @Bean - public Function uppercase() { - return value -> value.toUpperCase(); - } -} -``` -Given that we're effectively dealing with another Spring Application context and that functions are spring managed beans, -in addition to the `location` property we also specify `definition` property instead of `function-class`. - -``` ---spring.cloud.function.location=target/it/bootapp/target/bootapp-1.0.0.RELEASE-exec.jar ---spring.cloud.function.definition=uppercase -``` -For more details please reference the complete sample available https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-deployer/src/it/bootapp[here]. -You can also find a corresponding test in https://github.com/spring-cloud/spring-cloud-function/blob/master/spring-cloud-function-deployer/src/test/java/org/springframework/cloud/function/deployer/FunctionDeployerTests.java#L164[FunctionDeployerTests]. - -NOTE: This particular deployment option may or may not have Spring Cloud Function on it's classpath. From the deployer perspective this doesn't matter. - -[[functional-bean-definitions]] -== Functional Bean Definitions - -include::functional.adoc[leveloffset=+1] - -[[serverless-platform-adapters]] -== Serverless Platform Adapters - -As well as being able to run as a standalone process, a Spring Cloud -Function application can be adapted to run one of the existing -serverless platforms. In the project there are adapters for -https://github.com/spring-cloud/spring-cloud-function/tree/{branch}/spring-cloud-function-adapters/spring-cloud-function-adapter-aws[AWS -Lambda], and https://github.com/spring-cloud/spring-cloud-function/tree/{branch}/spring-cloud-function-adapters/spring-cloud-function-adapter-azure[Azure]. The https://github.com/fnproject/fn[Oracle Fn platform] has its own Spring Cloud Function adapter. And https://projectriff.io[Riff] supports Java functions and its -https://github.com/projectriff/java-function-invoker[Java Function Invoker] acts natively is an adapter for Spring Cloud Function jars. - -include::adapters/aws-intro.adoc[] -include::adapters/azure-intro.adoc[leveloffset=+1] -include::adapters/gcp-intro.adoc[] - diff --git a/docs/modules/ROOT/pages/spring-cloud-function/deploying-a-packaged.adoc b/docs/modules/ROOT/pages/spring-cloud-function/deploying-a-packaged.adoc new file mode 100644 index 000000000..a3b6ce4a1 --- /dev/null +++ b/docs/modules/ROOT/pages/spring-cloud-function/deploying-a-packaged.adoc @@ -0,0 +1,183 @@ +[[deploying-a-packaged-function]] += Deploying a Packaged Function + +Spring Cloud Function provides a "deployer" library that allows you to launch a jar file (or exploded archive, or set of jar files) with an isolated class loader and expose the functions defined in it. This is quite a powerful tool that would allow you to, for instance, adapt a function to a range of different input-output adapters without changing the target jar file. Serverless platforms often have this kind of feature built in, so you could see it as a building block for a function invoker in such a platform (indeed the https://projectriff.io[Riff] Java function invoker uses this library). + +The standard entry point is to add `spring-cloud-function-deployer` to the classpath, the deployer kicks in and looks for some configuration to tell it where to find the function jar. + +```xml + + org.springframework.cloud + spring-cloud-function-deployer + ${spring.cloud.function.version} + +``` + + +At a minimum the user has to provide a `spring.cloud.function.location` which is a URL or resource location for the archive containing +the functions. It can optionally use a `maven:` prefix to locate the artifact via a dependency lookup (see `FunctionProperties` +for complete details). A Spring Boot application is bootstrapped from the jar file, using the `MANIFEST.MF` to locate a start class, so +that a standard Spring Boot fat jar works well, for example. If the target jar can be launched successfully then the result is a function +registered in the main application's `FunctionCatalog`. The registered function can be applied by code in the main application, even though +it was created in an isolated class loader (by deault). + +Here is the example of deploying a JAR which contains an 'uppercase' function and invoking it . + +```java +@SpringBootApplication +public class DeployFunctionDemo { + + public static void main(String[] args) { + ApplicationContext context = SpringApplication.run(DeployFunctionDemo.class, + "--spring.cloud.function.location=..../target/uppercase-0.0.1-SNAPSHOT.jar", + "--spring.cloud.function.definition=uppercase"); + + FunctionCatalog catalog = context.getBean(FunctionCatalog.class); + Function function = catalog.lookup("uppercase"); + System.out.println(function.apply("hello")); + } +} +``` + +And here is the example using Maven URI (taken from one of the tests in `FunctionDeployerTests`): + +```java +@SpringBootApplication +public class DeployFunctionDemo { + + public static void main(String[] args) { + String[] args = new String[] { + "--spring.cloud.function.location=maven://oz.demo:demo-uppercase:0.0.1-SNAPSHOT", + "--spring.cloud.function.function-class=oz.demo.uppercase.MyFunction" }; + + ApplicationContext context = SpringApplication.run(DeployerApplication.class, args); + FunctionCatalog catalog = context.getBean(FunctionCatalog.class); + Function function = catalog.lookup("myFunction"); + + assertThat(function.apply("bob")).isEqualTo("BOB"); + } +} +``` + +Keep in mind that Maven resource such as local and remote repositories, user, password and more are resolved using default MavenProperties which +effectively use local defaults and will work for majority of cases. However if you need to customize you can simply provide a bean of type +`MavenProperties` where you can set additional properties (see example below). + +```java +@Bean +public MavenProperties mavenProperties() { + MavenProperties properties = new MavenProperties(); + properties.setLocalRepository("target/it/"); + return properties; +} +``` + +[[supported-packaging-scenarios]] +== Supported Packaging Scenarios + +Currently Spring Cloud Function supports several packaging scenarios to give you the most flexibility when it comes to deploying functions. + +[[simple-jar]] +=== Simple JAR + +This packaging option implies no dependency on anything related to Spring. +For example; Consider that such JAR contains the following class: +```java +package function.example; +. . . +public class UpperCaseFunction implements Function { + @Override + public String apply(String value) { + return value.toUpperCase(); + } +} +``` +All you need to do is specify `location` and `function-class` properties when deploying such package: + +``` +--spring.cloud.function.location=target/it/simplestjar/target/simplestjar-1.0.0.RELEASE.jar +--spring.cloud.function.function-class=function.example.UpperCaseFunction +``` + +It's conceivable in some cases that you might want to package multiple functions together. For such scenarios you can use +`spring.cloud.function.function-class` property to list several classes delimiting them by `;`. + +For example, + +``` +--spring.cloud.function.function-class=function.example.UpperCaseFunction;function.example.ReverseFunction +``` + +Here we are identifying two functions to deploy, which we can now access in function catalog by name (e.g., `catalog.lookup("reverseFunction");`). + + +For more details please reference the complete sample available https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-deployer/src/it/simplestjar[here]. +You can also find a corresponding test in https://github.com/spring-cloud/spring-cloud-function/blob/master/spring-cloud-function-deployer/src/test/java/org/springframework/cloud/function/deployer/FunctionDeployerTests.java#L70[FunctionDeployerTests]. + +*** Component Scanning *** + +Since version 3.1.4 you can simplify your configuration thru component scanning feature described in <>. If you place your functional class in +package named `functions`, you can omit `spring.cloud.function.function-class` property as framework will auto-discover functional classes loading them in function catalog. +Keep in mind the naming convention to follow when doing function lookup. For example function class `functions.UpperCaseFunction` will be available in `FunctionCatalog` +under the name `upperCaseFunction`. + +[[spring-boot-jar]] +=== Spring Boot JAR + +This packaging option implies there is a dependency on Spring Boot and that the JAR was generated as Spring Boot JAR. That said, given that the deployed JAR +runs in the isolated class loader, there will not be any version conflict with the Spring Boot version used by the actual deployer. +For example; Consider that such JAR contains the following class (which could have some additional Spring dependencies providing Spring/Spring Boot is on the classpath): +```java +package function.example; +. . . +public class UpperCaseFunction implements Function { + @Override + public String apply(String value) { + return value.toUpperCase(); + } +} +``` +As before all you need to do is specify `location` and `function-class` properties when deploying such package: + +``` +--spring.cloud.function.location=target/it/simplestjar/target/simplestjar-1.0.0.RELEASE.jar +--spring.cloud.function.function-class=function.example.UpperCaseFunction +``` +For more details please reference the complete sample available https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-deployer/src/it/bootjar[here]. +You can also find a corresponding test in https://github.com/spring-cloud/spring-cloud-function/blob/master/spring-cloud-function-deployer/src/test/java/org/springframework/cloud/function/deployer/FunctionDeployerTests.java#L50[FunctionDeployerTests]. + +[[spring-boot-application]] +=== Spring Boot Application + +This packaging option implies your JAR is complete stand alone Spring Boot application with functions as managed Spring beans. +As before there is an obvious assumption that there is a dependency on Spring Boot and that the JAR was generated as Spring Boot JAR. That said, given that the deployed JAR +runs in the isolated class loader, there will not be any version conflict with the Spring Boot version used by the actual deployer. +For example; Consider that such JAR contains the following class: +```java +package function.example; +. . . +@SpringBootApplication +public class SimpleFunctionAppApplication { + + public static void main(String[] args) { + SpringApplication.run(SimpleFunctionAppApplication.class, args); + } + + @Bean + public Function uppercase() { + return value -> value.toUpperCase(); + } +} +``` +Given that we're effectively dealing with another Spring Application context and that functions are spring managed beans, +in addition to the `location` property we also specify `definition` property instead of `function-class`. + +``` +--spring.cloud.function.location=target/it/bootapp/target/bootapp-1.0.0.RELEASE-exec.jar +--spring.cloud.function.definition=uppercase +``` +For more details please reference the complete sample available https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-deployer/src/it/bootapp[here]. +You can also find a corresponding test in https://github.com/spring-cloud/spring-cloud-function/blob/master/spring-cloud-function-deployer/src/test/java/org/springframework/cloud/function/deployer/FunctionDeployerTests.java#L164[FunctionDeployerTests]. + +NOTE: This particular deployment option may or may not have Spring Cloud Function on it's classpath. From the deployer perspective this doesn't matter. + diff --git a/docs/modules/ROOT/pages/spring-cloud-function/functional-bean-definitions.adoc b/docs/modules/ROOT/pages/spring-cloud-function/functional-bean-definitions.adoc new file mode 100644 index 000000000..69116d065 --- /dev/null +++ b/docs/modules/ROOT/pages/spring-cloud-function/functional-bean-definitions.adoc @@ -0,0 +1,5 @@ +[[functional-bean-definitions]] += Functional Bean Definitions + +include:../:functional.adoc[leveloffset=+1] + diff --git a/docs/modules/ROOT/pages/spring-cloud-function/getting-started.adoc b/docs/modules/ROOT/pages/spring-cloud-function/getting-started.adoc new file mode 100644 index 000000000..4fd7fa71e --- /dev/null +++ b/docs/modules/ROOT/pages/spring-cloud-function/getting-started.adoc @@ -0,0 +1,5 @@ +[[getting-started]] += Getting Started + +include:../:getting-started.adoc[] + diff --git a/docs/modules/ROOT/pages/spring-cloud-function/introduction.adoc b/docs/modules/ROOT/pages/spring-cloud-function/introduction.adoc new file mode 100644 index 000000000..9ae7af68c --- /dev/null +++ b/docs/modules/ROOT/pages/spring-cloud-function/introduction.adoc @@ -0,0 +1,7 @@ +[[introduction]] += Introduction + +include:../:_intro.adoc[] + +include:../:https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/docs/src/main/asciidoc/contributing-docs.adoc[] + diff --git a/docs/modules/ROOT/pages/spring-cloud-function/programming-model.adoc b/docs/modules/ROOT/pages/spring-cloud-function/programming-model.adoc new file mode 100644 index 000000000..e602901aa --- /dev/null +++ b/docs/modules/ROOT/pages/spring-cloud-function/programming-model.adoc @@ -0,0 +1,717 @@ +[[programming-model]] += Programming model + +[[function.catalog]] + +[[function-catalog-and-flexible-function-signatures]] +== Function Catalog and Flexible Function Signatures + +One of the main features of Spring Cloud Function is to adapt and support a range of type signatures for user-defined functions, +while providing a consistent execution model. +That's why all user defined functions are transformed into a canonical representation by `FunctionCatalog`. + +While users don't normally have to care about the `FunctionCatalog` at all, it is useful to know what +kind of functions are supported in user code. + +It is also important to understand that Spring Cloud Function provides first class support for reactive API +provided by https://projectreactor.io/[Project Reactor] allowing reactive primitives such as `Mono` and `Flux` +to be used as types in user defined functions providing greater flexibility when choosing programming model for +your function implementation. +Reactive programming model also enables functional support for features that would be otherwise difficult to impossible to implement +using imperative programming style. For more on this please read <> section. + +[[java-8-function-support]] +== Java 8 function support + +Spring Cloud Function embraces and builds on top of the 3 core functional interfaces defined by Java +and available to us since Java 8. + +- Supplier +- Function +- Consumer + +To avoid constantly mentioning `Supplier`, `Function` and `Consumer` we’ll refer to them a Functional beans for the rest of this manual where appropriate. + +In a nutshell, any bean in your Application Context that is Functional bean will lazily be registered with `FunctionCatalog`. +This means that it could benefit from all of the additional features described in this reference manual. + +In a simplest of application all you need to do is to declare `@Bean` of type `Supplier`, `Function` or `Consumer` in your application configuration. +Then you can access `FunctionCatalog` and lookup a particular function based on its name. + +For example: + + +[source, test] +---- +@Bean +public Function uppercase() { + return value -> value.toUpperCase(); +} + +. . . + +FunctionCatalog catalog = applicationContext.getBean(FunctionCatalog.class); +Function uppercase = catalog.lookup(“uppercase”); +---- + +Important to understand that given that `uppercase` is a bean, you can certainly get it form the `ApplicationContext` directly, but all you will get is just your bean as you declared it without any extra features provided by SCF. When you do lookup of a function via `FunctionCatalog`, the instance you will receive is wrapped (instrumented) with additional features (i.e., type conversion, composition etc.) described in this manual. Also, it is important to understand that a typical user does not use Spring Cloud Function directly. Instead a typical user implements Java `Function/Supplier/Consumer` with the idea of using it in different execution contexts without additional work. For example the same java function could be represented as _REST endpoint_ or _Streaming message handler_ or _AWS Lambda_ and more via Spring Cloud Function provided +adapters as well as other frameworks using Spring Cloud Function as the core programming model (e.g., https://spring.io/projects/spring-cloud-stream[Spring Cloud Stream]) +So in summary Spring Cloud Function instruments java functions with additional features to be utilised in variety of execution contexts. + + +[[function-definition]] +=== Function definition +While the previous example shows you how to lookup function in FunctionCatalog programmatically, in a typical integration case where Spring Cloud Function used as programming model by another framework (e.fg. Spring Cloud Stream), you declare which functions to use via `spring.cloud.function.definition` property. Knowing that it is important to understand some default behaviour when it comes to discovering functions in `FunctionCatalog`. For example, if you only have one Functional bean in your `ApplicationContext`, the `spring.cloud.function.definition` property typically will not be required, since a single function in `FunctionCatalog` can be looked up by an empty name or any name. For example, assuming that `uppercase` is the only function in your catalog, it can be looked up as `catalog.lookup(null)`, `catalog.lookup(“”)`, `catalog.lookup(“foo”)` +That said, for cases where you are using framework such as Spring Cloud Stream which uses `spring.cloud.function.definition` it is best practice and recommended to always use `spring.cloud.function.definition` property. + +For example, + +[source, test] +---- +spring.cloud.function.definition=uppercase +---- + +[[filtering-ineligible-functions]] +=== Filtering ineligible functions +A typical Application Context may include beans that are valid java functions, but not intended to be candidates to be registered with `FunctionCatalog`. +Such beans could be auto-configurations from other projects or any other beans that qualify to be Java functions. +The framework provides default filtering of known beans that should not be candidates for registration with function catalog. +You can also add to this list additional beans by providing coma delimited list of bean definition names using +`spring.cloud.function.ineligible-definitions` property. + +For example, + +[source, test] +---- +spring.cloud.function.ineligible-definitions=foo,bar +---- + +[[supplier]] +=== Supplier +Supplier can be _reactive_ - `Supplier>` +or _imperative_ - `Supplier`. From the invocation standpoint this should make no difference +to the implementor of such Supplier. However, when used within frameworks +(e.g., https://spring.io/projects/spring-cloud-stream[Spring Cloud Stream]), Suppliers, especially reactive, +often used to represent the source of the stream, therefore they are invoked once to get the stream (e.g., Flux) +to which consumers can subscribe to. In other words such suppliers represent an equivalent of an _infinite stream_. +However, the same reactive suppliers can also represent _finite_ stream(s) (e.g., result set on the polled JDBC data). +In those cases such reactive suppliers must be hooked up to some polling mechanism of the underlying framework. + +To assist with that Spring Cloud Function provides a marker annotation +`org.springframework.cloud.function.context.PollableBean` to signal that such supplier produces a +finite stream and may need to be polled again. That said, it is important to understand that Spring Cloud Function itself +provides no behavior for this annotation. + +In addition `PollableBean` annotation exposes a _splittable_ attribute to signal that produced stream +needs to be split (see https://www.enterpriseintegrationpatterns.com/patterns/messaging/Sequencer.html[Splitter EIP]) + +Here is the example: + +[source, java] +---- +@PollableBean(splittable = true) +public Supplier> someSupplier() { + return () -> { + String v1 = String.valueOf(System.nanoTime()); + String v2 = String.valueOf(System.nanoTime()); + String v3 = String.valueOf(System.nanoTime()); + return Flux.just(v1, v2, v3); + }; +} +---- + +[[function]] +=== Function +Function can also be written in imperative or reactive way, yet unlike Supplier and Consumer there are +no special considerations for the implementor other then understanding that when used within frameworks +such as https://spring.io/projects/spring-cloud-stream[Spring Cloud Stream] and others, reactive function is +invoked only once to pass a reference to the stream (Flux or Mono) and imperative is invoked once per event. + +[[consumer]] +=== Consumer +Consumer is a little bit special because it has a `void` return type, +which implies blocking, at least potentially. Most likely you will not +need to write `Consumer>`, but if you do need to do that, +remember to subscribe to the input flux. + +[[function-composition]] +== Function Composition +Function Composition is a feature that allows one to compose several functions into one. +The core support is based on function composition feature available with https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html#andThen-java.util.function.Function-[Function.andThen(..)] +support available since Java 8. However on top of it, we provide few additional features. + +[[declarative-function-composition]] +=== Declarative Function Composition + +This feature allows you to provide composition instruction in a declarative way using `|` (pipe) or `,` (comma) delimiter +when providing `spring.cloud.function.definition` property. + +For example +---- +--spring.cloud.function.definition=uppercase|reverse +---- +Here we effectively provided a definition of a single function which itself is a composition of +function `uppercase` and function `reverse`. In fact that is one of the reasons why the property name is _definition_ and not _name_, +since the definition of a function can be a composition of several named functions. +And as mentioned you can use `,` instead of pipe (such as `...definition=uppercase,reverse`). + +[[composing-non-functions]] +=== Composing non-Functions +Spring Cloud Function also supports composing Supplier with `Consumer` or `Function` as well as `Function` with `Consumer`. +What's important here is to understand the end product of such definitions. +Composing Supplier with Function still results in Supplier while composing Supplier with Consumer will effectively render Runnable. +Following the same logic composing Function with Consumer will result in Consumer. + +And of course you can't compose uncomposable such as Consumer and Function, Consumer and Supplier etc. + + +[[function-routing-and-filtering]] +== Function Routing and Filtering + +Since version 2.2 Spring Cloud Function provides routing feature allowing +you to invoke a single function which acts as a router to an actual function you wish to invoke +This feature is very useful in certain FAAS environments where maintaining configurations +for several functions could be cumbersome or exposing more than one function is not possible. + +The `RoutingFunction` is registered in _FunctionCatalog_ under the name `functionRouter`. For simplicity +and consistency you can also refer to `RoutingFunction.FUNCTION_NAME` constant. + +This function has the following signature: + +[source, java] +---- +public class RoutingFunction implements Function { +. . . +} +---- +The routing instructions could be communicated in several ways. We support providing instructions via Message headers, System +properties as well as pluggable strategy. So let's look at some of the details + +[[messageroutingcallback]] +=== MessageRoutingCallback + +The `MessageRoutingCallback` is a strategy to assist with determining the name of the route-to function definition. + +[source, java] +---- +public interface MessageRoutingCallback { + FunctionRoutingResult routingResult(Message message); + . . . +} +---- + +All you need to do is implement and register it as a bean to be picked up by the `RoutingFunction`. +For example: + +[source, java] +---- +@Bean +public MessageRoutingCallback customRouter() { + return new MessageRoutingCallback() { + @Override + public FunctionRoutingResult routingResult(Message message) { + return new FunctionRoutingResult((String) message.getHeaders().get("func_name")); + } + }; +} +---- + +In the preceding example you can see a very simple implementation of `MessageRoutingCallback` which determines the function definition from +`func_name` Message header of the incoming Message and returns the instance of `FunctionRoutingResult` containing the definition of function to invoke. + +*Message Headers* + +If the input argument is of type `Message`, you can communicate routing instruction by setting one of +`spring.cloud.function.definition` or `spring.cloud.function.routing-expression` Message headers. +As the name of the property suggests `spring.cloud.function.routing-expression` relies on Spring Expression Language (SpEL). +For more static cases you can use `spring.cloud.function.definition` header which allows you to provide +the name of a single function (e.g., `...definition=foo`) or a composition instruction (e.g., `...definition=foo|bar|baz`). +For more dynamic cases you can use `spring.cloud.function.routing-expression` header and provide SpEL expression that should resolve +into definition of a function (as described above). + +NOTE: SpEL evaluation context's root object is the +actual input argument, so in the case of `Message` you can construct expression that has access +to both `payload` and `headers` (e.g., `spring.cloud.function.routing-expression=headers.function_name`). + +IMPORTANT: SpEL allows user to provide string representation of Java code to be executed. Given that the `spring.cloud.function.routing-expression` could be provided via Message headers means that ability to set such expression could be exposed to the end user (i.e., HTTP Headers when using web module) which could result in some problems (e.g., malicious code). To manage that, all expressions coming via Message headers will only be evaluated against `SimpleEvaluationContext` which has limited functionality and designed to only evaluate the context object (Message in our case). On the other hand, all expressions that are set via property or system variable are evaluated against `StandardEvaluationContext`, which allows for full flexibility of Java language. +While setting expression via system/application property or environment variable is generally considered to be secure as it is not exposed to the end user in normal cases, there are cases where visibility as well as capability to update system, application and environment variables are indeed exposed to the end user via Spring Boot Actuator endpoints provided either by some of the Spring projects or third parties or custom implementation by the end user. Such endpoints must be secured using industry standard web security practices. +Spring Cloud Function does not expose any of such endpoints. + +In specific execution environments/models the adapters are responsible to translate and communicate +`spring.cloud.function.definition` and/or `spring.cloud.function.routing-expression` via Message header. +For example, when using _spring-cloud-function-web_ you can provide `spring.cloud.function.definition` as an HTTP +header and the framework will propagate it as well as other HTTP headers as Message headers. + +*Application Properties* + +Routing instruction can also be communicated via `spring.cloud.function.definition` +or `spring.cloud.function.routing-expression` as application properties. The rules described in the +previous section apply here as well. The only difference is you provide these instructions as +application properties (e.g., `--spring.cloud.function.definition=foo`). + +NOTE: It is important to understand that providing `spring.cloud.function.definition` +or `spring.cloud.function.routing-expression` as Message headers will only work for imperative functions (e.g., `Function`). +That is to say that we can _only_ route ***per-message*** with imperative functions. With reactive functions we can not route +***per-message***. Therefore you can only provide your routing instructions as Application Properties. +It's all about unit-of-work. In imperative function unit of work is Message so we can route based on such unit-of-work. +With reactive function unit-of-work is the entire stream, so we'll act only on the instruction provided via application +properties and route the entire stream. + +*Order of priority for routing instructions* + +Given that we have several mechanisms of providing routing instructions it is important to understand the priorities for +conflict resolutions in the event multiple mechanisms are used at the same time, so here is the order: + +1. `MessageRoutingCallback` (If function is imperative will take over regardless if anything else is defined) +2. Message Headers (If function is imperative and no `MessageRoutingCallback` provided) +3. Application Properties (Any function) + +*Unroutable Messages* + +In the event route-to function is not available in catalog you will get an exception stating that. + +There are cases when such behavior is not desired and you may want to have some "catch-all" type function which can handle such messages. +To accomplish that, framework provides `org.springframework.cloud.function.context.DefaultMessageRoutingHandler` strategy. All you need to do is register it as a bean. +Its default implementation will simply log the fact that the message is un-routable, but will allow message flow to proceed without the exception, effectively dropping the un-routable message. +If you want something more sophisticated all you need to do is provide your own implementation of this strategy and register it as a bean. + +[source, java] +---- +@Bean +public DefaultMessageRoutingHandler defaultRoutingHandler() { + return new DefaultMessageRoutingHandler() { + @Override + public void accept(Message message) { + // do something really cool + } + }; +} +---- + +[[function-filtering]] +=== Function Filtering +Filtering is the type of routing where there are only two paths - 'go' or 'discard'. In terms of functions it mean +you only want to invoke a certain function if some condition returns 'true', otherwise you want to discard input. +However, when it comes to discarding input there are many interpretation of what it could mean in the context of your application. +For example, you may want to log it, or you may want to maintain the counter of discarded messages. you may also want to do nothing at all. +Because of these different paths, we do not provide a general configuration option for how to deal with discarded messages. +Instead we simply recommend to define a simple Consumer which would signify the 'discard' path: + +[source, java] +---- +@Bean +public Consumer devNull() { + // log, count or whatever +} +---- +Now you can have routing expression that really only has two paths effectively becoming a filter. For example: + +[source, text] +---- +--spring.cloud.function.routing-expression=headers.contentType.toString().equals('text/plain') ? 'echo' : 'devNull' +---- +Every message that does not fit criteria to go to 'echo' function will go to 'devNull' where you can simply do nothing with it. +The signature `Consumer` will also ensure that no type conversion will be attempted resulting in almost no execution overhead. + + +IMPORTANT: When dealing with reactive inputs (e.g., Publisher), routing instructions must only be provided via Function properties. This is +due to the nature of the reactive functions which are invoked only once to pass a Publisher and the rest +is handled by the reactor, hence we can not access and/or rely on the routing instructions communicated via individual +values (e.g., Message). + +[[multiple-routers]] +=== Multiple Routers + +By default the framework will always have a single routing function configured as described in previous sections. However, there are times when you may need more than one routing function. +In that case you can create your own instance of the `RoutingFunction` bean in addition to the existing one as long as you give it a name other than `functionRouter`. + +You can pass `spring.cloud.function.routing-expression` or `spring.cloud.function.definition` to RoutinFunction as key/value pairs in the map. + +Here is a simple example + +---- +@Configuration +protected static class MultipleRouterConfiguration { + + @Bean + RoutingFunction mySpecialRouter(FunctionCatalog functionCatalog, BeanFactory beanFactory, @Nullable MessageRoutingCallback routingCallback) { + Map propertiesMap = new HashMap<>(); + propertiesMap.put(FunctionProperties.PREFIX + ".routing-expression", "'reverse'"); + return new RoutingFunction(functionCatalog, propertiesMap, new BeanFactoryResolver(beanFactory), routingCallback); + } + + @Bean + public Function reverse() { + return v -> new StringBuilder(v).reverse().toString(); + } + + @Bean + public Function uppercase() { + return String::toUpperCase; + } +} +---- + +and a test that demonstrates how it works + +` +---- +@Test +public void testMultipleRouters() { + System.setProperty(FunctionProperties.PREFIX + ".routing-expression", "'uppercase'"); + FunctionCatalog functionCatalog = this.configureCatalog(MultipleRouterConfiguration.class); + Function function = functionCatalog.lookup(RoutingFunction.FUNCTION_NAME); + assertThat(function).isNotNull(); + Message message = MessageBuilder.withPayload("hello").build(); + assertThat(function.apply(message)).isEqualTo("HELLO"); + + function = functionCatalog.lookup("mySpecialRouter"); + assertThat(function).isNotNull(); + message = MessageBuilder.withPayload("hello").build(); + assertThat(function.apply(message)).isEqualTo("olleh"); +} +---- + +[[input/output-enrichment]] +== Input/Output Enrichment + +There are often times when you need to modify or refine an incoming or outgoing Message and to keep your code clean of non-functional concerns. You don’t want to do it inside of your business logic. + +You can always accomplish it via <>. Such approach provides several benefits: + +- It allows you to isolate this non-functional concern into a separate function which you can compose with the business function as function definition. +- It provides you with complete freedom (and danger) as to what you can modify before incoming message reaches the actual business function. + +[source, java] +---- +@Bean +public Function, Message> enrich() { + return message -> MessageBuilder.fromMessage(message).setHeader("foo", "bar").build(); +} + +@Bean +public Function, Message> myBusinessFunction() { + // do whatever +} +---- + +And then compose your function by providing the following function definition `enrich|myBusinessFunction`. + +While the described approach is the most flexible, it is also the most involved as it requires you to write some code, make it a bean or +manually register it as a function before you can compose it with the business function as you can see from the preceding example. + +But what if modifications (enrichments) you are trying to make are trivial as they are in the preceding example? Is there a simpler and more dynamic and configurable + mechanism to accomplish the same? + +Since version 3.1.3, the framework allows you to provide SpEL expression to enrich individual message headers for both input going into function and +and output coming out of it. Let’s look at one of the tests as the example. + +[source, java] +---- +@Test +public void testMixedInputOutputHeaderMapping() throws Exception { + try (ConfigurableApplicationContext context = new SpringApplicationBuilder( + SampleFunctionConfiguration.class).web(WebApplicationType.NONE).run( + "--logging.level.org.springframework.cloud.function=DEBUG", + "--spring.main.lazy-initialization=true", + "--spring.cloud.function.configuration.split.output-header-mapping-expression.keyOut1='hello1'", + "--spring.cloud.function.configuration.split.output-header-mapping-expression.keyOut2=headers.contentType", + "--spring.cloud.function.configuration.split.input-header-mapping-expression.key1=headers.path.split('/')[0]", + "--spring.cloud.function.configuration.split.input-header-mapping-expression.key2=headers.path.split('/')[1]", + "--spring.cloud.function.configuration.split.input-header-mapping-expression.key3=headers.path")) { + + FunctionCatalog functionCatalog = context.getBean(FunctionCatalog.class); + FunctionInvocationWrapper function = functionCatalog.lookup("split"); + Message result = (Message) function.apply(MessageBuilder.withPayload("helo") + .setHeader(MessageHeaders.CONTENT_TYPE, "application/json") + .setHeader("path", "foo/bar/baz").build()); + assertThat(result.getHeaders().containsKey("keyOut1")).isTrue(); + assertThat(result.getHeaders().get("keyOut1")).isEqualTo("hello1"); + assertThat(result.getHeaders().containsKey("keyOut2")).isTrue(); + assertThat(result.getHeaders().get("keyOut2")).isEqualTo("application/json"); + } +} +---- + +Here you see a properties called `input-header-mapping-expression` and `output-header-mapping-expression` preceded by the name of the function (i.e., `split`) and followed by the name of the message header key you want to set and the value as SpEL expression. The first expression (for 'keyOut1') is literal SpEL expressions enclosed in single quotes, effectively setting 'keyOut1' to value `hello1`. The `keyOut2` is set to the value of existing 'contentType' header. + +You can also observe some interesting features in the input header mapping where we actually splitting a value of the existing header 'path', setting individual values of key1 and key2 to the values of split elements based on the index. + +NOTE: if for whatever reason the provided expression evaluation fails, the execution of the function will proceed as if nothing ever happen. +However you will see the WARN message in your logs informing you about it + +[source, text] +---- +o.s.c.f.context.catalog.InputEnricher : Failed while evaluating expression "hello1" on incoming message. . . +---- + +In the event you are dealing with functions that have multiple inputs (next section), you can use index immediately after `input-header-mapping-expression` + +[source, text] +---- +--spring.cloud.function.configuration.echo.input-header-mapping-expression[0].key1=‘hello1' +--spring.cloud.function.configuration.echo.input-header-mapping-expression[1].key2='hello2' +---- + +[[function-arity]] +== Function Arity + +There are times when a stream of data needs to be categorized and organized. For example, +consider a classic big-data use case of dealing with unorganized data containing, let’s say, +‘orders’ and ‘invoices’, and you want each to go into a separate data store. +This is where function arity (functions with multiple inputs and outputs) support +comes to play. + +Let’s look at an example of such a function (full implementation details are available +https://github.com/spring-cloud/spring-cloud-stream/blob/master/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/function/MultipleInputOutputFunctionTests.java#L342[here]), + +[source, java] +---- +@Bean +public Function, Tuple2, Flux>> organise() { + return flux -> ...; +} +---- + +Given that Project Reactor is a core dependency of SCF, we are using its Tuple library. +Tuples give us a unique advantage by communicating to us both _cardinality_ and _type_ information. +Both are extremely important in the context of SCSt. Cardinality lets us know +how many input and output bindings need to be created and bound to the corresponding +inputs and outputs of a function. Awareness of the type information ensures proper type +conversion. + +Also, this is where the ‘index’ part of the naming convention for binding +names comes into play, since, in this function, the two output binding +names are `organise-out-0` and `organise-out-1`. + +IMPORTANT: IMPORTANT: At the moment, function arity is *only* supported for reactive functions +(`Function...>, TupleN...>>`) centered on Complex event processing +where evaluation and computation on confluence of events typically requires view into a +stream of events rather than single event. + +[[input-header-propagation]] +== Input Header propagation + +In a typical scenario input Message headers are not propagated to output and rightfully so, since the output of a function may be an input to something else requiring it's own set of Message headers. +However, there are times when such propagation may be necessary so Spring Cloud Function provides several mechanisms to accomplish this. + +First you can always copy headers manually. For example, if you have a Function with the signature that takes `Message` and returns `Message` (i.e., `Function`), you can simply and selectively copy headers yourselves. Remember, if your function returns Message, the framework will not do anything to it other then properly converting its payload. +However, such approach may prove to be a bit tedious, especially in cases when you simply want to copy all headers. +To assist with cases like this we provide a simple property that would allow you to set a boolean flag on a function where you want input headers to be propagated. +The property is `copy-input-headers`. + +For example, let's assume you have the following configuration: + +[source, java] +---- +@EnableAutoConfiguration +@Configuration +protected static class InputHeaderPropagationConfiguration { + + @Bean + public Function uppercase() { + return x -> x.toUpperCase(); + } +} +---- + +As you know you can still invoke this function by sending a Message to it (framework will take care of type conversion and payload extraction) + +By simply setting `spring.cloud.function.configuration.uppercase.copy-input-headers` to `true`, the following assertion will be true as well + +---- +Function, Message> uppercase = catalog.lookup("uppercase", "application/json"); +Message result = uppercase.apply(MessageBuilder.withPayload("bob").setHeader("foo", "bar").build()); +assertThat(result.getHeaders()).containsKey("foo"); +---- + +[[type-conversion-content-type-negotiation]] +== Type conversion (Content-Type negotiation) + +Content-Type negotiation is one of the core features of Spring Cloud Function as it allows to not only transform the incoming data to the types declared +by the function signature, but to do the same transformation during function composition making otherwise un-composable (by type) functions composable. + +To better understand the mechanics and the necessity behind content-type negotiation, we take a look at a very simple use case by +using the following function as an example: + +[source, java] +---- +@Bean +public Function personFunction {..} +---- + +The function shown in the preceding example expects a `Person` object as an argument and produces a String type as an output. If such function is +invoked with the type `Person`, than all works fine. But typically function plays a role of a handler for the incoming data which most often comes +in the raw format such as `byte[]`, `JSON String` etc. In order for the framework to succeed in passing the incoming data as an argument to +this function, it has to somehow transform the incoming data to a `Person` type. + +Spring Cloud Function relies on two native to Spring mechanisms to accomplish that. + +. _MessageConverter_ - to convert from incoming Message data to a type declared by the function. +. _ConversionService_ - to convert from incoming non-Message data to a type declared by the function. + +This means that depending on the type of the raw data (Message or non-Message) Spring Cloud Function will apply one or the other mechanisms. + +For most cases when dealing with functions that are invoked as part of some other request (e.g., HTTP, Messaging etc) the framework relies on `MessageConverters`, +since such requests already converted to Spring `Message`. In other words, the framework locates and applies the appropriate `MessageConverter`. +To accomplish that, the framework needs some instructions from the user. One of these instructions is already provided by the signature of the function +itself (Person type). Consequently, in theory, that should be (and, in some cases, is) enough. However, for the majority of use cases, in order to +select the appropriate `MessageConverter`, the framework needs an additional piece of information. That missing piece is `contentType` header. + +Such header usually comes as part of the Message where it is injected by the corresponding adapter that created such Message in the first place. +For example, HTTP POST request will have its content-type HTTP header copied to `contentType` header of the Message. + +For cases when such header does not exist framework relies on the default content type as `application/json`. + + +[[content-type-versus-argument-type]] +=== Content Type versus Argument Type + +As mentioned earlier, for the framework to select the appropriate `MessageConverter`, it requires argument type and, optionally, content type information. +The logic for selecting the appropriate `MessageConverter` resides with the argument resolvers which trigger right before the invocation of the user-defined +function (which is when the actual argument type is known to the framework). +If the argument type does not match the type of the current payload, the framework delegates to the stack of the +pre-configured `MessageConverters` to see if any one of them can convert the payload. + +The combination of `contentType` and argument type is the mechanism by which framework determines if message can be converted to a target type by locating +the appropriate `MessageConverter`. +If no appropriate `MessageConverter` is found, an exception is thrown, which you can handle by adding a custom `MessageConverter` +(see `<>`). + +NOTE: Do not expect `Message` to be converted into some other type based only on the `contentType`. +Remember that the `contentType` is complementary to the target type. +It is a hint, which `MessageConverter` may or may not take into consideration. + +[[message-converters]] +=== Message Converters + +`MessageConverters` define two methods: + +[source, java] +---- +Object fromMessage(Message message, Class targetClass); + +Message toMessage(Object payload, @Nullable MessageHeaders headers); +---- + +It is important to understand the contract of these methods and their usage, specifically in the context of Spring Cloud Stream. + +The `fromMessage` method converts an incoming `Message` to an argument type. +The payload of the `Message` could be any type, and it is +up to the actual implementation of the `MessageConverter` to support multiple types. + + +[[provided-messageconverters]] +=== Provided MessageConverters + +As mentioned earlier, the framework already provides a stack of `MessageConverters` to handle most common use cases. +The following list describes the provided `MessageConverters`, in order of precedence (the first `MessageConverter` that works is used): + +. `JsonMessageConverter`: Supports conversion of the payload of the `Message` to/from POJO for cases when `contentType` is `application/json` using Jackson (DEFAULT) or Gson libraries. This message converter also aware of `type` parameter (e.g., _application/json;type=foo.bar.Person_). This is useful for cases where types may not be known at the time when function is developed, hence function signature may look like `Function` or `Function` or `Function`. In other words for type conversion we typically derive type from function signature. Having, mime-type parameter allows you to communicate type in a more dynamic way. +. `ByteArrayMessageConverter`: Supports conversion of the payload of the `Message` from `byte[]` to `byte[]` for cases when `contentType` is `application/octet-stream`. It is essentially a pass through and exists primarily for backward compatibility. +. `StringMessageConverter`: Supports conversion of any type to a `String` when `contentType` is `text/plain`. + +When no appropriate converter is found, the framework throws an exception. When that happens, you should check your code and configuration and ensure you did +not miss anything (that is, ensure that you provided a `contentType` by using a binding or a header). +However, most likely, you found some uncommon case (such as a custom `contentType` perhaps) and the current stack of provided `MessageConverters` +does not know how to convert. If that is the case, you can add custom `MessageConverter`. See <>. + +[[user-defined-message-converters]] +=== User-defined Message Converters + +Spring Cloud Function exposes a mechanism to define and register additional `MessageConverters`. +To use it, implement `org.springframework.messaging.converter.MessageConverter`, configure it as a `@Bean`. +It is then appended to the existing stack of `MessageConverter`s. + +NOTE: It is important to understand that custom `MessageConverter` implementations are added to the head of the existing stack. +Consequently, custom `MessageConverter` implementations take precedence over the existing ones, which lets you override as well as add to the existing converters. + +The following example shows how to create a message converter bean to support a new content type called `application/bar`: + +[source,java] +---- +@SpringBootApplication +public static class SinkApplication { + + ... + + @Bean + public MessageConverter customMessageConverter() { + return new MyCustomMessageConverter(); + } +} + +public class MyCustomMessageConverter extends AbstractMessageConverter { + + public MyCustomMessageConverter() { + super(new MimeType("application", "bar")); + } + + @Override + protected boolean supports(Class clazz) { + return (Bar.class.equals(clazz)); + } + + @Override + protected Object convertFromInternal(Message message, Class targetClass, Object conversionHint) { + Object payload = message.getPayload(); + return (payload instanceof Bar ? payload : new Bar((byte[]) payload)); + } +} +---- + +[[note-on-json-options]] +=== Note on JSON options + +In Spring Cloud Function we support Jackson and Gson mechanisms to deal with JSON. +And for your benefit have abstracted it under `org.springframework.cloud.function.json.JsonMapper` which itself is aware of two mechanisms and will use the one selected +by you or following the default rule. +The default rules are as follows: + +* Whichever library is on the classpath that is the mechanism that is going to be used. So if you have `com.fasterxml.jackson.*` to the classpath, Jackson is going to be used and if you have `com.google.code.gson`, then Gson will be used. +* If you have both, then Gson will be the default, or you can set `spring.cloud.function.preferred-json-mapper` property with either of two values: `gson` or `jackson`. + + +That said, the type conversion is usually transparent to the developer, however given that `org.springframework.cloud.function.json.JsonMapper` is also registered as a bean +you can easily inject it into your code if needed. + + +[[kotlin-lambda-support]] +== Kotlin Lambda support + +We also provide support for Kotlin lambdas (since v2.0). +Consider the following: + +[source, java] +---- +@Bean +open fun kotlinSupplier(): () -> String { + return { "Hello from Kotlin" } +} + +@Bean +open fun kotlinFunction(): (String) -> String { + return { it.toUpperCase() } +} + +@Bean +open fun kotlinConsumer(): (String) -> Unit { + return { println(it) } +} + +---- +The above represents Kotlin lambdas configured as Spring beans. The signature of each maps to a Java equivalent of +`Supplier`, `Function` and `Consumer`, and thus supported/recognized signatures by the framework. +While mechanics of Kotlin-to-Java mapping are outside of the scope of this documentation, it is important to understand that the +same rules for signature transformation outlined in "Java 8 function support" section are applied here as well. + +To enable Kotlin support all you need is to add Kotlin SDK libraries on the classpath which will trigger appropriate +autoconfiguration and supporting classes. + +[[function-component-scan]] +== Function Component Scan + +Spring Cloud Function will scan for implementations of `Function`, `Consumer` and `Supplier` in a package called `functions` if it exists. Using this +feature you can write functions that have no dependencies on Spring - not even the `@Component` annotation is needed. If you want to use a different +package, you can set `spring.cloud.function.scan.packages`. You can also use `spring.cloud.function.scan.enabled=false` to switch off the scan completely. + diff --git a/docs/modules/ROOT/pages/spring-cloud-function/serverless-platform-adapters.adoc b/docs/modules/ROOT/pages/spring-cloud-function/serverless-platform-adapters.adoc new file mode 100644 index 000000000..9e2abf136 --- /dev/null +++ b/docs/modules/ROOT/pages/spring-cloud-function/serverless-platform-adapters.adoc @@ -0,0 +1,14 @@ +[[serverless-platform-adapters]] += Serverless Platform Adapters + +As well as being able to run as a standalone process, a Spring Cloud +Function application can be adapted to run one of the existing +serverless platforms. In the project there are adapters for +https://github.com/spring-cloud/spring-cloud-function/tree/{branch}/spring-cloud-function-adapters/spring-cloud-function-adapter-aws[AWS +Lambda], and https://github.com/spring-cloud/spring-cloud-function/tree/{branch}/spring-cloud-function-adapters/spring-cloud-function-adapter-azure[Azure]. The https://github.com/fnproject/fn[Oracle Fn platform] has its own Spring Cloud Function adapter. And https://projectriff.io[Riff] supports Java functions and its +https://github.com/projectriff/java-function-invoker[Java Function Invoker] acts natively is an adapter for Spring Cloud Function jars. + +include:../:adapters/aws-intro.adoc[] +include:../:adapters/azure-intro.adoc[leveloffset=+1] +include:../:adapters/gcp-intro.adoc[] + diff --git a/docs/modules/ROOT/pages/spring-cloud-function/standalone-streaming-applications.adoc b/docs/modules/ROOT/pages/spring-cloud-function/standalone-streaming-applications.adoc new file mode 100644 index 000000000..e9898aec9 --- /dev/null +++ b/docs/modules/ROOT/pages/spring-cloud-function/standalone-streaming-applications.adoc @@ -0,0 +1,6 @@ +[[standalone-streaming-applications]] += Standalone Streaming Applications + +To send or receive messages from a broker (such as RabbitMQ or Kafka) you can leverage `spring-cloud-stream` project and it's integration with Spring Cloud Function. +Please refer to https://cloud.spring.io/spring-cloud-static/spring-cloud-stream/current/reference/html/spring-cloud-stream.html#spring_cloud_function[Spring Cloud Function] section of the https://spring.io/projects/spring-cloud-stream[Spring Cloud Stream] reference manual for more details and examples. + diff --git a/docs/modules/ROOT/pages/spring-cloud-function/standalone-web-applications.adoc b/docs/modules/ROOT/pages/spring-cloud-function/standalone-web-applications.adoc new file mode 100644 index 000000000..442989230 --- /dev/null +++ b/docs/modules/ROOT/pages/spring-cloud-function/standalone-web-applications.adoc @@ -0,0 +1,120 @@ +[[standalone-web-applications]] += Standalone Web Applications + +Functions could be automatically exported as HTTP endpoints. + +The `spring-cloud-function-web` module has autoconfiguration that +activates when it is included in a Spring Boot web application (with +MVC support). There is also a `spring-cloud-starter-function-web` to +collect all the optional dependencies in case you just want a simple +getting started experience. + +With the web configurations activated your app will have an MVC +endpoint (on "/" by default, but configurable with +`spring.cloud.function.web.path`) that can be used to access the +functions in the application context where function name becomes part of the URL path. The supported content types are +plain text and JSON. + +IMPORTANT: It is important to understand that while SCF provides ability to export Functional beans as REST endpoints it is NOT a replacement for Spring MVC/WebFlux etc. +It is primarily to accommodate _stateless serverless patterns_ where you simply want to have some stateless functionality to be exposed via HTTP. + +|=== +| Method | Path | Request | Response | Status + +| GET | /{supplier} | - | Items from the named supplier | 200 OK +| POST | /{consumer} | JSON object or text | Mirrors input and pushes request body into consumer | 202 Accepted +| PUT | /{consumer} | JSON object or text | Mirrors input and pushes request body into consumer | 202 Accepted +| DELETE | /{consumer} | JSON object or text | - | 204 NO CONTENT +| POST | /{function} | JSON object or text | The result of applying the named function | 200 OK +| PUT | /{function} | JSON object or text | The result of applying the named function | 200 OK +| GET | /{function}/{item} | - | Convert the item into an object and return the result of applying the function | 200 OK + +|=== + +As the table above shows the behavior of the endpoint depends on the method and also the type of incoming request data. When the incoming data is single valued, and the target function is declared as obviously single valued (i.e. not returning a collection or `Flux`), then the response will also contain a single value. +For multi-valued responses the client can ask for a server-sent event stream by sending `Accept: text/event-stream`. + +Functions and consumers that are declared with input and output in `Message` will see the request headers as _message headers_, and the output _message headers_ will be converted to HTTP headers. +The _payload_ of the Message will be a `body` or empty string if there is no `body` or it is null. + +When POSTing text the response format might be different with Spring Boot 2.0 and older versions, depending on the content negotiation (provide content type and accept headers for the best results). + +See <> to see the details and example on how to test such application. + +[[http-request-parameters]] +=== HTTP Request Parameters +As you have noticed from the previous table, you can pass an argument to a function as path variable (i.e., `/{function}/{item}`). +For example, `http://localhost:8080/uppercase/foo` will result in calling `uppercase` function with its input parameter being `foo`. + +While this is the recommended approach and the one that fits most use cases cases, there are times when you have to deal with HTTP request parameters (e.g., `http://localhost:8080/uppercase/foo?name=Bill`) +The framework will treat HTTP request parameters similar to the HTTP headers by storing them in the `Message` headers under the header key `http_request_param` +with its value being a `Map` of request parameters, so in order to access them your function input signature should accept `Message` type (e.g., `Function, String>`). For convenience we provide `HeaderUtils.HTTP_REQUEST_PARAM` constant. + +[[function-mapping-rules]] +== Function Mapping rules + +If there is only a single function (consumer etc.) in the catalog, the name in the path is optional. +In other words, providing you only have `uppercase` function in catalog +`curl -H "Content-Type: text/plain" localhost:8080/uppercase -d hello` and `curl -H "Content-Type: text/plain" localhost:8080/ -d hello` calls are identical. + +Composite functions can be addressed using pipes or commas to separate function names (pipes are legal in URL paths, but a bit awkward to type on the command line). +For example, `curl -H "Content-Type: text/plain" localhost:8080/uppercase,reverse -d hello`. + +For cases where there is more than a single function in catalog, each function will be exported and mapped with function name being +part of the path (e.g., `localhost:8080/uppercase`). +In this scenario you can still map specific function or function composition to the root path by providing +`spring.cloud.function.definition` property + +For example, +---- +--spring.cloud.function.definition=foo|bar +---- + +The above property will compose 'foo' and 'bar' function and map the composed function to the "/" path. + +The same property will also work for cases where function can not be resolved via URL. For example, your URL may be `localhost:8080/uppercase`, but there is no `uppercase` function. +However there are function `foo` and `bar`. So, in this case `localhost:8080/uppercase` will resolve to `foo|bar`. +This could be useful especially for cases when URL is used to communicate certain information since there will be Message header called `uri` with the value +of the actual URL, giving user ability to use it for evaluation and computation. + +[[function-filtering-rules]] +== Function Filtering rules + +In situations where there are more than one function in catalog there may be a need to only export certain functions or function compositions. In that case you can use +the same `spring.cloud.function.definition` property listing functions you intend to export delimited by `;`. +Note that in this case nothing will be mapped to the root path and functions that are not listed (including compositions) are not going to be exported + +For example, + +---- +--spring.cloud.function.definition=foo;bar +---- + +This will only export function `foo` and function `bar` regardless how many functions are available in catalog (e.g., `localhost:8080/foo`). + +---- +--spring.cloud.function.definition=foo|bar;baz +---- + +This will only export function composition `foo|bar` and function `baz` regardless how many functions are available in catalog (e.g., `localhost:8080/foo,bar`). + +[[crud-rest-with-spring-cloud-function]] +== CRUD REST with Spring Cloud Function + +By now it should be clear that functions are exported as REST endpoints and can be invoked using various HTTP methods. In other words a single +function could be triggered via GET, POST, PUT etc. + +However, it is not always desirable and certainly does not fit the CRUD concept. And while SCF does not support and has no intention of supporting +all the features of Spring web stack, the framework does provide support for CRUD mappings where a single function could be mapped to a particular HTTP method(s). +It is done via spring.cloud.function.http. property. + +For example, + +---- +spring.cloud.function.http.GET=uppercase;reverse;foo|bar +spring.cloud.function.http.POST=reverse +spring.cloud.function.http.DELETE=deleteById +---- + +As you can see, here we’re mapping functions to various HTTP methods using the same rules as `spring.cloud.function.definition` property where “;” allows us to define several functions and “|” signifies function composition. +