分布式微服务 SpringCloud Alibaba 中 Hystrix 断路器 组件搭建配置

Hystrix分为3中状态

关闭:当失败的请求数<总请求数的50%(默认是关闭状态,默认是50%)
打开:当失败的请求数>总请求数的50%
半开:每过一段时间释放一次请求来判断服务是否可用,不可用则打开,否则关闭,以此类推

导入依赖

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

启动Hystrix的自动配置

在启动类中添加EnableCircuitBreaker注解(Hystrix的自动配置)
不需要添加@EnableDiscoveryClient注解(Eureka服务注册发现)
package com.guigu.www;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableCircuitBreaker
public class UserConsumerHystrixApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserConsumerHystrixApplication.class,args);
    }
}

编写配置类

package com.guigu.www.config;

import feign.Logger;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ApplicationConfig {
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    @Bean
    public Logger.Level level(){
        return Logger.Level.BASIC;
    }
}

使用Hystrix注解

在方法上加入HystrixCommand
package com.guigu.www.controller;

import com.guigu.cloud.pojo.TbUser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@RequestMapping("Hystrix")
@Slf4j
public class UserController {

    @Resource
    RestTemplate restTemplate;
    
    @GetMapping("user/{id}")
    //指向异常或超时,熔断后进入的方法
    @HystrixCommand(fallbackMethod="hystrixUser")
    public TbUser findById(@PathVariable("id") Integer id){
        String url = "http://user-provider/user/find/"+id;
        TbUser forObject = restTemplate.getForObject(url, TbUser.class);
        log.info(forObject != null ? forObject.toString() : null);
        return forObject;
    }
    //该方法需要和Hystrix注解的方法参数返回值保持一致
    public TbUser hystrixUser(@PathVariable("id") Integer id){
        return TbUser.builder()
                .id(-1)
                .note("服务访问出现异常")
                .build();
    }
}

整合Fegin使用

点击Fegin使用教程
application.yml文件添加
feign:
  hystrix:
    enabled: true   #开启feign与hystrix整合
超时配置 (他们的超时时间不会叠加,以最大的为准)
hystrix:
  command:
    default:
      execution:
        timeout:
          enabled: true
        isolation:
          thread:
            timeoutInMilliseconds: 3000

# ribbon超时
ribbon:
  ReadTimeout: 3000
  ConnectTimeout: 3000
主配置类加上@EnableFeignClients

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableCircuitBreaker
@EnableFeignClients
public class UserConsumerHystrixApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserConsumerHystrixApplication.class,args);
    }
}

使用Fegin调用微服务

 	@Resource
    UserFeign userFeign;

    @GetMapping("user/{id}")
    //指向异常或超时,熔断后进入的方法
    @HystrixCommand(fallbackMethod="hystrixUser")
    public TbUser findById(@PathVariable("id") Integer id){
        return userFeign.findById(id);
    }
    //该方法需要和Hystrix注解的方法参数返回值保持一致
    public TbUser hystrixUser(@PathVariable("id") Integer id){
        return TbUser.builder()
                .id(-1)
                .note("服务访问出现异常")
                .build();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值