Создан второй сервис.

This commit is contained in:
2025-03-27 23:21:56 +04:00
parent c5caa4d5ee
commit cee4c55bef
3 changed files with 128 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
package ru.sguardian;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.Arrays;
@SpringBootApplication
@RestController
public class ContainerTwo {
private final RestTemplate restTemplate = new RestTemplate();
private final MatrixProcessor processor = new MatrixProcessor();
public static void main(String[] args){
SpringApplication.run(ContainerTwo.class, args);
System.out.println("Server-2 started");
}
@PostMapping("service-two")
public void processArrayPart(@RequestBody int size){
int[][] currentArray = processor.generateMatrix(size);
//System.out.println("After array:");
//processor.printArray(currentArray);
int mid = size / 2;
int[][] leftPart = Arrays.copyOfRange(currentArray, 0, mid);
int[][] rightPart = Arrays.copyOfRange(currentArray, mid, currentArray.length);
long startTime = System.nanoTime();
int[][] localResult = processor.operationMatrix(leftPart);
long endTime = System.nanoTime();
int[][] containerOneResult = restTemplate.postForObject("http://localhost:8080/service-one", rightPart, int[][].class);
int[][] result = processor.operationMatrix(processor.arraysMerge(localResult, containerOneResult));
//System.out.println("Before Array");
//processor.printArray(result);
System.out.println("Service-2 end work " + (endTime - startTime) / 1000000 + " ms");
}
}

View File

@@ -0,0 +1,79 @@
package ru.sguardian;
import java.util.Arrays;
import java.util.Random;
public class MatrixProcessor {
public int[][] operationMatrix(int[][] array) {
if (array.length <= 1) {
return array;
}
int mid = array.length / 2;
int[][] left = Arrays.copyOfRange(array, 0, mid);
int[][] right = Arrays.copyOfRange(array, mid, array.length);
left = operationMatrix(left);
right = operationMatrix(right);
return merge(left, right);
}
private int[][] merge(int[][] left, int[][] right) {
int[][] result = new int[left.length + right.length][];
int i = 0, j = 0, k = 0;
while (i < left.length && j < right.length) {
if (findMaxElementRow(left[i]) >= findMaxElementRow(right[j])) {
result[k++] = left[i++];
} else {
result[k++] = right[j++];
}
}
while (i < left.length) {
result[k++] = left[i++];
}
while (j < right.length) {
result[k++] = right[j++];
}
return result;
}
public static int findMaxElementRow(int[] row) {
int max = Integer.MIN_VALUE;
for (int num : row) {
max = Math.max(max, num);
}
return max;
}
public int[][] generateMatrix(int size){
int[][] array = new int[size][size];
Random rand = new Random();
for (int i = 0; i < size; i++){
for (int j = 0; j < size; j++){
array[i][j] = rand.nextInt(-1000, 1000);
}
}
return array;
}
public int[][] arraysMerge(int[][] arrayOne, int[][] arrayTwo){
int[][] result = new int[arrayOne.length + arrayTwo.length][];
System.arraycopy(arrayOne, 0, result, 0, arrayOne.length);
System.arraycopy(arrayTwo, 0, result, arrayOne.length, arrayTwo.length);
return result;
}
public void printArray(int[][] array){
for(int i = 0; i < array.length; i++){
for (int j = 0; j < array.length; j++){
System.out.print(array[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
}

View File

@@ -0,0 +1,3 @@
server.port=8081
remote.service.url=http://localhost:8080
spring.main.lazy-initialization=true