您现在的位置是:首页 > 文章详情

java B2B2C Springcloud仿淘宝电子商城系统-断路器监控

日期:2019-04-07点击:291

断路器

断路器模式源于Martin Fowler的Circuit Breaker一文。“断路器”本身是一种开关装置,用于在电路上保护线路过载,当线路中有电器发生短路时,“断路器”能够及时的切断故障电路,防止发生过载、发热、甚至起火等严重后果。

在分布式架构中,断路器模式的作用也是类似的,当某个服务单元发生故障(类似用电器发生短路)之后,通过断路器的故障监控(类似熔断保险丝),向调用方返回一个错误响应,而不是长时间的等待。这样就不会使得线程因调用故障服务被长时间占用不释放,避免了故障在分布式系统中的蔓延。

断路器监控

在微服务架构中为例保证程序的可用性,防止程序出错导致网络阻塞,出现了断路器模型。断路器的状况反应了一个程序的可用性和健壮性,它是一个重要指标。Hystrix Dashboard是作为断路器状态的一个组件,提供了数据监控和友好的图形化界面

改造项目

复制项目 spring-cloud-ribbon-consumer-hystrix,修改名称 spring-cloud-ribbon-consumer-hystrix-dashboard 在它的基础上进行改造。 Feign的改造和这一样。

在 pom的工程文件引入相应的依赖:

<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> <dependency> <groupId> org.springframework.cloud </groupId> <artifactId> spring-cloud-starter-hystrix-dashboard </artifactId> </dependency>

开启 HD

修改 RibbonConsumerApplication.java 类

在程序的入口 RibbonConsumerApplication类,加上 @EnableHystrix注解开启断路器,这个是必须的,并且需要在程序中声明断路点 @HystrixCommand;加上 @EnableHystrixDashboard注解,开启 `HystrixDashboard

package io.ymq.example.ribbon.consumer.hystrix;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.cloud.netflix.hystrix.EnableHystrix;import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;import org.springframework.context.annotation.Bean;..import org.springframework.web.client.RestTemplate;@EnableHystrix@EnableDiscoveryClient@EnableHystrixDashboard@SpringBootApplicationpublic class RibbonConsumerApplication {   @LoadBalanced   @Bean   RestTemplate restTemplate() {       return new RestTemplate();   public static void main(String[] args) {       SpringApplication.run(RibbonConsumerApplication.class, args); 

声明断路点

声明断路点 @HystrixCommand(fallbackMethod="defaultStores")

package io.ymq.example.ribbon.consumer.hystrix; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; * 描述:调用提供者的 `home` 方法 * @author yanpenglei @RestController public class ConsumerController {    @Autowired    private RestTemplate restTemplate;    @HystrixCommand(fallbackMethod = "defaultStores")    @GetMapping(value = "/hello")    public String hello() {        return restTemplate.getForEntity("http://eureka-provider/", String.class).getBody();    public String defaultStores() {    return "feign + hystrix Dashboard ,提供者服务挂了"; }

@HystrixCommand 表明该方法为 hystrix包裹,可以对依赖服务进行隔离、降级、快速失败、快速重试等等 hystrix相关功能 该注解属性较多,下面讲解其中几个

fallbackMethod 降级方法

commandProperties 普通配置属性,可以配置 HystrixCommand对应属性,例如采用线程池还是信号量隔离、熔断器熔断规则等等

ignoreExceptions 忽略的异常,默认 HystrixBadRequestException不计入失败

groupKey() 组名称,默认使用类名称

commandKey 命令名称,默认使用方法名

 java B2B2C springmvc mybatis电子商务平台源码 

原文链接:https://yq.aliyun.com/articles/697084
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章