【SpringCloud】(十三):全域性和單個禁用Feign Client對Hystrix的支援

00潤物無聲00發表於2017-08-14

  我們可以自定義配置Feign對Hystrix的支援。



  預設情況下:所有的Feign都支援Hystrix。但是我們可以自定義配置。

  全域性禁用:在配置檔案application.yml 中新增配置:feign.hystrix.enabled = false;

  當然我們也可以單個禁用。

  只需要在自定義的配置類中加入。

	@Bean
	@Scope("prototype")
	public Feign.Builder feignBuilder() {
		return Feign.builder();
	}


  因為:預設支援的是HystrixFeign.Builder.而通過配置之後,返回的是Feign.builder去掉了對Hystrix的支援。


自定義配置類AuthConfiguration 

package com.dynamic.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import feign.Feign;
import feign.auth.BasicAuthRequestInterceptor;

@Configuration
public class AuthConfiguration {
	@Bean
	public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
		return new BasicAuthRequestInterceptor("user", "pass123");
	}

	/**
	 * 為什麼加上這段就讓Feign禁用了Hystrix.
	 * Feign.Builder feignBuilder:HystrixHeign.Builder
	 * 
	 * @return
	 */
	@Bean
	@Scope("prototype")
	public Feign.Builder feignBuilder() {
		return Feign.builder();
	}

}


FeignClient2

package com.dynamic.cloud.feign;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.dynamic.config.AuthConfiguration;

@FeignClient(name="xxx",url="http://localhost:8761/",configuration=AuthConfiguration.class,fallback =HystrixClientFallback2.class)
public interface FeignClient2 {
	
	@RequestMapping(value="/eureka/apps/{serviceName}")
	public String findServiceInfoFromEurekaByServiceName(@PathVariable("serviceName") String serviceName);

}


HystrixClientFallback2 
package com.dynamic.cloud.feign;

import org.springframework.stereotype.Component;
@Component
public class HystrixClientFallback2 implements FeignClient2 {

	@Override
	public String findServiceInfoFromEurekaByServiceName(String serviceName) {
		System.out.println("禁用Feign");
		return "haha";
	}
}


相關文章