首页 > 其他分享 >@FeignClient 的使用

@FeignClient 的使用

时间:2022-09-04 21:36:53浏览次数:45  
标签: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
From: https://www.cnblogs.com/cc-cf/p/16656119.html

相关文章

  • npm太慢, 淘宝npm镜像使用方法
    淘宝npm地址:http://npm.taobao.org/如何使用有很多方法来配置npm的registry地址,下面根据不同情境列出几种比较常用的方法。以淘宝npm镜像举例:1.临时使用npm--reg......
  • 如何使用 ABAP 代码解析 XML 文件
    正如本教程的开篇介绍文章SAPOData开发教程-从入门到提高(包含SEGW,RAP和CDP)所提到的,SAPOData服务开发,从实现技术上来说,可以分为三大类。因此本教程也分为三大......
  • SAP UI5 应用中的 sap.ui.require 使用场景
    下图是笔者SAPUI5开发教程中使用到的一段代码:varmPath=sap.ui.require.toUrl('sap/ui5/walkthrough')+"/";console.log('Jerry:',mPath);本文介绍sap.ui.r......
  • lightdb使用一条sql实现高性能事务一致性归历史
    相比insertselect,delete,如下:--lightdb专有oracle匿名块写法BEGINTRANSACTIONISOLATIONLEVELREPEATABLEREAD;insertintoxxselectxxfromyywhereid<xxx;......
  • Git和gitee的使用(二、入门篇)
    一、准备工作Git的下载以及gitee的注册。Git下载链接:https://git-scm.com/downloads注:按照默认选项进行安装就ok。gitee官网:https://gitee.com/注:按照提示进行注册。......
  • Flask 学习-45.Flask-RESTX 自定义参数校验和自定义错误内容 error_msg 使用
    前言在校验请求参数的时候,除了一些基本的required=True,type类型外,还会遇到一些校验,比如是否为空,字符串长度,以及一些自定义的参数规则。add_argument参数classArgumen......
  • python-------assign的使用方法
    https://blog.csdn.net/qq_42240729/article/details/105628595?ops_request_misc=&request_id=&biz_id=102&utm_term=python%20assign&utm_medium=distribute.pc_search_r......
  • jekins 使用
    简介jekins是一套自动编译打包的工具在各大公司广泛应用参考链接https://blog.csdn.net/qq_32352777/article/details/109267847......
  • WIN10日语输入法怎么使用
    简单记录以下WIN10自带日语输入法的使用方法我也不完全会用1.如何打开微软WIN10自带的日语输入法设置-->时间和语言-->语言在首选语言那里添加日语2.怎么使用日语输......
  • Java表达式计算工具-Aviator的使用
    Java表达式计算工具-Aviator的使用添加依赖:<!--https://mvnrepository.com/artifact/com.googlecode.aviator/aviator--><dependency><groupId>com.googlecode.......