在微服务架构中为例保证程序的可用性,防止程序出错导致网络阻塞,出现了断路器模型。断路器的状况反应了一个程序的可用性和健壮性,它是一个重要指标。Hystrix Dashboard是作为断路器状态的一个组件,提供了数据监控和友好的图形化界面。
springboot 2.0 出现Unable to connect to Command Metric Stream. 所以使用1.5搭建
如果使用springboot2.0 和spring cloud Finchley.M8 版本搭建 使用(/actuator/hystrix.stream 而不是/hystrix.stream 为插入点)
pom.xml如下:
4.0.0
com.example
service_ribbon_hystrix_dashboard_1
0.0.1-SNAPSHOT
jar
service_ribbon_hystrix_dashboard_1
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.11.BUILD-SNAPSHOT
UTF-8
UTF-8
1.8
Edgware.BUILD-SNAPSHOT
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
org.springframework.cloud
spring-cloud-starter-netflix-hystrix-dashboard
org.springframework.cloud
spring-cloud-starter-netflix-ribbon
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-snapshots
Spring Snapshots
https://repo.spring.io/snapshot
true
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
spring-snapshots
Spring Snapshots
https://repo.spring.io/snapshot
true
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
配置文件
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/,http://localhost:8760/eureka/
server:
port: 8772
spring:
application:
name: service_ribbon_hystrix_dashboard_1
改造启动类 添加@EnableHystrixDashboard 开启HystrixDashboard,@EnableHystrix 开启断路器
@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
@EnableHystrixDashboard
public class ServiceRibbonHystrixDashboard1Application {
public static void main(String[] args) {
SpringApplication.run(ServiceRibbonHystrixDashboard1Application.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
Hystrix接口
@Service
public class HiService {
@Autowired
private RestTemplate restTemplate;
// http://服务提供者的serviceId/url
@HystrixCommand(fallbackMethod = "error")
public String hi(String name) {
return restTemplate.getForObject("http://SUPPLY-HI/hi?name=" + name, String.class);
}
public String error(String name) {
return "异常!";
}
}
controller
@RestController
public class HiController {
@Autowired
HiService hiService;
@RequestMapping("/hi")
public String hi(String name) {
return hiService.hi(name);
}
}
测试 请求 http://localhost:8772/hystrix.stream
输入 http://localhost:8772/hystrix
单个ribbon断路器监控搭建成功
feign与ribbon使用相差无异
创建spring-boot应用 使用的是spring_boot 1.5搭建
pom.xml如下
4.0.0
com.example
service_feign_hystrix_dashboard
0.0.1-SNAPSHOT
jar
service_feign_hystrix_dashboard
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.11.BUILD-SNAPSHOT
UTF-8
UTF-8
1.8
Edgware.BUILD-SNAPSHOT
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
org.springframework.cloud
spring-cloud-starter-netflix-hystrix-dashboard
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-snapshots
Spring Snapshots
https://repo.spring.io/snapshot
true
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
spring-snapshots
Spring Snapshots
https://repo.spring.io/snapshot
true
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
配置文件
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/,http://localhost:8760/eureka/
server:
port: 8733
spring:
application:
name: service_feign_hystrix_dashboard
feign:
hystrix:
enabled: true
修改启动类添加@EnableHystrixDashboard 开启HystrixDashboard
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
@EnableHystrixDashboard
public class ServiceFeignHystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceFeignHystrixDashboardApplication.class, args);
}
}
控制器
@RestController
public class HiController {
@Autowired
HiSend hiSend;
@RequestMapping(value = "/hi",method = RequestMethod.GET)
public String sayHi(String name){
return hiSend.sayHiFromClientOne(name);
}
}
接口
//通过@ FeignClient(“服务名”),来指定调用哪个服务。比如在代码中调用了service-hi服务的“/hi”接口,还可以使用url参数指定一个URL fallback 出现错误回调类
@FeignClient(value = "SUPPLY-HI", fallback = HiSendImpl.class)
public interface HiSend {
@RequestMapping(value = "/hi", method = RequestMethod.GET)
String sayHiFromClientOne(@RequestParam(value = "name") String name);//添加参数name 相当于/hi?name=xxx
}
实现类
@Component
public class HiSendImpl implements HiSend {
@Override
public String sayHiFromClientOne(String name) {
return "异常!";
}
}
测试 请求http://localhost:8733/hystrix.stream
请求 http://localhost:8733/hystrix