spring扩展之基于HandlerMapping实现接口灰度发布的demo

背景

面试经常被问到,你了解spring源码吗?有基于spring做过什么扩展吗?除了PropertyPlaceholderConfigurer处理占位符(被说烂了)还有其他的吗?

看了springmvc的源码,有了一个新的案例可讲(吹)

基于HandlerMapping实现接口灰度发布

说这个之前可以先铺垫一下,将故事讲的生动一点,我们来简单模拟一下。

面试官:“简历上说你对spring源码比较熟悉,能简单讲讲你对ioc、aop的理解吗?”

张三:“ioc是……aop是……巴拉巴拉”

(背了一大段八股文,毫无新意,面试官面无表情,然后打断张三,继续问)

面试官:你说对spring源码很熟悉,有多熟悉,在实际开发中有用过?扩展过吗?

张三:“有过呀,当时有个系统,升级改动比较大,对于同一批接口,老用户和新用户返回的数据不一样,为了实现系统的最小化改动,我当时就用spring的HandlerMapping解决了这个问题,前端访问的接口不变,只在参数中加一个接口版本号,就能实现灰度……,具体实现是这样的”

实现方案

自定义注解

新增一个自定义注解,用来标注接口的适配的版本号,默认为1.0版本

package com.fast.alibaba.annotation;

import java.lang.annotation.*;

/**
 * 自定义版本号注解
 */
@Documented
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiVersion {

    double value() default 1.0;
}

自定义HandleMapping

自定义HandleMapping,继承spring家的HandleMapping,并重写两个getCustomTypeCondition方法

package com.fast.alibaba.handlemapping;

import com.fast.alibaba.annotation.ApiVersion;
import com.fast.alibaba.condition.ApiVersionRequestCondition;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.servlet.mvc.condition.RequestCondition;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.lang.reflect.Method;

public class ApiVersionHandleMapping extends RequestMappingHandlerMapping {


    @Override
    protected RequestCondition getCustomTypeCondition(Class handlerType) {
        ApiVersion apiVersion = AnnotationUtils.getAnnotation(handlerType, ApiVersion.class);
        return new ApiVersionRequestCondition(apiVersion != null ? apiVersion.value() : 1.0);
    }


    @Override
    protected RequestCondition getCustomMethodCondition(Method method) {
        ApiVersion apiVersion = AnnotationUtils.getAnnotation(method, ApiVersion.class);
        if(apiVersion == null){
            apiVersion = AnnotationUtils.getAnnotation(method.getDeclaringClass(), ApiVersion.class);
        }
        return new ApiVersionRequestCondition(apiVersion != null ? apiVersion.value() : 1.0);
    }
}

实现RequestCondition接口

以便识别前端传送的版本与接口上注解版本匹配

package com.fast.alibaba.condition;

import com.alibaba.cloud.commons.lang.StringUtils;
import org.springframework.web.servlet.mvc.condition.RequestCondition;

import javax.servlet.http.HttpServletRequest;

public class ApiVersionRequestCondition implements RequestCondition {
    private double apiVersion = 1.0;

    private static final String VERSION_NAME = "api-version";

    public double getApiVersion() {
        return apiVersion;
    }

    public ApiVersionRequestCondition(double apiVersion){
        this.apiVersion=apiVersion;
    }

    @Override
    public ApiVersionRequestCondition combine(ApiVersionRequestCondition method) {
        return method;
    }

    @Override
    public int compareTo(ApiVersionRequestCondition other, HttpServletRequest request) {
        return Double.compare(other.getApiVersion(),this.getApiVersion());
    }

    @Override
    public ApiVersionRequestCondition getMatchingCondition(HttpServletRequest request) {

        double reqVersionDouble = 1.0;

        String reqVersion = request.getHeader(VERSION_NAME);
        if(StringUtils.isEmpty(reqVersion)){
            reqVersion = request.getParameter(VERSION_NAME);
        }

        if(!StringUtils.isEmpty(reqVersion)){
            reqVersionDouble = Double.parseDouble(reqVersion);
        }

        if(this.getApiVersion() == reqVersionDouble){
            return this;
        }
        return null;
    }
}

自定义WebMvcRegistrations

package com.fast.alibaba.register;

import com.fast.alibaba.handlemapping.ApiVersionHandleMapping;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

/**
 * 注册自定义的 ApiVersionHandleMapping
 */
public class ApiVersionMappingRegister implements WebMvcRegistrations {

    @Override
    public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
        return new ApiVersionHandleMapping();
    }
}

注入WebMvcRegistrations

package com.fast.alibaba.config;

import com.fast.alibaba.register.ApiVersionMappingRegister;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BaseConfiguration {
    @Bean
    public WebMvcRegistrations getWebMvcRegistrations(){
        return new ApiVersionMappingRegister();
    }
}

测试类

package com.fast.alibaba.controller;

import com.fast.alibaba.annotation.ApiVersion;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestHandlerMappingController {



    @GetMapping("/getInfo")
    @ApiVersion(1.0)
    public String getSomeInfoByV1(){

        return "version 1.0";
    }


    @GetMapping("/getInfo")
    @ApiVersion(2.0)
    public String getSomeInfoByV2(){

        return "version 2.0";
    }


    @GetMapping("/getInfo")
    @ApiVersion(3.0)
    public String getSomeInfoByV3(){

        return "version 3.0";
    }

}

spring扩展之基于HandlerMapping实现接口灰度发布的demo_第1张图片

spring扩展之基于HandlerMapping实现接口灰度发布的demo_第2张图片 

spring扩展之基于HandlerMapping实现接口灰度发布的demo_第3张图片 

 

面试官:“还算有点东西,你回去等通知吧”

你可能感兴趣的:(spring-boot,springmvc,spring,java,后端)