zoukankan      html  css  js  c++  java
  • Spring Cloud微服务实战 打造企业级优惠券系统 3-12 SpringBoot Actuator监控2(自定义端点)

    0    课程地址

    http://coding.imooc.com/lesson/380.html#mid=28363 

    1    浓缩精华
    1.1  浓缩精华

    4.1,4.2

    2    个人关注
    3    课程内容
    4    代码演练
    4.1  自定义端点

    配置类:

    package com.imooc.springboot.application.config;
    
    import com.imooc.springboot.application.endpoint.DateTimeEndPoint;
    import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * EndPointConfig
     *
     * @author 魏豆豆
     * @date 2021/4/5
     */
    @Configuration
    public class EndPointConfig {
    
        //自己,大概是读取datetimeEndPoint的,设置端点
        //记一下
        @Bean
        @ConditionalOnMissingBean//当该bean缺少的时候注入bean
        @ConditionalOnEnabledEndpoint//当监控端点开启的时候(application.yml打开监控端点)才会注入应用程序
        public DateTimeEndPoint dateTimeEndPoint(){
            return new DateTimeEndPoint();
        }
    }

     自定义端点类

    package com.imooc.springboot.application.endpoint;
    
    import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
    import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * DateTimeEndPoint
     *
     * @author 魏豆豆
     * @date 2021/4/5
     */
    @Endpoint(id="datetime")
    public class DateTimeEndPoint{
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    
        /**
         * 读取端点注解
         * @return
         */
        @ReadOperation//显示监控指标
        public Map<String,Object> readPoint(){
            Map<String,Object> map = new HashMap();
            map.put("name","小魏");
            map.put("age","18");
            map.put("birthday",simpleDateFormat.format(new Date()));
            return map;
        }
    }

    打印日志:

    4.2  自定义端点(写入)

    配置类(同上)

    自定义端点类:

    package com.imooc.springboot.application.endpoint;
    
    import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
    import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
    import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * DateTimeEndPoint
     *
     * @author 魏豆豆
     * @date 2021/4/5
     */
    @Endpoint(id="datetime")
    public class DateTimeEndPoint{
        private String forMat = "yyyy-MM-dd";
    
        /**
         * 读取端点注解
         * @return
         */
        @ReadOperation//显示监控指标
        public Map<String,Object> readPoint(){
            Map<String,Object> map = new HashMap();
            map.put("name","小魏");
            map.put("age","18");
            map.put("birthday",new SimpleDateFormat(forMat).format(new Date()));
            return map;
        }
    
        //记一下
        @WriteOperation
        public void writePoint(String forMat){
            this.forMat = forMat;
        }
    
    }

    post方式写入日期格式

    get读取

    诸葛
  • 相关阅读:
    使用Eclipse进行远程调试【转】
    JRE_HOME environment variable is not defined correctly This environment variableis needed to run this program
    Window 通过cmd查看端口占用、相应进程、杀死进程等的命令【转】
    A cycle was detected in the build path of project
    调用CXF工具 生成 WSDL【转】
    解决cxf+spring发布的webservice,types,portType和message以import方式导入
    Target runtime com.genuitec.runtime.generic.jee50 is not defined
    修改eclipse启动时eclipse使用的jre
    JAVA中堆栈和内存分配原理
    JVM -Xss调整Stack Space的大小 【转】
  • 原文地址:https://www.cnblogs.com/1446358788-qq/p/14296105.html
Copyright © 2011-2022 走看看