监控的意义
监控服务状态是否宕机。
监控服务运行指标(内存、虚拟机、线程、请求等)。
监控日志。
管理服务(服务下线)。
监控的实施方式:
1.显示监控信息的服务器:用于获取服务信息,并显示对应的信息。
2.运行的服务:启动时主动上报,告知监控服务器自己需要受到监控。
(可以控制台输入jconsole看Java程序的状态)
可视化监控平台
Spring Boot Admin,开源社区项目,用于管理和监控SpringBoot应用程序。 客户端注册到服务端后,通过HTTP请求方式,服务端定期从客户端获取对应的信息,并通过UI界面展示对应信息。
Admin服务端
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-dependencies</artifactId>
<version>${spring-boot-admin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Admin客户端
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-dependencies</artifactId>
<version>${spring-boot-admin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Admin服务端
server:
port: 8080
设置启动Spring-Admin
@SpringBootApplication
@EnableAdminServer
public class Springboot25AdminApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot25AdminApplication.class, args);
}
}
Admin客户端
server:
port: 80
spring:
boot:
admin:
client:
url: http://localhost:8080
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: '*'
监控原理
Actuator提供了SpringBoot生产就绪功能,通过端点的配置与访问,获取端点信息。
端点描述了一组监控信息,SpringBoot提供了多个内置端点,也可以根据需要自定义端点信息。
访问当前应用所有端点信息:/actuator。
访问端点详细信息:/actuator/端点名称。
启动指定端点
management:
endpoint:
health: # 端点名称
enabled: true
show-details: always
beans: # 端点名称
enabled: true
启动所有端点
management:
endpoints:
enabled-by-default: true
为info端点添加自定义指标
info:
appName: @project.artifactId@
version: @project.version@
author: itheima
为info端点添加自定义指标
@Component
public class AppInfoContributor implements InfoContributor {
@Override
public void contribute(Info.Builder builder) {
Map<String,Object> infoMap = new HashMap<>();
infoMap.put("buildTime","2006");
builder.withDetail("runTime",System.currentTimeMillis())
.withDetail("company","传智教育");
builder.withDetails(infoMap);
}
}
为Health端点添加自定义指标
@Component
public class AppHealthContributor extends AbstractHealthIndicator {
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
boolean condition = true;
if(condition){
Map<String,Object> infoMap = new HashMap<>();
infoMap.put("buildTime","2006");
builder.withDetail("runTime",System.currentTimeMillis())
.withDetail("company","传智教育");
builder.withDetails(infoMap);
builder.status(Status.UP);
}else{
builder.status(Status.DOWN);
}
}
}
自定义监控指标
为Metrics端点添加自定义指标
@Service
public class BookServiceImpl extends ServiceImpl<BookDao, Book> implements IBookService {
private Counter counter;
public BookServiceImpl(MeterRegistry meterRegistry){
counter = meterRegistry.counter("用户付费操作次数:");
}
@Override
public boolean delete(Integer id) {
counter.increment();
return bookDao.deleteById(id) > 0;
}
}
自定义端点
@Component
@Endpoint(id="pay")
public class PayEndPoint {
@ReadOperation
public Object getPay(){
//调用业务操作,获取支付相关信息结果,最终return出去
Map payMap = new HashMap();
payMap.put("level 1",103);
payMap.put("level 2",315);
payMap.put("level 3",666);
return payMap;
}
}