SpringBoot實現(xiàn)接口版本控制的示例代碼
概述
接口版本控制,比如微服務請求中某個接口需要升級,正常做法是升級我們的版本
比如:localhost:80/api/v1/test
技術實現(xiàn)
我們可以將版本相關的控制以包的形式引入到我們的項目中,那么我們就需要開發(fā)我們的對應的版本控制的sdk
1,自定義注解
@Documented
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiVersion {
/**
* 版本號
* @return
*/
int value() default 1;
}
2,控制開關屬性
@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());
}
/**
* // 檢查當前請求匹配條件和指定請求request是否匹配,如果不匹配返回null,
* // 如果匹配,生成一個新的請求匹配條件,該新的請求匹配條件是當前請求匹配條件
* // 針對指定請求request的剪裁。
* // 舉個例子來講,如果當前請求匹配條件是一個路徑匹配條件,包含多個路徑匹配模板,
* // 并且其中有些模板和指定請求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
到此這篇關于SpringBoot實現(xiàn)接口版本控制的示例代碼的文章就介紹到這了,更多相關SpringBoot接口版本控制內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java for循環(huán)性能優(yōu)化實現(xiàn)解析
這篇文章主要介紹了Java for循環(huán)性能優(yōu)化實現(xiàn)解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-01-01
Maven的pom.xml文件結(jié)構(gòu)中的build
本文主要介紹了Maven的pom.xml文件結(jié)構(gòu)中的build,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-07-07
詳解Java集合中的基本數(shù)據(jù)結(jié)構(gòu)
總有小伙伴讓我總結(jié)一下Java集合中的基本數(shù)據(jù)結(jié)構(gòu)的相關知識,今天特地整理了本篇文章,文中有非常詳細的介紹,需要的朋友可以參考下2021-06-06
詳解Java中PriorityQueue的作用和源碼實現(xiàn)
這篇文章主要為大家詳細介紹了Java中阻塞隊列PriorityQueue的作用和源碼實現(xiàn)的相關知識,文中的示例代碼講解詳細,需要的小伙伴可以了解下2024-02-02
實例講解Java的MyBatis框架對MySQL中數(shù)據(jù)的關聯(lián)查詢
這里我們來以實例講解Java的MyBatis框架對MySQL中數(shù)據(jù)的關聯(lián)查詢,包括一對多、多對一的關聯(lián)查詢以及自身關聯(lián)映射的方法等,需要的朋友可以參考下2016-06-06

