/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved.
 * <a>http://www.crunii.com</a>
 */
package com.crunii.micro.feign.client.config;

import com.crunii.micro.domain.dto.CallHolder;
import com.crunii.micro.feign.client.fallback.MicroCoreClientFallback;
import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 田平 create 2023-02-15 09:29:40
 */
@Configuration
public class FeignConfiguration {

    @Bean
    public MicroCoreClientFallback regMicroCoreClientFallback() {
        return new MicroCoreClientFallback();
    }

    @Bean("requestInterceptor")
    public RequestInterceptor requestInterceptor() {
        return requestTemplate -> {
            // 子线程继承父线程变量
            CallHolder callHolder = CallHolder.call.get();
            if (callHolder != null) {
                requestTemplate.header("token", callHolder.getToken());
            }
        };
    }

}
