Skip to content

Commit

Permalink
spring boot 3, java 21 and swagger upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
musab.bozkurt committed Feb 7, 2024
1 parent b8e0047 commit 916329c
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 121 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ci-maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ jobs:
permissions: write-all

steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
- uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '17'
java-version: '21'
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn -B package --file pom.xml

- name: Build and Push Docker Image
uses: mr-smithers-excellent/docker-build-push@v5
uses: mr-smithers-excellent/docker-build-push@v6
with:
image: musabbozkurt/github-projects
tags: kafka-debezium-service-v1, kafka-debezium-service-latest
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM openjdk:17-jdk-slim
FROM openjdk:21-jdk-slim
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} kafka-debezium-service.jar
ENTRYPOINT ["java","-jar","/kafka-debezium-service.jar"]
60 changes: 12 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
### Prerequisites

* Docker should be installed
* Java 17+ should be installed
* pgAdmin/DBeaver can be installed (Optional)
* Postman can be installed (Optional)
* Java 21 should be installed --> `export JAVA_HOME=$(/usr/libexec/java_home -v 21)`

-----

### `docker-compose` contains the followings

* Zookeeper: ```http://localhost:2181```
* Kafka Nodes: ```http://localhost:9092```,```http://localhost:9093```
* Kafdrop, a UI tool to visualize topics and messages on: `http://localhost:9000/`
* PostgreSQL DB connection details
* `POSTGRES_USER: postgres`
* `POSTGRES_PASSWORD: postgres`
* `Zookeeper`: http://localhost:2181
* `Kafka Nodes`: http://localhost:9092,http://localhost:9093
* `Kafdrop`, a UI tool to visualize topics and messages on: http://localhost:9000
* `PostgreSQL` DB connection details
* `User: postgres`
* `Password: postgres`
* `Database: postgres`
* `Port: 5433`

-----
Expand All @@ -24,9 +23,7 @@
* First way
* Run the following script --> [./scripts/run.sh](scripts%2Frun.sh)
* Second way
* Run the following command to start the services --> ```docker-compose up -d```
* Run the following command to use Java 17 (Or any java version installed on your system which should be
17+) --> `export JAVA_HOME=$(/usr/libexec/java_home -v 17)`
* Run `docker-compose up -d` command to start the services `(OPTIONAL)`
* Run `mvn clean install` or `mvn clean package`
* Run `mvn spring-boot:run` or `./mvnw spring-boot:run`
* Third way
Expand All @@ -35,36 +32,16 @@

-----

### Swagger and Actuator
### Check and Test REST APIs via Swagger and Actuator

* Swagger Url: http://localhost:8001/swagger-ui/index.html
* Actuator Url: http://localhost:8001/actuator

-----

### The following REST APIs can be tested via Swagger

* GET ```http://localhost:8001/producer``` --> check health of producer
* GET ```http://localhost:8001/consumer``` --> check health of consumer
* GET ```http://localhost:8001/consumer/orders``` --> retrieve List of unprocessed messages from orders topic
* POST ```http://localhost:8001/producer/orders``` --> publish messages

```
curl -X 'POST' \
'http://localhost:8001/producer/orders' \
-H 'accept: */*' \
-H 'Content-Type: application/json' \
-d '"hello world"'
```
-----
### How to test Debezium

* Connect to PostgreSQL with the following connection details
* `POSTGRES_USER: postgres`
* `POSTGRES_PASSWORD: postgres`
* `Port: 5433`
* Connect to `PostgreSQL` with the provided connection details above
* Run the following query to make sure there are records

