指标监控
yaml配置:
**默认所有的Endpoint除过shutdown都是开启的**
**需要开启或者禁用 , 用Endpoints 配置模式为:**
#management : 是所有actuator的配置 (指标监控)
management:
#配置所有端点的默认行为
endpoints:
enabled-by-default: true #true : 开启所有指标监控
web:
exposure:
include: '*' #设置以web方式暴露所有端点 指标监控
**或者想指定开启某些指标**:
#management.endpoints.端点名.xxx: 对某个端点的具体配置
management:
endpoints:
enabled-by-default: false #关闭所有指标监控
web:
exposure:
include: '*' #以web方式暴露所有端点
#配置单个指标监控端点的默认行为
endpoint:
health:
show-details: always #health的详细信息 always 显示health的详细信息
enabled: true
info:
enabled: true
beans:
enabled: true
metrics:
enabled: true
Mevan : github 开源项目 admin web 显示指标监控
注意 : 要引入 spring-boot-starter-actuator : 指标监控功能
开启 指标监控以 **web** 方式暴露所有端点
分为 客户端 和 服务端
<!--引入 指标监控功能-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 引入监控 客户端 github上开源项目 -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.5.1</version>
</dependency>
<!-- 另一个项目 -->
<!-- 引入监控 服务端 github开源项目-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.5.1
自定义指标 —>health 注意(所有自定义指标监控 @Component 都要放入容器)
注意 : 当前类的命名规则是 : xxxxHealthIndicator
AbstractHealthIndicator 继承 HealthIndicator 封装好的抽象类
@Component
public class MyComHealthIndicator extends AbstractHealthIndicator {
/**
* 真实的检查方法
* @param builder
* @throws Exception
*/
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
//mongodb 获取连接进行测试
Map<String,Object> map = new HashMap<>();
if (1 == 1){
// builder.up(); //健康
builder.status(Status.UP);
map.put("count",1);
map.put("ms",100);
}else {
// builder.down(); //不健康
builder.status(Status.OUT_OF_SERVICE);
map.put("err","连接超时");
map.put("ms",3000);
}
builder.withDetail("code",100)
.withDetails(map);
}
}
声明指标监控 Endpoint
@Endpoint(id = “myservice”) : 声明设置这是一个 Endpoint 指标监控
@ReadOperation : 声明是一个端点的读操作
@WriteOperation : 声明是一个端点的写操作
@Component
@Endpoint(id = "myservice") //设置这是一个Endpoint
public class MyServiceEndPoint {
//@ReadOperation 声明是一个端点的读操作
@ReadOperation
public Map getDockerInfo(){
return Collections.singletonMap("dockerInfo","docker started.....");
}
//@WriteOperation 声明是一个端点的写操作
@WriteOperation
public void stopDocker(){
System.out.println("docker stopped....");
}
}
声明指标监控 info
继承 InfoContributor 自定义设置 info
@Component
public class AppInfoInfoContributor implements InfoContributor{
@Override
public void contribute(Info.Builder builder) {
builder.withDetail("function","web")
.withDetail("edition","1.0.0");
}
}
MyBatis-Plus 配置
Dao MyBatis-Plus 继承BaseMapper
BaseMapper 接口中 接口写出所有CRUD功能
IService
extends ServiceImpl
/**
* 继承BaseMapper<Bean>
*/
@Mapper
public interface User1Dao extends BaseMapper<User1> {
}
public interface User1Service extends IService<User1> {
}
@Service
public class User1ServiceImpl extends ServiceImpl<User1Dao, User1> implements User1Service {
}
分页插件
@Autowired
private User1Service user1Service;
@GetMapping("/dynamic_table") //RequestParam 获取表单中的pn , 没有的话默认是1
public String dynamic_table(Model model,@RequestParam(value = "pn",defaultValue = "1")Integer pn){
//分页查询数据
Page<User1> userPage = new Page<>(pn, 2);
//分页查询结果
Page<User1> page1 = user1Service.page(userPage, null);
//当前页
long current = page1.getCurrent();
//总页数
long pages = page1.getPages();
//总条记录
long total = page1.getTotal();
//查出数据库的数据
List<User1> records = page1.getRecords();
model.addAttribute("page",page1);
return "table/dynamic_table";
}
MyBatis-plus Configuration MyBatis-Plus 分页配置
@Configuration
public class MyBatisConfig {
/**
* MybatisPlusInterceptor
* @return
*/
@Bean
public MybatisPlusInterceptor paginationInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
//这是分页拦截器
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
//设置请求的页面大于最大页后操作, true 调回到首页, false 继续请求 默认false
paginationInnerInterceptor.setOverflow(true);
//设置最大单页限制数量, 默认500条, -1 不受限制
paginationInnerInterceptor.setMaxLimit(500L);
mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
return mybatisPlusInterceptor;
}
}
HandlerInterceptor接口 Spring 拦截器
/**
* 登录检查
* 1,配置好拦截器要拦截哪些请求
* 2,把这些配置放在容器中
*/
public class LoginInterceptor implements HandlerInterceptor {
/**
* 目标方法执行之前 返回true or false
* @param request
* @param response
* @param handler
* @return
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {}
/**
* 目标方法执行完成以后
* @param request
* @param response
* @param handler
* @param modelAndView
* @throws Exception
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
/**
* 页面渲染以后
* @param request
* @param response
* @param handler
* @param ex
* @throws Exception
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
HandlerInterceptor Configurtion
/**
* 1,编写一个拦截器实现 HandlerInterceptor 接口
* 2,拦截器注册到容器中(实现WebMvcConfigurer的addInterceptors)
* 3,指定拦截规则[如果是拦截所有,静态拦截也会被拦截]
*/
@Configuration
public class AdMainWebConfig implements WebMvcConfigurer {
/**
* Filter,Interceptor 几乎拥有相同的功能?
* 1,Filter: 是servlet定义的原生组件.好处,脱离Spring应用也能使用
* 2,Interceptor: 是Spring定义的接口.可以Spring的自动装配等功能
*/
@Autowired
RedisUrlCountInterceptor redisUrlCountInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
//可以 new 对象 或者 放入容器直接拿容器中的对象
registry.addInterceptor(new LoginInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/","/login","/css/**","/fonts/**","/images/**","/js/**,","/error/**"); //放行的请求
//可以 new 对象 或者 放入容器直接拿容器中的对象
registry.addInterceptor(redisUrlCountInterceptor)
.addPathPatterns("/**")
.excludePathPatterns("/css/**","/fonts/**","/images/**","/js/**,");
}
}