diff --git a/gusev_vladislav_lab_2/README.md b/gusev_vladislav_lab_2/README.md new file mode 100644 index 0000000..18aca14 --- /dev/null +++ b/gusev_vladislav_lab_2/README.md @@ -0,0 +1,77 @@ +# Лабораторная работа №2 - Разработка простейшего распределённого приложения + +1) Согласно вашему варианту (выбирайте любой) разработать два приложения такие, что результат первого является исходными данными для второго. +2) Изучить файлы сборки образов docker и разработать их для созданных приложений. +3) Собрать файл docker-compose.yml для запуска приложений. Разобраться с монтированием каталогов из хост-системы. +4) Правильно закоммитить результат без лишних файлов. +5) Оформить pull request по правилам и отправить его на проверку. +6) +# Варианты: +- Для Worker1: Ищет в каталоге /var/data файл с наибольшим количеством строк и перекладывает его в /var/result/data.txt. +- Для Worker2: Ищет набольшее число из файла /var/data/data.txt и сохраняет количество таких чисел из последовательности в /var/result/result.txt. + +# Как запустить +Командой "docker-compose up -d" + +# Работа программы +Для Worker1 создали следующий java файл: + +![img.png](screens%2Fimg.png) + +И следующий DockerFile: + +![img_1.png](screens%2Fimg_1.png) + +Для Worker2 создали следующий файл: + +![img_2.png](screens%2Fimg_2.png) + +И следующий DockerFile: + +![img_3.png](screens%2Fimg_3.png) + +Файлы с данными: + +File1: + +![img_4.png](screens%2Fimg_4.png) + +File2: + +![img_5.png](screens%2Fimg_5.png) + +File3: + +![img_6.png](screens%2Fimg_6.png) + +Теперь создаем docker-compose файл: + +![img_7.png](screens%2Fimg_7.png) + +Запускаем в cmd с помощью docker-compose up -d: + +![img_8.png](screens%2Fimg_8.png) + +Образы: + +![img_9.png](screens%2Fimg_9.png) + +Контейнер: + +![img_10.png](screens%2Fimg_10.png) + +Файлы появились в папке: + +![img_11.png](screens%2Fimg_11.png) + +Файл data.txt: + +![img_12.png](screens%2Fimg_12.png) + +Файл result.txt: + +![img_13.png](screens%2Fimg_13.png) + +Наибольшее число 986 и число его вхождений равно 3, всё верно. + +Видео -> https://drive.google.com/file/d/1Jt1wFrdweIrUhDn-MAA4RKYrktykOekw/view?usp=sharing \ No newline at end of file diff --git a/gusev_vladislav_lab_2/data/file1.txt b/gusev_vladislav_lab_2/data/file1.txt new file mode 100644 index 0000000..0eb7e4c --- /dev/null +++ b/gusev_vladislav_lab_2/data/file1.txt @@ -0,0 +1,3 @@ +123 +456 +789 \ No newline at end of file diff --git a/gusev_vladislav_lab_2/data/file2.txt b/gusev_vladislav_lab_2/data/file2.txt new file mode 100644 index 0000000..ff9bf91 --- /dev/null +++ b/gusev_vladislav_lab_2/data/file2.txt @@ -0,0 +1,10 @@ +412 +564 +986 +785 +232 +986 +877 +434 +986 +086 \ No newline at end of file diff --git a/gusev_vladislav_lab_2/data/file3.txt b/gusev_vladislav_lab_2/data/file3.txt new file mode 100644 index 0000000..62b65c9 --- /dev/null +++ b/gusev_vladislav_lab_2/data/file3.txt @@ -0,0 +1,6 @@ +111 +222 +333 +444 +555 +666 diff --git a/gusev_vladislav_lab_2/docker-compose.yml b/gusev_vladislav_lab_2/docker-compose.yml new file mode 100644 index 0000000..89a5e56 --- /dev/null +++ b/gusev_vladislav_lab_2/docker-compose.yml @@ -0,0 +1,17 @@ +services: + worker1: + build: + context: /worker1 + dockerfile: Dockerfile + volumes: + - D:\java_labs\DAS\gusev_vladislav_lab_2\data:/var/data + - D:\java_labs\DAS\gusev_vladislav_lab_2\result:/var/result + worker2: + build: + context: /worker2 + dockerfile: Dockerfile + volumes: + - D:\java_labs\DAS\gusev_vladislav_lab_2\data:/var/data + - D:\java_labs\DAS\gusev_vladislav_lab_2\result:/var/result + depends_on: + - worker1 \ No newline at end of file diff --git a/gusev_vladislav_lab_2/result/data.txt b/gusev_vladislav_lab_2/result/data.txt new file mode 100644 index 0000000..ff9bf91 --- /dev/null +++ b/gusev_vladislav_lab_2/result/data.txt @@ -0,0 +1,10 @@ +412 +564 +986 +785 +232 +986 +877 +434 +986 +086 \ No newline at end of file diff --git a/gusev_vladislav_lab_2/result/result.txt b/gusev_vladislav_lab_2/result/result.txt new file mode 100644 index 0000000..e440e5c --- /dev/null +++ b/gusev_vladislav_lab_2/result/result.txt @@ -0,0 +1 @@ +3 \ No newline at end of file diff --git a/gusev_vladislav_lab_2/screens/img.png b/gusev_vladislav_lab_2/screens/img.png new file mode 100644 index 0000000..bd84af5 Binary files /dev/null and b/gusev_vladislav_lab_2/screens/img.png differ diff --git a/gusev_vladislav_lab_2/screens/img_1.png b/gusev_vladislav_lab_2/screens/img_1.png new file mode 100644 index 0000000..5888ce7 Binary files /dev/null and b/gusev_vladislav_lab_2/screens/img_1.png differ diff --git a/gusev_vladislav_lab_2/screens/img_10.png b/gusev_vladislav_lab_2/screens/img_10.png new file mode 100644 index 0000000..aaad16b Binary files /dev/null and b/gusev_vladislav_lab_2/screens/img_10.png differ diff --git a/gusev_vladislav_lab_2/screens/img_11.png b/gusev_vladislav_lab_2/screens/img_11.png new file mode 100644 index 0000000..2a9ae2c Binary files /dev/null and b/gusev_vladislav_lab_2/screens/img_11.png differ diff --git a/gusev_vladislav_lab_2/screens/img_12.png b/gusev_vladislav_lab_2/screens/img_12.png new file mode 100644 index 0000000..3ad0f6c Binary files /dev/null and b/gusev_vladislav_lab_2/screens/img_12.png differ diff --git a/gusev_vladislav_lab_2/screens/img_13.png b/gusev_vladislav_lab_2/screens/img_13.png new file mode 100644 index 0000000..3a222c8 Binary files /dev/null and b/gusev_vladislav_lab_2/screens/img_13.png differ diff --git a/gusev_vladislav_lab_2/screens/img_2.png b/gusev_vladislav_lab_2/screens/img_2.png new file mode 100644 index 0000000..ad57a2c Binary files /dev/null and b/gusev_vladislav_lab_2/screens/img_2.png differ diff --git a/gusev_vladislav_lab_2/screens/img_3.png b/gusev_vladislav_lab_2/screens/img_3.png new file mode 100644 index 0000000..51a3c52 Binary files /dev/null and b/gusev_vladislav_lab_2/screens/img_3.png differ diff --git a/gusev_vladislav_lab_2/screens/img_4.png b/gusev_vladislav_lab_2/screens/img_4.png new file mode 100644 index 0000000..00afe0e Binary files /dev/null and b/gusev_vladislav_lab_2/screens/img_4.png differ diff --git a/gusev_vladislav_lab_2/screens/img_5.png b/gusev_vladislav_lab_2/screens/img_5.png new file mode 100644 index 0000000..c5023d9 Binary files /dev/null and b/gusev_vladislav_lab_2/screens/img_5.png differ diff --git a/gusev_vladislav_lab_2/screens/img_6.png b/gusev_vladislav_lab_2/screens/img_6.png new file mode 100644 index 0000000..3eaf94d Binary files /dev/null and b/gusev_vladislav_lab_2/screens/img_6.png differ diff --git a/gusev_vladislav_lab_2/screens/img_7.png b/gusev_vladislav_lab_2/screens/img_7.png new file mode 100644 index 0000000..1f0528d Binary files /dev/null and b/gusev_vladislav_lab_2/screens/img_7.png differ diff --git a/gusev_vladislav_lab_2/screens/img_8.png b/gusev_vladislav_lab_2/screens/img_8.png new file mode 100644 index 0000000..7656415 Binary files /dev/null and b/gusev_vladislav_lab_2/screens/img_8.png differ diff --git a/gusev_vladislav_lab_2/screens/img_9.png b/gusev_vladislav_lab_2/screens/img_9.png new file mode 100644 index 0000000..5514e5c Binary files /dev/null and b/gusev_vladislav_lab_2/screens/img_9.png differ diff --git a/gusev_vladislav_lab_2/worker1/.gitignore b/gusev_vladislav_lab_2/worker1/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/gusev_vladislav_lab_2/worker1/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/gusev_vladislav_lab_2/worker1/.idea/.gitignore b/gusev_vladislav_lab_2/worker1/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/gusev_vladislav_lab_2/worker1/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/gusev_vladislav_lab_2/worker1/.idea/misc.xml b/gusev_vladislav_lab_2/worker1/.idea/misc.xml new file mode 100644 index 0000000..07115cd --- /dev/null +++ b/gusev_vladislav_lab_2/worker1/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/gusev_vladislav_lab_2/worker1/.idea/modules.xml b/gusev_vladislav_lab_2/worker1/.idea/modules.xml new file mode 100644 index 0000000..544774c --- /dev/null +++ b/gusev_vladislav_lab_2/worker1/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/gusev_vladislav_lab_2/worker1/.idea/vcs.xml b/gusev_vladislav_lab_2/worker1/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/gusev_vladislav_lab_2/worker1/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/gusev_vladislav_lab_2/worker1/Dockerfile b/gusev_vladislav_lab_2/worker1/Dockerfile new file mode 100644 index 0000000..60d7e5b --- /dev/null +++ b/gusev_vladislav_lab_2/worker1/Dockerfile @@ -0,0 +1,18 @@ +# Используем образ с Java +FROM openjdk:17 + +# Создаем директории для данных и результата внутри контейнера +RUN mkdir /var/data +RUN mkdir /var/result + +# Создаем директорию приложения внутри контейнера +WORKDIR /app + +# Копируем исходные файлы вашего Java-приложения внутрь контейнера +COPY src /app/src + +# Компилируем Java-код +RUN javac /app/src/Worker1.java + +# Запускаем Java-приложение +CMD ["java", "-cp", "/app/src", "Worker1"] diff --git a/gusev_vladislav_lab_2/worker1/src/Worker1.java b/gusev_vladislav_lab_2/worker1/src/Worker1.java new file mode 100644 index 0000000..b4c3719 --- /dev/null +++ b/gusev_vladislav_lab_2/worker1/src/Worker1.java @@ -0,0 +1,43 @@ +import java.io.*; +import java.nio.file.*; +import java.util.Comparator; + +public class Worker1 { + public static void main(String[] args) { + String sourceDirectory = "/var/data"; + String destinationDirectory = "/var/result"; + + try { + copyFileWithMostLines(sourceDirectory, destinationDirectory); + System.out.println("Файл успешно скопирован."); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static void copyFileWithMostLines(String sourceDirectory, String destinationDirectory) throws IOException { + Path sourceDir = Paths.get(sourceDirectory); + Path destDir = Paths.get(destinationDirectory); + + // Поиск файла с наибольшим числом строк + Path fileWithMostLines = Files.walk(sourceDir) + .filter(Files::isRegularFile) + .max(Comparator.comparingLong(file -> { + try { + return Files.lines(file).count(); + } catch (IOException e) { + e.printStackTrace(); + return 0L; + } + })) + .orElse(null); + + if (fileWithMostLines != null) { + + // Копирование файла + Files.copy(fileWithMostLines, Paths.get(destinationDirectory + "/data.txt"), StandardCopyOption.REPLACE_EXISTING); + } else { + System.out.println("Нет файлов для копирования."); + } + } +} \ No newline at end of file diff --git a/gusev_vladislav_lab_2/worker2/.gitignore b/gusev_vladislav_lab_2/worker2/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/gusev_vladislav_lab_2/worker2/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/gusev_vladislav_lab_2/worker2/.idea/.gitignore b/gusev_vladislav_lab_2/worker2/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/gusev_vladislav_lab_2/worker2/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/gusev_vladislav_lab_2/worker2/.idea/misc.xml b/gusev_vladislav_lab_2/worker2/.idea/misc.xml new file mode 100644 index 0000000..07115cd --- /dev/null +++ b/gusev_vladislav_lab_2/worker2/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/gusev_vladislav_lab_2/worker2/.idea/modules.xml b/gusev_vladislav_lab_2/worker2/.idea/modules.xml new file mode 100644 index 0000000..2a996ec --- /dev/null +++ b/gusev_vladislav_lab_2/worker2/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/gusev_vladislav_lab_2/worker2/.idea/vcs.xml b/gusev_vladislav_lab_2/worker2/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/gusev_vladislav_lab_2/worker2/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/gusev_vladislav_lab_2/worker2/Dockerfile b/gusev_vladislav_lab_2/worker2/Dockerfile new file mode 100644 index 0000000..e816819 --- /dev/null +++ b/gusev_vladislav_lab_2/worker2/Dockerfile @@ -0,0 +1,18 @@ +# Используем образ с Java +FROM openjdk:17 + +# Создаем директории для данных и результата внутри контейнера +RUN mkdir /var/data +RUN mkdir /var/result + +# Создаем директорию приложения внутри контейнера +WORKDIR /app + +# Копируем исходные файлы вашего Java-приложения внутрь контейнера +COPY src /app/src + +# Компилируем Java-код +RUN javac /app/src/Worker2.java + +# Запускаем Java-приложение +CMD ["java", "-cp", "/app/src", "Worker2"] diff --git a/gusev_vladislav_lab_2/worker2/src/Worker2.java b/gusev_vladislav_lab_2/worker2/src/Worker2.java new file mode 100644 index 0000000..ae8dea5 --- /dev/null +++ b/gusev_vladislav_lab_2/worker2/src/Worker2.java @@ -0,0 +1,47 @@ +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; + +public class Worker2 { + public static void main(String[] args) { + String sourceFilePath = "/var/result/data.txt"; + String resultFilePath = "/var/result/result.txt"; + + try { + int maxNumberOccurrences = countMaxNumberOccurrences(sourceFilePath); + saveResult(resultFilePath, maxNumberOccurrences); + System.out.println("Результат сохранен в файле: " + resultFilePath); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static int countMaxNumberOccurrences(String filePath) throws IOException { + try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { + int maxNumber = Integer.MIN_VALUE; + int maxNumberCount = 0; + String line; + + while ((line = reader.readLine()) != null) { + int number = Integer.parseInt(line.trim()); + + if (number > maxNumber) { + maxNumber = number; + maxNumberCount = 1; // Новое максимальное число + } else if (number == maxNumber) { + maxNumberCount++; // Увеличиваем количество встреч максимального числа + } + } + + return maxNumberCount; + } + } + + private static void saveResult(String filePath, int result) throws IOException { + String resultString = Integer.toString(result); + Files.write(Path.of(filePath), resultString.getBytes(), StandardOpenOption.CREATE); + } +} \ No newline at end of file