ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

@FeignClient 的使用

2022-09-04 21:31:48  阅读:182  来源: 互联网

标签:FeignClient feign obj point 使用 public append


 ​

 添加依赖   
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

 

启用Feign
@EnableFeignClients

 

使用示例:注可以作为调用三方接口的统一入口
@FeignClient(value = "base")
//@FeignClient(name = "labelPrintClient", url = "${boton.printUrl}")
//@FeignClient(name = ClientUrl.ELN_SERVICE,configuration = FeignConfig.class)
public interface BaseFeignService {
    @GetMapping(value = "/user/name/{userId}")
    RestResponse<String> getUserName(@PathVariable Long userId);
}

 

增加拦截方式1:
@Aspect
@Component
public class FeignInterceptor {

    @Around("@within(feign))")
    public Object around(ProceedingJoinPoint point, FeignClient feign) throws Exception {
        Object obj = null;
        try {
            obj = point.proceed();
        } catch (Throwable e) {
            error( e.getMessage() , point, feign);
        }
        // 匹配并校验响应结果
        checkResult( obj, point,  feign);
        return obj;
    }

    public static void checkResult(Object obj,ProceedingJoinPoint point, FeignClient feign) throws Exception {
        if (null!=obj) {
            if(obj instanceof RestResponse){
                RestResponse response = (RestResponse)obj;
                if(response.getCode()!=0){
                    error( response.getResult() , point, feign);
                }
            }else {
                JSONObject jsonObj = JSONUtil.parseObj(obj);
                Integer code = jsonObj.getInt("code");
                if(code!=0){
                    error( jsonObj.get("result") , point, feign);
                }
            }
        }
    }

    /** api调用异常 / api正常响应:失败场景 */
    public static void error(Object cause , ProceedingJoinPoint point, FeignClient feign) throws Exception {
        throw new Exception(
                new StringBuffer()
                        .append(feign.value()).append(" 服务调用异常:").append(cause)
                        .append(";Api方法名:").append(point.getSignature().getName())
                        .append(";Api参数:" ).append(ArrayUtil.toString(point.getArgs()))
                        .toString()
        );
    }

} 

 

增加拦截方式2:只有请求的拦截
@Configuration
public class FeignConfig implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        // 获取当前请求Spring信息
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        // 获取请求体
        HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();
        // 获取Header、或参数等
        String token = request.getHeader("Authorization");
        //添加token
        requestTemplate.header("Authorization",token);
    }
}

 

 


标签:FeignClient,feign,obj,point,使用,public,append
来源: https://www.cnblogs.com/cc-cf/p/16656119.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有