31、Sentinal:feignのサポート

6295 ワード

CentinelはFeignコンポーネントに適しています.使用するにはsentinel-starterの依存を導入する以外に2つのステップが必要です:プロファイルはsentinelのfeignに対するサポートを開く:feign.sentinel.Enabled=true openfeign starter依存に追加するとsentinel starterの自動化構成クラスが有効になります.
1、導入依存
<!--feign sentinel   -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

2、sentinelサポートを開く
工程でのアプリケーションymlにsentinelによるfeignのサポートを追加
#  sentinel   
feign:
  sentinel:
    enabled: true

3、FeignClientの配置
Hystrixを使用する方法とほぼ一致し、FeignClientインタフェースを構成し、fallbackで溶断降格方法を指定する必要があります.
/**
 *             
 *  @FeignClient
 *      * name :         
 *      * fallback :           
 *                     
 */
@FeignClient(name="service-product",fallback = ProductFeignClientCallBack.class)
public interface ProductFeignClient {

	/**
	 *             
	 */
	@RequestMapping(value="/product/{id}",method = RequestMethod.GET)
	public Product findById(@PathVariable("id") Long id);
}

4、溶断方法の配置
@Component
public class ProductFeignClientCallBack implements ProductFeignClient {

	/**
	 *        
	 */
	public Product findById(Long id) {
		Product product = new Product();
		product.setProductName("feign          ");
		return product;
	}
}