springcloud turbine

spring cloud turbine

簡介

turbine是聚合服務器發送事件流數據的一個工具,hystrix的監控中,只能監控單個節點,實際生產中都爲集羣,因此可以通過 
turbine來監控集羣下hystrix的metrics情況,通過eureka來發現hystrix服務。

netflix turbine

使用官方給定的war 
放入tomcat中運行,修改turbine-web-1.0.0/WEB-INF/classesconfig.properties文件

turbine.aggregator.clusterConfig=test
turbine.ConfigPropertyBasedDiscovery.test.instances=10.0.80.60,10.0.41.13
turbine.instanceUrlSuffix=:8080/configcenter-web/hystrix.stream


turbine.aggregator.clusterConfig 配置集羣名稱

turbine.ConfigPropertyBasedDiscovery.test.instances 配置集羣節點ip(用以發現服務,規則不限在ip列表)

turbine.instanceUrlSuffix 聚合實例訪問後綴

重啓tomcat後訪問http://localhost:${port}/turbine.stream?cluster=test 獲取聚合信息

spring cloud turbine

通過EnableTurbine註解啓用turbine,需要引入依賴:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-turbine</artifactId>
</dependency>

Application.java

package com.lkl.springcloud.turbine;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
/**
 * 創建turbine應用
 * Created by liaokailin on 16/5/1.
 */
@SpringBootApplication
@EnableTurbine
public class Application {
    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }
}

對應配置信息

server.port=9090
spring.application.name=turbine
turbine.appConfig=node01,node02
turbine.aggregator.clusterConfig= MAIN
turbine.clusterNameExpression= metadata['cluster']

turbine.appConfig 配置需要聚合的應用 
turbine.aggregator.clusterConfig turbine需要聚合的集羣名稱 通過 http://localhost:9090/turbine.stream?cluster=MAIN 訪問 
turbine.clusterNameExpression 獲取集羣名表達式,這裏表示獲取元數據中的cluster數據,在node01、node02爲配置對應信息

eureka服務

通過eureka做服務發現與註冊

EurekaServer.java

package com.lkl.springcloud.turbine;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
@EnableEurekaServer
public class EurekaServer {
    public static void main(String[] args) {
        new SpringApplicationBuilder(EurekaServer.class).properties(
                "spring.config.name:eureka", "logging.level.com.netflix.discovery:OFF")
                .run(args);
    }
}

對應配置信息 表明爲一個獨立的eureka服務

server.port=8761
spring.application.name=eureka
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false

Node

需要創建兩個節點組成集羣,同時向eureka註冊服務

Node01.java


package com.lkl.springcloud.turbine;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * Created by liaokailin on 16/5/4.
 */
@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
@EnableCircuitBreaker
@RestController
public class Node01 {
    public static void main(String[] args) {
        new SpringApplicationBuilder(Node01.class).properties(
                "spring.config.name:node01").run(args);
    }
    @Autowired
    private HelloService service;
    @RequestMapping("/")
    public String hello() {
        return this.service.hello();
    }
    @Component
    public static class HelloService {
        @HystrixCommand(fallbackMethod="fallback")
        public String hello() {
            return "Hello World";
        }
        public String fallback() {
            return "Fallback";
        }
    }
}

Node01調用hystrix command,對應配置


server.port= 8081 
spring.application.name=node01 
eureka.instance.hostname=localhost 
eureka.instance.metadata-map.cluster=MAIN 

配置比較簡單,需要注意的有eureka.instance.hostname,把Node02 展示出來再說明eureka.instance.hostname

Node02.java

package com.lkl.springcloud.turbine;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * Created by liaokailin on 16/5/4.
 */
@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
@EnableCircuitBreaker
@RestController
public class Node02 {
    public static void main(String[] args) {
        new SpringApplicationBuilder(Node02.class).properties(
                "spring.config.name:node02").run(args);
    }
    @Autowired
    private HelloService service;
    @RequestMapping("/")
    public String hello() {
        return this.service.hello();
    }
    @Component
 public static class HelloService {
        @HystrixCommand(fallbackMethod="fallback")
        public String hello() {
            return "Hello World";
        }
        public String fallback() {
            return "Fallback";
        }
    }
}

node02.properties

server.port= 8082
spring.application.name=node02
eureka.instance.hostname=mac
eureka.instance.metadata-map.cluster=MAIN

兩個節點中eureka.instance.hostname不同 
查看 cat /etc/hosts

127.0.0.1       mac
127.0.0.1   localhost
255.255.255.255 broadcasthost
::1             localhost

實質指向都爲127.0.0.1

這是由於turbine自身的一個bug,eureka.instance.hostname一致時只能檢測到一個節點,因此修改hosts,如果是在不同機器演示時不會出現這樣的情況

note 節點默認向http://localhost:8761/eureka/apps註冊,不需要單獨配置

運行

將所有的應用都啓動起來,訪問http://localhost:8761/ 可以發現註冊服務 
eureka server list

http://localhost:8080/hystrix-dashboard-1.4.10/中輸入http://localhost:9090/turbine.stream?cluster=MAIN 得到監控界面;

訪問http://localhost:8081/ http://localhost:8082/ 觀察dashboard的變化 
turbine dashboard

ok ~ it’s work ! more about is here


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章