将Spring执行器与Spring CommandLineRunner应用程序配合使用

fhity93d  于 2023-01-20  发布在  Spring
关注(0)|答案(1)|浏览(155)

我正在使用Sping Boot 在java中开发一个TCP套接字服务器应用程序。尽管我没有使用spring-boot-starter-web依赖项,但我仍然希望从执行器端点中受益,以便使用外部监视工具(如prometheus)来监视应用程序。
最小等效应用为:
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.7.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <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>

DemoApplication.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

SpringBootConsoleApplication.java

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class SpringBootConsoleApplication
    implements CommandLineRunner {

  private static Logger LOG = LoggerFactory
      .getLogger(SpringBootConsoleApplication.class);

  @Override
  public void run(String... args) throws InterruptedException {
    LOG.info("EXECUTING : command line runner");
    int i = 0;
    while(true) {
      LOG.info("iteration: {}", i++);
      Thread.sleep(1000);
    }
  }
}

application.yml

spring:
  jmx:
    enabled: true

management:
  endpoints:
    jmx:
      unique-names: true
      exposure:
        include: '*'
    web:
      exposure:
        include: '*'
    health:
      show-details: always

使用jconsole,我可以通过JMX获得执行器数据,但是如何从执行器的静止端点获得它们呢?
application.yml中是否缺少(或不必要)配置或缺少依赖项?
我已经阅读了其他职位:

而不设法使休息致动器在我的应用程序上工作。

gwbalxhn

gwbalxhn1#

由于您有一个命令行应用程序,因此不包含Web服务器,因此也没有Actuator REST端点。

相关问题