diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..4fa28e2 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,14 @@ + +pipeline { + agent { any { image 'maven:3.3.3' } } + stages { + stage('build') { + steps { + sh 'mvn --version' + sh 'mvn clean package' + sh 'mvn clean install' + } + } + + } +} diff --git a/pom.xml b/pom.xml index 2bc8983..17cc968 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 1.5.1.RELEASE + 2.3.12.RELEASE @@ -22,14 +22,71 @@ UTF-8 UTF-8 1.8 + - + + + io.opentracing.contrib + opentracing-spring-jaeger-web-starter + 3.3.1 + + + com.h2database + h2 + 1.4.200 + compile + + + org.springframework.boot + spring-boot-starter-jdbc + + + mysql + mysql-connector-java + + + org.springframework.boot + spring-boot-starter-actuator + 2.5.4 + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.projectlombok + lombok + 1.18.20 + provided + + + de.codecentric + chaos-monkey-spring-boot + + 2.5.3 + + + org.springframework.boot + spring-boot-starter-test + test + org.springframework.boot spring-boot-starter-web - + io.springfox springfox-swagger2 @@ -49,6 +106,18 @@ org.springframework.boot spring-boot-maven-plugin + + + + + + repackage + + + diff --git a/readme.md b/readme.md index f9b71b5..4c9d5ae 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,4 @@ -# Spring Boot @RestController Example +# Spring Boot @RestController Example with Chaos Monkey Enabled in Spring Application Properties ## Description @@ -8,8 +8,27 @@ The main goal of the project is to show how to write a `@RestController` in Spri ## How does it work? -You can use the endpoints behind `http://localhost:8080/spanish-greetings`. If you append `/random` to a `GET` request you'll receive a random Spanish greeting (which are actually hard-coded). You can also `GET` them by `id` (only from 1 to 3). To create a new one, you need to perform a `POST` operation. +You can use the endpoints behind `http://localhost:8082/spanish-greetings`. If you append `/random` to a `GET` request you'll receive a random Spanish greeting (which are actually hard-coded). You can also `GET` them by `id` (only from 1 to 3). To create a new one, you need to perform a `POST` operation. + +Actually, much better if you just start the application and navigate to `http://localhost:8082/swagger-ui.html`. There you'll find a nice API documentation thanks to Swagger. Moreover, *you can play with it*. + +![Swagger Documentation](images/swagger.png) + +Upon Succesful Initialization of Chaos Monkey you should see something like this : + +![image](https://user-images.githubusercontent.com/50335583/135620733-ee4eb8bc-918d-461a-8cca-40f085976f90.png) + + +In addition to the end points and controller, chaos testing can be done as shown using postman for the endpoints exposed under actuator: + +![image](https://user-images.githubusercontent.com/50335583/135618904-26996ad8-1824-4d03-a97c-4e878b8ef2a2.png) +![image](https://user-images.githubusercontent.com/50335583/135619103-a6322eba-2fb0-4df9-847c-68daf95f5a6b.png) +![image](https://user-images.githubusercontent.com/50335583/135619155-9b62c70a-b8fa-4028-b11a-d58f2ba09b75.png) +![image](https://user-images.githubusercontent.com/50335583/135619288-a3968210-c7aa-48d9-8105-36be2bbacc18.png) +![image](https://user-images.githubusercontent.com/50335583/135619450-1a7e2d03-0fa4-4c14-bcdb-7d95c6cbee98.png) +![image](https://user-images.githubusercontent.com/50335583/135620247-d62f0021-a1e1-4464-9e62-0d58e705d623.png) + + + -Actually, much better if you just start the application and navigate to `http://localhost:8080/swagger-ui.html`. There you'll find a nice API documentation thanks to Swagger. Moreover, *you can play with it*. -![Swagger Documentation](images/swagger.png) \ No newline at end of file diff --git a/src/main/java/es/macero/dev/SpringBootRestcontrollerExampleApplication.java b/src/main/java/es/macero/dev/SpringBootRestcontrollerExampleApplication.java index d1f6df4..093c9a4 100644 --- a/src/main/java/es/macero/dev/SpringBootRestcontrollerExampleApplication.java +++ b/src/main/java/es/macero/dev/SpringBootRestcontrollerExampleApplication.java @@ -1,19 +1,22 @@ package es.macero.dev; import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.*; import org.springframework.context.annotation.Bean; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; +//import es.macero.dev.restexample.SpanishGreeting; -@SpringBootApplication +@SpringBootApplication(scanBasePackages="es.macero") @EnableSwagger2 +@EnableAutoConfiguration public class SpringBootRestcontrollerExampleApplication { public static void main(String[] args) { + SpringApplication.run(SpringBootRestcontrollerExampleApplication.class, args); } @@ -24,6 +27,15 @@ public Docket api() { .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.regex("/spanish-greeting.*")) .build(); + + + } + /*@Bean + public SpanishGreetingSpanishGreeting() { + + return new SpanishGreeting(); + }*/ + } diff --git a/src/main/java/es/macero/dev/restexample/SpanishGreeting.java b/src/main/java/es/macero/dev/restexample/SpanishGreeting.java index 59a8018..267d685 100644 --- a/src/main/java/es/macero/dev/restexample/SpanishGreeting.java +++ b/src/main/java/es/macero/dev/restexample/SpanishGreeting.java @@ -1,7 +1,11 @@ package es.macero.dev.restexample; +import org.springframework.stereotype.*; +import org.springframework.beans.factory.annotation.*; -class SpanishGreeting { - +@Component +public class SpanishGreeting { + + //@Value("${name}") private String message; // Required for JSON deserialization diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e69de29..c439bca 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -0,0 +1,33 @@ +#spring.datasource.url=jdbc:mysql://localhost:3306/db +#spring.datasource.username="" +#spring.datasource.password="" +#spring.datasource.driver-class-name=com.mysql.jdbc.Driver +#spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect +spring.main.allow-bean-definition-overriding=true +opentracing.jaeger.http.sender.url=http://localhost:16686/api/traces +spring.datasource.url=jdbc:h2:mem:testdb +spring.datasource.driverClassName=org.h2.Driver +spring.datasource.username= +spring.datasource.password= +spring.jpa.database-platform=org.hibernate.dialect.H2Dialect +spring.jpa.generate-ddl=true +sring.jpa.hibernate.ddl-auto = update +spring.profiles.active=chaos-monkey +management.server.add-application-context-header=true +server.port=8082 +chaos.monkey.enabled=true +#chaos.monkey.assaults.latencyActive=true +#chaos.monkey.assaults.latencyRangeStart=1000 +#chaos.monkey.assaults.latencyRangeEnd=4500 +chaos.monkey.watcher.controller=true +chaos.monkey.watcher.restController=true +chaos.monkey.watcher.service=true +chaos.monkey.assaults.watchedCustomServices=* +#chaos.monkey.watcher.repository=false +chaos.monkey.assaults.level=1 +spring.aop.proxy-target-class=true +spring.application.admin.enabled=true +management.endpoint.chaosmonkey.enabled=true +#management.endpoint.chaosmonkeyjmx.enabled=false +management.endpoints.web.exposure.include=* +#management.server.port=8888 diff --git a/src/main/resources/application.resources b/src/main/resources/application.resources new file mode 100644 index 0000000..e69de29