docker run -d -p 4242:4242 -p 8428:8428 -v /opt/victoria:/victoria-metrics-data --name victoria victoriametrics/victoria-metrics:latest --opentsdbHTTPListenAddr=:4242 --retentionPeriod=36d
docker run -d -p 4242:4242 -p 8428:8428 -v /opt/victoria:/victoria-metrics-data:rw --name victoria victoriametrics/victoria-metrics:latest --opentsdbHTTPListenAddr=:4242 --retentionPeriod=36d
curl -H "Content-Type: application/json" -d "{\"metric\":\"x.y.z\",\"value\":45.34,\"tags\":{\"t1\":\"v1\",\"t2\":\"v2\"}}" http://localhost:4242/api/put
curl -H 'Content-Type: application/json' -d '{"metric":"x.y.z","value":45.34,"tags":{"t1":"v1","t2":"v2"}}' http://localhost:4242/api/put
GET | POST http://localhost:8428/api/v1/query?query=...&time=...&step=...
GET | POST http://localhost:8428/api/v1/query_range?query=...&start=...&end=...&step=...
默认情况下,Victoria Metrics 不会立即返回最近写入的样本。-search.latencyOffset相反,它检索在命令行标志指定的时间之前写入的最后结果,该时间的默认偏移量为 30 秒。query对于和来说都是如此,query_range并且可能会给人这样的印象:数据写入 VM 时有 30 秒的延迟
此标志可防止由于在最后的抓取间隔中仅抓取部分值而导致不一致的结果
docker run -d -p 4242:4242 -p 8428:8428 -v C:/victoria:/victoria-metrics-data --name victoria victoriametrics/victoria-metrics:latest --opentsdbHTTPListenAddr=:4242 --retentionPeriod=36d --httpAuth.username=admin -httpAuth.password=123456
public static void main2(String[] args) {
String url = "http://localhost:4242/api/put";
String username = "admin";
String password = "123456";
// 构建请求JSON数据
String jsonData = "{\"metric\":\"point\",\"value\":45.34,\"tags\":{\"t1\":\"v1\",\"t2\":\"v2\"}}";
try {
URL obj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
// 设置请求方法为 POST
conn.setRequestMethod("POST");
// 设置基本身份验证信息
String auth = username + ":" + password;
byte[] encodedAuth = java.util.Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8));
String authHeaderValue = "Basic " + new String(encodedAuth);
conn.setRequestProperty("Authorization", authHeaderValue);
// 设置请求头为 JSON 格式
conn.setRequestProperty("Content-Type", "application/json");
// 启用输出流,并写入请求体数据
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
os.write(jsonData.getBytes(StandardCharsets.UTF_8));
os.flush();
os.close();
// 获取响应
int responseCode = conn.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// 输出响应结果
System.out.println("Response Code: " + responseCode);
System.out.println("Response Body: " + response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String url = "http://localhost:4242/api/put";
String username = "admin";
String password = "123456";
// 构建请求JSON数据
String jsonData = "{\"metric\":\"point\",\"value\":40.34,\"tags\":{\"t1\":\"v1\",\"t2\":\"v2\"}}";
// 发送POST请求
HttpResponse response = HttpRequest.post(url)
.basicAuth(username, password)
.body(jsonData)
.header("Content-Type", "application/json")
.execute();
// 获取响应
int statusCode = response.getStatus();
String responseBody = response.body();
// 输出响应结果
System.out.println("Response Code: " + statusCode);
System.out.println("Response Body: " + responseBody);
}
<dependency>
<groupId>cn.hutoolgroupId>
<artifactId>hutool-httpartifactId>
<version>5.7.6version>
dependency>