Creating Your Custom Feedback Counter Service
Previously, you added “feedbackinput” object data and its API.
Here, you’ll add the simple Spring Boot application Kyle created to a Docker image, publish it, and create a new custom service using the image.
Adding Your Site’s Domain to the Feedback Counter Project
The Spring Boot project you downloaded previously works by showing feedback values retrieved via API calls from Liferay. A script on the page calls the microservice, which uses OAuth2 authorization to make the API calls for the object entry data from Liferay. The script uses this data to update the page.
It also uses an application.properties
file to register important values, including your Liferay instance’s domain. Complete the project by filling in your site’s domain.
In the folder you unzipped the feedback counter resources into, open the
src/main/resources/application.properties
file.Replace the placeholder value of the
liferay.host
property with your site’s domain (e.g.,www.delectablebonsai.com
):liferay.host=YOUR DOMAIN HERE
Now the project is complete and ready for you to build for publication.
Building the Project as an Executable JAR
As a Spring Boot application, Kyle’s project can be packaged into a self-contained, executable JAR file.
Run this command from the project’s base directory to build the JAR file:
./gradlew clean bootJar
The project builds and creates the spring-boot-webapp.jar
file in the project’s build/libs/
folder.
Creating the Dockerfile
You must create a Dockerfile
to build a Docker image that you can run in Liferay Cloud as a custom service.
In the same directory that you unzipped the project into, create a Dockerfile
so you can build a Docker image from this project.
touch Dockerfile
Liferay provides the jar-runner
base Docker image that you can use as a base image for these kinds of projects.
Open the Dockerfile
and fill in the file using jar-runner
as a base image.
Add
jar-runner
as the base image.FROM liferay/jar-runner:latest
Add a line to rename and copy your project’s JAR file into the Docker image in the expected directory (
/opt/liferay/
).COPY ./build/libs/spring-boot-webapp.jar /opt/liferay/jar-runner.jar
Add another line to expose port
8181
.EXPOSE 8181
Kyle’s application uses port
8181
to serve the HTML page, so you must expose that port when it’s running in Liferay Cloud.
Now your Dockerfile
has everything it needs to build an appropriate Docker image.
Building and Pushing the Project as a Docker Image
Use the Docker command line to build and publish your Docker image for your custom service to use.
Go to Docker Hub and add a new repository, named
feedbackcounter
.From your folder with the Spring Boot project, run this command to build the Docker image from the code, using your own Docker Hub username:
docker build -t USERNAME/feedbackcounter .
Log in to Docker Hub via the command line using your own credentials:
docker login -u USERNAME
Push the Docker image to your Docker Hub repository:
docker image push USERNAME/feedbackcounter
Now the Docker image is ready to be deployed as a custom service.
Creating a new Service in Your Repository
Next, open your project’s repository to create and deploy the custom service.
Create a new top-level folder for the new service, called
feedback
.mkdir feedback && cd feedback
Create a new
LCP.json
file for your service.touch LCP.json
Open the
LCP.json
file and add the key configurations defining the service’s type, identifier, and the Docker image to base it on:{ "kind": "Deployment", "id": "feedback", "image": "delectablebonsai/feedbackcounter:latest", }
Add configurations to specify the resources (memory in MB, CPUs, and number of nodes) the service needs:
"memory": 256, "cpu": 1, "scale": 1,
This service only needs minimal resources to run the simple Spring Boot application.
Add the network configuration needed to create an exposed endpoint to access your custom service externally.
"ports": [ { "port": 8181, "external": true } ],
Add the
liferay
service as a dependency, because the service requires access to Liferay’s API."dependencies": [ "liferay" ],
Add the last configuration to specify that this service is not valid for deployment to the
infra
environment. Sinceinfra
is a special environment for your CI (Continuous Integration) service, this service won’t run there."environments": { "infra": { "deploy": false } }
Add this new service’s
LCP.json
file to Git and commit it.git add LCP.json
git commit -m "Add a new custom counter service."
Now your new custom service is ready to run in Liferay Cloud. Next, you’ll deploy it to your UAT environment and see it working.