构建健康指示器弹簧靴

时间:2014-12-23 08:25:43

标签: java spring-security spring-boot health-monitoring

我想在我的配置中使用健康指示器进行弹簧启动。但我还没有把它管理好。 我有@component的下一课,但当我写了网址http://" mydomain" / admin / health,或http://" mydomain" / health我的应用程序没有检索任何东西。

@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可以帮助我吗?

谢谢

更新

这是我的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

2 个答案:

答案 0 :(得分:1)

默认情况下,spring boot配置应用程序可在8080端口上访问。如果您未指定端口,则您的运行状况服务URL为http://your.domain.com:8080/health

如果您将应用程序配置为可在80端口上访问,请检查管理配置:

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

默认情况下,management.port使用与应用程序相同的端口。

必需的项目依赖项:

<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>

添加这些依赖项并在application.properties(或yml)中打开安全性后,您将看到注册了以下端点:

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

如果你在启动日志中没有看到它们 - 那么你做错了什么,你需要回来检查你是否有项目依赖项并且安全模块已经打开。

有用的链接:

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

答案 1 :(得分:0)

这是我的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