Feign 请求动态URL方式

 更新时间:2022年07月04日 08:37:30   作者:行云之流水  
这篇文章主要介绍了Feign 请求动态URL方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

Feign 请求动态URL

注意事项

  • FeignClient 中不要写url, 使用 @RequestLine修饰方法
  • 调用地方必须引入 FeignClientConfiguration, 必须有Decoder, Encoder
  • 调用类必须以构建函数(Constructor) 的方式注入 FeignClient 类
  • 传入URL作为参数;

代码如下:

FeignClient类:

@CompileStatic
@FeignClient(name = "xxxxClient")
public interface XxxFeignClient {
    @RequestLine("POST")
    ResponseDto notifySomething(URI baseUri, ApproveNotifyDto notifyDto);
    /**
     * 
     * @param uri
     * @param queryMap: {userId: userId}
     * @return
     */
    @RequestLine("GET")
    ResponseDto getSomething(URI baseUri, @QueryMap Map<String, String> queryMap)
  
}

ClientCaller类:

@CompileStatic
@Slf4j
@Component
@Import(FeignClientsConfiguration.class)
public class CallerService {
    private XxxFeignClient xxxFeignClient
    @Autowired
    public CallerService(Decoder decoder, Encoder encoder) {
        xxxFeignClient = Feign.builder()
                //.client(client)
                .encoder(encoder)
                .decoder(decoder)
                .target(Target.EmptyTarget.create(XxxFeignClient.class))
    }
    public ResponseDto notifySomething(String url, XxxxDto dto) {
        return xxxFeignClient.notifySomething(URI.create(url), dto)
    }
    /**
     * @param url: http://localhost:9104/
     * @param userId 
     */
    public String getSomething(String url, String userId) {
        return xxxFeignClient.getSomething(URI.create(url), ["userId": userId])
    }
}

Feign重写URL以及RequestMapping

背景

由于项目采用的是 消费层 + API层(FeignClient) +服务层 的方式。 

导致原项目有太多代码冗余复制的地方。例如FeignClient上要写@RequestMapping,同时要写请求路径,消费层还要写上RequestBody等等,比较麻烦。遂改进了一下方案,简化日常代码开发的工作量

场景

项目的架构是微服务架构,SpringBoot与SpringCloud均为原生版本。

效果展示

feign层无需写RequestMapping以及RequestBody或者RequestParam等

public interface UserDemoApi {
    /**
     * 新增用户
     * @param userDemo 用户实体
     * @return 用户信息
     */
    ApiResponse<UserDemo> add(UserDemo userDemo);
    /**
     * 获取用户信息
     * @param userId 用户id
     * @param username 用户名
     * @return 用户信息
     */
    ApiResponse<UserDemo> findByUserIdAndUsername(String userId,String username);
    /**
     * 用户列表
     * @param reqVo 条件查询
     * @return 用户列表
     */
    ApiResponse<PageResult<UserDemo>> findByCondition(UserDemoReqVo reqVo);
}
@FeignClient(value = "${api.feign.method.value}",fallback = UserDemoFeignApiFallbackImpl.class)
public interface UserDemoFeignApi extends UserDemoApi {
}

整体思路

  • 首先要拆成两部分处理,一部分是服务端层,另一部分是客户端层
  • 服务端层:主要做的事情就是注册RequestMapping.由于我们的api上是没有写任何注解的,所以我们需要在项目启动的时候把api上的方法都注册上去。另外一个要做的就是,由于我们不写RequestBody,所以要做参数解析配置
  • 客户端层:主要做的事情是,项目启动的时候,feign会扫描api上的方法,在它扫描的同时,直接把url定下来存入RequestTemplate中。这样即使后续feign在执行apply没有路径时也不影响feign的正常请求。

实现

Feign的服务端层

1. 继承RequestMappingHandlerMapping,重写getMappingForMethod

  • METHOD_MAPPING 为自定义的路径(即RequestMapping的value值)
  • 在服务层的实现类中,打上自定义注解@CustomerAnnotation,只有有该注解的实现类才重写RequestMapping。
  • 自定义的路径采用的是 类名+方法名

重写的内容

