Spring Boot中的Hystrix仪表板问题

时间:2016-09-28 16:27:15

标签: spring spring-boot hystrix

我是Hystrix Dashboard的新手。我已经用Hystrix编写了示例应用程序。 我想看看Hystrix图表(命令度量标准流)。但我收到以下错误:

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

我正在使用STS和Maven。

以下是使用的代码:

简单的服务器微服务应用程序(在端口8085中运行的Spring引导Web)

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);
    }
}

简单的客户端微服务应用程序(在端口8095中运行的Spring引导Web)我已经将Hystrix和Hystrix Dashboard的依赖项与Web一起包含在内,因此所有Hystrix依赖项都在类路径中

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关闭时,它将成为回退方法。

两个网址都运行良好。     正常情况:

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.

但是当我尝试调用此网址http://localhost:8095/hystrix时,我正在获取Hystrix DashBoard页面并询问流值。

我尝试了http://localhost:8095/http://localhost:8095/to-read,然后点击了" Monitor Stream"它将转到下一页错误:

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

1 个答案:

答案 0 :(得分:3)

我也经历过同样的事情。主要的问题是,我在我的maven pom中没有执行器依赖性。所以我无法得到hystrix流。

  1. 包括弹簧启动器。
  2. 检查localhost:8085 / health是否正在运行。
  3. 尝试输入localhost:8085 / hystrix.stream以在Hystrix仪表板中传输值。
  4. 执行该服务几次 - >仪表板应显示受监视的方法/命令。