Добавлены первый микросервис

This commit is contained in:
dimon 2024-04-12 16:41:50 +03:00
commit 5c19cd579b
10 changed files with 426 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/microservice-back-commons/target/
/microservice-back-minus/target/
/microservice-back-minus/nbproject/

View File

@ -0,0 +1,24 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ru.microservice</groupId>
<artifactId>microservice-back-commons</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>6.1.4</version>
<type>jar</type>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,38 @@
package ru.microservice.back.commons;
/**
* Типичные действия, ожидаемые от наших микросервисов
*/
public interface ActionMethods {
/**
* Метод проверки доступности
*
* @return возвращает "pong" в ответ
*/
String ping();
/**
* Метод расчёта значения
*
* @param argument1 аргумент для расчётов 1
* @param argument2 аргумент для расчётов 2
*
* @return результат расчётов в ответ
*/
double calculate(double argument1, double argument2);
/**
* Возвращает текущее количество обработанных запросов
*
* @return текущее количество обработанных запросов
*/
int request_amount();
/**
* Обнуление статистики по количеству обработанных запросов
*
* @return текущее количество обработанных запросов
*/
int reset_amount();
}

View File

@ -0,0 +1,36 @@
package ru.microservice.back.commons;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Типовой REST сервис
*/
public abstract class RestService implements ActionMethods {
private int requestAmount = 0;
@GetMapping("/ping")
@Override
public @ResponseBody String ping() {
return "pong";
}
@GetMapping("/request_amount")
@Override
public @ResponseBody int request_amount() {
return requestAmount;
}
@DeleteMapping("/reset_amount")
@Override
public @ResponseBody int reset_amount() {
requestAmount = 0;
return requestAmount;
}
protected void incrementRequestAmount() {
requestAmount++;
}
}

View File

@ -0,0 +1,3 @@
#maven.buildNumber.plugin properties file
#Fri Apr 12 00:08:04 MSK 2024
buildNumber=1

View File

@ -0,0 +1,187 @@
<?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 http://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.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>ru.microservice</groupId>
<artifactId>microservice-back-minus</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<start-class>ru.microservice.back.minus.Main</start-class>
</properties>
<dependencies>
<!-- Commons -->
<dependency>
<groupId>ru.microservice</groupId>
<artifactId>microservice-back-commons</artifactId>
<version>1.0</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<!-- Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>3.2.4</version>
<scope>test</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.2</version>
<scope>test</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>3.1.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.24.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}-${project.version}-${buildNumber}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>${start-class}</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.44.0</version>
<configuration>
<dockerHost>tcp://my.docker.host:2375</dockerHost>
<pushRegistry>my.docker.host:5000</pushRegistry>
<images>
<image>
<alias>${project.artifactId}</alias>
<name>microservices/back-minus:${project.version}-${buildNumber}</name>
<build>
<tags>
<tag>${project.version}-${buildNumber}</tag>
</tags>
<from>openjdk:17.0.2-jdk-slim-buster</from>
<assembly>
<descriptorRef>artifact-with-dependencies</descriptorRef>
</assembly>
<cmd>
<shell>java -jar /maven/${project.build.finalName}.jar</shell>
</cmd>
<buildOptions>
<!-- linux/arm64/v8 linux/arm64 linux/amd64 -->
<platform>linux/amd64</platform>
</buildOptions>
</build>
</image>
</images>
</configuration>
<executions>
<execution>
<id>build</id>
<phase>pre-integration-test</phase>
<goals>
<goal>build</goal> <!-- "build" should be used to create the images with the artifact -->
</goals>
</execution>
<execution>
<id>start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
<execution>
<id>repository-push</id>
<phase>install</phase>
<goals>
<goal>push</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>buildnumber</id>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<format>{0,number}</format>
<items>
<item>buildNumber</item>
</items>
<doCheck>false</doCheck>
<doUpdate>false</doUpdate>
<revisionOnScmFailure>unknownbuild</revisionOnScmFailure>
</configuration>
</plugin>
</plugins>
</build>
<scm>
<connection>scm:svn:http://127.0.0.1/dummy</connection>
<developerConnection>scm:svn:https://127.0.0.1/dummy</developerConnection>
<tag>HEAD</tag>
<url>http://127.0.0.1/dummy</url>
</scm>
</project>

View File

@ -0,0 +1,23 @@
package ru.microservice.back.minus;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Main extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
@Override
public SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Main.class);
}
}

View File

@ -0,0 +1,22 @@
package ru.microservice.back.minus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import ru.microservice.back.commons.ActionMethods;
import ru.microservice.back.commons.RestService;
/**
* REST сервис для расчёта разницы значений
*/
@Controller
public class RestMinusService extends RestService implements ActionMethods {
@GetMapping("/calculate/{argument1}/{argument2}")
@Override
public @ResponseBody double calculate(@PathVariable double argument1, @PathVariable double argument2) {
incrementRequestAmount();
return argument1 - argument2;
}
}

View File

@ -0,0 +1,75 @@
package ru.microservice.back.minus;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.context.ContextConfiguration;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = Main.class)
@WebMvcTest(Main.class)
public class RestMinusServiceTest {
@Autowired
private MockMvc mvc;
@Test
public void testPing() throws Exception {
assertThat(
mvc.perform(get("/ping"))
//.andDo(print())
.andExpect(status().isOk())
.andReturn()
.getResponse()
.getContentAsString()
).isEqualTo("pong");
}
@Test
public void testAmount_0() throws Exception {
mvc.perform(delete("/reset_amount"));
assertThat(
mvc.perform(get("/request_amount"))
//.andDo(print())
.andExpect(status().isOk())
.andReturn()
.getResponse()
.getContentAsString()
).isEqualTo("0");
}
@Test
public void testAmount_1() throws Exception {
mvc.perform(delete("/reset_amount"));
mvc.perform(get("/calculate/0.0/0.0"));
assertThat(
mvc.perform(get("/request_amount"))
//.andDo(print())
.andExpect(status().isOk())
.andReturn()
.getResponse()
.getContentAsString()
).isEqualTo("1");
}
@Test
public void testCalc() throws Exception {
assertThat(
mvc.perform(get("/calculate/5.5/2.1"))
//.andDo(print())
.andExpect(status().isOk())
.andReturn()
.getResponse()
.getContentAsString()
).isEqualTo("3.4");
}
}

15
pom.xml Normal file
View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ru.test</groupId>
<artifactId>microservices</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<modules>
<module>microservice-back-commons</module>
<module>microservice-back-minus</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>