springcloud feign 熔斷降級

pom 文件

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

啓動類   注意 要加上 @EnableFeignClients

@SpringBootApplication
@EnableDiscoveryClient
@EnableEurekaClient
@EnableFeignClients
public class HelloService1Application {

   public static void main(String[] args) {
      SpringApplication.run(HelloService1Application.class, args);
   }

}

feign調用接口

@FeignClient(name = "two",fallback = Test2ServiceFeign.class)
public interface Test2Service {
    @RequestMapping(value = "/get1",method = RequestMethod.GET,produces= MediaType.APPLICATION_JSON_UTF8_VALUE)
    String get1(@RequestParam("aa") String aa);//RequestParam
}

降級所用的集成feign接口的類

@Component
public class Test2ServiceFeign implements Test2Service {
    @Override
    public String get1(String aa) {
        return "調用出錯";
    }
}

配置文件中開啓 hystrix 高版本默認的關閉的,要開啓

feign:
  hystrix:
    enabled: true

controller類的調用

@RequestMapping("/get2")
public String getTwo(){
    logger.info("tow---------------------------");
    String aa ="aa";
    String aa1 = service.get1(aa);
    logger.info(aa1);
    return "-------------4444444444----------------";
}

服務類的controller是

@RequestMapping("/get1")
public String  getHello(String aa){
    logger.info(aa);
    logger.info("-------------------hello------2");
    return "hello------------2";
}

所有的代碼稍後會上傳.

這幾篇代碼地址是:https://download.csdn.net/download/a863922230/11827197

 

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