Feign+Hystix搭建服務降級注意事項@Component public class IDeptClientServiceFallbackFactory implements Fallback

此feign搭建在consumer客戶端,,當服務端接口不可用時,進行服務降級處理。

1.編寫服務降級處理程序和feign配置類

@Component
public class IDeptClientServiceFallbackFactory  implements FallbackFactory<IDeptClientService>{
   //編輯IDeptClientService接口調用失敗處理程序
}

----------

@Configuration
public class FeignConfig {

    @Bean
    public BasicAuthRequestInterceptor  getBasicAuthRequestInterceptor(){
        System.out.println("***加載***");
        return new BasicAuthRequestInterceptor("ftest","ftest");
    }

    @Bean
    public Logger.Level getFeignLoggerLevel(){
        return Logger.Level.FULL;
    }

}

2.Feign的接口中添加fallbackFactory

@FeignClient(value = "SPRING-PROJECT-PROVIDER",configuration = FeignConfig.class,
 fallbackFactory = IDeptClientServiceFallbackFactory.class)
public interface IDeptClientService {
    @RequestMapping(value = "/dept/get/{id}",method = RequestMethod.GET)
    public Object get(@PathVariable("id") long id);

    @RequestMapping(value = "/dept/list",method = RequestMethod.GET)
    public Object list();

    @RequestMapping(value = "/dept/add",method = RequestMethod.POST)
    public boolean add(Dept vo);
}

3.在consumer啓動類中添加

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = {"cn.ftest.service"})
public class DeptConsumerFeignStartApplication {
    public static void main(String[] arg){
        SpringApplication.run(DeptConsumerFeignStartApplication.class,arg);
    }
}


---------
application.yml文件添加開啓hystrix開關
feign:
  hystrix:
    enabled: true

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章