springboot集成feign

  • Post author:
  • Post category:其他




springboot如何漂亮的集成feign

如果觉得好用,不要忘记点赞关注加收藏哟!!!绝对干货



第一步:springboot项目引入pom相关依赖
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-core</artifactId>
            <version>11.8</version>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-jackson</artifactId>
            <version>11.8</version>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-httpclient</artifactId>
            <version>11.8</version>
        </dependency>


第二步:创建3个相关的feign配置文件


1,CommonFeignConfig.java 如果需要token认证相关,可配置此处
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 描述: feign调用的Request配置
 *
 */
@Configuration
public class CommonFeignConfig implements RequestInterceptor {


    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        if(attributes == null){
            return;
        }
        HttpServletRequest request = attributes.getRequest();
        //添加token
        requestTemplate.header("Com-Tk","");
        requestTemplate.header("Com-Uid","asset");
    }
}


2,feign配置忽略ssl(如果没有相关证书,并且需要https)
import feign.Client;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.net.ssl.*;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

/**
 * 描述: feign配置忽略ssl
 *
 */
@Configuration
public class FeignHttpsConfig {

    @Bean
    @ConditionalOnMissingBean
    public Client feiClient() throws NoSuchAlgorithmException, KeyManagementException {
        SSLContext ctx = SSLContext.getInstance("SSL");
        X509TrustManager tm = new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {

            }

            @Override
            public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {

            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };
        ctx.init(null,new TrustManager[]{tm},null);
        return new Client.Default(
                    ctx.getSocketFactory(), new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession sslSession) {
                    return true;
                }
            }
        );
    }

}


3,feign远程调用地址相关配置
import cn.csbit.dataassets.feign.service.CommonFeignService;
import feign.Contract;
import feign.Feign;
import feign.Request;
import feign.Retryer;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 描述: feign远程调用
 *
 */
@Configuration
public class OpenFeignConfig {

    @Bean
    public Contract useFeignAnnotations(){
        return new Contract.Default();
    }

    @Bean
    CommonFeignService getFeignService(){
        return Feign.builder()
                .encoder(new JacksonEncoder())
                .decoder(new JacksonDecoder())
                .options(new Request.Options(2000,5000))
                .retryer(new Retryer.Default(5000,5000,3))
                .target(CommonFeignService.class,"http://127.0.0.1:8082");
    }

}


第三步:编写调用接口
import cn.csbit.common.http.Response;
import feign.Headers;
import feign.RequestLine;

public interface CommonFeignService {

    @RequestLine("GET /sso/oauth2/authorize")
    Response authorize();

}

@RequestLine(“GET /sso/oauth2/authorize”) 配置调用的接口:与第二步呼应,可解释为:本地调用

http://127.0.0.1:8082/sso/oauth2/authorize 接口



第四步:在项目代码中调用本地接口一样调用feign
import cn.csbit.dataassets.feign.service.CommonFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 描述:
 *
 */
@Controller
@RequestMapping("test")
public class TestController {

    @Autowired
    private CommonFeignService feignService;


    @RequestMapping("/feign")
    public Object test(){
        Object authorize = feignService.authorize();
        System.out.println(authorize);
        return authorize;
    }

}

到这里就结束了,这样就能开心的使用feign了,让代码更清晰,更方便管理



版权声明:本文为weixin_43480441原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。