重写feign接口

1、实现 RequestInterceptor,这种是全局有效,会自动拦截所有的feign接口

package com.****.config;

import feign.*;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Map;

@Configuration
public class LocalRequestInterceptor implements RequestInterceptor {
    @Autowired
    private LocalFeignUrlProperties localFeignUrlProperties;
    //请求头信息、地址拼接等都可以在这里重写,这里值写一个简单的例子
    @Override
    public void apply(RequestTemplate template) {
        String name = template.feignTarget().name();
        Map targetUrl = localFeignUrlProperties.getUrl();
        if (targetUrl.containsKey(name))
            //重定向地址
            template.target(targetUrl.get(name));
    }
}

2、重写feign.client的execute方法,从源码分析可知,所有的feign接口到client中进行执行,默认是

feign.Client.Default,重写Default的execute方法,并注入即可实现重写feign
package com.****.config;

import feign.Client;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class LocalFeignConfig {
    @Bean
    public Client delegate(){
        return new ReWriteFeignClient(null,null);
    }
}
package com.***.config;

import com.alibaba.fastjson.JSON;
import feign.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSocketFactory;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.*;

public class ReWriteFeignClient extends Client.Default {

    @Autowired
    private LocalFeignUrlProperties localFeignUrlProperties;

    public ReWriteFeignClient(SSLSocketFactory sslContextFactory, HostnameVerifier hostnameVerifier) {
        super(sslContextFactory, hostnameVerifier);
    }

    @Override
    public Response execute(Request request, Request.Options options) throws IOException {
        String url = request.url();
        RequestTemplate requestTemplate = request.requestTemplate();
        String name = requestTemplate.feignTarget().name();
        Map urlMap = localFeignUrlProperties.getUrl();
        if (urlMap != null && urlMap.containsKey(name)){
            URI uri = URI.create(url);
            StringBuffer strbuf = new StringBuffer();
            strbuf.append(urlMap.get(name)).append(uri.getPath());
            if (StringUtils.isNotBlank(uri.getQuery())){
                strbuf.append("?").append(uri.getQuery());
            }
            requestTemplate.target(urlMap.get(name));
            Map> headers = request.headers();
            Map> newheaders = new HashMap<>();
            //处理请求头
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest attributesRequest = attributes.getRequest();
            Enumeration headerNames = attributesRequest.getHeaderNames();
            if (headerNames != null){
                while (headerNames.hasMoreElements()){
                    String element = headerNames.nextElement();
                    String elementVal = attributesRequest.getHeader(element);
                    newheaders.put(element,new ArrayList(){{add(elementVal);}});
                }
            }
            //构建新的request
            Request newRequest = buildRequest(request, strbuf.toString(),newheaders);
            return super.execute(newRequest, options);
        }
        return super.execute(request, options);
    }
    protected Request buildRequest(Request request, String reconstructedUrl,Map> headers) {
        return Request.create(request.httpMethod(), reconstructedUrl, headers,
                request.body(), Charset.forName("UTF-8"), request.requestTemplate());
    }
}

4、通过@ConditionalOnProperty控制是否启用配置类,这样就可以通过配置文件来控制是否重写feign接口了

你可能感兴趣的:(java,开发语言)