弹簧启动执行器端点未激活

时间:2020-03-29 02:08:21

标签: java spring spring-boot

我正在学习弹簧启动执行器,请注意,弹簧启动执行器未启动。我正在阅读文档[spring docs] [1],因此,请按照上述方式添加maven依赖项。

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

我的POM.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>net.javapedia</groupId>
    <artifactId>spring-scheduling</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-scheduling</name>
    <description>Spring scheduling example</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>


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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

我的Spring应用程序:

package net.javapedia.springscheduling;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class SpringSchedulingApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringSchedulingApplication.class, args);
    }

}

application.properties

management.endpoints.enabled-by-default=true

控制台输出:

2020-03-28 22:05:31.184  INFO 63611 --- [           main] n.j.s.SpringSchedulingApplication        : Starting SpringSchedulingApplication on localhost with PID 63611 (/Users/admin/IdeaProjects/spring-scheduling/target/classes started by admin in /Users/admin/IdeaProjects/spring-scheduling)
2020-03-28 22:05:31.189  INFO 63611 --- [           main] n.j.s.SpringSchedulingApplication        : No active profile set, falling back to default profiles: default
2020-03-28 22:05:33.279  INFO 63611 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'taskScheduler'
Running fixedRate Task, Thread name: scheduling-1
Running scheduled Task, iteration :1 Thread name: scheduling-1
2020-03-28 22:05:33.342  INFO 63611 --- [           main] n.j.s.SpringSchedulingApplication        : Started SpringSchedulingApplication in 2.791 seconds (JVM running for 4.108)

我正在尝试访问位于http://localhost:8080/actuator/health的执行器运行状况端点,但是没有运气。

  [1]: http://%20https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html#production-ready-enabling

1 个答案:

答案 0 :(得分:1)

请在您的pom.xml中添加以下内容:

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

我已经复制了您的设置,但最初确实没有用。但是在我添加了spring-boot-starter-web之后,结果就解决了。似乎必须具有web依赖性才能启动任何控制器,不仅是特定于应用程序的控制器,甚至是actuator的控制器。

应用日志:

2020-03-29 10:27:36.934  INFO 29814 --- [           main] com.example.SpringSchedulingApplication  : Starting SpringSchedulingApplication on Users-MacBook-Pro-4.local with PID 29814 (/Users/user/projects/test/target/classes started by user in /Users/user/projects/test)
2020-03-29 10:27:36.936  INFO 29814 --- [           main] com.example.SpringSchedulingApplication  : No active profile set, falling back to default profiles: default
2020-03-29 10:27:37.623  INFO 29814 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (HTTP)
2020-03-29 10:27:37.628  INFO 29814 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-03-29 10:27:37.628  INFO 29814 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.33]
2020-03-29 10:27:37.677  INFO 29814 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-03-29 10:27:37.677  INFO 29814 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 715 ms
2020-03-29 10:27:37.890  INFO 29814 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-03-29 10:27:38.012  INFO 29814 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'taskScheduler'
2020-03-29 10:27:38.015  INFO 29814 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
2020-03-29 10:27:38.056  INFO 29814 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-03-29 10:27:38.058  INFO 29814 --- [           main] com.example.SpringSchedulingApplication  : Started SpringSchedulingApplication in 1.283 seconds (JVM running for 1.797)
2020-03-29 10:27:38.413  INFO 29814 --- [on(1)-127.0.0.1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-03-29 10:27:38.413  INFO 29814 --- [on(1)-127.0.0.1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-03-29 10:27:38.418  INFO 29814 --- [on(1)-127.0.0.1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 5 ms