Sync docs from master to gh-pages
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
<html><head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||
<title>7. Deploying a Packaged Function</title><link rel="stylesheet" type="text/css" href="css/manual-multipage.css"><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"><link rel="home" href="multi_spring-cloud-function.html" title="Spring Cloud Function"><link rel="up" href="multi_spring-cloud-function.html" title="Spring Cloud Function"><link rel="prev" href="multi__standalone_streaming_applications.html" title="6. Standalone Streaming Applications"><link rel="next" href="multi__dynamic_compilation.html" title="8. Dynamic Compilation"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">7. Deploying a Packaged Function</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="multi__standalone_streaming_applications.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="multi__dynamic_compilation.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="_deploying_a_packaged_function" href="#_deploying_a_packaged_function"></a>7. Deploying a Packaged Function</h1></div></div></div><p>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 <a class="link" href="https://projectriff.io" target="_top">Riff</a> Java function invoker uses this library).</p><p>The standard entry point of the API is the Spring configuration annotation <code class="literal">@EnableFunctionDeployer</code>. If that is used in a Spring Boot application the deployer kicks in and looks for some configuration to tell it where to find the function jar. At a minimum the user has to provide a <code class="literal">function.location</code> which is a URL or resource location for the archive containing the functions. It can optionally use a <code class="literal">maven:</code> prefix to locate the artifact via a dependency lookup (see <code class="literal">FunctionProperties</code> for complete details). A Spring Boot application is bootstrapped from the jar file, using the <code class="literal">MANIFEST.MF</code> 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 <code class="literal">FunctionCatalog</code>. The registered function can be applied by code in the main application, even though it was created in an isolated class loader (by deault).</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="multi__standalone_streaming_applications.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="multi__dynamic_compilation.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">6. Standalone Streaming Applications </td><td width="20%" align="center"><a accesskey="h" href="multi_spring-cloud-function.html">Home</a></td><td width="40%" align="right" valign="top"> 8. Dynamic Compilation</td></tr></table></div></body></html>
|
||||
<title>7. Deploying a Packaged Function</title><link rel="stylesheet" type="text/css" href="css/manual-multipage.css"><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"><link rel="home" href="multi_spring-cloud-function.html" title="Spring Cloud Function"><link rel="up" href="multi_spring-cloud-function.html" title="Spring Cloud Function"><link rel="prev" href="multi__standalone_streaming_applications.html" title="6. Standalone Streaming Applications"><link rel="next" href="multi__functional_bean_definitions.html" title="8. Functional Bean Definitions"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">7. Deploying a Packaged Function</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="multi__standalone_streaming_applications.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="multi__functional_bean_definitions.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="_deploying_a_packaged_function" href="#_deploying_a_packaged_function"></a>7. Deploying a Packaged Function</h1></div></div></div><p>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 <a class="link" href="https://projectriff.io" target="_top">Riff</a> Java function invoker uses this library).</p><p>The standard entry point of the API is the Spring configuration annotation <code class="literal">@EnableFunctionDeployer</code>. If that is used in a Spring Boot application the deployer kicks in and looks for some configuration to tell it where to find the function jar. At a minimum the user has to provide a <code class="literal">function.location</code> which is a URL or resource location for the archive containing the functions. It can optionally use a <code class="literal">maven:</code> prefix to locate the artifact via a dependency lookup (see <code class="literal">FunctionProperties</code> for complete details). A Spring Boot application is bootstrapped from the jar file, using the <code class="literal">MANIFEST.MF</code> 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 <code class="literal">FunctionCatalog</code>. The registered function can be applied by code in the main application, even though it was created in an isolated class loader (by deault).</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="multi__standalone_streaming_applications.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="multi__functional_bean_definitions.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">6. Standalone Streaming Applications </td><td width="20%" align="center"><a accesskey="h" href="multi_spring-cloud-function.html">Home</a></td><td width="40%" align="right" valign="top"> 8. Functional Bean Definitions</td></tr></table></div></body></html>
|
||||
@@ -1,6 +1,6 @@
|
||||
<html><head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||
<title>8. Dynamic Compilation</title><link rel="stylesheet" type="text/css" href="css/manual-multipage.css"><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"><link rel="home" href="multi_spring-cloud-function.html" title="Spring Cloud Function"><link rel="up" href="multi_spring-cloud-function.html" title="Spring Cloud Function"><link rel="prev" href="multi__deploying_a_packaged_function.html" title="7. Deploying a Packaged Function"><link rel="next" href="multi__serverless_platform_adapters.html" title="9. Serverless Platform Adapters"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">8. Dynamic Compilation</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="multi__deploying_a_packaged_function.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="multi__serverless_platform_adapters.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="_dynamic_compilation" href="#_dynamic_compilation"></a>8. Dynamic Compilation</h1></div></div></div><p>There is a sample app that uses the function compiler to create a
|
||||
<title>9. Dynamic Compilation</title><link rel="stylesheet" type="text/css" href="css/manual-multipage.css"><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"><link rel="home" href="multi_spring-cloud-function.html" title="Spring Cloud Function"><link rel="up" href="multi_spring-cloud-function.html" title="Spring Cloud Function"><link rel="prev" href="multi__functional_bean_definitions.html" title="8. Functional Bean Definitions"><link rel="next" href="multi__serverless_platform_adapters.html" title="10. Serverless Platform Adapters"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">9. Dynamic Compilation</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="multi__functional_bean_definitions.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="multi__serverless_platform_adapters.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="_dynamic_compilation" href="#_dynamic_compilation"></a>9. Dynamic Compilation</h1></div></div></div><p>There is a sample app that uses the function compiler to create a
|
||||
function from a configuration property. The vanilla "function-sample"
|
||||
also has that feature. And there are some scripts that you can run to
|
||||
see the compilation happening at run time. To run these examples,
|
||||
@@ -20,4 +20,4 @@ MESSAGE-6
|
||||
MESSAGE-7
|
||||
MESSAGE-8
|
||||
MESSAGE-9
|
||||
...</pre></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="multi__deploying_a_packaged_function.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="multi__serverless_platform_adapters.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">7. Deploying a Packaged Function </td><td width="20%" align="center"><a accesskey="h" href="multi_spring-cloud-function.html">Home</a></td><td width="40%" align="right" valign="top"> 9. Serverless Platform Adapters</td></tr></table></div></body></html>
|
||||
...</pre></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="multi__functional_bean_definitions.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="multi__serverless_platform_adapters.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">8. Functional Bean Definitions </td><td width="20%" align="center"><a accesskey="h" href="multi_spring-cloud-function.html">Home</a></td><td width="40%" align="right" valign="top"> 10. Serverless Platform Adapters</td></tr></table></div></body></html>
|
||||
81
multi/multi__functional_bean_definitions.html
Normal file
81
multi/multi__functional_bean_definitions.html
Normal file
@@ -0,0 +1,81 @@
|
||||
<html><head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||
<title>8. Functional Bean Definitions</title><link rel="stylesheet" type="text/css" href="css/manual-multipage.css"><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"><link rel="home" href="multi_spring-cloud-function.html" title="Spring Cloud Function"><link rel="up" href="multi_spring-cloud-function.html" title="Spring Cloud Function"><link rel="prev" href="multi__deploying_a_packaged_function.html" title="7. Deploying a Packaged Function"><link rel="next" href="multi__dynamic_compilation.html" title="9. Dynamic Compilation"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">8. Functional Bean Definitions</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="multi__deploying_a_packaged_function.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="multi__dynamic_compilation.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="_functional_bean_definitions" href="#_functional_bean_definitions"></a>8. Functional Bean Definitions</h1></div></div></div><p>Spring Cloud Function supports a "functional" style of bean declarations for small apps where you need fast startup. The functional style of bean declaration was a feature of Spring Framework 5.0 with significant enhancements in 5.1.</p><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_comparing_functional_with_traditional_bean_definitions" href="#_comparing_functional_with_traditional_bean_definitions"></a>8.1 Comparing Functional with Traditional Bean Definitions</h2></div></div></div><p>Here’s a vanilla Spring Cloud Function application from with the
|
||||
familiar <code class="literal">@Configuration</code> and <code class="literal">@Bean</code> declaration style:</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@SpringBootApplication</span></em>
|
||||
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> DemoApplication {
|
||||
|
||||
<em><span class="hl-annotation" style="color: gray">@Bean</span></em>
|
||||
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> Function<String, String> uppercase() {
|
||||
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> value -> value.toUpperCase();
|
||||
}
|
||||
|
||||
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">static</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">void</span> main(String[] args) {
|
||||
SpringApplication.run(DemoApplication.<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span>, args);
|
||||
}
|
||||
|
||||
}</pre><p>You can run the above in a serverless platform, like AWS Lambda or Azure Functions, or you can run it in its own HTTP server just by including <code class="literal">spring-cloud-function-starter-web</code> on the classpath. Running the main method would expose an endpoint that you can use to ping that <code class="literal">uppercase</code> function:</p><pre class="screen">$ curl localhost:8080 -d foo
|
||||
FOO</pre><p>The web adapter in <code class="literal">spring-cloud-function-starter-web</code> uses Spring MVC, so you needed a Servlet container. You can also use Webflux where the default server is netty (even though you can still use Servlet containers if you want to) - just include the <code class="literal">spring-cloud-starter-function-webflux</code> dependency instead. The functionality is the same, and the user application code can be used in both.</p><p>Now for the functional beans: the user application code can be recast into "functional"
|
||||
form, like this:</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@SpringBootConfiguration</span></em>
|
||||
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> DemoApplication <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">implements</span> ApplicationContextInitializer<GenericApplicationContext> {
|
||||
|
||||
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">static</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">void</span> main(String[] args) {
|
||||
FunctionalSpringApplication.run(DemoApplication.<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span>, args);
|
||||
}
|
||||
|
||||
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> Function<String, String> uppercase() {
|
||||
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> value -> value.toUpperCase();
|
||||
}
|
||||
|
||||
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
||||
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">void</span> initialize(GenericApplicationContext context) {
|
||||
context.registerBean(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"demo"</span>, FunctionRegistration.<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span>,
|
||||
() -> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">new</span> FunctionRegistration<>(uppercase())
|
||||
.type(FunctionType.from(String.<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span>).to(String.<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span>)));
|
||||
}
|
||||
|
||||
}</pre><p>The main differences are:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">The main class is an <code class="literal">ApplicationContextInitializer</code>.</li><li class="listitem">The <code class="literal">@Bean</code> methods have been converted to calls to <code class="literal">context.registerBean()</code></li><li class="listitem">The <code class="literal">@SpringBootApplication</code> has been replaced with
|
||||
<code class="literal">@SpringBootConfiguration</code> to signify that we are not enabling Spring
|
||||
Boot autoconfiguration, and yet still marking the class as an "entry
|
||||
point".</li><li class="listitem">The <code class="literal">SpringApplication</code> from Spring Boot has been replaced with a
|
||||
<code class="literal">FunctionalSpringApplication</code> from Spring Cloud Function (it’s a
|
||||
subclass).</li></ul></div><p>The business logic beans that you register in a Spring Cloud Function app are of type <code class="literal">FunctionRegistration</code>. This is a wrapper that contains both the function and information about the input and output types. In the <code class="literal">@Bean</code> form of the application that information can be derived reflectively, but in a functional bean registration some of it is lost unless we use a <code class="literal">FunctionRegistration</code>.</p><p>An alternative to using an <code class="literal">ApplicationContextInitializer</code> and <code class="literal">FunctionRegistration</code> is to make the application itself implement <code class="literal">Function</code> (or <code class="literal">Consumer</code> or <code class="literal">Supplier</code>). Example (equivalent to the above):</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@SpringBootConfiguration</span></em>
|
||||
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> DemoApplication <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">implements</span> Function<String, String> {
|
||||
|
||||
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">static</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">void</span> main(String[] args) {
|
||||
FunctionalSpringApplication.run(DemoApplication.<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span>, args);
|
||||
}
|
||||
|
||||
<em><span class="hl-annotation" style="color: gray">@Override</span></em>
|
||||
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> String uppercase(String value) {
|
||||
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> value.toUpperCase();
|
||||
}
|
||||
|
||||
}</pre><p>It would also work if you add a separate, standalone class of type <code class="literal">Function</code> and register it with the <code class="literal">SpringApplication</code> using an alternative form of the <code class="literal">run()</code> method. The main thing is that the generic type information is available at runtime through the class declaration.</p><p>The app runs in its own HTTP server if you add <code class="literal">spring-cloud-starter-function-webflux</code> (it won’t work with the MVC starter at the moment because the functional form of the embedded Servlet container hasn’t been implemented). The app also runs just fine in AWS Lambda or Azure Functions, and the improvements in startup time are dramatic.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>The "lite" web server has some limitations for the range of <code class="literal">Function</code> signatures - in particular it doesn’t (yet) support <code class="literal">Message</code> input and output, but POJOs and any kind of <code class="literal">Publisher</code> should be fine.</p></td></tr></table></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_testing_functional_applications" href="#_testing_functional_applications"></a>8.2 Testing Functional Applications</h2></div></div></div><p>Spring Cloud Function also has some utilities for integration testing that will be very familiar to Spring Boot users. For example, here is an integration test for the HTTP server wrapping the app above:</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@RunWith(SpringRunner.class)</span></em>
|
||||
<em><span class="hl-annotation" style="color: gray">@FunctionalSpringBootTest</span></em>
|
||||
<em><span class="hl-annotation" style="color: gray">@AutoConfigureWebTestClient</span></em>
|
||||
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> FunctionalTests {
|
||||
|
||||
<em><span class="hl-annotation" style="color: gray">@Autowired</span></em>
|
||||
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">private</span> WebTestClient client;
|
||||
|
||||
<em><span class="hl-annotation" style="color: gray">@Test</span></em>
|
||||
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">void</span> words() <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">throws</span> Exception {
|
||||
client.post().uri(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"/"</span>).body(Mono.just(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"foo"</span>), String.<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span>).exchange()
|
||||
.expectStatus().isOk().expectBody(String.<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span>).isEqualTo(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"FOO"</span>);
|
||||
}
|
||||
|
||||
}</pre><p>This test is almost identical to the one you would write for the <code class="literal">@Bean</code> version of the same app - the only difference is the <code class="literal">@FunctionalSpringBootTest</code> annotation, instead of the regular <code class="literal">@SpringBootTest</code>. All the other pieces, like the <code class="literal">@Autowired</code> <code class="literal">WebTestClient</code>, are standard Spring Boot features.</p><p>Or you could write a test for a non-HTTP app using just the <code class="literal">FunctionCatalog</code>. For example:</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@RunWith(SpringRunner.class)</span></em>
|
||||
<em><span class="hl-annotation" style="color: gray">@FunctionalSpringBootTest</span></em>
|
||||
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> FunctionalTests {
|
||||
|
||||
<em><span class="hl-annotation" style="color: gray">@Autowired</span></em>
|
||||
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">private</span> FunctionCatalog catalog;
|
||||
|
||||
<em><span class="hl-annotation" style="color: gray">@Test</span></em>
|
||||
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">void</span> words() <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">throws</span> Exception {
|
||||
Function<Flux<String>, Flux<String>> function = catalog.lookup(Function.<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span>,
|
||||
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"function"</span>);
|
||||
assertThat(function.apply(Flux.just(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"foo"</span>)).blockFirst()).isEqualTo(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"FOO"</span>);
|
||||
}
|
||||
|
||||
}</pre><p>(The <code class="literal">FunctionCatalog</code> always returns functions from <code class="literal">Flux</code> to <code class="literal">Flux</code>, even if the user declares them with a simpler signature.)</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_limitations_of_functional_bean_declaration" href="#_limitations_of_functional_bean_declaration"></a>8.3 Limitations of Functional Bean Declaration</h2></div></div></div><p>Most Spring Cloud Function apps have a relatively small scope compared to the whole of Spring Boot, so we are able to adapt it to these functional bean definitions easily. If you step outside that limited scope, you can extend your Spring Cloud Function app by switching back to <code class="literal">@Bean</code> style configuration, or by using a hybrid approach. If you want to take advantage of Spring Boot autoconfiguration for integrations with external datastores, for example, you will need to use <code class="literal">@EnableAutoConfiguration</code>. Your functions can still be defined using the functional declarations if you want (i.e. the "hybrid" style), but in that case you will need to explicitly switch off the "full functional mode" using <code class="literal">spring.functional.enabled=false</code> so that Spring Boot can take back control.</p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="multi__deploying_a_packaged_function.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="multi__dynamic_compilation.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">7. Deploying a Packaged Function </td><td width="20%" align="center"><a accesskey="h" href="multi_spring-cloud-function.html">Home</a></td><td width="40%" align="right" valign="top"> 9. Dynamic Compilation</td></tr></table></div></body></html>
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -313,6 +313,134 @@ Please refer to <link xl:href="https://docs.spring.io/spring-cloud-stream/docs/c
|
||||
<simpara>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 <link xl:href="https://projectriff.io">Riff</link> Java function invoker uses this library).</simpara>
|
||||
<simpara>The standard entry point of the API is the Spring configuration annotation <literal>@EnableFunctionDeployer</literal>. If that is used in a Spring Boot application the deployer kicks in and looks for some configuration to tell it where to find the function jar. At a minimum the user has to provide a <literal>function.location</literal> which is a URL or resource location for the archive containing the functions. It can optionally use a <literal>maven:</literal> prefix to locate the artifact via a dependency lookup (see <literal>FunctionProperties</literal> for complete details). A Spring Boot application is bootstrapped from the jar file, using the <literal>MANIFEST.MF</literal> 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 <literal>FunctionCatalog</literal>. The registered function can be applied by code in the main application, even though it was created in an isolated class loader (by deault).</simpara>
|
||||
</chapter>
|
||||
<chapter xml:id="_functional_bean_definitions">
|
||||
<title>Functional Bean Definitions</title>
|
||||
<simpara>Spring Cloud Function supports a "functional" style of bean declarations for small apps where you need fast startup. The functional style of bean declaration was a feature of Spring Framework 5.0 with significant enhancements in 5.1.</simpara>
|
||||
<section xml:id="_comparing_functional_with_traditional_bean_definitions">
|
||||
<title>Comparing Functional with Traditional Bean Definitions</title>
|
||||
<simpara>Here’s a vanilla Spring Cloud Function application from with the
|
||||
familiar <literal>@Configuration</literal> and <literal>@Bean</literal> declaration style:</simpara>
|
||||
<programlisting language="java" linenumbering="unnumbered">@SpringBootApplication
|
||||
public class DemoApplication {
|
||||
|
||||
@Bean
|
||||
public Function<String, String> uppercase() {
|
||||
return value -> value.toUpperCase();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DemoApplication.class, args);
|
||||
}
|
||||
|
||||
}</programlisting>
|
||||
<simpara>You can run the above in a serverless platform, like AWS Lambda or Azure Functions, or you can run it in its own HTTP server just by including <literal>spring-cloud-function-starter-web</literal> on the classpath. Running the main method would expose an endpoint that you can use to ping that <literal>uppercase</literal> function:</simpara>
|
||||
<screen>$ curl localhost:8080 -d foo
|
||||
FOO</screen>
|
||||
<simpara>The web adapter in <literal>spring-cloud-function-starter-web</literal> uses Spring MVC, so you needed a Servlet container. You can also use Webflux where the default server is netty (even though you can still use Servlet containers if you want to) - just include the <literal>spring-cloud-starter-function-webflux</literal> dependency instead. The functionality is the same, and the user application code can be used in both.</simpara>
|
||||
<simpara>Now for the functional beans: the user application code can be recast into "functional"
|
||||
form, like this:</simpara>
|
||||
<programlisting language="java" linenumbering="unnumbered">@SpringBootConfiguration
|
||||
public class DemoApplication implements ApplicationContextInitializer<GenericApplicationContext> {
|
||||
|
||||
public static void main(String[] args) {
|
||||
FunctionalSpringApplication.run(DemoApplication.class, args);
|
||||
}
|
||||
|
||||
public Function<String, String> uppercase() {
|
||||
return value -> value.toUpperCase();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize(GenericApplicationContext context) {
|
||||
context.registerBean("demo", FunctionRegistration.class,
|
||||
() -> new FunctionRegistration<>(uppercase())
|
||||
.type(FunctionType.from(String.class).to(String.class)));
|
||||
}
|
||||
|
||||
}</programlisting>
|
||||
<simpara>The main differences are:</simpara>
|
||||
<itemizedlist>
|
||||
<listitem>
|
||||
<simpara>The main class is an <literal>ApplicationContextInitializer</literal>.</simpara>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<simpara>The <literal>@Bean</literal> methods have been converted to calls to <literal>context.registerBean()</literal></simpara>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<simpara>The <literal>@SpringBootApplication</literal> has been replaced with
|
||||
<literal>@SpringBootConfiguration</literal> to signify that we are not enabling Spring
|
||||
Boot autoconfiguration, and yet still marking the class as an "entry
|
||||
point".</simpara>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<simpara>The <literal>SpringApplication</literal> from Spring Boot has been replaced with a
|
||||
<literal>FunctionalSpringApplication</literal> from Spring Cloud Function (it’s a
|
||||
subclass).</simpara>
|
||||
</listitem>
|
||||
</itemizedlist>
|
||||
<simpara>The business logic beans that you register in a Spring Cloud Function app are of type <literal>FunctionRegistration</literal>. This is a wrapper that contains both the function and information about the input and output types. In the <literal>@Bean</literal> form of the application that information can be derived reflectively, but in a functional bean registration some of it is lost unless we use a <literal>FunctionRegistration</literal>.</simpara>
|
||||
<simpara>An alternative to using an <literal>ApplicationContextInitializer</literal> and <literal>FunctionRegistration</literal> is to make the application itself implement <literal>Function</literal> (or <literal>Consumer</literal> or <literal>Supplier</literal>). Example (equivalent to the above):</simpara>
|
||||
<programlisting language="java" linenumbering="unnumbered">@SpringBootConfiguration
|
||||
public class DemoApplication implements Function<String, String> {
|
||||
|
||||
public static void main(String[] args) {
|
||||
FunctionalSpringApplication.run(DemoApplication.class, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uppercase(String value) {
|
||||
return value.toUpperCase();
|
||||
}
|
||||
|
||||
}</programlisting>
|
||||
<simpara>It would also work if you add a separate, standalone class of type <literal>Function</literal> and register it with the <literal>SpringApplication</literal> using an alternative form of the <literal>run()</literal> method. The main thing is that the generic type information is available at runtime through the class declaration.</simpara>
|
||||
<simpara>The app runs in its own HTTP server if you add <literal>spring-cloud-starter-function-webflux</literal> (it won’t work with the MVC starter at the moment because the functional form of the embedded Servlet container hasn’t been implemented). The app also runs just fine in AWS Lambda or Azure Functions, and the improvements in startup time are dramatic.</simpara>
|
||||
<note>
|
||||
<simpara>The "lite" web server has some limitations for the range of <literal>Function</literal> signatures - in particular it doesn’t (yet) support <literal>Message</literal> input and output, but POJOs and any kind of <literal>Publisher</literal> should be fine.</simpara>
|
||||
</note>
|
||||
</section>
|
||||
<section xml:id="_testing_functional_applications">
|
||||
<title>Testing Functional Applications</title>
|
||||
<simpara>Spring Cloud Function also has some utilities for integration testing that will be very familiar to Spring Boot users. For example, here is an integration test for the HTTP server wrapping the app above:</simpara>
|
||||
<programlisting language="java" linenumbering="unnumbered">@RunWith(SpringRunner.class)
|
||||
@FunctionalSpringBootTest
|
||||
@AutoConfigureWebTestClient
|
||||
public class FunctionalTests {
|
||||
|
||||
@Autowired
|
||||
private WebTestClient client;
|
||||
|
||||
@Test
|
||||
public void words() throws Exception {
|
||||
client.post().uri("/").body(Mono.just("foo"), String.class).exchange()
|
||||
.expectStatus().isOk().expectBody(String.class).isEqualTo("FOO");
|
||||
}
|
||||
|
||||
}</programlisting>
|
||||
<simpara>This test is almost identical to the one you would write for the <literal>@Bean</literal> version of the same app - the only difference is the <literal>@FunctionalSpringBootTest</literal> annotation, instead of the regular <literal>@SpringBootTest</literal>. All the other pieces, like the <literal>@Autowired</literal> <literal>WebTestClient</literal>, are standard Spring Boot features.</simpara>
|
||||
<simpara>Or you could write a test for a non-HTTP app using just the <literal>FunctionCatalog</literal>. For example:</simpara>
|
||||
<programlisting language="java" linenumbering="unnumbered">@RunWith(SpringRunner.class)
|
||||
@FunctionalSpringBootTest
|
||||
public class FunctionalTests {
|
||||
|
||||
@Autowired
|
||||
private FunctionCatalog catalog;
|
||||
|
||||
@Test
|
||||
public void words() throws Exception {
|
||||
Function<Flux<String>, Flux<String>> function = catalog.lookup(Function.class,
|
||||
"function");
|
||||
assertThat(function.apply(Flux.just("foo")).blockFirst()).isEqualTo("FOO");
|
||||
}
|
||||
|
||||
}</programlisting>
|
||||
<simpara>(The <literal>FunctionCatalog</literal> always returns functions from <literal>Flux</literal> to <literal>Flux</literal>, even if the user declares them with a simpler signature.)</simpara>
|
||||
</section>
|
||||
<section xml:id="_limitations_of_functional_bean_declaration">
|
||||
<title>Limitations of Functional Bean Declaration</title>
|
||||
<simpara>Most Spring Cloud Function apps have a relatively small scope compared to the whole of Spring Boot, so we are able to adapt it to these functional bean definitions easily. If you step outside that limited scope, you can extend your Spring Cloud Function app by switching back to <literal>@Bean</literal> style configuration, or by using a hybrid approach. If you want to take advantage of Spring Boot autoconfiguration for integrations with external datastores, for example, you will need to use <literal>@EnableAutoConfiguration</literal>. Your functions can still be defined using the functional declarations if you want (i.e. the "hybrid" style), but in that case you will need to explicitly switch off the "full functional mode" using <literal>spring.functional.enabled=false</literal> so that Spring Boot can take back control.</simpara>
|
||||
</section>
|
||||
</chapter>
|
||||
<chapter xml:id="_dynamic_compilation">
|
||||
<title>Dynamic Compilation</title>
|
||||
<simpara>There is a sample app that uses the function compiler to create a
|
||||
@@ -397,6 +525,9 @@ Invoker</link> acts natively is an adapter for Spring Cloud Function jars.</simp
|
||||
<screen>{
|
||||
"value": "test"
|
||||
}</screen>
|
||||
<note>
|
||||
<simpara>The AWS sample app is written in the "functional" style (as an <literal>ApplicationContextInitializer</literal>). This is much faster on startup in Lambda than the traditional <literal>@Bean</literal> style, so if you don’t need <literal>@Beans</literal> (or <literal>@EnableAutoConfiguration</literal>) it’s a good choice. Warm starts are not affected.</simpara>
|
||||
</note>
|
||||
</section>
|
||||
<section xml:id="_platfom_specific_features">
|
||||
<title>Platfom Specific Features</title>
|
||||
@@ -471,7 +602,7 @@ You can write an app with a single <literal>@Bean</literal> of type <literal>Fun
|
||||
<simpara><?asciidoc-hr?></simpara>
|
||||
<simpara><?asciidoc-hr?></simpara>
|
||||
<simpara>and <literal>curl -H "Content-Type: text/plain" localhost:8080/function -d '{"value": "hello foobar"}'</literal>.</simpara>
|
||||
<simpara>You will need the <literal>az</literal> CLI app and some node.js fu (see <link xl:href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-java-maven">https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-java-maven</link> for more detail). To deploy the function on Azure runtime:</simpara>
|
||||
<simpara>You will need the <literal>az</literal> CLI app (see <link xl:href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-java-maven">https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-java-maven</link> for more detail). To deploy the function on Azure runtime:</simpara>
|
||||
<screen>$ az login
|
||||
$ mvn azure-functions:deploy</screen>
|
||||
<simpara>On another terminal try this: <literal>curl <link xl:href="https://<azure-function-url-from-the-log>/api/uppercase">https://<azure-function-url-from-the-log>/api/uppercase</link> -d '{"value": "hello foobar!"}'</literal>. Please ensure that you use the right URL for the function above. Alternatively you can test the function in the Azure Dashboard UI (click on the function name, go to the right hand side and click "Test" and to the bottom right, "Run").</simpara>
|
||||
@@ -479,6 +610,9 @@ $ mvn azure-functions:deploy</screen>
|
||||
<screen>{
|
||||
"value": "foobar"
|
||||
}</screen>
|
||||
<note>
|
||||
<simpara>The Azure sample app is written in the "non-functional" style (using <literal>@Bean</literal>). The functional style (with just <literal>Function</literal> or <literal>ApplicationContextInitializer</literal>) is much faster on startup in Azure than the traditional <literal>@Bean</literal> style, so if you don’t need <literal>@Beans</literal> (or <literal>@EnableAutoConfiguration</literal>) it’s a good choice. Warm starts are not affected.</simpara>
|
||||
</note>
|
||||
</section>
|
||||
</section>
|
||||
<section xml:id="_apache_openwhisk">
|
||||
|
||||
Reference in New Issue
Block a user