Spring Boot开发一个小型的Http服务监控程序序

周末的时候在家里折腾了一下Prometheus,感觉它部署方便、功能强大。本来也是想用在实际的项目中,但是领导却觉得这个东西还是太复杂了,因为我们完全用不到那么高级,我我我我我我也不敢继续谏言,毕竟我只是一个刚来的临时工,所以就自己做一个简单的监控程序吧。

这里我用到的环境如下:

  • ideaIU-2019.3.3.win
  • Mybatis-Plus
  • Junit4及Junit插件
  • MariaDB

IDEA安装Junit插件

Junit也比较流行的单元测试工具类,这里也提供了IDEA插件,可以自动生成单元测类,使得单元测试更加快捷方便,安装步骤如下:
File->Settings->Plugins->搜索Junit->JUnit Generator V2.0->install,安装完成后需要重新才能生效哟

接下还需要对IDEA进行设置:File->Settings->Other Generator->JUnit Generator这里的Output Path根据自己的情况进行设置

快速生成测试类有两种方式,一种是在要测试的方式边快捷键Ctrl+Shift+T或者右键Go To->Test

最后会生成这样的测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import org.junit.Test;

import static org.junit.Assert.*;

/**
* @PACKAGE_NAME:
* @Author eyiadmin
* @Date 2020/3/10 8:30
* @Version 1.0
* @
*/
public class WechatMsgSenderTest {

@Test
public void send() {
}
}

整合Quartz

在Spring Boot中提供了Starter,所以整合起来也是非常方便,直接引入相应包即可

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>

在配置文件做如下配置

1
2
3
4
5
6
7
8
9
10
spring:
datasource:
url: jdbc:mariadb://127.0.0.1:3306/monitor?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
username: root
password: root
driver-class-name: org.mariadb.jdbc.Driver
quartz:
job-store-type: jdbc
jdbc:
initialize-schema: never #在第一次启动时,可以将这个值设置为ALWAYS,即可在指定的数据库中初始化相应的表结构

启动起来就会看到相应的信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2020-03-10 12:43:30.317  INFO 21824 --- [           main] org.quartz.core.SchedulerSignalerImpl    : Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
2020-03-10 12:43:30.317 INFO 21824 --- [ main] org.quartz.core.QuartzScheduler : Quartz Scheduler v.2.3.2 created.
2020-03-10 12:43:30.324 INFO 21824 --- [ main] o.s.s.quartz.LocalDataSourceJobStore : Using db table-based data access locking (synchronization).
2020-03-10 12:43:30.326 INFO 21824 --- [ main] o.s.s.quartz.LocalDataSourceJobStore : JobStoreCMT initialized.
2020-03-10 12:43:30.327 INFO 21824 --- [ main] org.quartz.core.QuartzScheduler : Scheduler meta-data: Quartz Scheduler (v2.3.2) 'quartzScheduler' with instanceId 'NON_CLUSTERED'
Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
NOT STARTED.
Currently in standby mode.
Number of jobs executed: 0
Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
Using job-store 'org.springframework.scheduling.quartz.LocalDataSourceJobStore' - which supports persistence. and is not clustered.

2020-03-10 12:43:30.327 INFO 21824 --- [ main] org.quartz.impl.StdSchedulerFactory : Quartz scheduler 'quartzScheduler' initialized from an externally provided properties instance.
2020-03-10 12:43:30.327 INFO 21824 --- [ main] org.quartz.impl.StdSchedulerFactory : Quartz scheduler version: 2.3.2
2020-03-10 12:43:30.327 INFO 21824 --- [ main] org.quartz.core.QuartzScheduler : JobFactory set to: org.springframework.scheduling.quartz.SpringBeanJobFactory@7e1ffe70

使用Antd开发作业管理界面

先使用模版新建一个项目git clone --depth=1 https://github.com/sendya/ant-design-pro-vue.git MinMonitor,进入到MinMonitor目录,先执行cnpm install安装依赖包。接下来就新建一个页面JobList.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<template>
<div>
<a-table :columns="columns" :dataSource="dataSource">
</a-table>
</div>
</template>

<script>
import { getJobList } from '@/api/manage'

export default {
name: 'JobList',
components: {

},
data () {
return {
mdl: {},
// 高级搜索 展开/关闭
advanced: false,
// 查询参数
queryParam: {},
dataSource: [],

// 表头
columns: [{
title: '作业名称',
dataIndex: 'jobName',
key: 'jobName'
}, {
title: '作业组',
dataIndex: 'jobGroupName',
key: 'jobGroupName'
}, {
title: '作业状态',
dataIndex: 'jobStatus',
key: 'jobStatus'
}],
// 加载数据方法 必须为 Promise 对象
loadData: () => {
return getJobList({})
.then(res => {
this.dataSource = res.result
})
}

}
},
created () {
this.loadData()
},
methods: {
}
}
</script>

这里我开发了一个后端接口,需要和Antd中的mock一起使用,所以这里需要配置一下代理,在vue.config.js下面做如下修改

1
2
3
4
5
6
7
8
9
10
11
12
13
devServer: {
// development server port 8000
port: 8000,

proxy: {
'/api/v1': { // 匹配模式,所有api开始的连接都匹配到下面服务器地址
target: 'http://127.0.0.1:8080', // 这是开发服务器的地址
ws: false,
changeOrigin: true

}
}
},

这个配置是意思是,只要匹配到/api/v1这个路径就会转发到127.0.0.1:8080这个地址

You forgot to set the qrcode for Alipay. Please set it in _config.yml.
You forgot to set the qrcode for Wechat. Please set it in _config.yml.
You forgot to set the business and currency_code for Paypal. Please set it in _config.yml.
You forgot to set the url Patreon. Please set it in _config.yml.
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×