1. 程式人生 > >Feign的配置及對hystrix的支援

Feign的配置及對hystrix的支援

文章目錄

Feign

自定義配置時,@Configuration和@ComponentScan包不應重疊

  • 示例:
    • @FeignClient註解的放在com.mmzs.cloud.feign包下面
        @FeignClient(name = "xxxx", url = "http://localhost:8761/", configuration = Configuration2.class)
    
    • @Configuration註解的放在com.mmzs.config包下面

@FeignClient所在的介面中,不支援@GetMapping等組合註解

//value此處的值一定要和指定的應用microservice-provider-user的controller中的對映路徑一致
@RequestMapping(method = RequestMethod.GET, value = "/user/{id}", consumes = "application/json")
// 此處有兩個坑:
//- 1. 不支援@GetMapping等組合註解   
//- 2. @PathVariable得設定value
public User findById(@PathVariable("id") Long id);
	

使用@PathVariable時,需要指定其value

  public String findServiceName(@PathVariable("serviceName") String serviceName);

Feign暫不支援複雜物件作為一個引數

  • 錯誤用法
// 該請求不會成功,只要引數是複雜物件,即使指定了是GET方法,feign依然會以POST方法進行傳送請求。可能是我沒找到相應的註解或使用方法錯誤。
@RequestMapping(method = RequestMethod.GET, value = "/feign-get-user")
public User getUser(User user);
  • 正確用法
@RequestMapping(method = RequestMethod.GET, value = "/feign-get-user")
public User getUser(@RequestParam("id") Long id, @RequestParam("username") String username, @RequestParam("age") String age);

feign對hystrix的支援

新增feign對hystrix的支援,全域性配置

application.xml配置:

feign.hystrix.enabled = true

禁用單個FegionClient的Hystrix的支援

// Configuration1表示feign的自定義配置類
@FeignClient(name = "microservice-provider-user", configuration = Configuration1.class)
@Configuration
public class Configuration1 {

	//禁用當前配置的hystrix,區域性禁用
	@Bean
	@Scope("prototype")
	public Feign.Builder feignBuilder() {
		return Feign.builder();
	}
	
}

fallback和fallbackFactory定義的類

  • fallback定義的類
    • 只重寫了一個方法
@Component
//這個類不一定要和UserFeignClient寫在同一個類中,
public class HystrixClientFallback implements UserFeignClient {

	@Override
	public User findById(Long id) {
		User user = new User();
		user.setId(1L);
		user.setUsername("我是HystrixClientFallback");
		return user;
	}
}
  • fallbackFactory定義的類
    • 不只重寫了方法,還能捕獲異常
@Component
public class HystrixClientFallbackFactory implements FallbackFactory<UserFeignClient> {
	
	private static Logger LOGGER = LoggerFactory.getLogger(HystrixClientFallbackFactory.class);
	
	@Override
	public UserFeignClient create(Throwable cause) {
		//列印日誌
		HystrixClientFallbackFactory.LOGGER.info("fallback; reason was: " + cause.getMessage());
		
		return new UserFeignClient() {
			@Override
			public User findById(Long id) {
				User user = new User();
				user.setId(-1L);
				user.setUsername("我是HystrixClientFallbackFactory");
				
				return user;
			}
		};
	}
	
}

fallbackFactory相當於fallback的增強版,也就是說fallbackFactory的範圍更廣,到收集異常的邊界處了。因此我們是可以利用fallbackFactory屬性來列印fallback異常的。
-正確用法如下:

@FeignClient(name = "microservice-provider-user", /*fallback = HystrixClientFallback.class,*/ fallbackFactory = HystrixClientFallbackFactory.class)

fallback和fallbackFactory

如下這種方式的註解,fallback和fallbackFactory是會有衝突的,但不會報錯,只是會讓斷路器執行fallback中重寫的方法

@FeignClient(name = "microservice-provider-user", fallback = HystrixClientFallback.class, fallbackFactory = HystrixClientFallbackFactory.class)