Skip to content

Commit 8a8680a

Browse files
committed
chore: added microprofile-liberty-redis-sentinel sample
Signed-off-by: Maximillian Arruda <[email protected]>
1 parent 1a95f51 commit 8a8680a

File tree

20 files changed

+1162
-0
lines changed

20 files changed

+1162
-0
lines changed

README.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ Open Liberty is a lightweight and highly flexible open-source application server
3131
| Simple CRUD REST application
3232
| Redis - Single node
3333

34+
| link:microprofile-liberty-redis-sentinel/[]
35+
| Simple CRUD REST application
36+
| Redis - Sentinel
37+
38+
3439
|===
3540

3641
== Wildfly
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
target/
2+
!target/*.war
3+
!target/liberty/wlp/usr/shared/resources/*
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
target/
2+
pom.xml.tag
3+
pom.xml.releaseBackup
4+
pom.xml.versionsBackup
5+
pom.xml.next
6+
release.properties
7+
dependency-reduced-pom.xml
8+
buildNumber.properties
9+
.mvn/timing.properties
10+
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
11+
.mvn/wrapper/maven-wrapper.jar
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.1/apache-maven-3.9.1-bin.zip
2+
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
FROM icr.io/appcafe/open-liberty:kernel-slim-java21-openj9-ubi-minimal
3+
4+
COPY --chown=1001:0 /src/main/liberty/config /config
5+
6+
RUN features.sh
7+
8+
COPY --chown=1001:0 target/*.war /config/apps
9+
10+
RUN configure.sh
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
= MicroProfile Open Liberty with Redis sample
2+
3+
This project is intent to be a sample for MicroProfile with MongoDB integration by using Jakarta NoSQL implementation.
4+
5+
== Setup Redis
6+
7+
image::https://jnosql.github.io/img/logos/redis.png[Redis Project,align="center" width=25%,height=25%]
8+
9+
https://redis.com/[Redis] is a software project that implements data structure servers.
10+
It is open-source, networked, in-memory, and stores keys with optional durability.
11+
12+
The project is configured to reach out a Redis instance with the following configuration:
13+
14+
[source, text]
15+
----
16+
host: localhost
17+
port: 6379
18+
----
19+
20+
This Eclipse JNoSQL configuration is defined on the file `src/main/resources/META-INF/microprofile-config.properties`. More info about it, check the following link: https://github.com/eclipse/jnosql-databases?tab=readme-ov-file#configuration-14
21+
22+
[source,properties]
23+
----
24+
include::src/main/resources/META-INF/microprofile-config.properties[]
25+
----
26+
*PS:warning:* Pay attention to that when you try to set up your own Redis instance.
27+
28+
== Using Docker Compose
29+
30+
The easier way to execute this project is to use the provided docker-compose.yaml file in the root directory.
31+
You can run it by performing the following command:
32+
33+
[source, bash]
34+
----
35+
docker-compose up -d
36+
----
37+
38+
== Local installation
39+
40+
Follow the instructions in: https://redis.io/docs/latest/operate/oss_and_stack/install/install-redis/
41+
42+
== Using Docker
43+
44+
1. Install docker: https://www.docker.com/
45+
2. https://hub.docker.com/_/redis
46+
3. Run docker command
47+
48+
[source, bash]
49+
----
50+
docker run -d --name redis-instance -p 6379:6379 redis
51+
----
52+
53+
== Run the code
54+
55+
The generation of the executable jar file can be performed by issuing the following command
56+
[source, bash]
57+
----
58+
mvn clean package
59+
----
60+
61+
This will create an executable jar file **microprofile-liberty-redis.jar** within the _target_ maven folder. This can be started by executing the following command
62+
63+
[source, bash]
64+
----
65+
java -jar target/microprofile-liberty-redis.jar
66+
----
67+
68+
=== Liberty Dev Mode
69+
70+
During development, you can use Liberty's development mode (dev mode) to code while observing and testing your changes on the fly.
71+
With the dev mode, you can code along and watch the change reflected in the running server right away;
72+
unit and integration tests are run on pressing Enter in the command terminal; you can attach a debugger to the running server at any time to step through your code.
73+
74+
[source, bash]
75+
----
76+
mvn liberty:dev
77+
----
78+
79+
80+
81+
82+
To launch the test page, open your browser at the following URL
83+
84+
[source, text]
85+
----
86+
http://localhost:9080/index.html
87+
----
88+
89+
90+
=== Specification examples
91+
92+
By default, there is always the creation of a JAX-RS application class to define the path on which the JAX-RS endpoints are available.
93+
94+
Also, a simple Hello world endpoint is created, have a look at the class **HelloController**.
95+
96+
More information on MicroProfile can be found [here](https://microprofile.io/)
97+
98+
99+
=== Config
100+
101+
Configuration of your application parameters. Specification [here](https://microprofile.io/project/eclipse/microprofile-config)
102+
103+
The example class **ConfigTestController** shows you how to inject a configuration parameter and how you can retrieve it programmatically.
104+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
After you generate a starter project, these instructions will help you with what to do next.
2+
3+
The Open Liberty starter gives you a simple, quick way to get the necessary files to start building
4+
an application on Open Liberty. There is no need to search how to find out what to add to your
5+
Maven build files. A simple RestApplication.java file is generated for you to start
6+
creating a REST based application. A server.xml configuration file is provided with the necessary
7+
features for the MicroProfile and Jakarta EE versions that you previously selected.
8+
9+
If you plan on developing and/or deploying your app in a containerized environment, the included
10+
Dockerfile will make it easier to create your application image on top of the Open Liberty Docker
11+
image.
12+
13+
1) Once you download the starter project, unpackage the .zip file on your machine.
14+
2) Open a command line session, navigate to the installation directory, and run `./mvnw liberty:dev` (Linux/Mac) or `mvnw liberty:dev` (Windows).
15+
This will install all required dependencies and start the default server. When complete, you will
16+
see the necessary features installed and the message "server is ready to run a smarter planet."
17+
18+
For information on developing your application in dev mode using Maven, see the
19+
dev mode documentation (https://openliberty.io/docs/latest/development-mode.html).
20+
21+
For further help on getting started actually developing your application, see some of our
22+
MicroProfile guides (https://openliberty.io/guides/?search=microprofile&key=tag) and Jakarta EE
23+
guides (https://openliberty.io/guides/?search=jakarta%20ee&key=tag).
24+
25+
If you have problems building the starter project, make sure the Java SE version on your
26+
machine matches the Java SE version you picked from the Open Liberty starter on the downloads
27+
page (https://openliberty.io/downloads/). You can test this with the command `java -version`.
28+
29+
Open Liberty performs at its best when running using Open J9 which can be obtained via IBM Semeru
30+
(https://developer.ibm.com/languages/java/semeru-runtimes/downloads/). For a full list of supported
31+
Java SE versions and where to obtain them, reference the Java SE support page
32+
(https://openliberty.io/docs/latest/java-se.html).
33+
34+
If you find any issues with the starter project or have recommendations to improve it, open an
35+
issue in the starter GitHub repo (https://github.com/OpenLiberty/start.openliberty.io).
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
version: '3'
2+
services:
3+
redis-master:
4+
container_name: "redis-master"
5+
image: redis
6+
command: redis-server
7+
ports:
8+
- "6379:6379"
9+
volumes:
10+
- redis-master:/data
11+
12+
redis-slave1:
13+
container_name: "redis-slave1"
14+
image: redis
15+
ports:
16+
- "6380:6379"
17+
command: redis-server --slaveof redis-master 6379
18+
depends_on:
19+
- redis-master
20+
volumes:
21+
- redis-slave1:/data
22+
23+
redis-slave2:
24+
container_name: "redis-slave2"
25+
image: redis
26+
ports:
27+
- "6381:6379"
28+
command: redis-server --slaveof redis-master 6379
29+
depends_on:
30+
- redis-master
31+
volumes:
32+
- redis-slave2:/data
33+
34+
redis-sentinel1:
35+
container_name: 'redis-sentinel1'
36+
image: redis
37+
ports:
38+
- "26379:26379"
39+
command: >
40+
sh -c 'echo "bind 0.0.0.0" > /etc/sentinel.conf &&
41+
echo "sentinel monitor mymaster redis-master 6379 2" >> /etc/sentinel.conf &&
42+
echo "sentinel resolve-hostnames yes" >> /etc/sentinel.conf &&
43+
echo "sentinel down-after-milliseconds mymaster 10000" >> /etc/sentinel.conf &&
44+
echo "sentinel failover-timeout mymaster 10000" >> /etc/sentinel.conf &&
45+
echo "sentinel parallel-syncs mymaster 1" >> /etc/sentinel.conf &&
46+
redis-sentinel /etc/sentinel.conf'
47+
depends_on:
48+
- redis-master
49+
- redis-slave1
50+
- redis-slave2
51+
52+
redis-sentinel2:
53+
container_name: 'redis-sentinel2'
54+
image: redis
55+
ports:
56+
- "26380:26379"
57+
command: >
58+
sh -c 'echo "bind 0.0.0.0" > /etc/sentinel.conf &&
59+
echo "sentinel monitor mymaster redis-master 6379 2" >> /etc/sentinel.conf &&
60+
echo "sentinel resolve-hostnames yes" >> /etc/sentinel.conf &&
61+
echo "sentinel down-after-milliseconds mymaster 10000" >> /etc/sentinel.conf &&
62+
echo "sentinel failover-timeout mymaster 10000" >> /etc/sentinel.conf &&
63+
echo "sentinel parallel-syncs mymaster 1" >> /etc/sentinel.conf &&
64+
redis-sentinel /etc/sentinel.conf'
65+
depends_on:
66+
- redis-master
67+
- redis-slave1
68+
- redis-slave2
69+
70+
redis-sentinel3:
71+
container_name: 'redis-sentinel3'
72+
image: redis
73+
ports:
74+
- "26381:26379"
75+
command: >
76+
sh -c 'echo "bind 0.0.0.0" > /etc/sentinel.conf &&
77+
echo "sentinel monitor mymaster redis-master 6379 2" >> /etc/sentinel.conf &&
78+
echo "sentinel resolve-hostnames yes" >> /etc/sentinel.conf &&
79+
echo "sentinel down-after-milliseconds mymaster 10000" >> /etc/sentinel.conf &&
80+
echo "sentinel failover-timeout mymaster 10000" >> /etc/sentinel.conf &&
81+
echo "sentinel parallel-syncs mymaster 1" >> /etc/sentinel.conf &&
82+
redis-sentinel /etc/sentinel.conf'
83+
depends_on:
84+
- redis-master
85+
- redis-slave1
86+
- redis-slave2
87+
88+
volumes:
89+
redis-master:
90+
driver: local
91+
redis-slave1:
92+
driver: local
93+
redis-slave2:
94+
driver: local

0 commit comments

Comments
 (0)