spring cloud: Hystrix(五):如禁止单个FeignClient使用hystrix

时间:2024-12-18 18:35:44

spring cloud: Hystrix(五):如禁止单个FeignClient使用hystrix

首先application.yml / applicatoin.propreties的配置项:feign.hystrix.enabled=true是针对全局的。

feign.hystrix.enabled=true

或者
feign:
hystrix:
enabled: true

那么怎么配置禁止单个的FeignClient使用hystrix呢。

在自定义的configuration.java的配置文件里加入:

       //关闭feign的hystrix
@Bean
@Scope("property")
public Feign.Builder feignBuilder()
{
return Feign.builder(); }

  

例子:复写Feign的默认配置(fooConfiguration.java)

看例子:

spring cloud: Hystrix(五):如禁止单个FeignClient使用hystrix

1.入口文件,开启eureka, feign配置

@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients
public class FeignApp { public static void main(String[] args) {
SpringApplication.run(FeignApp.class, args);
}
}

  

2.复写Feign的默认配置(fooConfiguration.java, fooConfiguration2.java,这两个文件一个是关闭hystrix的,一个没有关闭)

a.返回默认的feign配置

@Configuration
public class FooConfiguration { @Bean
public Contract feignContract()
{
return new feign.Contract.Default();
} }

  

b.返回默认的feign配置,以及关闭Hystrix

@Configuration
public class FooConfiguration2 { /**
* 配置Url用户和密码,当eureka启用用户名和密码时
* @return
*/
/*@Bean
public BasicAuthRequestInterceptor basicAuthRequestInterceptor()
{
return new BasicAuthRequestInterceptor("root", "root123"); }*/ //默认配置
@Bean
public Contract getContract()
{
return new feign.Contract.Default(); } //feign日志配置
@Bean
Logger.Level feignLoggerLevel()
{
return Logger.Level.FULL;
} //关闭feign的hystrix
@Bean
@Scope("prototype")
public Feign.Builder feignBuilder()
{
return Feign.builder(); } }

  

3.FeignClient的代码

a.调取spring-boot-user服务器

@FeignClient(name="spring-boot-user", configuration=FooConfiguration.class, fallback=HystrixClientFallback.class)
public interface UserFeignClient { /**
* 代用feign原生模式
* @param id
* @return
*/
@RequestLine("GET /simple/{id}")
public User findById(@Param("id") Long id);
}

  

b.调取eureka的apps/serviceName(要填入具体项目名称)服务

/**
* 当@FeignClient有name和url还有configuration时,取值为url的地址,name只是为一个名称而已(无意义)
* 当@FeignClient只有name和configuration时,name的取值为eureka中的application项目的名称即虚拟地址
* @author Administrator
*
*/
@FeignClient(name="boot-user", url="http://localhost:8761", configuration=FooConfiguration2.class, fallback=HystrixClientFallback2.class)
public interface UserFeignClient2 { @RequestLine("GET /eureka/apps/{serviceName}")
public String findServiceInfoFromEurekaByServiceName(@Param("serviceName") String serviceName); //@RequestLine("GET /simple/{id}")
//public User findById(@Param("id") Long id);
}

  

4.fallback方法,hystrix的断容器代码

a.

@Component
public class HystrixClientFallback implements UserFeignClient{ @Override
public User findById(Long id) {
// TODO Auto-generated method stub
User user = new User();
user.setId(0L);
return user;
} }

  

b.直接返回字符

@Component
public class HystrixClientFallback2 implements UserFeignClient2 { @Override
public String findServiceInfoFromEurekaByServiceName(String serviceName) {
// TODO Auto-generated method stub
return "hahah";
} }

  

5.controller代码

@RestController
public class UserController { @Autowired
private UserFeignClient userFeignClient; @Autowired
private UserFeignClient2 userFeignClient2; @GetMapping("/simple/{id}")
public User findById(@PathVariable Long id) {
return this.userFeignClient.findById(id);
} @GetMapping("/eureka/apps/{serviceName}")
public String findEurekaInfo(@PathVariable String serviceName) {
return this.userFeignClient2.findServiceInfoFromEurekaByServiceName(serviceName);
} }

  

6测试

a.eureka服务,spring-boot-user服务未关闭的情况
spring cloud: Hystrix(五):如禁止单个FeignClient使用hystrix

spring cloud: Hystrix(五):如禁止单个FeignClient使用hystrix

b.eureka服务器、spring-boot-user服务器关闭