SpringBoot實現(xiàn)接口版本控制的示例代碼
概述
接口版本控制,比如微服務(wù)請求中某個接口需要升級,正常做法是升級我們的版本
比如:localhost:80/api/v1/test
技術(shù)實現(xiàn)
我們可以將版本相關(guān)的控制以包的形式引入到我們的項目中,那么我們就需要開發(fā)我們的對應(yīng)的版本控制的sdk
1,自定義注解
@Documented @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface ApiVersion { /** * 版本號 * @return */ int value() default 1; }
2,控制開關(guān)屬性
@RefreshScope @Data @ConfigurationProperties(prefix = "api.version") public class ApiVersionProperties { boolean enabled; }
3,重寫RequestMapping
public class ApiRequestHandlerMapping extends RequestMappingHandlerMapping { @Override protected RequestCondition<ApiVersionCondition> getCustomTypeCondition(Class<?> handlerType) { ApiVersion apiVersion = AnnotationUtils.findAnnotation(handlerType, ApiVersion.class); return createCondition(apiVersion); } @Override protected RequestCondition<ApiVersionCondition> getCustomMethodCondition(Method method) { ApiVersion apiVersion = AnnotationUtils.findAnnotation(method, ApiVersion.class); return createCondition(apiVersion); } private RequestCondition<ApiVersionCondition> createCondition(ApiVersion apiVersion) { return apiVersion == null ? null : new ApiVersionCondition(apiVersion.value()); } }
public class ApiVersionCondition implements RequestCondition<ApiVersionCondition> { /** * extract the version part from url. example [v0-9] */ private final static Pattern VERSION_PREFIX_PATTERN = Pattern.compile("v(\\d+)/"); private int apiVersion; public ApiVersionCondition(int apiVersion) { this.apiVersion = apiVersion; } /** * // 和另外一個請求匹配條件合并,具體合并邏輯由實現(xiàn)類提供 * @param other * @return */ @Override public ApiVersionCondition combine(ApiVersionCondition other) { // latest defined would be take effect, that means, methods definition with // override the classes definition return new ApiVersionCondition(other.getApiVersion()); } /** * // 檢查當(dāng)前請求匹配條件和指定請求request是否匹配,如果不匹配返回null, * // 如果匹配,生成一個新的請求匹配條件,該新的請求匹配條件是當(dāng)前請求匹配條件 * // 針對指定請求request的剪裁。 * // 舉個例子來講,如果當(dāng)前請求匹配條件是一個路徑匹配條件,包含多個路徑匹配模板, * // 并且其中有些模板和指定請求request匹配,那么返回的新建的請求匹配條件將僅僅 * // 包含和指定請求request匹配的那些路徑模板。 * @param request * @return */ @Override public ApiVersionCondition getMatchingCondition(HttpServletRequest request) { Matcher m = VERSION_PREFIX_PATTERN.matcher(request.getRequestURI()); if (m.find()) { Integer version = Integer.valueOf(m.group(1)); // when applying version number bigger than configuration, then it will take if (version >= this.apiVersion) { // effect return this; } } return null; } /** * 針對指定的請求對象request比較兩個請求匹配條件。 * 該方法假定被比較的兩個請求匹配條件都是針對該請求對象request調(diào)用了 * #getMatchingCondition方法得到的,這樣才能確保對它們的比較 * 是針對同一個請求對象request,這樣的比較才有意義(最終用來確定誰是 * 更匹配的條件)。 * @param other * @param request * @return */ @Override public int compareTo(ApiVersionCondition other, HttpServletRequest request) { // when more than one configured version number passed the match rule, then only // the biggest one will take effect. return other.getApiVersion() - this.apiVersion; } public int getApiVersion() { return apiVersion; } }
4,注冊
@Configuration @EnableConfigurationProperties(ApiVersionProperties.class) @ConditionalOnProperty( value = {"api.version.enabled"}, matchIfMissing = true ) public class WebConfig implements WebMvcRegistrations { @Override public RequestMappingHandlerMapping getRequestMappingHandlerMapping() { return new ApiRequestHandlerMapping(); } }
5,最后resource/MATE-INFO/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.example.springbootgray.apiversion.WebConfig,\
使用
@RestController @ApiVersion(value = 1) @RequestMapping("/api/{version}")
調(diào)用使用
localhost:8080/api/v1/test
到此這篇關(guān)于SpringBoot實現(xiàn)接口版本控制的示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot接口版本控制內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java Swing窗體關(guān)閉事件的調(diào)用關(guān)系
這篇文章主要為大家詳細(xì)介紹了Java Swing窗體關(guān)閉事件的調(diào)用關(guān)系,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07從源碼角度簡單看StringBuilder和StringBuffer的異同(全面解析)
下面小編就為大家分享一篇從源碼角度簡單看StringBuilder和StringBuffer的異同(全面解析),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12