The use of springCloud 04-----the use of the fuse hystrix

1. restTemplate+ribbon uses hystrix

  1.1 Introducing dependencies

<!-- 配置hystrix断路器 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>

  1.2 Add annotations to methods that need to be blown

@Service
public class HiService {

    @Autowired
    RestTemplate restTemplate;
    
    // The method that needs to be blown 
    @HystrixCommand(fallbackMethod="hiError") // The method to be executed after the fuse 
    public String sayHi() {
         return restTemplate.getForObject("http://SERVICE-HI/info", String.class ) ;
    }
    
    // Method to be executed after fuse 
    public String hiError() {
         return "sorry hi error" ;
    }
}

  1.3 Declare the use of hystrix in the startup class

@SpringBootApplication
@EnableDiscoveryClient // register @RestController with the service center

@EnableHystrix // Enable circuit breaker 
public  class ConsumerRibbon {
 
    @Autowired
    private HiService hiService;
    
    public static void main(String[] args) {
        SpringApplication.run(ConsumerRibbon.class, args);
    }
    
    @Bean
    @LoadBalanced // Use this restTemplate to open load balancing 
    RestTemplate initRestTemplate(){
         return  new RestTemplate();
    }
    
    @RequestMapping("info")
    public String hiConsumer() {
        String response=hiService.sayHi();
        return response;
    }
}

  1.4 Start the registry and cloud-consumer-ribbon, visit http://localhost:8764/info and return sorry hi error

    Start service-hi, visit http://localhost:8764/info and return hello eureka client 8762

2 feign uses hystrix

  2.1 feign comes with a circuit breaker, no need to import the dependencies of hystrix, but you need to import the following dependencies, otherwise it will return java.lang.NoClassDefFoundError: com/netflix/hystrix/contrib/javanica/aop/aspectj/HystrixCommandAspect error

<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-javanica</artifactId>
</dependency>

  2.2 Enable hystrix in the configuration file, which is disabled by default

feign:
 hystrix:
  enabled: true

  2.3 Specify the class to be executed after fusing

@FeignClient(value="service-hi",fallback=HiServiceHystric. class ) // Specify which service provider to call, and specify the class to be executed after the fuse 
public  interface IHiService {
    
    @RequestMapping(value = "/info",method=RequestMethod.GET) // Specify which interface of the service provider to call 
    String info();
    
    @RequestMapping(value = "/info",method=RequestMethod.GET) // Specify which interface of the service provider to call 
    String hi();
}

  2.4 Specify the corresponding method to be executed after fusing

@Component
public class HiServiceHystric implements IHiService {

    // Execute the corresponding method after the fuse 
    public String info() {
         return "sorry info feign" ;
    }

    public String hi() {
        return "sorry hi feign";
    }
}

  2.5 Declare the startup hystrix in the startup class

@EnableHystrix

  2.6 Start the registry and cloud-consumer-feign, visit http://localhost:8765/info and return sorry info feign

    Start service-hi, visit http://localhost:8765/info and return hello eureka client 8762

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326000581&siteId=291194637