This commit is contained in:
Yuriy Dvorzhetskiy
2021-12-22 23:00:40 +06:00
parent 0750a54e25
commit 8401695f69
13 changed files with 375 additions and 0 deletions
+17
View File
@@ -0,0 +1,17 @@
<?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.otus</groupId>
<artifactId>spring-30</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<modules>
<module>spring-30-exercise</module>
<module>spring-30-solution</module>
</modules>
</project>
@@ -0,0 +1,57 @@
<?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.otus</groupId>
<artifactId>spring-30-exercise</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
<relativePath/>
</parent>
<properties>
<swagger.version>2.8.0</swagger.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-hal-explorer</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,30 @@
package ru.otus.spring.microservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.hateoas.config.EnableHypermediaSupport;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import ru.otus.spring.microservice.domain.Person;
import ru.otus.spring.microservice.repostory.PersonRepository;
import javax.annotation.PostConstruct;
@SpringBootApplication
public class App {
@Autowired
private PersonRepository repository;
public static void main(String[] args) {
SpringApplication.run(App.class);
}
@PostConstruct
public void init() {
for(int i = 0 ; i < 1000; ++i) {
repository.save(new Person("Ivan"));
repository.save(new Person("Maria"));
}
}
}
@@ -0,0 +1,37 @@
package ru.otus.spring.microservice.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Person {
@Id
@GeneratedValue
private int id;
private String name;
public Person() {
}
public Person(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@@ -0,0 +1,17 @@
package ru.otus.spring.microservice.repostory;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.data.rest.core.annotation.RestResource;
import ru.otus.spring.microservice.domain.Person;
import java.util.List;
@RepositoryRestResource(path = "person")
public interface PersonRepository extends JpaRepository<Person, Integer> {
List<Person> findAll();
@RestResource(path = "names")
List<Person> findByName(String name);
}
@@ -0,0 +1,61 @@
<?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.otus</groupId>
<artifactId>spring-30-solution</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
<relativePath/>
</parent>
<properties>
<swagger.version>2.8.0</swagger.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-hal-explorer</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,30 @@
package ru.otus.spring.microservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.hateoas.config.EnableHypermediaSupport;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import ru.otus.spring.microservice.domain.Person;
import ru.otus.spring.microservice.repostory.PersonRepository;
import javax.annotation.PostConstruct;
@SpringBootApplication
public class App {
@Autowired
private PersonRepository repository;
public static void main(String[] args) {
SpringApplication.run(App.class);
}
@PostConstruct
public void init() {
for(int i = 0 ; i < 1000; ++i) {
repository.save(new Person("Ivan"));
repository.save(new Person("Maria"));
}
}
}
@@ -0,0 +1,27 @@
package ru.otus.spring.microservice.actuators;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.Status;
import org.springframework.stereotype.Component;
import java.util.Random;
@Component
public class MyHealthIndicator implements HealthIndicator {
private final Random random = new Random();
@Override
public Health health() {
boolean serverIsDown = random.nextBoolean();
if (serverIsDown) {
return Health.down()
.status(Status.DOWN)
.withDetail("message", "Караул!")
.build();
} else {
return Health.up().withDetail("message", "Ура, товарищи!").build();
}
}
}
@@ -0,0 +1,37 @@
package ru.otus.spring.microservice.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Person {
@Id
@GeneratedValue
private int id;
private String name;
public Person() {
}
public Person(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@@ -0,0 +1,14 @@
package ru.otus.spring.microservice.repostory;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import ru.otus.spring.microservice.domain.Person;
import java.util.List;
// Эта аннотация позволяет изменить имя ресурса
@RepositoryRestResource(path = "person")
public interface PersonRepository extends JpaRepository<Person, Integer> {
List<Person> findByName(String name);
}
@@ -0,0 +1,25 @@
package ru.otus.spring.microservice.rest;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MetricsCounter {
private final MeterRegistry registry;
public MetricsCounter(MeterRegistry registry) {
this.registry = registry;
}
@GetMapping("/counter")
public void counter() {
registry.counter("counter.example").increment();
}
@GetMapping("/gauge")
public void gauge() {
registry.gauge("gauge.example", Math.random());
}
}
@@ -0,0 +1,23 @@
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: always
health:
defaults:
enabled: true
info:
git:
mode: full
cors:
allowed-origins: http://localhost:9090/
allowed-methods: GET,POST,OPTIONS
info:
version: @project.version@
author: dkogan
descriptiion: THis is SPring Bot example