Skip to content

Commit

Permalink
Merge pull request #22 from mairess/add-messaging-kafka
Browse files Browse the repository at this point in the history
Add messaging kafka
  • Loading branch information
mairess authored Jul 10, 2024
2 parents a836ee1 + 384a103 commit 90945f5
Show file tree
Hide file tree
Showing 20 changed files with 480 additions and 33 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ jobs:

validate-tests:
runs-on: ubuntu-latest

env:
MAIL_HOST: ${{ secrets.MAIL_HOST }}
MAIL_PORT: ${{ secrets.MAIL_PORT }}
MAIL_USERNAME: ${{ secrets.MAIL_USERNAME }}
MAIL_PASSWORD: ${{ secrets.MAIL_PASSWORD }}

steps:
- name: Checkout repository and switch to branch
uses: actions/checkout@v3
Expand All @@ -16,6 +23,18 @@ jobs:
distribution: 'temurin'
java-version: '17'

- name: Run Kafka KRaft Broker
uses: spicyparrot/kafka-kraft-action@v1.1.0
with:
kafka-version: "3.7.0"
kafka-topics: "installationCompleted.request,1"

env:
MAIL_HOST: ${{ secrets.MAIL_HOST }}
MAIL_PORT: ${{ secrets.MAIL_PORT }}
MAIL_USERNAME: ${{ secrets.MAIL_USERNAME }}
MAIL_PASSWORD: ${{ secrets.MAIL_PASSWORD }}

- name: Validate checkstyle
run: mvn checkstyle:check

Expand Down
80 changes: 70 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,40 @@ This is an internet provider management system.

<img src="/images/diagramaER.svg" alt="Description of image" style="height: 500px; width: 600px;" />

## Run project locally
## Run locally

