此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