首页 文章

Spring Boot中的Hystrix仪表板问题

提问于
浏览
1

我是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.

但是当我尝试调用此URL 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 回答

  • 3

    我也经历过同样的经历 . 主要问题是,我在maven pom中没有 Actuator 依赖性 . 所以我无法得到hystrix流 .

    • 包括 spring 启动 Actuator .

    • 检查localhost:8085 / health是否正在运行 .

    • 尝试输入localhost:8085 / hystrix.stream以在Hystrix仪表板中传输值 .

    • 执行该服务几次 - >仪表板应显示受监视的方法/命令 .

相关问题