spring cloud圖形化dashboard是如何實現指標的收集展現的

spring cloud圖形化dashboard是如何實現指標的收集展現的

一、dashboard圖形化界面入口

http://localhost:10000/hystrix.streamspring

說明:端口是由配置文件server.port=10000來指定的,能夠修改。json

打開後能夠看到以下的界面瀏覽器

clipboard.png

輸入須要監控的集羣,而後點擊Monitor Stream按鈕,進行集羣監控
這邊假設輸入
http://localhost:10000/turbine.stream?cluster=default
能夠看到下面的界面網絡

clipboard.png

注:若是看到的是空白頁面,須要訪問一下開啓了@HystrixCommand註解的rest方法,本文中有兩個方法,hello、hellosleep方法。app

@GetMapping("/hello")
    @HystrixCommand(fallbackMethod = "helloFallback")
    public String hello() {
        return "provide hello world";
    }
@GetMapping("/hellosleep")
    @HystrixCommand(fallbackMethod = "helloexFallback")
    public String hellosleep() throws InterruptedException {
        int i = 2000;
        TimeUnit.MILLISECONDS.sleep(i);
        return "provide hellosleep world!";
    }

二、dashboard是如何實現監控數據的獲取的。

咱們經過瀏覽器工具打開網絡監控,或者使用F12快捷鍵查看監控的url。
以下:ide

clipboard.png

url:
http://localhost:10000/proxy.stream?origin=http%3A%2F%2Flocalhost%3A10000%2Fturbine.stream%3Fcluster%3Ddefault函數

咱們看到,它是經過proxy.stream來獲取數據,有一個origin參數,就是前面咱們輸入的想要監控的那個url地址,即【http://localhost:10000/turbine.stream?cluster=default】。proxy.stream其實對應的是一個servlet
查看源碼:
HystrixDashboardConfiguration能夠看到它有一個內部類ProxyStreamServlet就是用來處理這個url的。
關鍵源碼以下:工具

clipboard.png

這個servlet其實就是經過origin來獲取數據,若是咱們直接訪問origin所對應的地址,能夠看到以下的數據,像流同樣源源不斷的打印出來。url

clipboard.png

圖形化的界面其實就是從這個servlet獲取數據,並展現的spa

三、servlet怎麼把流數據返回

httpget = new HttpGet(proxyUrl);
        HttpClient client = ProxyConnectionManager.httpClient;
        HttpResponse httpResponse = client.execute(httpget);
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode == HttpStatus.SC_OK) {
            is = httpResponse.getEntity().getContent();
            //省略代碼...
            OutputStream os = response.getOutputStream();
            int b = -1;
            while ((b = is.read()) != -1) {
                try {
                    os.write(b);
                    if (b == 10 /** flush buffer on line feed */
                            ) {
                        os.flush();
                    }
                }
                catch (Exception ex) {
                    //省略代碼...
                }
            }
        }

servlet其實就是經過url去獲取響應結果,而後不斷的輸出到前臺頁面。

四、前臺界面怎麼展現不斷響應回來的流數據呢

這邊主要用到了HTML5的一個對象EventSource,能夠獲取到這個數據。
注意: EventSource不支持IE瀏覽器,這邊使用谷歌瀏覽器

咱們仍是經過瀏覽器工具,查看圖形化界面的使用到js腳本。
以下:

clipboard.png

EventSource對象能夠對url進行監聽,並註冊響應函數。這裏不進行展開,有興趣的同窗能夠詳細的閱讀裏面的腳本。

五、總結

turbine收集的數據是一種json格式的數據,並且像流同樣不斷輸出。因此咱們須要藉助圖形化工具來展現。而圖形化工具的數據訂閱咱們經過上面的分析已經知道,其實他就是經過servlet來訪問turbine的連接來獲取數據並展示的。

相關文章
相關標籤/搜索