Updating to latest spring cloud release and refactoring code (#838)

* updating to latest spring cloud release and refactoring code

* new health endpoints

* implementing simplified echo function in template

* adding banner

* adding NL at EOF

* updating native to release

* fixing trailing whitespace

* adding invocation back

* adding Newline

* whitespaces

* fix lint and regenerate

* regenerate zz filesystem after rebase
This commit is contained in:
salaboy 2022-03-11 15:22:32 +00:00 committed by GitHub
parent e5039891eb
commit 57909f71fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 8027 additions and 8131 deletions

1
.gitignore vendored
View File

@ -15,3 +15,4 @@ __pycache__
.vscode .vscode
.idea .idea
.DS_Store

View File

@ -2,7 +2,7 @@
Welcome to your new Function project! Welcome to your new Function project!
This sample project contains a single function based on Spring Cloud Function: `functions.CloudFunctionApplication.uppercase()`, which returns the uppercase of the data passed via CloudEvents. This sample project contains a single function based on Spring Cloud Function: `echo.EchoFunction`, which returns an echo of the data passed via CloudEvents.
## Local execution ## Local execution
@ -95,11 +95,11 @@ Using CloudEvents `Ce-Type` routing:
curl -v "$URL/" \ curl -v "$URL/" \
-H "Content-Type:application/json" \ -H "Content-Type:application/json" \
-H "Ce-Id:1" \ -H "Ce-Id:1" \
-H "Ce-Subject:Uppercase" \ -H "Ce-Subject:Echo" \
-H "Ce-Source:cloud-event-example" \ -H "Ce-Source:cloud-event-example" \
-H "Ce-Type:uppercase" \ -H "Ce-Type:MyEvent" \
-H "Ce-Specversion:1.0" \ -H "Ce-Specversion:1.0" \
-d "{\"input\": \"$(whoami)\"}\"" -d "hello"
``` ```
Using Path-Based routing: Using Path-Based routing:
@ -107,11 +107,11 @@ Using Path-Based routing:
curl -v "$URL/uppercase" \ curl -v "$URL/uppercase" \
-H "Content-Type:application/json" \ -H "Content-Type:application/json" \
-H "Ce-Id:1" \ -H "Ce-Id:1" \
-H "Ce-Subject:Uppercase" \ -H "Ce-Subject:Echo" \
-H "Ce-Source:cloud-event-example" \ -H "Ce-Source:cloud-event-example" \
-H "Ce-Type:my-event" \ -H "Ce-Type:MyEvent" \
-H "Ce-Specversion:1.0" \ -H "Ce-Specversion:1.0" \
-d "{\"input\": \"$(whoami)\"}\"" -d "hello"
``` ```
### HTTPie ### HTTPie
@ -121,23 +121,23 @@ Using CloudEvents `Ce-Type` routing:
http -v "$URL/" \ http -v "$URL/" \
Content-Type:application/json \ Content-Type:application/json \
Ce-Id:1 \ Ce-Id:1 \
Ce-Subject:Uppercase \ Ce-Subject:Echo \
Ce-Source:cloud-event-example \ Ce-Source:cloud-event-example \
Ce-Type:uppercase \ Ce-Type:MyEvent \
Ce-Specversion:1.0 \ Ce-Specversion:1.0 \
input=$(whoami) hello
``` ```
Using Path-Based routing: Using Path-Based routing:
```shell script ```shell script
http -v "$URL/uppercase" \ http -v "$URL/echo" \
Content-Type:application/json \ Content-Type:application/json \
Ce-Id:1 \ Ce-Id:1 \
Ce-Subject:Uppercase \ Ce-Subject:Echo \
Ce-Source:cloud-event-example \ Ce-Source:cloud-event-example \
Ce-Type:uppercase \ Ce-Type:MyEvent \
Ce-Specversion:1.0 \ Ce-Specversion:1.0 \
input=$(whoami) hello
``` ```
## Cleanup ## Cleanup

View File

@ -1,3 +1,11 @@
builders:
default: gcr.io/paketo-buildpacks/builder:base
buildEnvs:
- name: BP_NATIVE_IMAGE
value: "false"
healthEndpoints:
liveness: /actuator/health
readiness: /actuator/health
# optional. Invocation defines hints for how Functions created using this # optional. Invocation defines hints for how Functions created using this
# template can be invoked. These settings can be updated on the resultant # template can be invoked. These settings can be updated on the resultant
# Function as development progresses to ensure 'invoke' can always trigger the # Function as development progresses to ensure 'invoke' can always trigger the

View File

@ -6,7 +6,7 @@
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version> <version>2.6.3</version>
<relativePath/> <relativePath/>
</parent> </parent>
@ -18,10 +18,9 @@
<properties> <properties>
<java.version>11</java.version> <java.version>11</java.version>
<spring-cloud.version>2020.0.4</spring-cloud.version> <spring-cloud.version>2021.0.1</spring-cloud.version>
<spring-native.version>0.10.3</spring-native.version> <spring-native.version>0.11.3</spring-native.version>
<compiler-plugin.version>3.8.1</compiler-plugin.version> <compiler-plugin.version>3.8.1</compiler-plugin.version>
<log4j2.version>2.16.0</log4j2.version>
<maven.compiler.parameters>true</maven.compiler.parameters> <maven.compiler.parameters>true</maven.compiler.parameters>
<maven.compiler.source>11</maven.compiler.source> <maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target> <maven.compiler.target>11</maven.compiler.target>
@ -42,6 +41,10 @@
</dependencyManagement> </dependencyManagement>
<dependencies> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId> <artifactId>spring-boot-starter-webflux</artifactId>
@ -75,10 +78,9 @@
<configuration> <configuration>
<classifier>${repackage.classifier}</classifier> <classifier>${repackage.classifier}</classifier>
<image> <image>
<name>apps/native-image</name>
<builder>paketobuildpacks/builder:tiny</builder> <builder>paketobuildpacks/builder:tiny</builder>
<env> <env>
<BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE> <BP_NATIVE_IMAGE>false</BP_NATIVE_IMAGE>
</env> </env>
</image> </image>
</configuration> </configuration>
@ -102,41 +104,9 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*Tests.java</include>
<include>**/*Test.java</include>
</includes>
<excludes>
<exclude>**/Abstract*.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins> </plugins>
</build> </build>
<repositories> <repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot-local</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone-local</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository> <repository>
<id>spring-releases</id> <id>spring-releases</id>
<name>Spring Releases</name> <name>Spring Releases</name>
@ -147,46 +117,27 @@
</repository> </repository>
</repositories> </repositories>
<pluginRepositories> <pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot-local</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone-local</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository> <pluginRepository>
<id>spring-releases</id> <id>spring-releases</id>
<name>Spring Releases</name> <name>Spring Releases</name>
<url>https://repo.spring.io/libs-release-local</url> <url>https://repo.spring.io/release</url>
<snapshots> <snapshots>
<enabled>false</enabled> <enabled>false</enabled>
</snapshots> </snapshots>
</pluginRepository> </pluginRepository>
</pluginRepositories> </pluginRepositories>
<profiles> <profiles>
<profile> <profile>
<id>native</id> <id>native</id>
<properties> <properties>
<repackage.classifier>exec</repackage.classifier> <repackage.classifier>exec</repackage.classifier>
<native-buildtools.version>0.9.0</native-buildtools.version> <native-buildtools.version>0.9.9</native-buildtools.version>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.graalvm.buildtools</groupId> <groupId>org.junit.platform</groupId>
<artifactId>junit-platform-native</artifactId> <artifactId>junit-platform-launcher</artifactId>
<version>${native-buildtools.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
@ -196,6 +147,7 @@
<groupId>org.graalvm.buildtools</groupId> <groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId> <artifactId>native-maven-plugin</artifactId>
<version>${native-buildtools.version}</version> <version>${native-buildtools.version}</version>
<extensions>true</extensions>
<executions> <executions>
<execution> <execution>
<id>test-native</id> <id>test-native</id>

View File

@ -0,0 +1,43 @@
package echo;
import org.springframework.cloud.function.cloudevent.CloudEventMessageBuilder;
import org.springframework.cloud.function.web.util.HeaderUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;
import java.net.URI;
import java.util.UUID;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.springframework.cloud.function.cloudevent.CloudEventMessageUtils.*;
@Component("echo")
public class EchoFunction implements Function<Message<String>, Message<String>> {
private static final Logger LOGGER = Logger.getLogger(
EchoFunction.class.getName());
@Override
public Message<String> apply(Message<String> inputMessage) {
/*
* YOUR CODE HERE
*
* Try running `mvn test`. Add more test as you code in `test/java/echo/SpringCloudEventsApplicationTests`.
*/
HttpHeaders httpHeaders = HeaderUtils.fromMessage(inputMessage.getHeaders());
LOGGER.log(Level.INFO, "Input CE Id:{0}", httpHeaders.getFirst(ID));
LOGGER.log(Level.INFO, "Input CE Spec Version:{0}", httpHeaders.getFirst(SPECVERSION));
LOGGER.log(Level.INFO, "Input CE Source:{0}", httpHeaders.getFirst(SOURCE));
LOGGER.log(Level.INFO, "Input CE Subject:{0}", httpHeaders.getFirst(SUBJECT));
String input = inputMessage.getPayload();
LOGGER.log(Level.INFO, "Input {0} ", input);
return CloudEventMessageBuilder.withData(input)
.setType("Echo").setId(UUID.randomUUID().toString())
.setSubject("Echo event")
.setSource(URI.create("http://example.com/echo")).build();
}
}

View File

@ -0,0 +1,11 @@
package echo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringCloudEventsApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudEventsApplication.class, args);
}
}

