Skip to content

Commit

Permalink
add: Monitor microservice
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioscardace committed Jun 11, 2024
1 parent 33b6623 commit d0e28d5
Show file tree
Hide file tree
Showing 18 changed files with 619 additions and 0 deletions.
85 changes: 85 additions & 0 deletions monitor/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
<relativePath/>
</parent>

<groupId>netwatch</groupId>
<artifactId>monitor</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>monitor</name>
<description>Microservice for Checking Services Health.</description>

<properties>
<java.version>21</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20231013</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<source>21</source>
<target>21</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>

</project>
21 changes: 21 additions & 0 deletions monitor/src/main/java/netwatch/monitor/MonitorApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package netwatch.monitor;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.client.RestTemplate;

@EnableScheduling
@SpringBootApplication
public class MonitorApplication {

public static void main(String[] args) {
SpringApplication.run(MonitorApplication.class, args);
}

@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
20 changes: 20 additions & 0 deletions monitor/src/main/java/netwatch/monitor/config/RabbitMqConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package netwatch.monitor.config;

import org.springframework.amqp.core.FanoutExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

// This class configures a Fanout Exchange for broadcasting messages to all bound queues.
// @author Antonio Scardace
// @version 1.0

@Configuration
public class RabbitMqConfig {

private static final String EXCHANGE_NAME = System.getenv("RABBITMQ_EXCHANGE");

@Bean
public FanoutExchange fanoutExchange() {
return new FanoutExchange(EXCHANGE_NAME);
}
}
46 changes: 46 additions & 0 deletions monitor/src/main/java/netwatch/monitor/config/RedisConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package netwatch.monitor.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import netwatch.monitor.entities.ObservationId;

// Configuration class for connecting to Redis and for using RedisTemplate.
// @author Antonio Scardace
// @version 1.0

@Configuration
public class RedisConfig {

@Value("${spring.redis.host}")
private String redisHost;

@Value("${spring.redis.port}")
private int redisPort;

@Value("${spring.redis.password}")
private String redisPassword;

@Bean
public LettuceConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(redisHost, redisPort);
config.setPassword(redisPassword);
return new LettuceConnectionFactory(config);
}

@Bean
public RedisTemplate<String, ObservationId> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, ObservationId> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(ObservationId.class));
return redisTemplate;
}
}
21 changes: 21 additions & 0 deletions monitor/src/main/java/netwatch/monitor/entities/Contact.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package netwatch.monitor.entities;

import java.io.Serializable;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

// This class represents the "contacts" entity in the database.
// The Primary Key is the field "value".
// @author Antonio Scardace
// @version 1.0

@NoArgsConstructor
@AllArgsConstructor
@Data
public class Contact implements Serializable {

private String value;
private String type;
}
25 changes: 25 additions & 0 deletions monitor/src/main/java/netwatch/monitor/entities/Observation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package netwatch.monitor.entities;

import java.io.Serializable;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

// This class represents the "observes" entity in the database.
// The Primary Key is the composite field "observationId" defined by the Embedded class {@link ObservationId}.
// It represents the Many-To-Many association table between "utilities" and "contacts" tables.
// @author Antonio Scardace
// @version 1.0

@NoArgsConstructor
@AllArgsConstructor
@Data
public class Observation implements Serializable {

private ObservationId observationId;

private Utility utility;
private Contact contact;
private String environment;
}
22 changes: 22 additions & 0 deletions monitor/src/main/java/netwatch/monitor/entities/ObservationId.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package netwatch.monitor.entities;

import java.io.Serializable;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

// It represents a composite Primary Key for the entity {@link Observation} of the table "observes".
// It is composed by "address" and "contact" fields. They are both Foreign Keys.
// Respectively, they are the utility address and its referent contact.
// @author Antonio Scardace
// @version 1.0

@NoArgsConstructor
@AllArgsConstructor
@Data
public class ObservationId implements Serializable {

private String address;
private String contact;
}
22 changes: 22 additions & 0 deletions monitor/src/main/java/netwatch/monitor/entities/Utility.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package netwatch.monitor.entities;

import java.io.Serializable;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

// This class represents the "utilities" entity in the database.
// The Primary Key is the field "address".
// @author Antonio Scardace
// @version 1.0

@NoArgsConstructor
@AllArgsConstructor
@Data
public class Utility implements Serializable {

private String address;
private String name;
private String type;
}
9 changes: 9 additions & 0 deletions monitor/src/main/java/netwatch/monitor/requests/IRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package netwatch.monitor.requests;

// Interface defining methods to handle address-related operations.
// @author Antonio Scardace
// @version 1.0

public interface IRequest {
public Boolean request(String address);
}
34 changes: 34 additions & 0 deletions monitor/src/main/java/netwatch/monitor/requests/PingRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package netwatch.monitor.requests;

import java.io.IOException;
import java.net.InetAddress;

import lombok.extern.java.Log;

// Implementation of {@link IRequest} for handling ping-related operations.
// It supports IPv4 and IPv6 addresses. The use of their machine name is discouraged.
// The timeout (the time before the call aborts) is set to 4000ms (4 seconds).
// @author Antonio Scardace
// @version 1.0

@Log
public class PingRequest implements IRequest {

private final Integer timeoutMs;

public PingRequest() {
this.timeoutMs = 4000;
}

@Override
public Boolean request(String ip) {
try {
InetAddress address = InetAddress.getByName(ip);
return address.isReachable(this.timeoutMs);
}
catch(IOException e) {
log.warning(e.getMessage());
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package netwatch.monitor.requests;

// Factory class to create {@link IRequest} instances based on the type.
// Factory-Method Design Pattern used to make the Request component modular.
// @author Antonio Scardace
// @version 1.0

public class RequestCreator {

private RequestCreator() {

}

public static IRequest getRequest(String type) {
if (type.equals("ip")) return new PingRequest();
throw new IllegalArgumentException("Unknown Request type " + type);
}
}
Loading

0 comments on commit d0e28d5

Please sign in to comment.