Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

javascript - 如何以Java或Javascript连续接收Docker统计信息(How to continuously receive docker stats in Java or Javascript)

I've pulled docker stats using docker API endpoint http://localhost:2375/containers/containerId/stats?stream=0 ( stream=0 will pull stats once only) (see my answer here ) and used the response to calculate memory and CPU usage percentage of that container.

(我已经使用Docker API端点http://localhost:2375/containers/containerId/stats?stream=0stream=0将仅提取一次统计信息)拉取了docker统计信息(请参阅此处的答案),并使用响应来计算内存以及该容器的CPU使用率百分比。)

My requirement is that I want to show a continuous graph of memory and CPU usage percentages over time.

(我的要求是我想显示一段时间内内存和CPU使用率百分比的连续图表。)

I know this can be done by repeatedly pulling stats using AJAX after let's say 2 seconds and update the graph but that is not how I want to do it.

(我知道这可以通过在2秒后再使用AJAX重复获取统计信息并更新图表来完成,但这不是我想要的方式。)

Now the docker stats API is over HTTP which is a request-response protocol, so have can I receive the continuous stream of JSON given from http://localhost:2375/containers/containerId/stats?stream=1 if the response will never finish as it is a continuous stream?

(现在docker stats API是通过HTTP发出的,HTTP是一个请求-响应协议,因此我可以从http://localhost:2375/containers/containerId/stats?stream=1提供连续的JSON流http://localhost:2375/containers/containerId/stats?stream=1是连续流吗?)

This is what I tried-

(这就是我尝试的)

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.client.RestTemplate;

@RestController
public class DockerController {
    @GetMapping("/getStats/{containerId}")
    public String getStats(@PathVariable String containerId){
        String statistics = null;
        RestTemplate restTemplate =  new RestTemplate();
        statistics = restTemplate.getForObject("http://localhost:2375/containers/"+containerId+"/stats?stream=1", String.class);
        if(statistics != null){
            System.out.println("Container statistics - 
" + statistics); //I'll never receive this
        }
        return statistics;
    }
}

I've just started reading WebSockets and as per my understanding, both stream producer and consumer need to communicate over ws protocol.

(我刚刚开始阅读WebSockets,据我了解,流生产者和消费者都需要通过ws协议进行通信。)

But as docker API endpoint is already producing the stream of JSON over HTTP I think WebSockets won't work here.

(但是由于docker API端点已经通过HTTP生成JSON流,因此我认为WebSockets在这里不起作用。)

Any help would be greatly appreciated.

(任何帮助将不胜感激。)

  ask by Mithilesh Tipkari translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...