gpt4 book ai didi

java - 构建健康指标 spring boot

转载 作者:行者123 更新时间:2023-11-30 11:10:44 26 4
gpt4 key购买 nike

我希望在我的 spring boot 配置中有一个运行状况指示器。但我还没有成功。我有下一节课,使用@component,但是当我编写 url 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();
}

}

}

任何人都可以帮助我吗?

谢谢

更新:

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

最佳答案

默认情况下,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/-- 查找名为:“添加生产级服务”的部分

关于java - 构建健康指标 spring boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27616606/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com