public class CustomerRequestMapping extends RequestMappingHandlerMapping {
    private final static String METHOD_MAPPING = "/remote/%s/%s";
    @Override
    protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
        RequestMappingInfo requestMappingInfo = super.getMappingForMethod(method,handlerType);
        if(requestMappingInfo!=null){
            if(handlerType.isAnnotationPresent(CustomerAnnotation.class)){
                if(requestMappingInfo.getPatternsCondition().getPatterns().isEmpty()||
                        requestMappingInfo.getPatternsCondition().getPatterns().contains("")){
                    String[] path = getMethodPath(method, handlerType);
                    requestMappingInfo = RequestMappingInfo.paths(path).build().combine(requestMappingInfo);
                }
            }
        }else{
            if(handlerType.isAnnotationPresent(CustomerAnnotation.class)){
                String[] path = getMethodPath(method, handlerType);
                requestMappingInfo = RequestMappingInfo.paths(path).methods(RequestMethod.POST).build();
            }
        }
        return requestMappingInfo;
    }
    private String[] getMethodPath(Method method, Class<?> handlerType) {
        Class<?>[] interfaces = handlerType.getInterfaces();
        String methodClassName = interfaces[0].getSimpleName();
        methodClassName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, methodClassName);
        String[] path = {String.format(METHOD_MAPPING,methodClassName, method.getName())};
        return path;
    }

覆盖RequestMappingHandlerMapping

@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
public class VersionControlWebMvcConfiguration implements WebMvcRegistrations {
    @Override
    public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
        return new CustomerRequestMapping();
    }
}

2. 服务层Controller

  • 实现api接口(即Feign层的接口)
  • 打上自定义的标识注解@CustomerAnnotation
@RestController
@CustomerAnnotation
@RequestMapping
public class UserDemoController implements UserDemoFeignApi {
    private final static Logger logger = LoggerFactory.getLogger(UserDemoController.class);
    @Resource
    private UserDemoService userDemoService;
    @Override
    public ApiResponse<UserDemo> add(UserDemo userDemo) {
        logger.info("request data:<{}>",userDemo);
        return userDemoService.add(userDemo);
    }
    @Override
    public ApiResponse<UserDemo> findByUserIdAndUsername(String userId,String username) {
        logger.info("request data:<{}>",userId+":"+username);
        return ApiResponse.success(new UserDemo());
    }
    @Override
    public ApiResponse<PageResult<UserDemo>> findByCondition(UserDemoReqVo reqVo) {
        logger.info("request data:<{}>",reqVo);
        return userDemoService.findByCondition(reqVo);
    }
}

自此,Fiegn的服务端的配置已经配置完毕。现在我们来配置Feign的客户端

Feign的客户端配置

重写Feign的上下文SpringMvcContract

核心代码为重写processAnnotationOnClass的内容。

  • 从MethodMetadata 对象中获取到RequestTemplate,后续的所有操作都是针对于这个
  • 获取到类名以及方法名,作为RequestTemplate对象中url的值,此处应与服务层的配置的URL路径一致
  • 默认请求方式都为POST

特殊情况处理:在正常情况下,多参数且没有@RequestParams参数注解的情况下,Feign会直接抛异常且终止启动。所以需要对多参数做额外处理

  • 判断当前方法的参数数量,如果不超过2个不做任何处理
  • 对于超过2个参数的方法,需要对其做限制。首先,方法必须满足命名规范,即类似findByUserIdAndUsername。以By为起始,And作为连接。
  • 截取并获取参数名称
  • 将名称按顺序存入RequestTemplate对象中的querie属性中。同时,要记得MethodMetadata 对象中的indexToName也需要存入信息。Map<Integer,Collection> map,key为参数的位置(从0开始),value为参数的名称
