断路器聚合监控 hystrix-dashboard-turbine

1.监控单体应用


首先搭建一个spring cloud 环境,然后创建一个服务注册中心。然后新建一个module,我的项目名为hystrix-dashboard。

pom.xml

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
            <version>1.4.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
            <version>1.4.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>2.0.6.RELEASE</version>
        </dependency>

配置文件

spring.application.name=hystrix-dashboard
server.port=8001
feign.hystrix.enabled=true

eureka.client.service-url.defaultZone=http://localhost:8000/eureka/

主启动类

@SpringBootApplication
@EnableFeignClients
@EnableHystrix
@EnableCircuitBreaker
@EnableDiscoveryClient
@EnableEurekaClient
@EnableHystrixDashboard
public class HystrixDashboardApplication {

    public static void main(String[] args) {
        SpringApplication.run(HystrixDashboardApplication.class, args);
    }

        @Bean
        public ServletRegistrationBean hystrixMetricsStreamServlet() {
            ServletRegistrationBean registration = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
            registration.addUrlMappings("/actuator/hystrix.stream");
            return registration;
        }
    
}

主启动类中需要配置"/actuator/hystrix.stream",不然无法访问http://localhost:8001/actuator/hystrix.stream

然后创建Controller层,service层。

@RestController
public class ConsumerController {

    @Autowired
    HelloRemote helloRemote;

    @RequestMapping("/hello")
    public String index(@RequestParam("name") String name) {
        return helloRemote.hello(name);
    }
}
@FeignClient(name = "service-node3", fallback = HelloRemoteHystrix.class)
@Service
public interface HelloRemote {

    @RequestMapping(value = "/hello")
    public String hello(@RequestParam(value = "name") String name);

}
@Service
public class HelloRemoteHystrix implements HelloRemote {
    @Override
    public String hello(String name) {
        return "hello " + name + ", this message send faild .";
    }
}

好了,该项目创建完成了。然后创建一个专门用来调用的工程service-node3,这个项目只有一个接口/hello?name=xx

启动服务中心eureka-server,hystrix-dashboard,service-node3

然后访问http://localhost:8001//actuator/hystrix.stream,会一直ping,调用/hello?name=xx,接口之后,会刷新出数据如下图所示:

访问http://localhost:8001/hystrix

 

之后进入页面

页面中的具体参数如下:


Turbine集群监控

重新创建一个项目hystrix-dashboard-turbine,pom.xml文件

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
            <version>1.4.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
            <version>1.4.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>2.0.6.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-turbine</artifactId>
            <version>1.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-turbine</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>

启动类

@SpringBootApplication
@EnableHystrixDashboard
@EnableTurbine
public class HystrixDashboardApplication {

    public static void main(String[] args) {
        SpringApplication.run(HystrixDashboardApplication.class, args);
    }
}

properties配置

spring.application.name=hystrix-dashboard-turbine
server.port=8001
feign.hystrix.enabled=true

eureka.client.service-url.defaultZone=http://localhost:8000/eureka/

turbine.app-config=service-node,service-node2
turbine.cluster-name-expression= new String("default")

turbine.aggregator.clusterConfig=default

该项目创建完成,然后创建service-node,service-node2

pom.xml文件

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
            <version>1.4.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
            <version>1.4.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>2.0.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>

properties配置

spring.application.name=service-node
server.port=9001
feign.hystrix.enabled=true

eureka.client.service-url.defaultZone=http://localhost:8000/eureka/

service-node2 的端口号为9002

 

主启动类

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
public class ServiceNodeApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceNodeApplication.class, args);
    }

    @Bean
    public ServletRegistrationBean hystrixMetricsStreamServlet() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
        registration.addUrlMappings("/actuator/hystrix.stream");
        return registration;
    }

}

controlle层和service层如前面一样,全部调用service-node3

service-node中我多加了一个接口hi?name=xx

访问完两个项目的接口之后,访问http://localhost:8001/turbine.stream有数据。

访问http://localhost:8001/hystrix,然后填入http://localhost:8001/turbine.stream

然后出现仪表盘

over

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值