> ⚠️ You need to have [Java](https://www.oracle.com/java/) installed on your machine.
To run this project locally, follow these steps:

> ⚠️ You need to have [Docker](https://www.docker.com/get-started/) installed on your machine.
### Prerequisites

Make sure you have the following installed on your machine:

[Java](https://www.oracle.com/java/)

[Docker](https://www.docker.com/get-started/)

[Apache kafka](https://kafka.apache.org/documentation/#quickstart)

Also, set up the following env variables for the mail service:

```
MAIL_HOST=smtp.domainExample.com
MAIL_PORT=your-port-number-like 000
MAIL_USERNAME=myMail@domainExample.com
MAIL_PASSWORD=your-app-password
```

### Steps:

1. Clone the repository:

```BASH
git clone git@github.com:mairess/w-net-internet.git

cd w-net-internet
```

2. Install dependencies:
Expand All @@ -26,41 +50,77 @@ git clone git@github.com:mairess/w-net-internet.git
mvn install -DskipTests
```

3. Start the database:
3. Start ZooKeeper:

```BASH
# Start ZooKeeper:

bin/zookeeper-server-start.sh config/zookeeper.properties
```

4. Start Kafka:

```BASH
# Start Kafka:

bin/kafka-server-start.sh config/server.properties
```

5. Start the database:

```BASH
docker compose up database -d --build
```

4. Run the API:
6. Run the API:

```BASH
mvn spring-boot:run
```

5. View available routes at:
7. Access the API documentation and available routes on your web browser at:

```BASH
http://localhost:8080/swagger-ui/index.html
```

## Run project with Docker
## Run with Docker

### Prerequisites

Make sure you have the following installed on your machine:

[Docker](https://www.docker.com/get-started/)

> ⚠️ You need to have [Docker](https://www.docker.com/get-started/) installed on your machine.
Also, set up the following env variables for the mail service:

```
MAIL_HOST=smtp.domainExample.com
MAIL_PORT=your-port-number-like 000
MAIL_USERNAME=myMail@domainExample.com
MAIL_PASSWORD=your-app-password
```

### Steps:

1. Clone the repository:

```BASH
git clone git@github.com:mairess/w-net-internet.git

cd w-net-internet
```

2. Start the API and the database:
2. Start application:

```BASH
docker compose up -d --build
```

3. View available routes at:
3. Access the API documentation and available routes on your web browser at:

```BASH
http://localhost:8080/swagger-ui/index.html
Expand Down
42 changes: 41 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ services:
- 8080:8080
environment:
POSTGRES_HOST: database
MAIL_HOST: ${MAIL_HOST}
MAIL_PORT: ${MAIL_PORT}
MAIL_USERNAME: ${MAIL_USERNAME}
MAIL_PASSWORD: ${MAIL_PASSWORD}
KAFKA_HOST: kafka
depends_on:
database:
condition: service_healthy
networks:
- wnet-net

database:
image: postgres
Expand All @@ -25,4 +32,37 @@ services:
healthcheck:
test: [ "CMD-SHELL", "pg_isready -U postgres" ]
timeout: 10s
retries: 5
retries: 5
networks:
- wnet-net

zookeeper:
container_name: zookeeper
image: "bitnami/zookeeper:latest"
networks:
- wnet-net
ports:
- "2181:2181"
environment:
- ALLOW_ANONYMOUS_LOGIN=yes

kafka:
container_name: kafka
image: "bitnami/kafka:latest"
ports:
- "9092:9092"
environment:
KAFKA_BROKER_ID: "1"
KAFKA_ZOOKEEPER_CONNECT: "zookeeper:2181"
KAFKA_ADVERTISED_HOST_NAME: "localhost"
KAFKA_ADVERTISED_LISTENERS: "PLAINTEXT://kafka:9092"
ALLOW_PLAINTEXT_LISTENER: "yes"
KAFKA_AUTO_CREATE_TOPICS_ENABLE: "true"
depends_on:
- zookeeper
networks:
- wnet-net

networks:
wnet-net:
driver: bridge
16 changes: 16 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@
</plugins>
</build>
<dependencies>
<!-- Messaging and queue -->
<dependency>
<artifactId>spring-kafka</artifactId>
<groupId>org.springframework.kafka</groupId>
</dependency>

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

<!-- validations -->
<dependency>
<artifactId>spring-boot-starter-validation</artifactId>
Expand Down Expand Up @@ -123,6 +134,11 @@
</dependency>

<!-- test -->
<dependency>
<artifactId>spring-kafka-test</artifactId>
<groupId>org.springframework.kafka</groupId>
<scope>test</scope>
</dependency>
<dependency>
<artifactId>spring-boot-starter-test</artifactId>
<groupId>org.springframework.boot</groupId>
Expand Down
68 changes: 68 additions & 0 deletions src/main/java/com/maires/wnet/configuration/ProducerConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.maires.wnet.configuration;

import java.util.Map;
import org.apache.kafka.clients.admin.NewTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.config.TopicBuilder;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;


/**
* The type Producer config.
*/
@Configuration
public class ProducerConfig {

private final KafkaProperties kafkaProperties;

@Value("${kafka.topic}")
private String newInstallationRequest;

/**
* Instantiates a new Producer config.
*
* @param kafkaProperties the kafka properties
*/
@Autowired
public ProducerConfig(KafkaProperties kafkaProperties) {
this.kafkaProperties = kafkaProperties;
}

/**
* Producer factory producer factory.
*
* @return the producer factory
*/
@Bean
public ProducerFactory<String, String> producerFactory() {
Map<String, Object> properties = kafkaProperties.buildProducerProperties();
return new DefaultKafkaProducerFactory<>(properties);
}

/**
* Kafka template kafka template.
*
* @return the kafka template
*/
@Bean
public KafkaTemplate<String, String> kafkaTemplate() {
return new KafkaTemplate<>(producerFactory());
}

/**
* Messaging request topic build new topic.
*
* @return the new topic
*/
@Bean
public NewTopic messagingRequestTopicBuild() {
return TopicBuilder.name(newInstallationRequest).partitions(1).replicas(1).build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.maires.wnet.consumer;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.maires.wnet.controller.dto.MessagingNewInstallationDto;
import com.maires.wnet.service.EmailService;
import jakarta.mail.MessagingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

/**
* The type New installation consumer.
*/
@Service
public class NewInstallationConsumer {

private final EmailService emailService;
private final ObjectMapper objectMapper;

/**
* Instantiates a new New installation consumer.
*
* @param emailService the email service
* @param objectMapper the object mapper
*/
@Autowired
public NewInstallationConsumer(EmailService emailService, ObjectMapper objectMapper) {
this.emailService = emailService;
this.objectMapper = objectMapper;
}

/**
* Consume installation message.
*
* @param message the message
* @throws MessagingException the messaging exception
* @throws JsonProcessingException the json processing exception
*/
@KafkaListener(
topics = "${kafka.topic}",
groupId = "message-request-consumer1"
)
public void consumeNewInstallationMessage(String message)
throws MessagingException, JsonProcessingException {

MessagingNewInstallationDto messagingNewInstallationDto = objectMapper.readValue(message,
MessagingNewInstallationDto.class);

emailService.sendNewInstallationMail(
messagingNewInstallationDto.customerMail(),
messagingNewInstallationDto.customerName()
);

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.maires.wnet.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.maires.wnet.controller.dto.AddressCreationDto;
import com.maires.wnet.controller.dto.AddressDto;
import com.maires.wnet.controller.dto.InstallationCreationDto;
Expand Down Expand Up @@ -100,7 +101,7 @@ public InstallationDto createAddressInstallation(
AddressNotFoundException,
EquipmentNotFoundException,
TechnicianNotFoundException,
PlanNotFoundException {
PlanNotFoundException, JsonProcessingException {

Installation newInstallation = addressService.createAddressInstallation(
addressId,
Expand Down
Loading

0 comments on commit 90945f5

Please sign in to comment.