在微服务架构中为例保证程序的可用性,防止程序出错致使网络阻塞,出现了断路器模型。断路器的情况反应了一个程序的可用性和健壮性,它是一个重要指标。Hystrix Dashboard是做为断路器状态的一个组件,提供了数据监控和友好的图形化界面。spring
本文的的工程栗子,在它的基础上进行改造。bash
在pom的工程文件引入相应的依赖:网络
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>复制代码
其中,这三个依赖是必须的,缺一不可。架构
在程序的入口ServiceHiApplication类,加上@EnableHystrix注解开启断路器,这个是必须的,而且须要在程序中声明断路点HystrixCommand;加上@EnableHystrixDashboard注解,开启HystrixDashboardapp
@SpringBootApplication
@EnableEurekaClient
@RestController
@EnableHystrix
@EnableHystrixDashboard
public class ServiceHiApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceHiApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/hi")
@HystrixCommand(fallbackMethod = "hiError")
public String home(@RequestParam String name) {
return "hi "+name+",i am from port:" +port;
}
public String hiError(String name) {
return "hi,"+name+",sorry,error!";
}
}复制代码
运行程序: 依次开启eureka-server 和service-hi.分布式
注册中心spring-boot
Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码请加企鹅求求:一零三八七七四六二六
微服务