View File

@ -1,13 +0,0 @@
package functions;
public class Input {
public String input;
@Override
public String toString() {
return "Input{" +
"input='" + input + '\'' +
'}';
}
}

View File

@ -1,19 +0,0 @@
package functions;
public class Output {
public String input;
public String operation;
public String output;
public String error;
@Override
public String toString() {
return "Output{" +
"input='" + input + '\'' +
", operation='" + operation + '\'' +
", output='" + output + '\'' +
", error='" + error + '\'' +
'}';
}
}

View File

@ -1,81 +0,0 @@
package functions;
import static org.springframework.cloud.function.cloudevent.CloudEventMessageUtils.ID;
import static org.springframework.cloud.function.cloudevent.CloudEventMessageUtils.SOURCE;
import static org.springframework.cloud.function.cloudevent.CloudEventMessageUtils.SPECVERSION;
import static org.springframework.cloud.function.cloudevent.CloudEventMessageUtils.SUBJECT;
import java.util.UUID;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.function.cloudevent.CloudEventHeaderEnricher;
import org.springframework.cloud.function.web.util.HeaderUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpHeaders;
import org.springframework.messaging.Message;
@SpringBootApplication
public class SpringCloudEventsApplication {
private static final Logger LOGGER = Logger.getLogger(
SpringCloudEventsApplication.class.getName());
public static void main(String[] args) {
SpringApplication.run(SpringCloudEventsApplication.class, args);
}
@Bean
public Function<Message<Input>, Output> uppercase(CloudEventHeaderEnricher enricher) {
return m -> {
HttpHeaders httpHeaders = HeaderUtils.fromMessage(m.getHeaders());
LOGGER.log(Level.INFO, "Input CE Id:{0}", httpHeaders.getFirst(
ID));
LOGGER.log(Level.INFO, "Input CE Spec Version:{0}",
httpHeaders.getFirst(SPECVERSION));
LOGGER.log(Level.INFO, "Input CE Source:{0}",
httpHeaders.getFirst(SOURCE));
LOGGER.log(Level.INFO, "Input CE Subject:{0}",
httpHeaders.getFirst(SUBJECT));
Input input = m.getPayload();
LOGGER.log(Level.INFO, "Input {0} ", input);
Output output = new Output();
output.input = input.input;
output.operation = httpHeaders.getFirst(SUBJECT);
output.output = input.input != null ? input.input.toUpperCase() : "NO DATA";
return output;
};
}
@Bean
public CloudEventHeaderEnricher attributesProvider() {
return attributes -> attributes
.setSpecVersion("1.0")
.setId(UUID.randomUUID()
.toString())
.setSource("http://example.com/uppercase")
.setType("com.redhat.faas.springboot.events");
}
/**
* Health checks
*
* @return
*/
@Bean
public Function<String, String> health() {
return probe -> {
if ("readiness".equals(probe)) {
return "ready";
} else if ("liveness".equals(probe)) {
return "live";
} else {
return "OK";
}
};
}
}

