项目在服务器上运行,对服务器或容器进行监控是必不可少的。spring-boot-admin提供了一套对容器进行监控的框架。在springboot上面配置非常简单。
先直接配置个例子跑起来看看效果。
服务端
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>1.5.0</version>
</dependency>
</dependencies>
application.yml
logging:
config: classpath:logback.xml
path: d:/logs
server:
port: 80
session-timeout: 60
spring:
application:
name: monitor-sba
boot:
admin:
url: http://localhost:${server.port}
management:
security:
enabled: false
info:
app:
name: "@project.name@" #从pom.xml中获取,pom.xml中需build中加入插件spring-boot-maven-plugin
description: "@project.description@"
version: "@project.version@"
spring-boot-version: "@project.parent.version@"
management.security.enabled设置为false,必须的
启动类Application.java
package com.fei.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import de.codecentric.boot.admin.config.EnableAdminServer;
@SpringBootApplication
@EnableAdminServer //激活sba
public class Application extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {
// configurableEmbeddedServletContainer.setPort(9090);
}
}
@EnableAdminServer不可缺少。服务端配置就这样OK了,还对自己进行了监控。启动,然后浏览器访问http://localhost
点击details
监控的信息还是能满足一般需求的。就不每个监控页面都贴图了。
客户端(被监控的服务器)
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>1.5.0</version>
</dependency>
</dependencies>
application.yml
logging:
config: classpath:logback.xml
path: d:/logs
server:
port: 8081
session-timeout: 60
spring:
boot:
admin:
client:
prefer-ip: true #解决window下无法识别主机名的问题
url: http://localhost #服务端的地址
management:
security:
enabled: false
info:
app:
name: "@project.name@" #从pom.xml中获取,pom.xml中需build中加入插件spring-boot-maven-plugin
description: "@project.description@"
version: "@project.version@"
spring-boot-version: "@project.parent.version@"
启动类Application.java
package com.fei.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.support.SpringBootServletInitializer;
@SpringBootApplication
public class Application extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {
// configurableEmbeddedServletContainer.setPort(9090);
}
}
启动后,监控页面就看到它了