springboot的部署测试监控

部署

基于maven

打包

JAR

打包方式一般采用的jar包,使用springboot的默认方式即可;

使用maven命令:

mvn clean package -Dmaven.test.skip=true

执行成功之后,可以在对应的target目录下找到对应的包,比如:  eg-zuul-0.0.1-SNAPSHOT.jar

WAR

运行

内置容器运行

springboot内置了web container容器 tomcat, 可以直接使用 java -jar命令运行;

例如:

java -jar xxx/target/eg-zuul-0.0.1-SNAPSHOT.jar

外置容器运行

也可使用war的方式,使用外置的tomcat运行,不过代码需要改造一下:
1 改造打包方式 
打包方式改为 war包;
在pom.xml中,在version标签的下面添加配置:

<package>war</package>

2 添加改造启动代码

package com.springbootpractice.egzuul;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; /**
* @author <a href="mailto:505847426@qq.com">carterbrother</a>
* @description 通过外置的容器运行springboot
* @date 2019年06月21日 15:32
* @Copyright (c) carterbrother
*/
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(EgZuulApplication.class);
}
}

原理是: 从servlet3.1开始,运行无web.xml的web程序,只需要实现ServletContainerInitializer接口,而SpringBootServletInitializer扩展了该类,所以可以实现无xml启动;

3 配置外置tomcat

4 忽略打包检查

  <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>

热部署

开发的时候用到, spring-boot-devtools ;
引入依赖:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>

暴露的配置信息可以配置更多细节方面的处理:

测试

基于junit 和 mockito (消除各种环境对于http带来的困难)

测试业务层

测试REST

package com.springbootpractice.eguser;

import com.springbootpractice.api.user.dto.UserPro;
import com.springbootpractice.eguser.service.UserService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.junit4.SpringRunner; import java.util.Map; @RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class EgUserApplicationTests { @Autowired
private UserService userService; @Autowired
private TestRestTemplate testRestTemplate; @Test
public void testUserService() { UserPro userPro = new UserPro();
userPro.setId(1L);
userPro.setUserName("xxxaaa"); final Map<String, Object> map = userService.insertUser(userPro); Assert.assertEquals("插入失败",true,map.get("success")); final UserPro userProReturn = userService.getUserPro(1L); Assert.assertEquals(userPro,userProReturn);
} @Test
public void testUserRest() {
UserPro userPro = new UserPro();
userPro.setId(2L);
userPro.setUserName("BBBB"); Map map = testRestTemplate.postForObject("/insert", userPro, Map.class);
Assert.assertEquals("插入失败",true,map.get("success")); UserPro userProReturn = testRestTemplate.getForObject("/user/{id}", UserPro.class, 2L);
Assert.assertEquals(userPro,userProReturn);
} }

Mock测试

当依赖的服务还没有开发完毕,而需要测试的功能却强烈依赖,可以使用Mock来测试;

package com.springbootpractice.egproduct;

import com.springbootpractice.api.user.UserApi;
import com.springbootpractice.api.user.dto.UserPro;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.BDDMockito;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class EgProductApplicationTests { @MockBean
private UserApi userApi; @Test
public void mockUserApiTest() { UserPro mockUserPro = new UserPro();
mockUserPro.setId(1L);
mockUserPro.setUserName("xxxx"); BDDMockito.given(userApi.getUserPro(1L)).willReturn(mockUserPro); UserPro userProReturn = userApi.getUserPro(1L); Assert.assertEquals(userProReturn,mockUserPro);
} }

监控

基于 actuator ,监控运行状态,进行一些简单的管理

WEB监控

引入依赖:

 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency> <dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
<version>0.24.0.RELEASE</version>
</dependency>

默认只开放了 health , info ;

如果要开放所有的监控点:
management.endpoint.web.exposure.include=*

常见的监控点有:

url 监控说明
health 监控信息
info
beans 容器中的Bean
mappings url mapping
env 配置参数
shutdown 关闭服务
conditions 自动装配相关的信息

对敏感的配置信息,可以使用spring-security来控制保护起来;

shutdown端点默认是关闭的,开启的配置属性是:
management.endpoint.shutdown.enabled=true

开启之后 访问 /actuator/shutdown 需要是post请求才能调用;

一般的配置端点开关的方式是:

//默认所有的端点都是关闭的,然后选择一些需要暴露的端点进行打开
management.endpoints.enabled-by-default=false

标注@Endpoint注解类,
@ReadOperation标注方法 标识GET方法
@WriteOperation  标识POST方法
@DeleteOperation 标识Delete方法

package com.springbootpractice.egproduct.endpoint;

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component; /**
* @author <a href="mailto:505847426@qq.com">carterbrother</a>
* @description 新增actuator的一个端点
* @date 2019年06月21日 18:42
* @Copyright (c) carterbrother
*/
@Endpoint(id = "dbCheck",enableByDefault = true)
@Component
public class DBCheckEndpoint { @ReadOperation
public String test(){
return "db check ok";
} }

actuator内置了很多的健康指标 需要配置才能显示,配置方法:

management.endpoint.health.show-details=always

package com.springbootpractice.egproduct.health;

