diff --git a/spring-boot-actuator/.classpath b/spring-boot-actuator/.classpath new file mode 100644 index 0000000000..63cc387026 --- /dev/null +++ b/spring-boot-actuator/.classpath @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-boot-actuator/.gitignore b/spring-boot-actuator/.gitignore new file mode 100644 index 0000000000..24d64373c4 --- /dev/null +++ b/spring-boot-actuator/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/spring-boot-actuator/.project b/spring-boot-actuator/.project new file mode 100644 index 0000000000..29b4319649 --- /dev/null +++ b/spring-boot-actuator/.project @@ -0,0 +1,42 @@ + + + spring-boot-actuator + + + + + + org.eclipse.wst.jsdt.core.javascriptValidator + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.wst.validation.validationbuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.wst.common.modulecore.ModuleCoreNature + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + org.eclipse.wst.common.project.facet.core.nature + org.eclipse.wst.jsdt.core.jsNature + + diff --git a/spring-boot-actuator/pom.xml b/spring-boot-actuator/pom.xml new file mode 100644 index 0000000000..17260b23a0 --- /dev/null +++ b/spring-boot-actuator/pom.xml @@ -0,0 +1,50 @@ + + 4.0.0 + org.baeldung + spring-boot-actuator + 0.0.1-SNAPSHOT + war + Spring Boot Actuator + This is simple boot application for Spring boot actuator test + + + + org.springframework.boot + spring-boot-starter-parent + 1.2.3.RELEASE + + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-actuator + + + + org.springframework.boot + spring-boot-starter-security + + + + io.dropwizard.metrics + metrics-core + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/spring-boot-actuator/src/main/java/org/baeldung/endpoints/CustomEndpoint.java b/spring-boot-actuator/src/main/java/org/baeldung/endpoints/CustomEndpoint.java new file mode 100644 index 0000000000..5581ce1052 --- /dev/null +++ b/spring-boot-actuator/src/main/java/org/baeldung/endpoints/CustomEndpoint.java @@ -0,0 +1,35 @@ +package org.baeldung.endpoints; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.boot.actuate.endpoint.Endpoint; +import org.springframework.stereotype.Component; + +@Component +public class CustomEndpoint implements Endpoint> { + + public CustomEndpoint() { + + } + + public String getId() { + return "customEndpoint"; + } + + public boolean isEnabled() { + return true; + } + + public boolean isSensitive() { + return true; + } + + public List invoke() { + //Your logic to display the output + List messages = new ArrayList(); + messages.add("This is message 1"); + messages.add("This is message 2"); + return messages; + } +} diff --git a/spring-boot-actuator/src/main/java/org/baeldung/endpoints/ListEndpoints.java b/spring-boot-actuator/src/main/java/org/baeldung/endpoints/ListEndpoints.java new file mode 100644 index 0000000000..89d4db5bfe --- /dev/null +++ b/spring-boot-actuator/src/main/java/org/baeldung/endpoints/ListEndpoints.java @@ -0,0 +1,23 @@ +package org.baeldung.endpoints; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.actuate.endpoint.AbstractEndpoint; +import org.springframework.boot.actuate.endpoint.Endpoint; +import org.springframework.stereotype.Component; + +@Component +public class ListEndpoints extends AbstractEndpoint> { + private List endpoints; + + @Autowired + public ListEndpoints(List endpoints) { + super("listEndpoints"); + this.endpoints = endpoints; + } + + public List invoke() { + return this.endpoints; + } +} \ No newline at end of file diff --git a/spring-boot-actuator/src/main/java/org/baeldung/endpoints/MyHealthCheck.java b/spring-boot-actuator/src/main/java/org/baeldung/endpoints/MyHealthCheck.java new file mode 100644 index 0000000000..67079beb6b --- /dev/null +++ b/spring-boot-actuator/src/main/java/org/baeldung/endpoints/MyHealthCheck.java @@ -0,0 +1,22 @@ +package org.baeldung.endpoints; + +import org.springframework.boot.actuate.health.Health; +import org.springframework.boot.actuate.health.HealthIndicator; +import org.springframework.stereotype.Component; + +@Component +public class MyHealthCheck implements HealthIndicator { + + public Health health() { + int errorCode = check(); // perform some specific health check + if (errorCode != 0) { + return Health.down().withDetail("Error Code", errorCode).withDetail("Description", "You custom MyHealthCheck endpoint is down").build(); + } + return Health.up().build(); + } + + public int check() { + // Your logic to check health + return 1; + } +} diff --git a/spring-boot-actuator/src/main/java/org/baeldung/main/SpringBootActuatorApplication.java b/spring-boot-actuator/src/main/java/org/baeldung/main/SpringBootActuatorApplication.java new file mode 100644 index 0000000000..2099f11225 --- /dev/null +++ b/spring-boot-actuator/src/main/java/org/baeldung/main/SpringBootActuatorApplication.java @@ -0,0 +1,30 @@ +package org.baeldung.main; + + +import org.baeldung.service.LoginService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@EnableAutoConfiguration +@ComponentScan({"org.baeldung.endpoints", "org.baeldung.service", "org.baeldung.monitor.jmx"}) +public class SpringBootActuatorApplication { + + @Autowired + private LoginService service; + + @RequestMapping("/") + String home() { + service.login("admin", "admin".toCharArray()); + return "TADA!!! You are in Spring Boot Actuator test application."; + } + + public static void main(String[] args) { + SpringApplication.run(SpringBootActuatorApplication.class, args); + } + +} diff --git a/spring-boot-actuator/src/main/java/org/baeldung/monitor/jmx/MonitoringConfig.java b/spring-boot-actuator/src/main/java/org/baeldung/monitor/jmx/MonitoringConfig.java new file mode 100644 index 0000000000..6a99dcf8f4 --- /dev/null +++ b/spring-boot-actuator/src/main/java/org/baeldung/monitor/jmx/MonitoringConfig.java @@ -0,0 +1,21 @@ +package org.baeldung.monitor.jmx; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.codahale.metrics.JmxReporter; +import com.codahale.metrics.MetricRegistry; + +@Configuration +public class MonitoringConfig { + @Autowired + private MetricRegistry registry; + + @Bean + public JmxReporter jmxReporter() { + JmxReporter reporter = JmxReporter.forRegistry(registry).build(); + reporter.start(); + return reporter; + } +} diff --git a/spring-boot-actuator/src/main/java/org/baeldung/service/LoginService.java b/spring-boot-actuator/src/main/java/org/baeldung/service/LoginService.java new file mode 100644 index 0000000000..4a959d38e1 --- /dev/null +++ b/spring-boot-actuator/src/main/java/org/baeldung/service/LoginService.java @@ -0,0 +1,5 @@ +package org.baeldung.service; + +public interface LoginService { + public boolean login(String userName, char[] password); +} diff --git a/spring-boot-actuator/src/main/java/org/baeldung/service/LoginServiceImpl.java b/spring-boot-actuator/src/main/java/org/baeldung/service/LoginServiceImpl.java new file mode 100644 index 0000000000..bd2056d827 --- /dev/null +++ b/spring-boot-actuator/src/main/java/org/baeldung/service/LoginServiceImpl.java @@ -0,0 +1,32 @@ +package org.baeldung.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.actuate.metrics.CounterService; +import org.springframework.stereotype.Service; + +@Service +public class LoginServiceImpl implements LoginService { + + private CounterService counterService; + + @Autowired + public LoginServiceImpl(CounterService counterService) { + this.counterService = counterService; + } + + public boolean login(String userName, char[] password) { + boolean success; + if (userName.equals("admin") && "secret".toCharArray().equals(password)) + { + counterService.increment("counter.login.success"); + success = true; + } + else + { + counterService.increment("counter.login.failure"); + success = false; + } + return success; + } + +} diff --git a/spring-boot-actuator/src/main/resources/application.properties b/spring-boot-actuator/src/main/resources/application.properties new file mode 100644 index 0000000000..c876ff740e --- /dev/null +++ b/spring-boot-actuator/src/main/resources/application.properties @@ -0,0 +1,27 @@ +management.port=8081 +management.address=127.0.0.1 + +endpoints.shutdown.enabled=true + +endpoints.jmx.domain=Spring Sample Application +endpoints.jmx.uniqueNames=true + +##jolokia.config.debug=true +##endpoints.jolokia.enabled=true +##endpoints.jolokia.path=jolokia + +spring.jmx.enabled=true +endpoints.jmx.enabled=true + +## for pretty printing of json when endpoints accessed over HTTP +http.mappers.jsonPrettyPrint=true + +## Configuring info endpoint +info.app.name=Spring Sample Application +info.app.description=This is my first spring boot application G1 +info.app.version=1.0.0 + +## Spring Security Configurations +security.user.name=admin1 +security.user.password=secret1 +management.security.role=SUPERUSER \ No newline at end of file