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=0
( stream=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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…