@Component
public class CustomerContact extends SpringMvcContract {
    private final static Logger logger = LoggerFactory.getLogger(CustomerContact.class);
    private final static String METHOD_PATTERN_BY = "By";
    private final static String METHOD_PATTERN_AND = "And";
    private final Map<String, Method> processedMethods = new HashMap<>();
    private final static String METHOD_MAPPING = "/remote/%s/%s";
    private Map<String, Integer> parameterIndexMap = new ConcurrentHashMap<>(100);
    public CustomerContact() {
        this(Collections.emptyList());
    }
    public CustomerContact(List<AnnotatedParameterProcessor> annotatedParameterProcessors) {
        this(annotatedParameterProcessors,new DefaultConversionService());
    }
    public CustomerContact(List<AnnotatedParameterProcessor> annotatedParameterProcessors, ConversionService conversionService) {
        super(annotatedParameterProcessors, conversionService);
    }
    /**
     * 重写URL
     * @param data 类名以及方法名信息
     * @param clz api类
     */
    @Override
    protected void processAnnotationOnClass(MethodMetadata data, Class<?> clz) {
        RequestTemplate template = data.template();
        String configKey = data.configKey();
        if(StringUtils.isBlank(template.url())){
            template.append(getTemplatePath(configKey));
            template.method(RequestMethod.POST.name());
        }
        // 构造查询条件
        templateQuery(template,data);
        super.processAnnotationOnClass(data, clz);
    }
    /**
     * @param template 请求模板
     */
    private void templateQuery(RequestTemplate template,MethodMetadata data){
        try{
            String configKey = data.configKey();
            if(manyParameters(data.configKey())){
                Method method = processedMethods.get(configKey);
                String methodName = method.getName();
                String key = getTemplatePath(configKey);
                Integer parameterIndex = 0;
                if(parameterIndexMap.containsKey(key)){
                    parameterIndexMap.put(key,parameterIndex++);
                }else{
                    parameterIndexMap.put(key,parameterIndex);
                }
                int index = methodName.indexOf(METHOD_PATTERN_BY);
                if(index>=0){
                    String[] parametersName = methodName.substring(index+METHOD_PATTERN_BY.length()).split(METHOD_PATTERN_AND);
                    String parameterName = parametersName[parameterIndex];
                    String caseName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, parameterName);
                    Collection<String> param = addTemplatedParam(template.queries().get(parameterName), caseName);
                    template.query(caseName,param);
                    setNameParam(data,caseName,parameterIndex);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("template construct query failed:<{}>",e.getMessage());
        }
    }
    /**
     * 构造url路径
     * @param configKey 类名#方法名信息
     * @return URL路径
     */
    private String getTemplatePath(String configKey) {
        Method method = processedMethods.get(configKey);
        int first = configKey.indexOf("#");
        String apiName = configKey.substring(0,first);
        String methodClassName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, apiName);
        return String.format(METHOD_MAPPING,methodClassName,method.getName());
    }
    @Override
    public MethodMetadata parseAndValidateMetadata(Class<?> targetType, Method method) {
        this.processedMethods.put(Feign.configKey(targetType, method), method);
        return super.parseAndValidateMetadata(targetType,method);
    }
    @Override
    protected boolean processAnnotationsOnParameter(MethodMetadata data, Annotation[] annotations, int paramIndex) {
        if(manyParameters(data.configKey())){
            return true;
        }
        return super.processAnnotationsOnParameter(data,annotations,paramIndex);
    }
    private void setNameParam(MethodMetadata data, String name, int i) {
        Collection<String>
                names =
                data.indexToName().containsKey(i) ? data.indexToName().get(i) : new ArrayList<String>();
        names.add(name);
        data.indexToName().put(i, names);
    }
    /**
     *
     * 多参数校验
     * @param configKey 类名#方法名
     * @return 参数是否为1个以上
     */
    private boolean manyParameters(String configKey){
        Method method = processedMethods.get(configKey);
        return method.getParameterTypes().length > 1;
    }

最后还有一处修改

由于我们在方法上没有写上RequestBody注解,所以此处需要进行额外的处理

