user3322664
user3322664

Reputation:

Build a health Indicator spring boot

I would like have a health indicator in my configuration o spring boot. But I haven't managed it work. I have the next class, with @component, but when I written the url http://"mydomain"/admin/health, or http://"mydomain"/health my application doesn't retrieve anything.

@Component
public class DBHealthIndicator extends AbstractHealthIndicator {

private final AccountsService accountsService;


@Autowired
public DBHealthIndicator(AccountsService accountsService) {
    if (accountsService == null) {
        throw new IllegalArgumentException(
                "An AccountsService is mandatory.");
    }
    this.accountsService = accountsService;

}


@Override
protected void doHealthCheck(Builder builder) throws Exception {
    if (accountsService.getAccounts(1, 1)!= null){
         builder.up();
    }else{
        builder.down();
    }

}

}

Anyboyd can help me?

Thank you

Update:

Here my application.yml

server: servlet-path: /api port: 8080
mongodb: host: conexion port: 27017 databaseName: abc #uri: conexion
management: context-path: /admin
endpoints: health: enabled: yes shutdown: enabled: yes
logging: # Enable this to specify the path and the name of the log file. By default, it creates a # file named spring.log in the temp directory. file: /tmp/abc.log
level: com.abc: INFO

Upvotes: 2

Views: 9019

Answers (2)

user3322664
user3322664

Reputation:

Here my application.yml

server:
  servlet-path: /api
  port: 8080

mongodb:
  host: conexion
  port: 27017
  databaseName: abc
  #uri: conexion

management:
  context-path: /admin

endpoints:
  health:
    enabled: yes
  shutdown:
    enabled: yes

logging:
   # Enable this to specify the path and the name of the log file. By default, it creates a
   # file named spring.log in the temp directory.
   file: /tmp/abc.log      

   # Add other levels if you want to override the level configured in the logback.xml configuration file.
   level: 
   com.abc: INFO

Upvotes: 0

Ilya Ovesnov
Ilya Ovesnov

Reputation: 4257

By default spring boot configure application to be accessible on the 8080 port. If you didn't specify the port then your health service URL is http://your.domain.com:8080/health.

If you configure you application to be accessible on 80 port then check management configuration:

management.port=8081
management.context-path=/
management.security.enabled=true

By default management.port uses the same port as application.

Required project dependencies:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

After you add these dependencies and turn on security in application.properties (or yml) you will see the following endpoints registered:

2014-12-29 12:22:06.239  INFO 14786 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'springSecurityFilterChain' to: [/*]
2014-12-29 12:22:06.298  INFO 14786 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/autoconfig],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2014-12-29 12:22:06.299  INFO 14786 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/mappings],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2014-12-29 12:22:06.299  INFO 14786 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/beans],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2014-12-29 12:22:06.300  INFO 14786 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.util.Map<java.lang.String, java.lang.Object> org.springframework.boot.actuate.endpoint.mvc.ManagementErrorEndpoint.invoke()
2014-12-29 12:22:06.300  INFO 14786 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/configprops],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2014-12-29 12:22:06.301  INFO 14786 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/env/{name:.*}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint.value(java.lang.String)
2014-12-29 12:22:06.301  INFO 14786 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/env],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2014-12-29 12:22:06.301  INFO 14786 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/info],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2014-12-29 12:22:06.301  INFO 14786 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/trace],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2014-12-29 12:22:06.302  INFO 14786 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/health],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.invoke()
2014-12-29 12:22:06.302  INFO 14786 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/dump],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2014-12-29 12:22:06.303  INFO 14786 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/metrics/{name:.*}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint.value(java.lang.String)
2014-12-29 12:22:06.303  INFO 14786 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/metrics],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2014-12-29 12:22:06.347  INFO 14786 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8081/http
2014-12-29 12:22:06.350  INFO 14786 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080/http

If you don't see them in startup log -- then you did something wrong and you need to come back and check that you have both project dependencies and that security module is turned on.

Useful links:

http://spring.io/guides/gs/spring-boot/ -- look for the section called: "Add production-grade services"

Upvotes: 0

Related Questions