Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ivangfr/springboot-kafka-websocket

The goal of this project is to implement two Spring Boot applications: bitcoin-api and bitcoin-client. The bitcoin-api application simulates BTC price changes, while the bitcoin-client application listens to these changes and updates a real-time UI. The bitcoin-client UI is secured using Basic Authentication.
https://github.com/ivangfr/springboot-kafka-websocket

basic-authentication docker java jib kafka mysql spring-boot spring-cloud-stream spring-data-jpa spring-security springdoc-openapi thymeleaf web websocket

Last synced: 15 days ago
JSON representation

The goal of this project is to implement two Spring Boot applications: bitcoin-api and bitcoin-client. The bitcoin-api application simulates BTC price changes, while the bitcoin-client application listens to these changes and updates a real-time UI. The bitcoin-client UI is secured using Basic Authentication.

Awesome Lists containing this project

README

        

= springboot-kafka-websocket

The goal of this project is to implement two https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/[`String Boot`] applications: `bitcoin-api` and `bitcoin-client`. The `bitcoin-api` application simulates `BTC` price changes, while the `bitcoin-client` application listens to these changes and updates a real-time UI. The `bitcoin-client` UI is secured using Basic Authentication.

== Proof-of-Concepts & Articles

On https://ivangfr.github.io:[ivangfr.github.io], I have compiled my Proof-of-Concepts (PoCs) and articles. You can easily search for the technology you are interested in by using the filter. Who knows, perhaps I have already implemented a PoC or written an article about what you are looking for.

== Additional Readings

* [Medium]: https://medium.com/@ivangfr/building-a-web-chat-with-social-login-using-spring-boot-introduction-644702e6be8e[**Building a Web Chat with Social Login using Spring Boot: Introduction**]
* [Medium]: https://medium.com/javarevisited/implementing-a-kafka-producer-and-consumer-using-spring-cloud-stream-d4b9a6a9eab1[**Implementing a Kafka Producer and Consumer using Spring Cloud Stream**]
* [Medium]: https://medium.com/javarevisited/implementing-unit-tests-for-a-kafka-producer-and-consumer-that-uses-spring-cloud-stream-f7a98a89fcf2[**Implementing Unit Tests for a Kafka Producer and Consumer that uses Spring Cloud Stream**]
* [Medium]: https://medium.com/javarevisited/implementing-end-to-end-testing-for-a-kafka-producer-and-consumer-that-uses-spring-cloud-stream-fbf5e666899e[**Implementing End-to-End testing for a Kafka Producer and Consumer that uses Spring Cloud Stream**]
* [Medium]: https://medium.com/javarevisited/configuring-distributed-tracing-with-zipkin-in-a-kafka-producer-and-consumer-that-uses-spring-cloud-9f1e55468b9e[**Configuring Distributed Tracing with Zipkin in a Kafka Producer and Consumer that uses Spring Cloud Stream**]
* [Medium]: https://medium.com/@ivangfr/using-cloudevents-in-a-kafka-producer-and-consumer-that-uses-spring-cloud-stream-9c51670b5566[**Using Cloudevents in a Kafka Producer and Consumer that uses Spring Cloud Stream**]

== Project Diagram

image::./documentation/project-diagram.jpeg[]

== Applications

* *bitcoin-api*
+
`Spring Boot` Web Java application service that simulates `BTC` price changes and pushes those changes to https://kafka.apache.org/[`Kafka`]

* *bitcoin-client*
+
`Spring Boot` Web Java application that was implemented using `Thymeleaf` as HTML template. It reads from `Kafka` and updates its UI using `Websocket`. It has also a chat where users can talk to each other, by sending messages publicly or privately.

== Prerequisites

* https://www.oracle.com/java/technologies/downloads/#java17[`Java 17+`]
* https://www.docker.com/[`Docker`]

== Start Environment

* Open a terminal and inside `springboot-kafka-websocket` root folder run:
+
[source]
----
docker compose up -d
----

* Wait for Docker containers to be up and running. To check it, run:
+
[source]
----
docker compose ps
----

== Running applications with Maven

Inside `springboot-kafka-websocket` root folder, run the following `Maven` commands in different terminals:

* *bitcoin-api*
+
[source]
----
./mvnw clean spring-boot:run --projects bitcoin-api -Dspring-boot.run.jvmArguments="-Dserver.port=9081"
----

* *bitcoin-client*
+
[source]
----
./mvnw clean spring-boot:run --projects bitcoin-client -Dspring-boot.run.jvmArguments="-Dserver.port=9082"
----

== Running Applications as Docker containers

=== Build Application's Docker Image

* In a terminal, make sure you are in `springboot-kafka-websocket` root folder;

* In order to build the application docker images, run the following script:
+
[source]
----
./docker-build.sh
----

=== Application's Environment Variables

* *bitcoin-api*
+
|===
|Environment Variable | Description

|`MYSQL_HOST`
|Specify host of the `MySQL` database to use (default `localhost`)

|`MYSQL_PORT`
|Specify port of the `MySQL` database to use (default `3306`)

|`KAFKA_HOST`
|Specify host of the `Kafka` message broker to use (default `localhost`)

|`KAFKA_PORT`
|Specify port of the `Kafka` message broker to use (default `29092`)
|===

* *bitcoin-client*
+
|===
|Environment Variable | Description

|`KAFKA_HOST`
|Specify host of the `Kafka` message broker to use (default `localhost`)

|`KAFKA_PORT`
|Specify port of the `Kafka` message broker to use (default `29092`)
|===

=== Start Application's Docker container

* In a terminal, make sure you are inside `springboot-kafka-websocket` root folder;

* Run following script:
+
[source]
----
./start-apps.sh
----

== Applications URLs

|===
|Application |URL |Credentials (user/pass)

|bitcoin-api
|http://localhost:9081/swagger-ui.html
|

|bitcoin-client
|http://localhost:9082
|`user1/123` or `user2/123`
|===

The gif below shows two users checking real-time the `BTC` price changes. Besides, they are using a chat channel to communicate with each other.

image::./documentation/two-users-example.gif[]

== Useful Links & Commands

* *Kafdrop*
+
`Kafdrop` can be accessed at http://localhost:9000

* *MySQL*
+
[source]
----
docker exec -it -e MYSQL_PWD=secret mysql mysql -uroot --database bitcoindb
select * from prices;
----

== Shutdown

* To stop applications
** If they were started with `Maven`, go to the terminals where they are running and press `Ctrl+C`;
** If they were started as Docker containers, go to a terminal and, inside `springboot-kafka-websocket` root folder, run the script below:
+
[source]
----
./stop-apps.sh
----
* To stop and remove docker compose containers, network and volumes, go to a terminal and, inside `springboot-kafka-websocket` root folder, run the following command:
+
[source]
----
docker compose down -v
----

== Cleanup

To remove the Docker images created by this project, go to a terminal and, inside `springboot-kafka-websocket` root folder, run the script below:
[source]
----
./remove-docker-images.sh
----