断路器 Hystrix - 代码示例

ribbon中使用断路器

pom

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>

@EnableHystrix

@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class ServiceRibbonApplication {
   public static void main(String[] args) {
      SpringApplication.run(ServiceRibbonApplication.class, args);
   }

   @Bean
   @LoadBalanced
   RestTemplate restTemplate() {
      return new RestTemplate();
   }
}

@HystrixCommand(fallbackMethod = “hiError”)

@Service
public class HelloService {
    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "hiError")
    public String hiService(String name) {
        return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
    }

    public String hiError(String name) {
        return "hi,"+name+",sorry,error!";
    }
}

Feign中使用断路器

@FeignClient( fallback = xxx.class)

@FeignClient(value = "service-hi",fallback = SchedualServiceHiHystric.class)
public interface SchedualServiceHi {
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

定义实现类

@Component
public class SchedualServiceHiHystric implements SchedualServiceHi {
    @Override
    public String sayHiFromClientOne(String name) {
        return "sorry "+name;
    }
}

Hystrix Dashboard (Hystrix 仪表盘)

pom

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

@EnableHystrixDashboard

@SpringBootApplication
@EnableDiscoveryClient
@RestController
@EnableHystrix
@EnableHystrixDashboard
public class ServiceRibbonApplication {
   public static void main(String[] args) {
      SpringApplication.run(ServiceRibbonApplication.class, args);
   }

   @Bean
   @LoadBalanced
   RestTemplate restTemplate() {
      return new RestTemplate();
   }
}

可视化界面

http://localhost:8764/hystrix

点击monitor stream,进入下一个界面,
访问:http://localhost:8764/hi?name=forezp,此时会出现监控界面:

在这里插入图片描述

断路器 聚合监控(Hystrix Turbine)

pom

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-netflix-turbine</artifactId>
</dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

配置文件

spring:
  application.name: service-turbine
server:
  port: 8769
security.basic.enabled: false
turbine:
  aggregator:
    clusterConfig: default  
 		# 指定聚合哪些集群,多个使用","分割,默认为default。可使用
		http://.../turbine.stream?cluster={clusterConfig之一}访问
  appConfig: service-hi,service-lucy  
		# 配置Eureka中的serviceId列表,表明监控哪些服务
  clusterNameExpression: new String("default")
  	# 1. clusterNameExpression指定集群名称,默认表达式appName;此时:	turbine.aggregator.clusterConfig需要配置想要监控的应用名称
  	# 2. 当clusterNameExpression: default时,		turbine.aggregator.clusterConfig可以不写,因为默认就是default
 	# 3. 当clusterNameExpression: metadata['cluster']时,假设想要监控	的应用配置了eureka.instance.metadata-map.cluster: ABC,则需要配置,	同时turbine.aggregator.clusterConfig: ABC
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

@EnableTurbine

@SpringBootApplication
@EnableTurbine		//开启turbine,包含了@EnableDiscoveryClient
public class ServiceTurbineApplication {
   public static void main(String[] args) {
         new SpringApplicationBuilder(ServiceTurbineApplication.class).
	web(true).
	run(args);
   }
}

测试

http://localhost:8769/turbine.stream

在这里插入图片描述

依次请求:http://localhost:8762/hi?name=forezp
打开:http://localhost:8763/hystrix,输入监控流http://localhost:8769/turbine.stream,点击monitor stream 进入页面:

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值