首页 > 其他分享 >springboot 自动配置 自动监控demo

springboot 自动配置 自动监控demo

时间:2023-01-12 18:11:08浏览次数:42  
标签:springboot demo 自动 key import jd org com ump

1、注解定义

@Target({java.lang.annotation.ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Ump {

    public abstract String key() default "";

}

2、配置数据类定义

package com.jd.rcloud.common.config;

import com.jd.rcloud.common.constant.UmpConstant;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;


@Data
@ConfigurationProperties(prefix = "cms.ump")
public class UmpProperties {

    /**
     * App Name
     */
    private String app;
    
    private String env;

    
    private String prefix;

    /**
     * key值是否需要包含应用名称
     *
     */
    private boolean keyStartWithApp = true;

    /**
     * 心跳key
     */
    private String systemKey;

    /**
     * jvm 监控的key
     */
    private String jvmKey;

    /**
     * 获取ump平台需要的应用名称
     *
     * @return jdos上应用名称
     */
    public String getUmpAppName() {
        if(StringUtils.isBlank(this.app)) {
            return null;
        }else if(this.app.startsWith(UmpConstant.JDOS)) {
            return this.app;
        } else {
            return UmpConstant.JDOS + this.app;
        }
    }


    @Override
    public String toString() {
        return "UmpProperties{" +
                "app='" + app + '\'' +
                ", env='" + env + '\'' +
                ", prefix='" + prefix + '\'' +
                ", keyStartWithApp='" + keyStartWithApp + '\'' +
                ", systemKey='" + systemKey + '\'' +
                ", jvmKey='" + jvmKey + '\'' +
                '}';
    }
}

3、自动配置类定义:

package com.jd.rcloud.common.config;

import com.jd.rcloud.common.ump.UmpAop;
import com.jd.rcloud.common.util.UmpKeyBuilder;
import com.jd.rcloud.common.util.UmpUtil;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.PostConstruct;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.jd.ump.profiler.proxy.Profiler;


@Slf4j
@Configuration
@ConditionalOnClass(UmpAop.class)
@EnableConfigurationProperties(UmpProperties.class)
public class UmpAutoConfigure implements InitializingBean {

    @Autowired
    private UmpProperties umpProperties;

    @PostConstruct
    public void init() {
        UmpKeyBuilder.setUmpProperties(umpProperties);
        UmpUtil.setUmpProperties(umpProperties);

        if(log.isInfoEnabled()) {
            log.info("decorate.ump init. config: [{}]", umpProperties);
        }
    }

    @Bean
    @ConditionalOnMissingBean
    public UmpAop umpAspect() {
        return new UmpAop();
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        if (StringUtils.isNotBlank(umpProperties.getSystemKey())) {
            String key = UmpKeyBuilder.genKey(umpProperties.getSystemKey());
            Profiler.InitHeartBeats(key);
        }

        if (StringUtils.isNotBlank(umpProperties.getJvmKey())) {
            String key = UmpKeyBuilder.genKey(umpProperties.getJvmKey());
            Profiler.registerJVMInfo(key);
        }
    }
}

4、切面的定义:

package com.jd.rcloud.common.ump;

import com.jd.rcloud.common.ann.Ump;
import com.jd.rcloud.common.config.UmpProperties;
import com.jd.rcloud.common.util.UmpKeyBuilder;
import com.jd.ump.profiler.CallerInfo;
import com.jd.ump.profiler.proxy.Profiler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;


@Slf4j
@Order(Integer.MIN_VALUE)
@Aspect
@Component("umpAop")
public class UmpAop {

    @Autowired
    private UmpProperties umpProperties;

    @Around("@annotation(com.jd.rcloud.common.ann.Ump)")
    public Object invoke(ProceedingJoinPoint pjp) throws Throwable {
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method method = signature.getMethod();
        Ump ump = method.getAnnotation(Ump.class);
        if (ump == null) {
            return pjp.proceed();
        }

        //必须设置appName ump才能生效
        String appName = umpProperties.getUmpAppName();
        if (StringUtils.isBlank(appName)) {
            if(log.isWarnEnabled()) {
                log.warn("property[deorate.ump.app] not found");
            }
            return pjp.proceed();
        }

        // 获取key
        String key = ump.key();
        if (StringUtils.isBlank(key)) {
            key = UmpKeyBuilder.genKey(pjp);
        } else {
            key = UmpKeyBuilder.genKey(key);
        }


        CallerInfo info = null;
        try {
            info = Profiler.registerInfo(key, appName, ump.heartbeat(), ump.tp());
            return pjp.proceed();
        } catch (Throwable throwable) {
            log.error("{}.{} throw exception", signature.getDeclaringTypeName(), signature.getName(), throwable);

            if (info != null && ump.error()) {
                if(shouldRecord(throwable, ump.excludes())) {
                    Profiler.functionError(info);
                } else {
                    if(log.isDebugEnabled()) {
                        log.debug("Profiler.functionError ignored, key={} clazz={}", key, throwable.getClass().getName());
                    }
                }
            }
            throw throwable;
        } finally {
            if (info != null) {
                Profiler.registerInfoEnd(info);

                if(log.isDebugEnabled()) {
                    log.debug("UmpAspect appName: {}, key: {}, elapsed time: {} ms", appName, key, info.getElapsedTime());
                }

            }
        }
    }

    /**
     * 判断是否需要统计可用率
     *
     * @param throwable
     * @param clazzArr
     * @return
     */
    private boolean shouldRecord(Throwable throwable, Class<? extends Exception>[] clazzArr) {
        if(throwable == null || clazzArr == null || clazzArr.length == 0) {
            return true;
        }

        for(Class<? extends Exception> clzz : clazzArr) {
            if(throwable.getClass().equals(clzz)) {
                return false;
            }
        }

        return true;
    }
}

 

标签:springboot,demo,自动,key,import,jd,org,com,ump
From: https://www.cnblogs.com/niun/p/17047461.html

相关文章

  • IDEA的Services中添加SpringBoot启动和npm启动
    1、添加SpringBoot启动点击页面最下方Services,在弹出框中点击左上角最右侧的+,在点击RunConfigurationType,在弹出框AddConfigurationType中找到SpringBoot点击......
  • 【SpringBoot】配置篇
      POM.XML<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"......
  • 用Python找出了删除自己微信的所有人并将他们自动化删除了
    哈喽兄弟们,今天我们来试试用Python找出了删除自己微信的所有人,并将他们自动化删除,免得每次看到感叹号心塞。这不,昨晚小姨子一个人喝多了喊我去接她,但是我睡到现在,刚发......
  • 使用python自动发送图片给微信好友
    使用python自动发送图片给微信好友由于pyautogui模块,可以基于UI操作鼠标和键盘,但是无法直接发送图片所以引入了pyperclip模块,直接在运行窗口,打开+复制图片,然后在微信输......
  • Web自动化测试03:Selenium安装配置,详细教程
    文章目录​​系列文章目录​​​​学习目标​​​​一、环境搭建​​​​1.1安装selenium包​​​​1.2安装浏览器驱动​​​​火狐浏览器:​​​​谷歌浏览器:​​​​Edge......
  • sikulix___自动化办公___重复性_机械性_的电脑操作___python脚本___Java运行环境下德j
    sikulix___自动化办公___重复性_机械性_的电脑操作___python脚本___Java运行环境下德jar包完成自动化测试相关___截图编程控制键盘鼠标  应用场景:      公司......
  • SpringBoot不同环境的配置文件讲解
    前言​ 源于工作中发现项目中的的application.yml有多样的application.yml,如下图所示:​ 了解过后发现是因为测试环境和生产环境一些配置可能会有差异,比如数据库的地址、......
  • springboot集成minio
    1、docker安装miniosudodockerrun-d--nameminio--restart=always-p9000:9000-p9001:9001-e"MINIO_ROOT_USER=minioroot"-e"MINIO_ROOT_PASSWORD=minioroot......
  • SpringBoot系列之Redis实现消息队列
    1前言本教程主要讲解的是如何在SpringBoot中用Redis实现消息队列。学习本教程的前提知识点是有SpringBoot、Redis、消息队列等基础。所以默认各位大佬已经掌握以上知识点。......
  • Springboot 整合JPA
    Springboot整合JPAhttps://blog.csdn.net/qq_21344887/article/details/123847180Jpa1.JPA是什么JavaPersistenceAPI:用于对象持久化的APIJavaEE5.0平台标......