此内容没有您所选择的语言版本。
Chapter 3. Developing and deploying a Spring Boot runtime application
You can create new Spring Boot applications from scratch and deploy them to OpenShift. The recommended approach for specifying and using supported and tested Maven artifacts in a Spring Boot application is to use the OpenShift Application Runtimes Spring Boot BOM.
3.1. Developing Spring Boot application 复制链接链接已复制到粘贴板!
For a basic Spring Boot application, you need to create the following:
- A Java class containing Spring Boot methods.
-
A
pom.xmlfile containing information required by Maven to build the application.
The following procedure creates a simple Greeting application that returns "{"content":"Greetings!"}" as response.
For building and deploying your applications to OpenShift, Spring Boot 2.4 only supports builder images based on OpenJDK 8 and OpenJDK 11. Oracle JDK and OpenJDK 9 builder images are not supported.
Prerequisites
- OpenJDK 8 or OpenJDK 11 installed.
- Maven installed.
Procedure
Create a new directory
myApp, and navigate to it.mkdir myApp cd myApp
$ mkdir myApp $ cd myAppCopy to Clipboard Copied! Toggle word wrap Toggle overflow This is the root directory for the application.
Create directory structure
src/main/java/com/example/in the root directory, and navigate to it.mkdir -p src/main/java/com/example/ cd src/main/java/com/example/
$ mkdir -p src/main/java/com/example/ $ cd src/main/java/com/example/Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a Java class file
MyApp.javacontaining the application code.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a
pom.xmlfile in the application root directorymyAppwith the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Build the application using Maven from the root directory of the application.
mvn clean package -Popenshift -Ddekorate.deploy=true
$ mvn clean package -Popenshift -Ddekorate.deploy=trueCopy to Clipboard Copied! Toggle word wrap Toggle overflow Verify that the application is running.
Using
curlor your browser, verify your application is running athttp://localhost:8080.curl http://localhost:8080
$ curl http://localhost:8080 {"content":"Greetings!"}Copy to Clipboard Copied! Toggle word wrap Toggle overflow
3.2. Deploying Spring Boot application to OpenShift 复制链接链接已复制到粘贴板!
To deploy your Spring Boot application to OpenShift, configure the pom.xml to use the Dekorate Maven dependency.
You can specify the Dekorate Maven dependency in the pom.xml file as follows:
<dependency>
<groupId>io.dekorate</groupId>
<artifactId>openshift-spring-starter</artifactId>
</dependency>
<dependency>
<groupId>io.dekorate</groupId>
<artifactId>openshift-spring-starter</artifactId>
</dependency>
You can specify a Java image in the application.properties file as follows:
dekorate.s2i.builder-image=registry.access.redhat.com/ubi8/openjdk-8:1.3
dekorate.s2i.builder-image=registry.access.redhat.com/ubi8/openjdk-8:1.3
The images are available in the Red Hat Ecosystem Catalog.
3.2.1. Supported Java images for Spring Boot 复制链接链接已复制到粘贴板!
Spring Boot is certified and tested with various Java images that are available for different operating systems. For example, Java images are available for RHEL 7 and RHEL 8 with OpenJDK 8 or OpenJDK 11.
You require Docker or podman authentication to access the RHEL 8 images in the Red Hat Ecosystem Catalog.
The following table lists the OpenJDK images supported by Spring Boot for different architectures. These container images are available in the Red Hat Ecosystem Catalog. In the catalog, you can search and download the images listed in the table below. The image pages contain authentication procedures required to access the images.
| JDK (OS) | Architecture supported | Red Hat Ecosystem Catalog |
|---|---|---|
| OpenJDK8 (RHEL 7) | x86_64 | redhat-openjdk-18/openjdk18-openshift |
| OpenJDK11 (RHEL 7) | x86_64 | openjdk/openjdk-11-rhel7 |
| OpenJDK8 (RHEL 8) | x86_64 | ubi8/openjdk-8-runtime |
| OpenJDK11 (RHEL 8) | x86_64, IBM Z, and IBM Power Systems | ubi8/openjdk-11 |
The use of a RHEL 8-based container on a RHEL 7 host, for example with OpenShift 3 or OpenShift 4, has limited support. For more information, see the Red Hat Enterprise Linux Container Compatibility Matrix.
In the following procedure, a profile with Dekorate Maven dependency is used for building and deploying the application to OpenShift.
Prerequisites
- Maven is installed.
- Docker or podman authentication into Red Hat Ecosystem Catalog to access RHEL 8 images.
Procedure
Add the following content to the
pom.xmlfile in the application root directory:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Set the Java image in the
application.propertiesfile.x86_64 architecture
RHEL 7 with OpenJDK 8
dekorate.s2i.builder-image=registry.access.redhat.com/ubi7/openjdk-8:1.3
dekorate.s2i.builder-image=registry.access.redhat.com/ubi7/openjdk-8:1.3Copy to Clipboard Copied! Toggle word wrap Toggle overflow RHEL 7 with OpenJDK 11
dekorate.s2i.builder-image=registry.access.redhat.com/openjdk/openjdk-11-rhel7:latest
dekorate.s2i.builder-image=registry.access.redhat.com/openjdk/openjdk-11-rhel7:latestCopy to Clipboard Copied! Toggle word wrap Toggle overflow RHEL 8 with OpenJDK 8
dekorate.s2i.builder-image=registry.access.redhat.com/ubi8/openjdk-8:1.3
dekorate.s2i.builder-image=registry.access.redhat.com/ubi8/openjdk-8:1.3Copy to Clipboard Copied! Toggle word wrap Toggle overflow RHEL 8 with OpenJDK 11
dekorate.s2i.builder-image=registry.access.redhat.com/ubi8/openjdk-11:latest
dekorate.s2i.builder-image=registry.access.redhat.com/ubi8/openjdk-11:latestCopy to Clipboard Copied! Toggle word wrap Toggle overflow
x86_64, IBM Z, and IBM Power System architectures
RHEL 8 with OpenJDK 11
dekorate.s2i.builder-image=registry.access.redhat.com/ubi8/openjdk-11:latest
dekorate.s2i.builder-image=registry.access.redhat.com/ubi8/openjdk-11:latestCopy to Clipboard Copied! Toggle word wrap Toggle overflow
To deploy your Spring Boot application to OpenShift, you must perform the following:
- Log in to your OpenShift instance.
- Deploy the application to the OpenShift instance.
Prerequisites
-
ocCLI client installed. - Maven installed.
Procedure
Log in to your OpenShift instance with the
occlient.oc login ...
$ oc login ...Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a new project in the OpenShift instance.
oc new-project MY_PROJECT_NAME
$ oc new-project MY_PROJECT_NAMECopy to Clipboard Copied! Toggle word wrap Toggle overflow Deploy the application to OpenShift using Maven from the application’s root directory. The root directory of an application contains the
pom.xmlfile.mvn clean package -Popenshift -Ddekorate.deploy=true
$ mvn clean package -Popenshift -Ddekorate.deploy=trueCopy to Clipboard Copied! Toggle word wrap Toggle overflow This command uses Dekorate to launch the S2I process on OpenShift and start the pod.
Verify the deployment.
Check the status of your application and ensure your pod is running.
oc get pods -w
$ oc get pods -w NAME READY STATUS RESTARTS AGE MY_APP_NAME-1-aaaaa 1/1 Running 0 58s MY_APP_NAME-s2i-1-build 0/1 Completed 0 2mCopy to Clipboard Copied! Toggle word wrap Toggle overflow The
MY_APP_NAME-1-aaaaapod should have a status ofRunningonce it is fully deployed and started.Your specific pod name will vary.
Determine the route for the pod.
Example Route Information
oc get routes
$ oc get routes NAME HOST/PORT PATH SERVICES PORT TERMINATION MY_APP_NAME MY_APP_NAME-MY_PROJECT_NAME.OPENSHIFT_HOSTNAME MY_APP_NAME 8080Copy to Clipboard Copied! Toggle word wrap Toggle overflow The route information of a pod gives you the base URL which you use to access it.
In this example,
http://MY_APP_NAME-MY_PROJECT_NAME.OPENSHIFT_HOSTNAMEis the base URL to access the application.Verify that your application is running in OpenShift.
curl http://MY_APP_NAME-MY_PROJECT_NAME.OPENSHIFT_HOSTNAME
$ curl http://MY_APP_NAME-MY_PROJECT_NAME.OPENSHIFT_HOSTNAME {"content":"Greetings!"}Copy to Clipboard Copied! Toggle word wrap Toggle overflow
To deploy your Spring Boot application to stand-alone Red Hat Enterprise Linux, configure the pom.xml file in the application, package it using Maven and deploy using the java -jar command.
Prerequisites
- RHEL 7 or RHEL 8 installed.
For deploying your Spring Boot application to stand-alone Red Hat Enterprise Linux, you must first package the application using Maven.
Prerequisites
- Maven installed.
Procedure
Add the following content to the
pom.xmlfile in the application’s root directory:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Package your application using Maven.
mvn clean package
$ mvn clean packageCopy to Clipboard Copied! Toggle word wrap Toggle overflow The resulting JAR file is in the
targetdirectory.
To deploy your Spring Boot application to stand-alone Red Hat Enterprise Linux, use java -jar command.
Prerequisites
- RHEL 7 or RHEL 8 installed.
- OpenJDK 8 or OpenJDK 11 installed.
- A JAR file with the application.
Procedure
Deploy the JAR file with the application.
java -jar my-project-1.0.0.jar
$ java -jar my-project-1.0.0.jarCopy to Clipboard Copied! Toggle word wrap Toggle overflow Verify the deployment.
Use
curlor your browser to verify your application is running athttp://localhost:8080:curl http://localhost:8080
$ curl http://localhost:8080Copy to Clipboard Copied! Toggle word wrap Toggle overflow