spring-boot监控

spring-boot-actuator-monitor

基于spring boot的 监控平台

#spring-boot-admin 监控后台

  1. 新建一个maven工程 boot-admin , 继承 spring-boot-starter-parent

    1
    2
    3
    4
    5
    6

    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.4.RELEASE</version>
    </parent>
  2. 加入依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<optional>false</optional>
</dependency>
  1. 加入plugin 以 exec jar包运行

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15

    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
    <execution>
    <goals>
    <goal>repackage</goal>
    </goals>
    <configuration>
    <classifier>exec</classifier>
    </configuration>
    </execution>
    </executions>
    </plugin>
  2. 新建执行执行类 com.izerui.boot.admin.Application.java

1
2
3
4
5
6
7
8
9
@SpringBootApplication
@EnableAdminServer
@EnableDiscoveryClient
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
  1. 声明监控后台的应用名称、端口、上下文、基本授权验证用户名、密码等信息 application.properties
1
2
3
4
5
6
7
# application
server.port = 8888

# basic security
security.ignored=/api/**
security.user.name=boot
security.user.password=boot1234
  1. mvn install 后 执行 java -jar boot-admin-exec.jar

  2. 访问 http://主机ID:端口/boot-admin


注册应用到监控后台

  1. 确保应用继承 spring-boot-starter-parent

    1
    2
    3
    4
    5
    6

    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.4.RELEASE</version>
    </parent>
  2. 引入依赖

1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<optional>false</optional>
</dependency>
  1. 配置被监控的应用的名称和监控后台地址 application.properties

    jar 方式运行

    web application

    server.port=28584
    info.version=@pom.version@
    info.info=微信代理服务
    management.context-path=/management
    server.context-path=/@pom.artifactId@
    endpoints.jmx.domain=@pom.artifactId@
    spring.application.name=@pom.artifactId@

    boot admin

    spring.boot.admin.url=http://192.168.1.128:8888/boot-admin


    war 方式运行

    web application

    info.version=@pom.version@
    info.info=阿里支付服务
    management.context-path=/management
    endpoints.jmx.domain=@pom.artifactId@
    spring.application.name=@pom.artifactId@

    boot admin

    spring.boot.admin.url=http://192.168.1.128:8888
    spring.boot.admin.client.service-url=http://192.168.1.106:8098/qq-pay-web

注意: 别忘了添加logging配置
# LOGGING
logging.file=/tmp/logs/offline-proxy-tcp.log

注意: 有些应用被权限框架拦截了。故需要加入 
management.context-path=/management 
并在权限框架内 将 /management/** = anon 权限放开,声明url注意上下文。
  1. 配置logback的jmx接口调用

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <jmxConfigurator/>
    <logger name="com.izerui" level="DEBUG"/>
    <root level="INFO">
    <appender-ref ref="CONSOLE" />
    <appender-ref ref="FILE" />
    </root>
    </configuration>
  2. 发布自定义jmx接口

在spring bean 类的头信息上增加注解 @ManagedResource(currencyTimeLimit = 20) 和 @Lazy

声明属性: @ManagedAttribute(description = "当前TCP连接数")

声明方法: @ManagedOperation(description = "关闭对应IP的所有连接")
          @ManagedOperationParameters(
            @ManagedOperationParameter(name = "ip", description = "终端IP")
          )

参考资料: