Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Containerising App and verify Mongo and Kafka connections #16

Merged
merged 4 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ RUN apt-get update && apt-get install -y telnet
WORKDIR /app

# Copy the jar file
COPY target/kafkaStudy-0.0.1-SNAPSHOT.jar .
COPY target/kafkaStudy-0.0.1-SNAPSHOT.jar app.jar

# Command to run the application
CMD ["java", "-jar", "kafkaStudy-0.0.1-SNAPSHOT.jar"]
CMD ["java", "-jar", "app.jar"]
46 changes: 42 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
version: '2'
version: '3.8' # Specify the version of the Compose file

networks:
app-network: # Define the network here
driver: bridge

services:
app:
build:
context: . # Build from the current directory, where Dockerfile is located
dockerfile: Dockerfile # Specify the Dockerfile
container_name: kafka-study-app

ports:
- "8080:8080" # Expose the app on localhost:8080
depends_on:
- kafka
- mongodb
command: ["java", "-jar", "/app/app.jar"] # Command to run the app
networks:
- app-network

zookeeper:
image: wurstmeister/zookeeper:latest
ports:
- "2181:2181"
container_name: zookeeper
networks:
- app-network

kafka:
image: wurstmeister/kafka:latest
ports:
Expand All @@ -24,17 +46,23 @@ services:
- zookeeper
volumes:
- /var/run/docker.sock:/var/run/docker.sock
networks:
- app-network

mongodb:
image: mongo:latest
container_name: mongodb-container
container_name: mongodb
ports:
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

INSIDE Listener:
Purpose: This is used for communication between Kafka brokers and clients (like producers and consumers) that are within the Docker network. It is typically configured to listen on a specific internal IP address or hostname.
Example Usage: If you have other services running in the same Docker network (e.g., your Spring Boot application), they will connect to Kafka using the INSIDE listener.
OUTSIDE Listener:
Purpose: This listener is exposed to the outside world, allowing clients outside of the Docker network (such as applications running on your local machine) to connect to Kafka. This typically listens on localhost or a specific external IP address.
Example Usage: If you are testing Kafka from your local machine or another service outside the Docker network, you will connect using the OUTSIDE listener.

- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
volumes:
- mongo-data:/data/db
#sonar - code quality
networks:
- app-network

# SonarQube for code quality
sonarqube:
image: sonarqube
ports:
Expand All @@ -47,7 +75,10 @@ services:
- sonarqube_data:/opt/sonarqube/data
- sonarqube_logs:/opt/sonarqube/logs
- sonarqube_extensions:/opt/sonarqube/extensions
#Sonar-related postgres
networks:
- app-network

# SonarQube-related PostgreSQL database
postgres:
image: postgres
environment:
Expand All @@ -56,6 +87,8 @@ services:
POSTGRES_PASSWORD: sonar
volumes:
- postgres_data:/var/lib/postgresql/data
networks:
- app-network

prometheus:
image: prom/prometheus:latest
Expand All @@ -65,6 +98,8 @@ services:
- prometheus_data:/prometheus
ports:
- "9090:9090"
networks:
- app-network

grafana:
image: grafana/grafana:latest
Expand All @@ -75,6 +110,9 @@ services:
- GF_SECURITY_ADMIN_PASSWORD=admin
volumes:
- grafana-storage:/var/lib/grafana
networks:
- app-network

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grafana Working as expected:
image

volumes:
mongo-data:
sonarqube_data:
Expand Down
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@
<artifactId>sonar-maven-plugin</artifactId>
<version>4.0.0.4121</version>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
4 changes: 2 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ spring.application.name=KafkaStudy
kafka.regular.topic=my-topic
regular.kafka.autostart=false
reactive.kafka.autostart=true
spring.kafka.consumer.bootstrap-servers=127.0.0.1:9092
spring.kafka.consumer.bootstrap-servers=kafka:9092
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Able to produce message to Kafka and App is consuming data
image

App logs:
image

spring.kafka.consumer.group-id=demo-3
spring.kafka.consumer.auto-offset-reset=earliest
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.properties.session.timeout.ms=30000
spring.kafka.consumer.properties.heartbeat.interval.ms=10000
spring.data.mongodb.uri=mongodb://root:example@localhost:27017/testMongoDb?authSource=admin
spring.data.mongodb.uri=mongodb://root:example@mongodb:27017/testMongoDb?authSource=admin
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Data successfully saved to MongoDB.
image

mask.headers.list=pwd,pass
management.endpoints.web.exposure.include = *
Loading