java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot 部署与监控

SpringBoot 项目部署与监控的过程

作者:guslegend

SpringBoot项目部署在互联网背景下前后端分离开发已经成为主流趋势,SpringBoot构建web项目非常快速,只需要将其打成一个jar包,然后通过java-jar命令启动,本文介绍SpringBoot 项目部署与监控的过程,感兴趣的朋友跟随小编一起看看吧

SpringBoot项目部署

在如今的互联网背景下前后端分离开发已经成为互联网的主流趋势,SpringBoot构建web项目已经非常快速了,只需要将其打成一个jar包,然后通过java -jar jar包的名称就可以启动。

jar包

首先我们需要导入springBoot的maven依赖

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

package完成后,会出现一个jar包

我们可以将jar包上传到Linux服务器上面,以jar运行(此处本地验证打包成功)

java -jar zdy-spring-boot-start-1.0-SNAPSHOT.jar

war包

首先我们需要修改pom.xml配置文件

<packaging>jar</packaging>
//修改为
<packaging>war</packaging>

然后在pom.xml文件中添加依赖

<!--添加servlet-api的依赖,用来打war包 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>

还需要排除springboot内置的tomcat干扰       

 <!--最终打成war包,排除内置的tomcat-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

然后我们需要改造启动类

如果是war包发布,需要增加SpringBootServletInitializer子类,并重写其configure方法,或者将main函数所在的类继承SpringBootSerletInitializer,并重写configure方法。

当时打包为war时上传tomact服务器访问项目始终报错404就是忽略了这个。

@SpringBootApplication
public class MainApplication  extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(MainApplication.class);
    }
}

war包和jar方式对比

SpringBoot 项目 jar 与 war 打包方式对比

启动与部署方式

jar 包:操作简单,通过java -jar xx.jar直接启动,使用最广泛;

war 包:需部署到 Tomcat 的 webapps 目录,随 Tomcat 启动而启动。

资源打包差异

jar 包:不会包含src/main/webapp下的内容(若路径错误会出现 404);

war 包:会将src/main/webapp下的内容打包进去。

适用场景

选 jar 包:提供 rest 服务的项目,命令行运行便捷;

选 war 包:含大量 css、js、html 且需频繁改动的项目(可直接替换静态资源,无需重新打包上传,效率更高)。

多环境部署

@Profile

使用要求:

@Profile的使用位置:

Profile激活

在实际使用中,注解中标识了prod,test,qa等多个环境,运行时使用哪个profile有spring.profiles.active控制,可以使用配置文件或命令行方式创建。

  1. 配置文件激活
spring:
  profiles:
    active: dev
  1. 命令行方式激活
java -jar spring-boot-config-0.1-SNAPSHOT.jar --spring.profiles.active=dev

多Proflie的资源文件

一般为4个配置文件:

不同的properties配置文件也可以是在application.properties文件中来激活profile:spring.profiles.active = test

SpringBoot监控

Auturator

HTTP 方法

路径

描述

GET

/auditevents

显示应用暴露的审计事件(如认证进入、订单失效)

GET

/beans

描述应用程序上下文里全部的 Bean 及它们的关系

GET

/conditions

(对应 1.0 的 /autoconfig)提供自动配置生效的条件情况,记录哪些条件通过 / 没通过

GET

/configprops

描述配置属性(包含默认值)如何注入 Bean

GET

/env

获取全部环境属性

GET

/env/{name}

根据名称获取特定的环境属性值

GET

/flyway

提供 Flyway 数据库迁移信息

GET

/liquibase

显示 Liquibase 数据库迁移的详细信息

GET

/health

报告应用程序的健康指标(由 HealthIndicator 实现类提供)

GET

/heapdump

dump 一份应用的 JVM 堆信息

GET

/httptrace

显示 HTTP 踪迹,最近 100 个 HTTP request/response

GET

/info

获取应用程序的定制信息(由 info 打头的属性提供)

GET

/logfile

返回 log 文件内容(需配置 logging.file 或 logging.path)

GET

/loggers

显示和修改配置的 loggers

GET

/metrics

报告各种应用程序度量信息(如内存用量、HTTP 请求计数)

GET

/metrics/{name}

报告指定名称的应用程序度量值

GET

/scheduledtasks

展示应用中的定时任务信息

GET

/sessions

若使用 Spring Session,展示应用中的 HTTP sessions 信息

POST

/shutdown

关闭应用程序(需配置 endpoints.shutdown.enabled=true)

GET

/mappings

描述全部的 URI 路径,及它们和控制器(包含 Actuator 端点)的映射关系

GET

/threaddump

获取线程活动的快照

体验Actuator

使用Actuator功能与springBoot使用其他功能一样简单,只需要加入xml依赖

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

为了保证监控接口的安全性,需要添加spring-boot-start-secuity依赖,访问应用监控端点是,都需要输入验证信息。也可以选择不加,不进行安全管理。

编写配置文件

配置详情

Actuator 基础配置(Spring Boot 2.x)

  1. 默认开放端点:仅 /actuator/health/actuator/info(为安全考虑)。
  2. 端点暴露配置
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.include=beans,trace
  1. 自定义监控路径
management.endpoints.web.base-path=/manage

配置后访问地址变为 /manage/*

常用端点功能说明

1. health
{
  "status": "UP",
  "components": {
    "db": {
      "status": "UP",
      "details": {
        "database": "MySQL",
        "result": 1,
        "validationQuery": "/* ping */ SELECT 1"
      }
    },
    "diskSpace": {
      "status": "UP",
      "details": {
        "total": 429496729600,
        "free": 295998197760,
        "threshold": 10485760
      }
    },
    "ping": {
      "status": "UP"
    },
    "redis": {
      "status": "UP",
      "details": {
        "version": "7.0.15"
      }
    }
  }
}
management.health.redis.enabled=false
2. info
info.app.name=spring-boot-actuator
info.app.version=1.0.0
{
  "app": {
    "name": "SpringCacheDemo",
    "version": 1,
    "test": "test"
  }
}
3. beans
4. conditions
5. heapdump
6. mappings
7. threaddump
8. shutdown
management.endpoint.shutdown.enabled=true
curl -X POST "http://localhost:8000/actuator/shutdown"
{"message": "Shutting down, bye..."}

Spring Boot Admin

服务端

配置xml文件      

<dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>2.7.10</version> 
        </dependency>

需要在启动类上添加注解

@EnableAdminServer

客户端

添加maven依赖

<dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>2.1.0</version>
        </dependency>

配置yml文件

server:
  port: 8080
# 自定义配置信息用于“/actuator/info”读取
info:
  name: 老王
  age: 100
  phone: 110
# 通过下面的配置启用所有的监控端点,默认情况下,这些端点是禁用的:
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always
# 将Client作为服务注册到Server,通过Server来监听项目的运行情况
spring:
  boot:
    admin:
      client:
        url: http://localhost:8081
# #application实例名
# application:
#   name: spring-boot-admin-client

到此这篇关于java线程池的文章就介绍到这了,更多相关java线程池内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文