本文介绍了Spring Boot 中的 Hystrix 仪表板问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Hystrix 仪表板的新手.我已经用 Hystrix 编写了示例应用程序.我想看一下 Hystrix 图表(命令指标流).但我收到以下错误:

I am new to Hystrix Dashboard. I have written sample application with Hystrix.I want to see the Hystrix chart (command metric stream). But I am getting the below error:

Circuit: Unable to connect to Command Metric Stream
Thread Pools: Loading...

我在 Maven 中使用 STS.

I am using STS with Maven.

以下是使用的代码:

简单的服务器微服务应用程序(运行在 8085 端口的 Spring boot web)

Simple server microservice application (Spring boot web running in port 8085)

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
@SpringBootApplication
public class BookstoreApplication {

    @RequestMapping(value = "/recommended")
    public String readingList(){
    return "Spring in Action (Manning), Cloud Native Java (O'Reilly), Learning Spring Boot (Packt)";
    }

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

简单的客户端微服务应用(Spring boot web 8095端口运行)我已经包含了Hystrix和Hystrix Dashboard的依赖以及Web,所以所有的Hystrix依赖都在classpath中

Simple client microservice application (Spring boot web running in port 8095) I have included the dependency of Hystrix and Hystrix Dashboard along with Web, so all the Hystrix dependencies are in classpath

package hello;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.net.URI;

@Service
public class BookService {

    private final RestTemplate restTemplate;

    public BookService(RestTemplate rest) {
    this.restTemplate = rest;
    }

    @HystrixCommand(fallbackMethod = "reliable")
    public String readingList() {
    URI uri = URI.create("http://localhost:8090/recommended");

    return this.restTemplate.getForObject(uri, String.class);
    }

    public String reliable() {
    return "Cloud Native Java (O'Reilly)";
    }

}


package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.web.client.RestTemplate;

@EnableHystrixDashboard
@EnableHystrix
@EnableCircuitBreaker
@RestController
@SpringBootApplication
public class ReadingApplication {

    @Autowired
    private BookService bookService;

    @Bean
    public RestTemplate rest(RestTemplateBuilder builder) {
    return builder.build();
    }

    @RequestMapping("/to-read")
    public String toRead() {
    return bookService.readingList();
    }

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

通过运行上面的代码,hystrix 工作正常,当 BooKStoreApplication 宕机时,它会回退方法.

By running the above code, the hystrix is working fine, when the BooKStoreApplication is down, it is going to fallback method.

两个网址都工作正常.正常情况:

Both the urls are working fine. Normal Case:

http://localhost:8085/recommended
Output: Spring in Action (Manning), Cloud Native Java (O'Reilly), Learning Spring Boot (Packt)

http://localhost:8095/to-read
Output: Spring in Action (Manning), Cloud Native Java (O'Reilly), Learning Spring Boot (Packt)


When BookStoreApplication is down (http://localhost:8085/recommended) accessing http://localhost:8095/to-read returns "Cloud Native Java (O'Reilly)" as expected.

但是当我尝试调用这个 url http://localhost:8095/hystrix 时,我得到了Hystrix DashBoard 页面并询问流值.

But when I tried to invoke this url http://localhost:8095/hystrix, I am getting the Hystrix DashBoard Page and asking for the stream value.

我试过给定 http://localhost:8095/http://localhost:8095/to-read,点击Monitor Stream",跳转到下一页,报错:

I have tried given http://localhost:8095/ or http://localhost:8095/to-read, and clicked "Monitor Stream" and it is going to next page with error:

Circuit: Unable to connect to Command Metric Stream
Thread Pools: Loading...

推荐答案

我也有过同样的经历.主要问题是,我的 maven pom 中没有执行器依赖项.所以我无法获得 hystrix 流.

I've experienced the same. The main problem was, that I didn't have the actuator dependency in my maven pom. So I could not get the hystrix stream.

  1. 包括 spring-boot-actuator.
  2. 检查 localhost:8085/health 是否正在运行.
  3. 尝试输入 localhost:8085/hystrix.stream 以在 Hystrix Dashboard 中传输值.
  4. 多次执行服务 -> 仪表板应显示受监控的方法/命令.

这篇关于Spring Boot 中的 Hystrix 仪表板问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 11:02