1、HystrixDashboard有什么作用?
在微服务架构中为例保证程序的可用性,防止程序出错导致网络阻塞,出现了断路器模型。断路器的状况反应了一个程序的可用性和健壮性,它是一个重要指标。
Hystrix Dashboard是作为断路器状态的一个组件,提供熔断器的数据监控和友好的图形化界面。
2、使用
2.1、熔断器工程添加依赖
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
App.java
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboard9001App {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboard9001App.class, args);
}
}
2.2、被监控的服务
pom.xml中以下依赖必须
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
package com.zemel;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
/**
* Hello world!
*
*/
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableHystrix
public class Dept8001App
{
public static void main( String[] args )
{
SpringApplication.run(Dept8001App.class, args);
// AbstractInstanceRegistry
}
}
Rest.java
package com.zemel.rest;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zemel.service.IDeptService;
import com.zemel.vo.Dept;
@RestController
@RequestMapping("/dept")
public class DeptRest {
@Autowired
private IDeptService deptService;
@Autowired
private DiscoveryClient discoveryClient; // 进行eureka的发现服务
/**
* 直接返回发现服务信息
* eureka服务管理页面就是通过此类返回的信息显示
* @author zemel
* @date 2018年12月5日 下午2:34:43
* @return
*/
@GetMapping("/discover")
public Object discover(){
return this.discoveryClient;
}
@GetMapping("/sessionId")
public Object IDeptService(HttpServletRequest request){
return request.getSession().getId();
}
@GetMapping("/get/{id}")
@HystrixCommand(fallbackMethod="fallback")
public Dept get(@PathVariable("id") long id){
Dept dept = deptService.get(id);
if(dept == null){
throw new RuntimeException("部门信息不存在");
}
return dept;
}
//
public Dept fallback(@PathVariable("id") long id){
Dept dept = new Dept();
dept.setDeptno(999999L);
dept.setDname("get method is error");
dept.setLoc("dept - provider ");
return dept;
}
@GetMapping("/list")
@HystrixCommand(fallbackMethod="listFallback")
public List<Dept> list(){
return this.deptService.list();
}
public List<Dept> listFallback(){
Dept dept = new Dept();
dept.setDeptno(999999L);
dept.setDname("get method is error");
dept.setLoc("dept - provider ");
List<Dept> list = new ArrayList<>();
list.add(dept);
return list;
}
@PostMapping("/add")
public Object add(@RequestBody Dept dept){
return this.deptService.add(dept);
}
}
2.3、启动服务之后访问
访问HystrixDashboard的首页
注意:红框的地址是被监控的微服务。其中wendy:wendy@是我使用了Spring-security的安全控制,
如果没有使用,可以去掉。
点击【Monitor Stream】按钮
这个时候还没数据,我们访问一下服务。
未出现熔断
出现熔断
可以清晰的看到这些服务接口的熔断器信息。
3、以上监控的服务端,那监控消费端是怎么样的情况呢?