我是 Hystrix 仪表板的新手。我已经用 Hystrix 编写了示例应用程序。
我想查看 Hystrix 图表(命令指标流)。但我收到以下错误:
Circuit: Unable to connect to Command Metric Stream
Thread Pools: Loading...
我将 STS 与 Maven 一起使用。
下面是使用的代码:
简单的服务器微服务应用程序(在端口8085运行的Spring boot 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 boot 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://本地主机:8095/ or http://localhost:8095/to-read,然后点击“监控流”,然后进入下一页,出现错误:
Circuit: Unable to connect to Command Metric Stream
Thread Pools: Loading...