View File

@ -1,4 +1,4 @@
package functions; package echo;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
@ -30,26 +30,21 @@ public class SpringCloudEventsApplicationTests {
@Autowired @Autowired
private TestRestTemplate rest; private TestRestTemplate rest;
@Autowired
ObjectMapper objectMapper = new ObjectMapper();
@Test @Test
public void testUpperCaseJsonInput() throws Exception { public void testEchoInput() throws Exception {
Input input = new Input(); String input ="hello";
input.input = "hello";
HttpHeaders ceHeaders = new HttpHeaders(); HttpHeaders ceHeaders = new HttpHeaders();
ceHeaders.add(SPECVERSION, "1.0"); ceHeaders.add(SPECVERSION, "1.0");
ceHeaders.add(ID, UUID.randomUUID() ceHeaders.add(ID, UUID.randomUUID()
.toString()); .toString());
ceHeaders.add(TYPE, "com.redhat.faas.springboot.test"); ceHeaders.add(TYPE, "MyEvent");
ceHeaders.add(SOURCE, "http://localhost:8080/uppercase"); ceHeaders.add(SOURCE, "http://localhost:8080/echo");
ceHeaders.add(SUBJECT, "Convert to UpperCase"); ceHeaders.add(SUBJECT, "Echo content");
ResponseEntity<String> response = this.rest.exchange( ResponseEntity<String> response = this.rest.exchange(
RequestEntity.post(new URI("/uppercase")) RequestEntity.post(new URI("/echo"))
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.headers(ceHeaders) .headers(ceHeaders)
.body(input), .body(input),
@ -59,12 +54,33 @@ public class SpringCloudEventsApplicationTests {
.value(), equalTo(200)); .value(), equalTo(200));
String body = response.getBody(); String body = response.getBody();
assertThat(body, notNullValue()); assertThat(body, notNullValue());
Output output = objectMapper.readValue(body, assertThat(body, equalTo(input));
Output.class); }
assertThat(output, notNullValue());
assertThat(output.input, equalTo("hello")); @Test
assertThat(output.operation, equalTo("Convert to UpperCase")); public void testEchoRoutingBasedOnType() throws Exception {
assertThat(output.output, equalTo("HELLO"));
assertThat(output.error, nullValue()); String input ="hello";
HttpHeaders ceHeaders = new HttpHeaders();
ceHeaders.add(SPECVERSION, "1.0");
ceHeaders.add(ID, UUID.randomUUID()
.toString());
ceHeaders.add(TYPE, "MyEvent");
ceHeaders.add(SOURCE, "http://localhost:8080/echo");
ceHeaders.add(SUBJECT, "Echo content");
ResponseEntity<String> response = this.rest.exchange(
RequestEntity.post(new URI("/"))
.contentType(MediaType.APPLICATION_JSON)
.headers(ceHeaders)
.body(input),
String.class);
assertThat(response.getStatusCode()
.value(), equalTo(200));
String body = response.getBody();
assertThat(body, notNullValue());
assertThat(body, equalTo(input));
} }
} }

File diff suppressed because it is too large Load Diff