前言
本文來分析GaugeService的實現,其類圖如下:
解析
GaugeService
GaugeService–> 1個可以用來提交1個被命名的duble值為了存儲和分析的服務.任意的統計和分析需要留給其他人去做,但是最終還是需要被該接口的實現所控制.舉例說明:這個值可能是通過1個定時任務提交的,并且保存在后端的直方圖中以進行比較。或者 可以是一個傳感器值的簡單測量(比如溫度讀數)以原始形式傳遞給監測系統.
spring boot注解詳解?其只聲明了1個方法,如下:
// 設置給定的測量值
void submit(String metricName, double value);
DefaultGaugeService
DefaultGaugeService實現了GaugeService接口.該類是JDK1.8之前默認裝配的
其字段,構造器如下:
Spring boot?// 此時注入的是InMemoryMetricRepository
private final MetricWriter writer;
// key-->metricName,value-->加入前綴后的metricName
private final ConcurrentHashMapnames = new ConcurrentHashMap();
public DefaultGaugeService(MetricWriter writer) {
this.writer = writer;
java.lang.throwable。}
submit 實現如下:
public void submit(String metricName, double value) {
this.writer.set(new Metric(wrap(metricName), value));
}
嘗試為傳入的metricName加上前綴.代碼如下:
springboot cms、private String wrap(String metricName) {
// 1. 如果緩存中有的話,則直接返回對應的值
String cached = this.names.get(metricName);
if (cached != null) {
return cached;
}
spring源碼,// 2. 如果傳入的metricName是gauge或者histogram或者timer開頭的則直接返回
if (metricName.startsWith("gauge") || metricName.startsWith("histogram")
|| metricName.startsWith("timer")) {
return metricName;
}
// 3. 為metricName 加上gauge.的前綴,放入到names緩存中,然后進行返回
springboot項目源碼。String name = "gauge." + metricName;
this.names.put(metricName, name);
return name;
}
如果緩存中有的話,則直接返回對應的值
如果傳入的metricName是gauge或者histogram或者timer開頭的則直接返回
springboot注解、為metricName 加上gauge.的前綴,放入到names緩存中,然后進行返回
實例化Metric
加入到InMemoryMetricRepository中.
GaugeBuffer
GaugeBuffer實現了Buffer接口,代碼如下:
public class GaugeBuffer extends Buffer {
怎樣分析springboot?private volatile double value;
public GaugeBuffer(long timestamp) {
super(timestamp);
this.value = 0;
}
@Override
spring boot vue?public Double getValue() {
return this.value;
}
public void setValue(double value) {
this.value = value;
}
springboot源碼解析,}
GaugeBuffers
GaugeBuffers繼承自Buffers,泛型參數為GaugeBuffer.該類是1.8以后默認裝配的.
createBuffer,直接實例化了1個GaugeBuffer,時間戳為當前時間,值為0.實現如下:
protected GaugeBuffer createBuffer() {
return new GaugeBuffer(0L);
springboot優秀開源項目。}
此外,還聲明了1個set方法–> 對給定名字的GaugeBuffer設置值.代碼如下:
public void set(final String name, final double value) {
doWith(name, new Consumer() {
@Override
public void accept(GaugeBuffer buffer) {
java integer.parseint。buffer.setTimestamp(System.currentTimeMillis());
buffer.setValue(value);
}
});
}
從父類中的buffers獲得給定名字所對應的GaugeBuffer,如果不存在,則創建1個
java cursor?將GaugeBuffer中的時間戳設為當前時間,并設置給定的值
使用案例
新建GaugeController.代碼如下:
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.metrics.GaugeService;
Java -jar?import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GaugeController {
@Autowired
private GaugeService gaugeService;
java lang illegalargument、@RequestMapping("/test-gauge")
public String testGauge() {
long start = System.currentTimeMillis();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
深入剖析springboot源碼、}
gaugeService.submit("test-gauge.use", System.currentTimeMillis()-start);
return "操作成功";
}
}
auge.test-gauge.use: 5005
java lang reflect invocation。來源:[]()
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态