Spring Boot - Actuator服务监控以及打包部署

Spring Boot - Actuator服务监控以及打包部署

1.Actuator服务监控

 在生产环境中,需要定时或者实时去监控服务的各种状态保证服务的可用性,Spring Boot提供的Actuator服务监控功能就具有很多监控功能相关接口。Actuator是Spring Boot提供的对应用系统的自省和监控的集成功能,可以对应用系统进行配置查看、健康检查、获取环境属性等。随着我们对于微服务架构的应用开发,由于各个模块的部署是分布式的,大部分模块是运行在不同的服务器上的,彼此之间进行通讯,若一个复杂的流程出现问题要如何快速定位出现问题的环节,则服务监控也是必不可少的。

1.1 Actuator端点

 在集成Actuator之前,我们先了解一下他所给我们提供的端点,即功能性接口。Actuator监控分成两类:原生端点(内置端点)和自定义端点,自定义端点即是我们根据自己实际需求去进行设置监控的功能性端点,例如某项特定的指标;原生端点是Actuator内置提供的一些接口,通过它们了解应用程序运行时的内部状况。
 通过执行器端点,我们可以监控程序与之交互。我们可以启用或禁用每个端点。可以通过JMX或HTTP公开端点 。但大多数应用程序都选择HTTP方式,其中端点标识和前缀(默认/actuator)拼接映射出URL。例如,默认情况下,health端点映射到 /actuator/health。这里简单介绍几个端点,更多具体的功能内容需要大家自己去深入研究,大家可以根据具体版本的官方文档了解使用Spring Boot官方文档-Actuator端点

ID 描述 默认启用
beans 显示应用程序中所有Spring bean的完整列表
env 显示所有环境变量,通过env/{evn_name}可获取特定环境变量
health 显示应用程序健康指标信息
configprops 显示配置属性
metrics 显示应用程序指标信息(内存使用率、HTTP请求数等),通过metrics/{metrics_name}可获取特定指标信息,最重要的监控内容之一,主要监控了JVM内容使用、GC情况、类加载信息等
shutdown 关闭应用程序,该断点默认关闭,需要通过management.endpoint.shutdown.enabled=true配置开启
1.2 集成Actuator
1.2.1 引入依赖
<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>

 这里spring-boot-starter-security是否需要添加根据各自需要,主要是为了保证Actuator提供端点的安全性,若添加之后没有配置相关属性,则访问监控端点时都需要进行验证,默认账号为user,对应凭证会生成在控制台或打印在日志文件中。

1.2.2 配置属性
# actuator监控端口(可配可不配)
management.server.port=8100
# actuator监控端口路径(不配置则和项目路径一致) 例:http://localhost:8100/springboot-mybatis/actuator/health 一般都会启用独立的端口进行监控信息公布
management.server.servlet.context-path=/springboot-mybatis
# 默认开启health和info,设置*则包括所有web端口
management.endpoints.web.exposure.include=*
# 打开默认关闭的shutdown端点
management.endpoint.shutdown.enabled=true

 只需简单两步,就可以通过上面介绍的端点去查看应用程序的监控信息

2.非Web应用程序开发

 在某些情况下,我们使用Spring Boot去开发的项目并非Web项目,而是简单的Java项目,那么这里主要有两种方法

2.1 手动获取Bean(第一种)

 这里我们主要是通过SpringApplication.run()返回的Spring容器对象去操作业务Bean完成业务

package com.springboot;

import com.springboot.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * 非Web SpringBoot项目启动调用方式
 * 1.手动获取service操作bean(原生)
 * 2.实现CommandLineRunner(springboot提供)
 */
@SpringBootApplication
public class SpringbootJavaApplication{

	/**
	 * 第一种启动方式:手动获取service操作bean
	 * @param args
	 */
	public static void main(String[] args){
	    //返回Spring容器对象
		ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(SpringbootJavaApplication.class, args);
		IUserService userService = (IUserService) configurableApplicationContext.getBean("userServiceImpl");
		System.out.println(userService.getTime("test"));
	}
}


2.2实现CommandLineRunner接口(第二种)

 这种方式我们需要通过实现CommandLineRunner接口重写run方法即可,这种方式可以通过依赖注入的方式获取业务Bean

package com.springboot;

import com.springboot.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * 非Web SpringBoot项目启动调用方式
 * 1.手动获取service操作bean(原生)
 * 2.实现CommandLineRunner(springboot提供)
 */
@SpringBootApplication
public class SpringbootJavaApplication implements CommandLineRunner{

	@Autowired
	private IUserService userService;

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

	@Override
	public void run(String... strings) throws Exception {
		System.out.println(userService.getTime("test"));
	}
}


3.打包(jar/war)

 Spring Boot打包总体和我们以前打包区别不大,唯一有区别的主要就是打War包的时候有一些需要注意的,这里我们看一下Spring Boot如何对应用程序打包

3.1 Jar包

 首先要将程序打成Jar包,若我们使用的是Maven则需要保证pom.xml中包类型正确,加上Maven打包插件即可

pom.xml
<packaging>jar</packaging>

<build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <!-- 打jar包插件可能需要用1.4.2.RELEASE 其他版本可能有问题-->
          <!--<version>1.4.2.RELEASE</version>-->
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
3.2 War包

 打War包和我们之前有一些区别,因为是Spring Boot程序,我们的入口类需要实现SpringBootServletInitializer接口并重写configure方法

package com.springboot;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

/**
 * 继承SpringBootServletInitializer (打war)
 * EnableDubboConfigurati 开启dubbo功能
 * @author hzk
 */
@SpringBootApplication
@MapperScan(basePackages = "com.springboot.dao")
@EnableDubboConfiguration
public class SpringBootDubboProviderApplicationWar extends SpringBootServletInitializer
{
    public static void main( String[] args )
    {
        SpringApplication.run(SpringBootDubboProviderApplicationWar.class);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBootDubboProviderApplicationWar.class);
    }
}

 当然pom配置文件中需要改为<packaging>war</packaging>,这时大家就能够成功将应用程序打包成我们所需指定的包

猜你喜欢

转载自blog.csdn.net/u013985664/article/details/86607885