```sql
Expand Down Expand Up @@ -92,23 +69,10 @@

For further reference, please consider the following sections:

* [Official Apache Maven documentation](https://maven.apache.org/guides/index.html)
* [Spring Boot Maven Plugin Reference Guide](https://docs.spring.io/spring-boot/docs/3.1.3/maven-plugin/reference/html/)
* [Create an OCI image](https://docs.spring.io/spring-boot/docs/3.1.3/maven-plugin/reference/html/#build-image)
* [Spring Web](https://docs.spring.io/spring-boot/docs/3.1.3/reference/htmlsingle/index.html#web)
* [Spring for Apache Kafka](https://docs.spring.io/spring-boot/docs/3.1.3/reference/htmlsingle/index.html#messaging.kafka)
* [Kafka Local](https://developer.confluent.io/quickstart/kafka-local/)
* [Run Debezium Kafka Connect using Docker | Kafka | Zookeeper | Kafdrop | Docker Compose](https://www.youtube.com/watch?v=yZy4QZYMUrY)
* [Debezium connector for PostgreSQL](https://debezium.io/documentation/reference/stable/connectors/postgresql.html)
* [Setting Up a Local Kafka Using Testcontainers in Spring Boot](https://medium.com/@truongbui95/setting-up-a-local-kafka-using-testcontainers-in-spring-boot-bb41466751e6)
* [Posting Request Body with Curl [Curl/Bash Code]](https://reqbin.com/req/curl/c-d2nzjn3z/curl-post-body)

-----

### Guides

The following guides illustrate how to use some features concretely:

* [Building a RESTful Web Service](https://spring.io/guides/gs/rest-service/)
* [Serving Web Content with Spring MVC](https://spring.io/guides/gs/serving-web-content/)
* [Building REST services with Spring](https://spring.io/guides/tutorials/rest/)]
-----
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ services:
restart: "no"
environment:
KAFKA_BROKERCONNECT: "kafka1:29092,kafka2:29093"
JVM_OPTS: "-Xms16M -Xmx512M -Xss180K -XX:-TieredCompilation -XX:+UseStringDeduplication -noverify"
ports:
- "9000:9000"
depends_on:
Expand Down
80 changes: 29 additions & 51 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,35 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.3</version>
<version>3.2.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>com.mb</groupId>
<artifactId>kafka-debezium-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>kafka-debezium-service</name>
<description>Spring Boot project for Kafka, Debezium and PostgreSQL integration</description>

<properties>
<java.version>17</java.version>
<testcontainers.version>1.18.3</testcontainers.version>
<testcontainers-junit-jupiter.version>1.18.3</testcontainers-junit-jupiter.version>
<testcontainers-kafka.version>1.18.3</testcontainers-kafka.version>
<springdoc-openapi-starter-webmvc-ui.version>2.2.0</springdoc-openapi-starter-webmvc-ui.version>

<java.version>21</java.version>
<testcontainers.version>1.19.4</testcontainers.version>
<springdoc-openapi-starter-webmvc-ui.version>2.3.0</springdoc-openapi-starter-webmvc-ui.version>
<dockerfile-maven-plugin.version>1.4.13</dockerfile-maven-plugin.version>
<registry>${env.CI_REGISTRY}/${env.CI_REGION}/${project.artifactId}</registry>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
Expand All @@ -42,76 +44,52 @@
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>${springdoc-openapi-starter-webmvc-ui.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<scope>test</scope>
</dependency>

<!-- Swagger -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-common</artifactId>
</dependency>

<!-- Testcontainers -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>kafka</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<!-- Swagger -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>${springdoc-openapi-starter-webmvc-ui.version}</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-common</artifactId>
<version>${springdoc-openapi-starter-webmvc-ui.version}</version>
</dependency>
<!-- Testcontainers -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers-junit-jupiter.version}</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>kafka</artifactId>
<version>${testcontainers-kafka.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-docker-compose</artifactId>
</dependency>

</dependencies>

<build>
<plugins>
Expand All @@ -127,7 +105,7 @@
</excludes>
</configuration>
</plugin>

<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
Expand Down
9 changes: 1 addition & 8 deletions scripts/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,7 @@ echo "BASE_SCRIPTS_DIR: $BASE_SCRIPTS_DIR"

echo "Current JAVA_HOME: $JAVA_HOME"

# docker-compose -f docker-compose.yml down
# echo "docker-compose down is run..."

docker-compose -f docker-compose.yml up -d
echo "docker-compose up is run..."
logInfo "docker-compose up is run test..."

export JAVA_HOME=$(/usr/libexec/java_home -v 17)
export JAVA_HOME=$(/usr/libexec/java_home -v 21)
echo "Current JAVA_HOME after export: $JAVA_HOME"

sleep 10
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mb.kafkadebeziumservice.api.controller;

import com.mb.kafkadebeziumservice.service.ConsumerService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
Expand All @@ -19,12 +20,14 @@ public class ConsumerController {
private final ConsumerService consumerService;

@GetMapping
@Operation(summary = "Check health of consumer")
public ResponseEntity<String> health() {
log.info("Consumer health is called.");
return ResponseEntity.ok("Consumer service is up.");
}

@GetMapping("/orders")
@Operation(summary = "Retrieve List of unprocessed messages from orders topic")
public ResponseEntity<List<String>> consumeOrders() {
return ResponseEntity.ok(consumerService.consumeOrders());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mb.kafkadebeziumservice.api.controller;

import com.mb.kafkadebeziumservice.service.ProducerService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
Expand All @@ -15,15 +16,16 @@ public class ProducerController {
private final ProducerService producerService;

@GetMapping
@Operation(summary = "Check health of producer")
public ResponseEntity<String> health() {
log.info("Producer health is called.");
return ResponseEntity.ok("Producer is running.");
}

@PostMapping("/{topic}")
@Operation(summary = "Publish messages. {topic} should be obtained from application.yml")
public ResponseEntity<String> publishToTopic(@PathVariable("topic") final String topic, @RequestBody String message) {
producerService.publishMessage(topic, message);
return ResponseEntity.ok("Message is published.");
}

}
7 changes: 7 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ spring:
serialization:
indent_output: true

docker:
compose:
enabled: true
lifecycle-management: start_only
readiness:
wait: never

springdoc:
api-docs:
path: /api-docs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ public static void main(String[] args) {
.initializers(new LocalKafkaInitializer())
.run(args);
}

}
Loading

0 comments on commit 916329c

Please sign in to comment.