  • 只针对于带有FeignClient的实现类才做特殊处理
  • 如果入参为非自定义对象,即为基本数据类型,则直接返回即可
  • 自定义对象,json转换后再返回
@Configuration
public class CustomerArgumentResolvers implements HandlerMethodArgumentResolver {
    // 基本数据类型
    private static final Class[] BASE_TYPE = new Class[]{String.class,int.class,Integer.class,boolean.class,Boolean.class, MultipartFile.class};
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        //springcloud的接口入参没有写@RequestBody,并且是自定义类型对象 也按JSON解析
        if (AnnotatedElementUtils.hasAnnotation(parameter.getContainingClass(), FeignClient.class)) {
            if(parameter.getExecutable().getParameters().length<=1){
                return true;
            }
        }
        return false;
    }
    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        final Type type = parameter.getGenericParameterType();
        String parameters = getParameters(nativeWebRequest);
        if(applyType(type)){
            return parameters;
        }else {
            return JSON.parseObject(parameters,type);
        }
    }
    private String getParameters(NativeWebRequest nativeWebRequest) throws Exception{
        HttpServletRequest servletRequest = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
        String jsonBody = "";
        if(servletRequest!=null){
            ServletInputStream inputStream = servletRequest.getInputStream();
            jsonBody =  IOUtils.toString(inputStream);
        }
        return jsonBody;
    }
    private boolean applyType(Type type){
        for (Class classType : BASE_TYPE) {
            if(type.equals(classType)){
                return true;
            }
        }
        return false;
    }
}
@Configuration
public class CustomerConfigAdapter implements WebMvcConfigurer {
    @Resource
    private CustomerArgumentResolvers customerArgumentResolvers;
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(customerArgumentResolvers);
    }
}

以上就是配置的所有内容,整体代码量很少。

但可能需要读下源码才能理解 

这些仅为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Java使用CountDownLatch实现网络同步请求的示例代码

    Java使用CountDownLatch实现网络同步请求的示例代码

    CountDownLatch 是一个同步工具类,用来协调多个线程之间的同步,它能够使一个线程在等待另外一些线程完成各自工作之后,再继续执行。被将利用CountDownLatch实现网络同步请求,异步同时获取商品信息组装,感兴趣的可以了解一下
    2023-01-01
  • Maven在Windows中的配置以及IDE中的项目创建实例

    Maven在Windows中的配置以及IDE中的项目创建实例

    下面小编就为大家带来一篇Maven在Windows中的配置以及IDE中的项目创建实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • Java中的分割字符串 split(“.”)无效问题

    Java中的分割字符串 split(“.”)无效问题

    这篇文章主要介绍了Java中的分割字符串 split(“.”)无效问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-12-12
  • 深入理解java线程通信

    深入理解java线程通信

    开发中不免会遇到需要所有子线程执行完毕通知主线程处理某些逻辑的场景。或者是线程 A 在执行到某个条件通知线程 B 执行某个操作。下面我们来一起学习如何解决吧
    2019-05-05
  • Java实现DES加密与解密,md5加密以及Java实现MD5加密解密类

    Java实现DES加密与解密,md5加密以及Java实现MD5加密解密类

    这篇文章主要介绍了Java实现DES加密与解密,md5加密以及Java实现MD5加密解密类 ,需要的朋友可以参考下
    2015-11-11
  • 史上最佳springboot Locale 国际化方案

    史上最佳springboot Locale 国际化方案

    今天给大家分享史上最佳springboot Locale 国际化方案,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2021-08-08
  • java 按行读取文件并输出到控制台的方法

    java 按行读取文件并输出到控制台的方法

    今天小编就为大家分享一篇java 按行读取文件并输出到控制台的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • Java SpringBoot自动装配原理详解及源码注释

    Java SpringBoot自动装配原理详解及源码注释

    SpringBoot的自动装配是拆箱即用的基础,也是微服务化的前提。其实它并不那么神秘,我在这之前已经写过最基本的实现了,大家可以参考这篇文章,来看看它是怎么样实现的,我们透过源代码来把握自动装配的来龙去脉
    2021-10-10
  • 详解Spring Security 中的四种权限控制方式

    详解Spring Security 中的四种权限控制方式

    这篇文章主要介绍了详解Spring Security 中的四种权限控制方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • Oracle+Mybatis的foreach insert批量插入报错的快速解决办法

    Oracle+Mybatis的foreach insert批量插入报错的快速解决办法

    本文给大家介绍Oracle+Mybatis的foreach insert批量插入报错的快速解决办法,非常不错,具有参考借鉴价值,感兴趣的朋友参考下吧
    2016-08-08

最新评论