From 9a9bbb13afe5bea41db8e099eb6c47cf3f3a724c Mon Sep 17 00:00:00 2001 From: Joe Di Pol Date: Thu, 4 Mar 2021 16:39:31 -0800 Subject: [PATCH 01/11] Add multiple port examples for WebServer and MicroProfile --- examples/microprofile/multiport/README.md | 40 ++++++ examples/microprofile/multiport/pom.xml | 92 +++++++++++++ .../examples/microprofile/multiport/Main.java | 44 ++++++ .../multiport/PrivateApplication.java | 34 +++++ .../multiport/PrivateResource.java | 33 +++++ .../multiport/PublicApplication.java | 33 +++++ .../multiport/PublicResource.java | 33 +++++ .../microprofile/multiport/package-info.java | 19 +++ .../src/main/resources/META-INF/beans.xml | 25 ++++ .../src/main/resources/application.yaml | 32 +++++ .../microprofile/multiport/MainTest.java | 110 +++++++++++++++ examples/webserver/multiport/README.md | 38 ++++++ examples/webserver/multiport/pom.xml | 84 ++++++++++++ .../examples/webserver/multiport/Main.java | 125 ++++++++++++++++++ .../webserver/multiport/package-info.java | 16 +++ .../META-INF/native-image/reflect-config.json | 1 + .../src/main/resources/application.yaml | 23 ++++ .../src/main/resources/logging.properties | 34 +++++ .../webserver/multiport/MainTest.java | 123 +++++++++++++++++ 19 files changed, 939 insertions(+) create mode 100644 examples/microprofile/multiport/README.md create mode 100644 examples/microprofile/multiport/pom.xml create mode 100644 examples/microprofile/multiport/src/main/java/io/helidon/examples/microprofile/multiport/Main.java create mode 100644 examples/microprofile/multiport/src/main/java/io/helidon/examples/microprofile/multiport/PrivateApplication.java create mode 100644 examples/microprofile/multiport/src/main/java/io/helidon/examples/microprofile/multiport/PrivateResource.java create mode 100644 examples/microprofile/multiport/src/main/java/io/helidon/examples/microprofile/multiport/PublicApplication.java create mode 100644 examples/microprofile/multiport/src/main/java/io/helidon/examples/microprofile/multiport/PublicResource.java create mode 100644 examples/microprofile/multiport/src/main/java/io/helidon/examples/microprofile/multiport/package-info.java create mode 100644 examples/microprofile/multiport/src/main/resources/META-INF/beans.xml create mode 100644 examples/microprofile/multiport/src/main/resources/application.yaml create mode 100644 examples/microprofile/multiport/src/test/java/io/helidon/examples/microprofile/multiport/MainTest.java create mode 100644 examples/webserver/multiport/README.md create mode 100644 examples/webserver/multiport/pom.xml create mode 100644 examples/webserver/multiport/src/main/java/io/helidon/examples/webserver/multiport/Main.java create mode 100644 examples/webserver/multiport/src/main/java/io/helidon/examples/webserver/multiport/package-info.java create mode 100644 examples/webserver/multiport/src/main/resources/META-INF/native-image/reflect-config.json create mode 100644 examples/webserver/multiport/src/main/resources/application.yaml create mode 100644 examples/webserver/multiport/src/main/resources/logging.properties create mode 100644 examples/webserver/multiport/src/test/java/io/helidon/examples/webserver/multiport/MainTest.java diff --git a/examples/microprofile/multiport/README.md b/examples/microprofile/multiport/README.md new file mode 100644 index 00000000000..d78c3434fb3 --- /dev/null +++ b/examples/microprofile/multiport/README.md @@ -0,0 +1,40 @@ +# Helidon MicroProfile Multiple Port Example + +It is common when deploying a microservice to run your service on +multiple ports so that you can control the visibility of your +service's endpoints. For example you might want to use three ports: + +- 8080: public REST endpoints of application +- 8081: private REST endpoints of application +- 8082: admin endpoints for health, metrics, etc. + +This lets you expose only the public endpoints via your +ingress controller or load balancer. + +This example shows a Helidon JAX-RS application running on three ports +as described above. + +The ports are configured in `application.yaml` by using named sockets. + +Two Applications are defined, each associated with a different socket +using @RoutingName. + +## Build and run + +With JDK11+ +```bash +mvn package +java -jar target/helidon-examples-microprofile-multiport.jar +``` + +## Exercise the application + +``` +curl -X GET http://localhost:8080/public/hello + +curl -X GET http://localhost:8081/private/hello + +curl -X GET http://localhost:8082/health + +curl -X GET http://localhost:8082/metrics +``` diff --git a/examples/microprofile/multiport/pom.xml b/examples/microprofile/multiport/pom.xml new file mode 100644 index 00000000000..321681d8776 --- /dev/null +++ b/examples/microprofile/multiport/pom.xml @@ -0,0 +1,92 @@ + + + + 4.0.0 + + io.helidon.applications + helidon-mp + 2.3.0-SNAPSHOT + ../../../applications/mp/pom.xml + + io.helidon.examples.microprofile + helidon-examples-microprofile-multiport + Helidon Microprofile Multiple Port Example + + + io.helidon.examples.microprofile.multiport.Main + + + + + io.helidon.microprofile.bundles + helidon-microprofile-core + + + io.helidon.config + helidon-config-yaml + + + io.helidon.microprofile.metrics + helidon-microprofile-metrics + + + io.helidon.microprofile.health + helidon-microprofile-health + + + org.junit.jupiter + junit-jupiter-api + test + + + org.hamcrest + hamcrest-all + test + + + org.junit.jupiter + junit-jupiter-params + test + + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-libs + + + + + org.jboss.jandex + jandex-maven-plugin + + + make-index + + + + + + + diff --git a/examples/microprofile/multiport/src/main/java/io/helidon/examples/microprofile/multiport/Main.java b/examples/microprofile/multiport/src/main/java/io/helidon/examples/microprofile/multiport/Main.java new file mode 100644 index 00000000000..04c9e4c3098 --- /dev/null +++ b/examples/microprofile/multiport/src/main/java/io/helidon/examples/microprofile/multiport/Main.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2021 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.helidon.examples.microprofile.multiport; + +import io.helidon.config.Config; +import io.helidon.microprofile.server.Server; + +/** + * Runs helidon on two ports - one for admin, one for business logic. + */ +public final class Main { + private static Server server; + + private Main() { + } + + /** + * Run the MP application on multiple ports. + * @param args ignored + */ + public static void main(String[] args) { + server = Server.builder() + .config(Config.create()) + .build() + .start(); + } + + static Server server() { + return server; + } +} diff --git a/examples/microprofile/multiport/src/main/java/io/helidon/examples/microprofile/multiport/PrivateApplication.java b/examples/microprofile/multiport/src/main/java/io/helidon/examples/microprofile/multiport/PrivateApplication.java new file mode 100644 index 00000000000..b39f1fa4fef --- /dev/null +++ b/examples/microprofile/multiport/src/main/java/io/helidon/examples/microprofile/multiport/PrivateApplication.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2021 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.helidon.examples.microprofile.multiport; + +import io.helidon.microprofile.server.RoutingName; + +import javax.enterprise.context.ApplicationScoped; +import javax.ws.rs.core.Application; +import java.util.Set; + +/** + * Application to expose private resource. + */ +@ApplicationScoped +@RoutingName("private") +public class PrivateApplication extends Application { + @Override + public Set> getClasses() { + return Set.of(PrivateResource.class); + } +} diff --git a/examples/microprofile/multiport/src/main/java/io/helidon/examples/microprofile/multiport/PrivateResource.java b/examples/microprofile/multiport/src/main/java/io/helidon/examples/microprofile/multiport/PrivateResource.java new file mode 100644 index 00000000000..3a8c9daf928 --- /dev/null +++ b/examples/microprofile/multiport/src/main/java/io/helidon/examples/microprofile/multiport/PrivateResource.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2021 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.helidon.examples.microprofile.multiport; + +import javax.enterprise.context.RequestScoped; +import javax.ws.rs.GET; +import javax.ws.rs.Path; + +/** + * Simple resource. + */ +@RequestScoped +@Path("/private") +public class PrivateResource { + @Path("/hello") + @GET + public String helloWorld() { + return "Private Hello World!!"; + } +} diff --git a/examples/microprofile/multiport/src/main/java/io/helidon/examples/microprofile/multiport/PublicApplication.java b/examples/microprofile/multiport/src/main/java/io/helidon/examples/microprofile/multiport/PublicApplication.java new file mode 100644 index 00000000000..1ca417c527a --- /dev/null +++ b/examples/microprofile/multiport/src/main/java/io/helidon/examples/microprofile/multiport/PublicApplication.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2021 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.helidon.examples.microprofile.multiport; + +import java.util.Set; + +import javax.enterprise.context.ApplicationScoped; +import javax.ws.rs.core.Application; + +/** + * Application to expose public resource + */ +@ApplicationScoped +public class PublicApplication extends Application { + + @Override + public Set> getClasses() { + return Set.of(PublicResource.class); + } +} diff --git a/examples/microprofile/multiport/src/main/java/io/helidon/examples/microprofile/multiport/PublicResource.java b/examples/microprofile/multiport/src/main/java/io/helidon/examples/microprofile/multiport/PublicResource.java new file mode 100644 index 00000000000..b7a322d1225 --- /dev/null +++ b/examples/microprofile/multiport/src/main/java/io/helidon/examples/microprofile/multiport/PublicResource.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.helidon.examples.microprofile.multiport; + +import javax.enterprise.context.RequestScoped; +import javax.ws.rs.GET; +import javax.ws.rs.Path; + +/** + * Simple resource. + */ +@RequestScoped +@Path("/public") +public class PublicResource { + @Path("/hello") + @GET + public String helloWorld() { + return "Public Hello World!!"; + } +} diff --git a/examples/microprofile/multiport/src/main/java/io/helidon/examples/microprofile/multiport/package-info.java b/examples/microprofile/multiport/src/main/java/io/helidon/examples/microprofile/multiport/package-info.java new file mode 100644 index 00000000000..f438f5b6510 --- /dev/null +++ b/examples/microprofile/multiport/src/main/java/io/helidon/examples/microprofile/multiport/package-info.java @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2021 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * Application that exposes multiple ports. + */ +package io.helidon.examples.microprofile.multiport; diff --git a/examples/microprofile/multiport/src/main/resources/META-INF/beans.xml b/examples/microprofile/multiport/src/main/resources/META-INF/beans.xml new file mode 100644 index 00000000000..041b085a50b --- /dev/null +++ b/examples/microprofile/multiport/src/main/resources/META-INF/beans.xml @@ -0,0 +1,25 @@ + + + + + diff --git a/examples/microprofile/multiport/src/main/resources/application.yaml b/examples/microprofile/multiport/src/main/resources/application.yaml new file mode 100644 index 00000000000..96c6aaef969 --- /dev/null +++ b/examples/microprofile/multiport/src/main/resources/application.yaml @@ -0,0 +1,32 @@ +# +# Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +server: + port: 8080 + host: "localhost" + sockets: + private: + port: 8081 + bind-address: "localhost" + admin: + port: 8082 + bind-address: "localhost" + +# Metrics and health run on admin port +metrics: + routing: "admin" + +health: + routing: "admin" \ No newline at end of file diff --git a/examples/microprofile/multiport/src/test/java/io/helidon/examples/microprofile/multiport/MainTest.java b/examples/microprofile/multiport/src/test/java/io/helidon/examples/microprofile/multiport/MainTest.java new file mode 100644 index 00000000000..e75befa3d6f --- /dev/null +++ b/examples/microprofile/multiport/src/test/java/io/helidon/examples/microprofile/multiport/MainTest.java @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.helidon.examples.microprofile.multiport; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.Response; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +/** + * Unit test for {@link Main}. + */ +class MainTest { + private static final int PUBLIC_PORT = 8080; + private static final int PRIVATE_PORT = 8081; + private static final int ADMIN_PORT = 8082; + + private static Client client; + + @BeforeAll + static void initClass() { + Main.main(new String[0]); + client = ClientBuilder.newClient(); + } + + @AfterAll + static void destroyClass() { + Main.server().stop(); + } + + @Test + void testEndpoint() { + String base = "http://localhost:"; + Response response; + WebTarget baseTarget; + + // Probe PUBLIC port + baseTarget = client.target(base + PUBLIC_PORT); + response = baseTarget.path("/public/hello") + .request() + .get(); + assertThat("port " + PUBLIC_PORT + " should be serving public resource", + response.getStatusInfo().toEnum(), is(Response.Status.OK)); + assertThat("port " + PUBLIC_PORT + " should return public data", + response.readEntity(String.class), is("Public Hello World!!")); + + response = baseTarget.path("/private/hello") + .request() + .get(); + assertThat("port " + PUBLIC_PORT + " should NOT be serving private resource", + response.getStatusInfo().toEnum(), is(Response.Status.NOT_FOUND)); + + response = baseTarget.path("/health") + .request() + .get(); + assertThat("port " + PUBLIC_PORT + " should NOT be serving health", + response.getStatusInfo().toEnum(), is(Response.Status.NOT_FOUND)); + + response = baseTarget.path("/metrics") + .request() + .get(); + assertThat("port " + PUBLIC_PORT + " should NOT be serving metrics", + response.getStatusInfo().toEnum(), is(Response.Status.NOT_FOUND)); + + // Probe PRIVATE port + baseTarget = client.target(base + PRIVATE_PORT); + response = baseTarget.path("/private/hello") + .request() + .get(); + assertThat("port " + PRIVATE_PORT + " should be serving private resource", + response.getStatusInfo().toEnum(), is(Response.Status.OK)); + assertThat("port " + PRIVATE_PORT + " should return private data", + response.readEntity(String.class), is("Private Hello World!!")); + + // Probe ADMIN port + baseTarget = client.target(base + ADMIN_PORT); + response = baseTarget.path("/health") + .request() + .get(); + assertThat("port " + ADMIN_PORT + " should be serving health", + response.getStatusInfo().toEnum(), is(Response.Status.OK)); + + response = baseTarget.path("/metrics") + .request() + .get(); + assertThat("port " + ADMIN_PORT + " should be serving metrics", + response.getStatusInfo().toEnum(), is(Response.Status.OK)); + } +} \ No newline at end of file diff --git a/examples/webserver/multiport/README.md b/examples/webserver/multiport/README.md new file mode 100644 index 00000000000..37711db9dda --- /dev/null +++ b/examples/webserver/multiport/README.md @@ -0,0 +1,38 @@ +# Helidon WebServer Multiple Port Example + +It is common when deploying a microservice to run your service on +multiple ports so that you can control the visibility of your +service's endpoints. For example you might want to use three ports: + +- 8080: public REST endpoints of application +- 8081: private REST endpoints of application +- 8082: admin endpoints for health, metrics, etc. + +This lets you expose only the public endpoints via your +ingress controller or load balancer. + +This example shows a Helidon application running on three ports +as described above. + +The ports are configured in `application.yaml` by using named sockets. + +Seperate routing is defined for each named socket in `Main.java` + +## Build and run + +With JDK11+ +```bash +mvn package +java -jar target/helidon-examples-webserver-multiport.jar +``` +## Exercise the application + +``` +curl -X GET http://localhost:8080/public/hello + +curl -X GET http://localhost:8081/private/hello + +curl -X GET http://localhost:8082/health + +curl -X GET http://localhost:8082/metrics +``` diff --git a/examples/webserver/multiport/pom.xml b/examples/webserver/multiport/pom.xml new file mode 100644 index 00000000000..fcac806f4f1 --- /dev/null +++ b/examples/webserver/multiport/pom.xml @@ -0,0 +1,84 @@ + + + + 4.0.0 + + io.helidon.applications + helidon-se + 2.2.1 + + + io.helidon.examples.webserver + helidon-examples-webserver-multiport + 1.0-SNAPSHOT + myproject + + + io.helidon.examples.webserver.multiport.Main + + + + + io.helidon.webserver + helidon-webserver + + + io.helidon.config + helidon-config-yaml + + + io.helidon.health + helidon-health + + + io.helidon.health + helidon-health-checks + + + io.helidon.metrics + helidon-metrics + + + org.junit.jupiter + junit-jupiter-api + test + + + io.helidon.webclient + helidon-webclient + test + + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-libs + + + + + + diff --git a/examples/webserver/multiport/src/main/java/io/helidon/examples/webserver/multiport/Main.java b/examples/webserver/multiport/src/main/java/io/helidon/examples/webserver/multiport/Main.java new file mode 100644 index 00000000000..b8c3f61c994 --- /dev/null +++ b/examples/webserver/multiport/src/main/java/io/helidon/examples/webserver/multiport/Main.java @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2021 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.helidon.examples.webserver.multiport; + +import io.helidon.common.LogConfig; +import io.helidon.config.Config; +import io.helidon.health.HealthSupport; +import io.helidon.health.checks.HealthChecks; +import io.helidon.metrics.MetricsSupport; +import io.helidon.webserver.Routing; +import io.helidon.webserver.WebServer; + +/** + * The application main class. + */ +public final class Main { + + /** + * Cannot be instantiated. + */ + private Main() { + } + + /** + * Application main entry point. + * @param args command line arguments. + */ + public static void main(final String[] args) { + startServer(); + } + + /** + * Start the server. + * @return the created {@link WebServer} instance + */ + static WebServer startServer() { + // load logging configuration + LogConfig.configureRuntime(); + + // By default this will pick up application.yaml from the classpath + Config config = Config.create(); + + // Build server using three ports: + // default public port, admin port, private port + WebServer server = WebServer.builder(createPublicRouting()) + .config(config.get("server")) + // Add a set of routes on the named socket "admin" + .addNamedRouting("admin", createAdminRouting()) + // Add a set of routes on the named socket "private" + .addNamedRouting("private", createPrivateRouting()) + .build(); + + // Try to start the server. If successful, print some info and arrange to + // print a message at shutdown. If unsuccessful, print the exception. + server.start() + .thenAccept(ws -> { + System.out.println( + "WEB server is up! http://localhost:" + ws.port()); + ws.whenShutdown().thenRun(() + -> System.out.println("WEB server is DOWN. Good bye!")); + }) + .exceptionally(t -> { + System.err.println("Startup failed: " + t.getMessage()); + t.printStackTrace(System.err); + return null; + }); + + // Server threads are not daemon. No need to block. Just react. + + return server; + } + + /** + * Creates private {@link Routing}. + * + * @return routing for use on "private" port + */ + private static Routing createPrivateRouting() { + return Routing.builder() + .get("/private/hello", (req, res) -> { res.send("Private Hello!!"); }) + .build(); + } + + /** + * Creates public {@link Routing}. + * + * @return routing for use on "public" port + */ + private static Routing createPublicRouting() { + return Routing.builder() + .get("/public/hello", (req, res) -> { res.send("Public Hello!!"); }) + .build(); + } + + /** + * Creates admin {@link Routing}. + * + * @return routing for use on admin port + */ + private static Routing createAdminRouting() { + MetricsSupport metrics = MetricsSupport.create(); + HealthSupport health = HealthSupport.builder() + .addLiveness(HealthChecks.healthChecks()) // Adds a convenient set of checks + .build(); + + return Routing.builder() + .register(health) + .register(metrics) + .build(); + } +} diff --git a/examples/webserver/multiport/src/main/java/io/helidon/examples/webserver/multiport/package-info.java b/examples/webserver/multiport/src/main/java/io/helidon/examples/webserver/multiport/package-info.java new file mode 100644 index 00000000000..7416b5ad2e0 --- /dev/null +++ b/examples/webserver/multiport/src/main/java/io/helidon/examples/webserver/multiport/package-info.java @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2021 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.helidon.examples.webserver.multiport; diff --git a/examples/webserver/multiport/src/main/resources/META-INF/native-image/reflect-config.json b/examples/webserver/multiport/src/main/resources/META-INF/native-image/reflect-config.json new file mode 100644 index 00000000000..fe51488c706 --- /dev/null +++ b/examples/webserver/multiport/src/main/resources/META-INF/native-image/reflect-config.json @@ -0,0 +1 @@ +[] diff --git a/examples/webserver/multiport/src/main/resources/application.yaml b/examples/webserver/multiport/src/main/resources/application.yaml new file mode 100644 index 00000000000..514c848a927 --- /dev/null +++ b/examples/webserver/multiport/src/main/resources/application.yaml @@ -0,0 +1,23 @@ +# +# Copyright (c) 2021 Oracle and/or its affiliates. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +server: + port: 8080 + host: 0.0.0.0 + sockets: + private: + port: 8081 + admin: + port: 8082 \ No newline at end of file diff --git a/examples/webserver/multiport/src/main/resources/logging.properties b/examples/webserver/multiport/src/main/resources/logging.properties new file mode 100644 index 00000000000..aced7e48602 --- /dev/null +++ b/examples/webserver/multiport/src/main/resources/logging.properties @@ -0,0 +1,34 @@ +# +# Copyright (c) 2021 Oracle and/or its affiliates. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# Example Logging Configuration File +# For more information see $JAVA_HOME/jre/lib/logging.properties + +# Send messages to the console +handlers=io.helidon.common.HelidonConsoleHandler + +# HelidonConsoleHandler uses a SimpleFormatter subclass that replaces "!thread!" with the current thread +java.util.logging.SimpleFormatter.format=%1$tY.%1$tm.%1$td %1$tH:%1$tM:%1$tS %4$s %3$s !thread!: %5$s%6$s%n + +# Global logging level. Can be overridden by specific loggers +.level=INFO + +# Component specific log levels +#io.helidon.webserver.level=INFO +#io.helidon.config.level=INFO +#io.helidon.security.level=INFO +#io.helidon.common.level=INFO +#io.netty.level=INFO diff --git a/examples/webserver/multiport/src/test/java/io/helidon/examples/webserver/multiport/MainTest.java b/examples/webserver/multiport/src/test/java/io/helidon/examples/webserver/multiport/MainTest.java new file mode 100644 index 00000000000..03653416d67 --- /dev/null +++ b/examples/webserver/multiport/src/test/java/io/helidon/examples/webserver/multiport/MainTest.java @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2021 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.helidon.examples.webserver.multiport; + +import java.util.concurrent.TimeUnit; + +import io.helidon.webclient.WebClient; +import io.helidon.webserver.WebServer; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +public class MainTest { + + private static WebServer webServer; + private static WebClient webClient; + private static final int PUBLIC_PORT = 8080; + private static final int PRIVATE_PORT = 8081; + private static final int ADMIN_PORT = 8082; + + @BeforeAll + public static void startTheServer() throws Exception { + webServer = Main.startServer(); + + long timeout = 2000; // 2 seconds should be enough to start the server + long now = System.currentTimeMillis(); + + while (!webServer.isRunning()) { + Thread.sleep(100); + if ((System.currentTimeMillis() - now) > timeout) { + Assertions.fail("Failed to start webserver"); + } + } + + webClient = WebClient.builder() + .build(); + } + + @AfterAll + public static void stopServer() throws Exception { + if (webServer != null) { + webServer.shutdown() + .toCompletableFuture() + .get(10, TimeUnit.SECONDS); + } + } + + @Test + public void portTest() throws Exception { + webClient.get() + .uri("http://localhost:" + PUBLIC_PORT) + .path("/public/hello") + .request(String.class) + .thenAccept(s -> Assertions.assertEquals("Public Hello!!", s)) + .toCompletableFuture() + .get(); + + webClient.get() + .uri("http://localhost:" + PUBLIC_PORT) + .path("/private/hello") + .request() + .thenAccept(response -> Assertions.assertEquals(404, response.status().code())) + .toCompletableFuture() + .get(); + + webClient.get() + .uri("http://localhost:" + PUBLIC_PORT) + .path("/health") + .request() + .thenAccept(response -> Assertions.assertEquals(404, response.status().code())) + .toCompletableFuture() + .get(); + + webClient.get() + .uri("http://localhost:" + PUBLIC_PORT) + .path("/metrics") + .request() + .thenAccept(response -> Assertions.assertEquals(404, response.status().code())) + .toCompletableFuture() + .get(); + + webClient.get() + .uri("http://localhost:" + PRIVATE_PORT) + .path("/private/hello") + .request(String.class) + .thenAccept(s -> Assertions.assertEquals("Private Hello!!", s)) + .toCompletableFuture() + .get(); + + webClient.get() + .uri("http://localhost:" + ADMIN_PORT) + .path("/health") + .request() + .thenAccept(response -> Assertions.assertEquals(200, response.status().code())) + .toCompletableFuture() + .get(); + + webClient.get() + .uri("http://localhost:" + ADMIN_PORT) + .path("/metrics") + .request() + .thenAccept(response -> Assertions.assertEquals(200, response.status().code())) + .toCompletableFuture() + .get(); + } + +} \ No newline at end of file From 16684eba74b2a7e97e1399b74a4b7a900c923c4b Mon Sep 17 00:00:00 2001 From: Joe Di Pol Date: Thu, 4 Mar 2021 16:51:01 -0800 Subject: [PATCH 02/11] Update poms for multiport examples --- examples/microprofile/multiport/pom.xml | 2 +- examples/microprofile/pom.xml | 1 + examples/webserver/multiport/pom.xml | 5 ++--- examples/webserver/pom.xml | 1 + 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/microprofile/multiport/pom.xml b/examples/microprofile/multiport/pom.xml index 321681d8776..0d2d5f41d66 100644 --- a/examples/microprofile/multiport/pom.xml +++ b/examples/microprofile/multiport/pom.xml @@ -26,7 +26,7 @@ io.helidon.examples.microprofile helidon-examples-microprofile-multiport - Helidon Microprofile Multiple Port Example + Helidon Microprofile Examples Multiple Ports io.helidon.examples.microprofile.multiport.Main diff --git a/examples/microprofile/pom.xml b/examples/microprofile/pom.xml index 0fb9eec65c5..514f3b6122e 100644 --- a/examples/microprofile/pom.xml +++ b/examples/microprofile/pom.xml @@ -44,5 +44,6 @@ messaging-sse cors tls + multiport diff --git a/examples/webserver/multiport/pom.xml b/examples/webserver/multiport/pom.xml index fcac806f4f1..cfbb7617067 100644 --- a/examples/webserver/multiport/pom.xml +++ b/examples/webserver/multiport/pom.xml @@ -23,13 +23,12 @@ io.helidon.applications helidon-se - 2.2.1 + 2.3.0-SNAPSHOT io.helidon.examples.webserver helidon-examples-webserver-multiport - 1.0-SNAPSHOT - myproject + Helidon WebServer Examples Multiple Ports io.helidon.examples.webserver.multiport.Main diff --git a/examples/webserver/pom.xml b/examples/webserver/pom.xml index fc2383b2fad..43a4ce81915 100644 --- a/examples/webserver/pom.xml +++ b/examples/webserver/pom.xml @@ -43,5 +43,6 @@ tls mutual-tls fault-tolerance + multiport From dc4fd0dcdaa8ec00cf74648ad5d720cc9ec50c98 Mon Sep 17 00:00:00 2001 From: Joe Di Pol Date: Fri, 5 Mar 2021 09:55:00 -0800 Subject: [PATCH 03/11] Fix application.yaml syntax. Fix copyright. Fix parent pom path. --- .../multiport/src/main/resources/application.yaml | 6 +++--- examples/webserver/multiport/pom.xml | 2 +- .../multiport/src/main/resources/application.yaml | 10 ++++++---- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/examples/microprofile/multiport/src/main/resources/application.yaml b/examples/microprofile/multiport/src/main/resources/application.yaml index 96c6aaef969..34af19c84ea 100644 --- a/examples/microprofile/multiport/src/main/resources/application.yaml +++ b/examples/microprofile/multiport/src/main/resources/application.yaml @@ -1,5 +1,5 @@ # -# Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -17,10 +17,10 @@ server: port: 8080 host: "localhost" sockets: - private: + - name: "private" port: 8081 bind-address: "localhost" - admin: + - name: "admin" port: 8082 bind-address: "localhost" diff --git a/examples/webserver/multiport/pom.xml b/examples/webserver/multiport/pom.xml index cfbb7617067..707555e2135 100644 --- a/examples/webserver/multiport/pom.xml +++ b/examples/webserver/multiport/pom.xml @@ -24,7 +24,7 @@ io.helidon.applications helidon-se 2.3.0-SNAPSHOT - + ../../../applications/se/pom.xml io.helidon.examples.webserver helidon-examples-webserver-multiport diff --git a/examples/webserver/multiport/src/main/resources/application.yaml b/examples/webserver/multiport/src/main/resources/application.yaml index 514c848a927..f77248bd3e1 100644 --- a/examples/webserver/multiport/src/main/resources/application.yaml +++ b/examples/webserver/multiport/src/main/resources/application.yaml @@ -15,9 +15,11 @@ # server: port: 8080 - host: 0.0.0.0 + host: "localhost" sockets: - private: + - name: "private" port: 8081 - admin: - port: 8082 \ No newline at end of file + bind-address: "localhost" + - name: "admin" + port: 8082 + bind-address: "localhost" From 0f310dc198fa34ce4869d5b6e69231061d79b02f Mon Sep 17 00:00:00 2001 From: Joe Di Pol Date: Fri, 5 Mar 2021 10:37:36 -0800 Subject: [PATCH 04/11] Update copyright --- .../multiport/src/main/resources/META-INF/beans.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/microprofile/multiport/src/main/resources/META-INF/beans.xml b/examples/microprofile/multiport/src/main/resources/META-INF/beans.xml index 041b085a50b..68ffb25b5ed 100644 --- a/examples/microprofile/multiport/src/main/resources/META-INF/beans.xml +++ b/examples/microprofile/multiport/src/main/resources/META-INF/beans.xml @@ -1,7 +1,7 @@