Separate into files, prepare for Prometheus&Grafana
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
FROM ubuntu:latest
|
FROM ubuntu:latest
|
||||||
LABEL authors="lumijiez"
|
LABEL authors="lumijiez"
|
||||||
|
|
||||||
FROM maven:3.9.8-eclipse-temurin-21 AS build
|
FROM maven:3.9.9-eclipse-temurin-21 AS build
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,41 @@
|
|||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.rabbitmq</groupId>
|
||||||
|
<artifactId>amqp-client</artifactId>
|
||||||
|
<version>5.23.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
<artifactId>gson</artifactId>
|
||||||
|
<version>2.11.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
<version>1.7.32</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>ch.qos.logback</groupId>
|
||||||
|
<artifactId>logback-classic</artifactId>
|
||||||
|
<version>1.5.12</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>ch.qos.logback</groupId>
|
||||||
|
<artifactId>logback-core</artifactId>
|
||||||
|
<version>1.5.12</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-simple</artifactId>
|
||||||
|
<version>1.7.32</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@@ -24,6 +59,20 @@
|
|||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
|
<version>3.2.1</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>shade</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-jar-plugin</artifactId>
|
<artifactId>maven-jar-plugin</artifactId>
|
||||||
@@ -38,5 +87,4 @@
|
|||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package io.github.lumijiez;
|
||||||
|
|
||||||
|
import com.rabbitmq.client.Channel;
|
||||||
|
import com.rabbitmq.client.Connection;
|
||||||
|
import com.rabbitmq.client.ConnectionFactory;
|
||||||
|
import com.rabbitmq.client.DeliverCallback;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
public class BrokerConnector {
|
||||||
|
private static final String QUEUE_NAME = "random_json_queue";
|
||||||
|
private static final String RABBITMQ_HOST = "rabbitmq";
|
||||||
|
private static final String RABBITMQ_USER = "symphony";
|
||||||
|
private static final String RABBITMQ_PASSWORD = "symphony";
|
||||||
|
|
||||||
|
public static void connect() {
|
||||||
|
ScheduledExecutorService reconnectExecutor = Executors.newSingleThreadScheduledExecutor();
|
||||||
|
|
||||||
|
reconnectExecutor.scheduleWithFixedDelay(() -> {
|
||||||
|
try {
|
||||||
|
connectToRabbitMQ();
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("Awaiting broker connection: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}, 0, 5, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void connectToRabbitMQ() throws Exception {
|
||||||
|
ConnectionFactory factory = new ConnectionFactory();
|
||||||
|
factory.setHost(RABBITMQ_HOST);
|
||||||
|
factory.setUsername(RABBITMQ_USER);
|
||||||
|
factory.setPassword(RABBITMQ_PASSWORD);
|
||||||
|
|
||||||
|
try (Connection connection = factory.newConnection();
|
||||||
|
Channel channel = connection.createChannel()) {
|
||||||
|
|
||||||
|
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
|
||||||
|
System.out.println("Waiting for messages. To exit press CTRL+C");
|
||||||
|
|
||||||
|
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
|
||||||
|
String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
|
||||||
|
System.out.println("Received: " + message);
|
||||||
|
};
|
||||||
|
|
||||||
|
channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {});
|
||||||
|
|
||||||
|
Thread.currentThread().join();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
package io.github.lumijiez;
|
package io.github.lumijiez;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.print("Manager started!");
|
BrokerConnector.connect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -39,6 +39,12 @@
|
|||||||
<artifactId>logback-core</artifactId>
|
<artifactId>logback-core</artifactId>
|
||||||
<version>1.5.12</version>
|
<version>1.5.12</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-simple</artifactId>
|
||||||
|
<version>1.7.32</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -0,0 +1,83 @@
|
|||||||
|
package io.github.lumijiez;
|
||||||
|
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.gson.JsonPrimitive;
|
||||||
|
import com.rabbitmq.client.Channel;
|
||||||
|
import com.rabbitmq.client.Connection;
|
||||||
|
import com.rabbitmq.client.ConnectionFactory;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
public class BrokerConnector {
|
||||||
|
private static final String QUEUE_NAME = "random_json_queue";
|
||||||
|
private static final String RABBITMQ_HOST = "rabbitmq";
|
||||||
|
private static final String RABBITMQ_USER = "symphony";
|
||||||
|
private static final String RABBITMQ_PASSWORD = "symphony";
|
||||||
|
|
||||||
|
public static void connect() {
|
||||||
|
CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
|
||||||
|
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||||
|
System.out.println("Shutdown signal received.");
|
||||||
|
latch.countDown();
|
||||||
|
}));
|
||||||
|
|
||||||
|
boolean success = connectToRabbitMQ(latch);
|
||||||
|
System.out.println("Success: " + success);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean connectToRabbitMQ(CountDownLatch latch) {
|
||||||
|
ConnectionFactory factory = new ConnectionFactory();
|
||||||
|
factory.setHost(RABBITMQ_HOST);
|
||||||
|
factory.setUsername(RABBITMQ_USER);
|
||||||
|
factory.setPassword(RABBITMQ_PASSWORD);
|
||||||
|
|
||||||
|
try (Connection connection = factory.newConnection();
|
||||||
|
Channel channel = connection.createChannel();
|
||||||
|
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor()) {
|
||||||
|
|
||||||
|
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
|
||||||
|
System.out.println("Connected to RabbitMQ and queue declared.");
|
||||||
|
|
||||||
|
scheduler.scheduleAtFixedRate(() -> {
|
||||||
|
try {
|
||||||
|
String jsonMessage = generateRandomJson();
|
||||||
|
channel.basicPublish("", QUEUE_NAME, null, jsonMessage.getBytes(StandardCharsets.UTF_8));
|
||||||
|
System.out.println("Sent: " + jsonMessage);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Failed to send message: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}, 0, 10, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
latch.await();
|
||||||
|
scheduler.shutdown();
|
||||||
|
|
||||||
|
return scheduler.awaitTermination(5, TimeUnit.SECONDS);
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("Awaiting broker connection: " + e.getMessage());
|
||||||
|
try {
|
||||||
|
Thread.sleep(5000);
|
||||||
|
connectToRabbitMQ(latch);
|
||||||
|
} catch (InterruptedException ie) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String generateRandomJson() {
|
||||||
|
Random random = new Random();
|
||||||
|
JsonObject jsonObject = new JsonObject();
|
||||||
|
jsonObject.add("id", new JsonPrimitive(random.nextInt(1000)));
|
||||||
|
jsonObject.add("name", new JsonPrimitive("Item_" + random.nextInt(100)));
|
||||||
|
jsonObject.add("value", new JsonPrimitive(random.nextDouble() * 100));
|
||||||
|
|
||||||
|
return jsonObject.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,82 +1,8 @@
|
|||||||
package io.github.lumijiez;
|
package io.github.lumijiez;
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
|
||||||
import com.google.gson.JsonPrimitive;
|
|
||||||
import com.rabbitmq.client.Channel;
|
|
||||||
import com.rabbitmq.client.Connection;
|
|
||||||
import com.rabbitmq.client.ConnectionFactory;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.Random;
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import java.util.concurrent.CountDownLatch;
|
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
private static final String QUEUE_NAME = "random_json_queue";
|
|
||||||
private static final String RABBITMQ_HOST = "rabbitmq";
|
|
||||||
private static final String RABBITMQ_USER = "symphony";
|
|
||||||
private static final String RABBITMQ_PASSWORD = "symphony";
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
CountDownLatch latch = new CountDownLatch(1);
|
BrokerConnector.connect();
|
||||||
|
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
|
||||||
System.out.println("Shutdown signal received.");
|
|
||||||
latch.countDown();
|
|
||||||
}));
|
|
||||||
|
|
||||||
boolean success = connectToRabbitMQ(latch);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean connectToRabbitMQ(CountDownLatch latch) {
|
|
||||||
ConnectionFactory factory = new ConnectionFactory();
|
|
||||||
factory.setHost(RABBITMQ_HOST);
|
|
||||||
factory.setUsername(RABBITMQ_USER);
|
|
||||||
factory.setPassword(RABBITMQ_PASSWORD);
|
|
||||||
|
|
||||||
try (Connection connection = factory.newConnection();
|
|
||||||
Channel channel = connection.createChannel();
|
|
||||||
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor()) {
|
|
||||||
|
|
||||||
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
|
|
||||||
System.out.println("Connected to RabbitMQ and queue declared.");
|
|
||||||
|
|
||||||
scheduler.scheduleAtFixedRate(() -> {
|
|
||||||
try {
|
|
||||||
String jsonMessage = generateRandomJson();
|
|
||||||
channel.basicPublish("", QUEUE_NAME, null, jsonMessage.getBytes(StandardCharsets.UTF_8));
|
|
||||||
System.out.println("Sent: " + jsonMessage);
|
|
||||||
} catch (IOException e) {
|
|
||||||
System.err.println("Failed to send message: " + e.getMessage());
|
|
||||||
}
|
|
||||||
}, 0, 10, TimeUnit.SECONDS);
|
|
||||||
|
|
||||||
latch.await();
|
|
||||||
scheduler.shutdown();
|
|
||||||
|
|
||||||
return scheduler.awaitTermination(5, TimeUnit.SECONDS);
|
|
||||||
} catch (Exception e) {
|
|
||||||
System.err.println("RabbitMQ connection error: " + e.getMessage());
|
|
||||||
try {
|
|
||||||
Thread.sleep(5000);
|
|
||||||
connectToRabbitMQ(latch);
|
|
||||||
} catch (InterruptedException ie) {
|
|
||||||
Thread.currentThread().interrupt();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String generateRandomJson() {
|
|
||||||
Random random = new Random();
|
|
||||||
JsonObject jsonObject = new JsonObject();
|
|
||||||
jsonObject.add("id", new JsonPrimitive(random.nextInt(1000)));
|
|
||||||
jsonObject.add("name", new JsonPrimitive("Item_" + random.nextInt(100)));
|
|
||||||
jsonObject.add("value", new JsonPrimitive(random.nextDouble() * 100));
|
|
||||||
|
|
||||||
return jsonObject.toString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
2
config/rabbitmq/rabbit.conf
Normal file
2
config/rabbitmq/rabbit.conf
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
log.console = false
|
||||||
|
log.console.level = error
|
||||||
@@ -27,7 +27,7 @@ services:
|
|||||||
- RABBITMQ_DEFAULT_PASS=symphony
|
- RABBITMQ_DEFAULT_PASS=symphony
|
||||||
- RABBITMQ_NODENAME=rabbit@rabbitmq
|
- RABBITMQ_NODENAME=rabbit@rabbitmq
|
||||||
volumes:
|
volumes:
|
||||||
- rabbitmq_data:/var/lib/rabbitmq
|
- ./config/rabbitmq/rabbit.conf:/etc/rabbitmq/rabbitmq.conf:ro
|
||||||
networks:
|
networks:
|
||||||
- symphony-network
|
- symphony-network
|
||||||
restart: always
|
restart: always
|
||||||
@@ -35,6 +35,3 @@ services:
|
|||||||
networks:
|
networks:
|
||||||
symphony-network:
|
symphony-network:
|
||||||
driver: bridge
|
driver: bridge
|
||||||
|
|
||||||
volumes:
|
|
||||||
rabbitmq_data:
|
|
||||||
Reference in New Issue
Block a user