import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.stereotype.Component; import java.io.IOException;
import java.net.InetAddress; /**
* @author <a href="mailto:505847426@qq.com">carterbrother</a>
* @description 进行网络检查
* @date 2019年06月21日 18:30
* @Copyright (c) carterbrother
*/
@Component
public class WWWHealthIndicator extends AbstractHealthIndicator {
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception { if (ping()){
builder.withDetail("message","可以正常连接互联网").up();
return;
} builder.withDetail("message","无法连接互联网").unknown(); } private boolean ping() { try {
return InetAddress.getByName("www.baidu.com").isReachable(3000);
} catch (IOException e) {
return false;
} }
}

JMX监控

相对于http监控,也提供了jmx的监控方式;

典型使用方式使用的是jdk的 jconsole,使用jmx协议连接本地的jvm,进行监控,MBean下的Health下可以查看到返回信息;以此来进行监控。

原创不易,转载请注明出处。

0109 springboot的部署测试监控的更多相关文章

  1. springboot热部署与监控

    一.热部署 添加依赖+Ctrl+F9 <dependency> <groupId>org.springframework.boot</groupId> <ar ...

  2. SpringBoot 原理分析、监控、项目部署

    目录 SpringBoot 监控 概述 使用 SpringBoot Admin 概述 使用 SpringBoot 项目部署 SpringBoot 监控 概述 SpringBoot 自带监控功能 Act ...

  3. SpringBoot系列——admin服务监控

    前言 springboot项目部署起来后,如何实时监控项目的运行状况呢?本文记录使用springboot-admin对服务进行监控. springboot-admin介绍:https://codece ...

  4. CentOS 6.5安装部署Zabbix监控系统

    CentOS 6.5安装部署Zabbix监控系统 先说一点废话,我没有用centos7做实验,讲真,centos 7我也不常用,喜欢新版本的同学其实可以尝试下,注意一点的就是centos 6.5只支持 ...

  5. SpringBoot(六):springboot热部署

    在j2ee项目开发中,热部署插件是JRebel.JRebel的使用为开发人员带来了极大的帮助,且挺高了开发便捷.而在SpringBoot开发生态环境中,SpringBoot热部署常用插件是:sprin ...

  6. SpringBoot热部署配置(基于Maven)

    热部署的意思是只要类中的代码被修改了,就能实时生效,而不用重启项目.spring-boot-devtools 是一个为开发者服务的一个模块,其中最重要的功能就是自动应用代码更改到最新的App上面去.原 ...

  7. HDFS部署测试记录(2019/05)

    目录 HDFS部署测试记录 0.HDFS基础知识 1.基本组成结构与文件访问过程 2.NameNode启动时如何维护元数据 3.HDFS文件上传流程 1.系统环境 1.安装大致记录: 2.磁盘分区 3 ...

  8. 部署zabbix监控平台(源码安装)

    案例:部署Zabbix监控平台 1 问题 本案例要求部署一台Zabbix监控服务器,一台被监控主机,为进一步执行具体的监控任务做准备: 安装LNMP环境 源码安装Zabbix 安装监控端主机,修改基本 ...

  9. CentOS 7.4 下安装部署Nagios监控系统详细攻略(三)

    Nagios是一个流行的电脑系统和网络监控程序,它检测主机和服务,当异常发生和解除时能提醒用户.它是基于GPLv2开发的开源软件,可免费获得及使用. nagios工作原理 nagios的功能是监控服务 ...

随机推荐

  1. nginx 的提升多个小文件访问的性能模块

    阿里开源的第三方模块下载地址:https://github.com/alibaba/nginx-http-concat 下载模块 git clone https://github.com/alibab ...

  2. leetcode刷题-- 5. 动态规划

    动态规划思路 参考 状态转移方程: 明确「状态」-> 定义dp数组/函数的含义 -> 明确「选择」-> 明确 base case 试题 53最大子序和 题目描述 53 给定一个整数数 ...

  3. 1014 Waiting in Line (30分)

    1014 Waiting in Line (30分)   Suppose a bank has N windows open for service. There is a yellow line i ...

  4. WCF服务调用方式

    WCF服务调用通过两种常用的方式:一种是借助代码生成工具SvcUtil.exe或者添加服务引用的方式,一种是通过ChannelFactory直接创建服务代理对象进行服务调用.

  5. Centos7 使用yum安装MariaDB与MariaDB的简单配置与使用

    一.mariadb的安装 MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可. 开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜 ...

  6. Python 之网络编程之socket(2)黏包现象和socketserver并发

    一:黏包 ###tcp协议在发送数据时,会出现黏包现象.     (1)数据粘包是因为在客户端/服务器端都会有一个数据缓冲区,     缓冲区用来临时保存数据,为了保证能够完整的接收到数据,因此缓冲区 ...

  7. 124、Java面向对象之引用传递实例二,String类型按值传递

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  8. 一文解读SDN (转)

    一. 什么是SDN? SDN字面意思是软件定义网络,其试图摆脱硬件对网络架构的限制,这样便可以像升级.安装软件一样对网络进行修改,便于更多的APP(应用程序)能够快速部署到网络上. 如果把现有的网络看 ...

  9. XPath 和 CSS

    1.XPath XPath 即 XML 路径语言 (XML Path Language),他是一种用来确定 xml 文档中某部分位置的语言. xml文档(html 属于 xml)是由一系列节点构成的树 ...

  10. idea配置使用

    1.下载时注意连带下载git 2.实时清除内存 打开 show memory indicator 3.插件安装  前端常用插件(vue,element,css,html,node,ts等)找到适合自己 ...