Skip to content

Commit

Permalink
修改Knife4jGatewayProperties.Discover传输逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
lihui committed Aug 24, 2023
1 parent 9c66ce6 commit b3b2a7e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,44 +54,49 @@ public abstract class AbstactServiceRouterConvert implements ServiceRouterConver
*/
abstract String convertPathPrefix(Map<String, String> predicateArgs);

/**
* 获取服务发现策略配置信息
*
* @return 服务发现策略配置信息
*/
abstract Knife4jGatewayProperties.Discover getDiscover();

/**
* 解析gateway的路由定义
*
* @param routerHolder
* @param discover
* @param predicateDefinitions
* @param id
* @param serviceName
*/
protected void parseRouteDefinition(ServiceRouterHolder routerHolder, Knife4jGatewayProperties.Discover discover,
protected void parseRouteDefinition(ServiceRouterHolder routerHolder,
List<PredicateDefinition> predicateDefinitions, String id, String serviceName) {
predicateDefinitions.stream().filter(
predicateDefinition -> GlobalConstants.ROUTER_PATH_NAME.equalsIgnoreCase(predicateDefinition.getName()))
.findFirst().ifPresent(predicateDefinition -> this.processRouteDefinition(routerHolder, discover, id,
serviceName, predicateDefinition));
.findFirst().ifPresent(predicateDefinition -> this.processRouteDefinition(routerHolder, id, serviceName,
predicateDefinition));
}

/**
* 处理gateway的路由定义
*
* @param routerHolder
* @param discover
* @param id
* @param serviceName
* @param predicateDefinition
*/
private void processRouteDefinition(ServiceRouterHolder routerHolder, Knife4jGatewayProperties.Discover discover,
String id, String serviceName, PredicateDefinition predicateDefinition) {
private void processRouteDefinition(ServiceRouterHolder routerHolder, String id, String serviceName,
PredicateDefinition predicateDefinition) {
log.debug("serviceId:{},serviceName:{}", id, serviceName);
Map<String, Knife4jGatewayProperties.ServiceConfigInfo> configInfoMap = discover.getServiceConfig();
String pathPrefix = this.convertPathPrefix(predicateDefinition.getArgs());
log.debug("pathPrefix:{}", pathPrefix);
String contextPath = GlobalConstants.EMPTY_STR;
String groupName = id;
int order = 0;
Knife4jGatewayProperties.Discover discover = this.getDiscover();
String targetUrl = ServiceUtils.getOpenAPIURL(discover, pathPrefix, null);
// 如果自定义了setting内容 拼接
Knife4jGatewayProperties.ServiceConfigInfo configInfo = configInfoMap.get(serviceName);
Knife4jGatewayProperties.ServiceConfigInfo configInfo = this.getServiceConfigInfo(serviceName);
if (configInfo != null) {
order = configInfo.getOrder();
if (discover.getVersion() == OpenApiVersion.OpenAPI3) {
Expand Down Expand Up @@ -131,6 +136,17 @@ private void processRouteDefinition(ServiceRouterHolder routerHolder, Knife4jGat
routerHolder.add(this.buildOpenApi2Resource(serviceName, contextPath, groupName, order, targetUrl));
}

/**
* 从配置里面获取服务配置信息
*
* @param serviceName
* @return
*/
private Knife4jGatewayProperties.ServiceConfigInfo getServiceConfigInfo(String serviceName) {
Map<String, Knife4jGatewayProperties.ServiceConfigInfo> configInfoMap = this.getDiscover().getServiceConfig();
return configInfoMap.get(serviceName);
}

/**
* 构建资源
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void process(ServiceRouterHolder routerHolder) {
.stream()
.filter(routeDefinition -> ServiceUtils.startLoadBalance(routeDefinition.getUri()))
.filter(routeDefinition -> ServiceUtils.includeService(routeDefinition.getUri(), routerHolder.getService(), routerHolder.getExcludeService()))
.forEach(routeDefinition -> parseRouteDefinition(routerHolder, this.knife4jGatewayProperties.getDiscover(), routeDefinition.getPredicates(), routeDefinition.getUri().getHost(),
.forEach(routeDefinition -> parseRouteDefinition(routerHolder, routeDefinition.getPredicates(), routeDefinition.getUri().getHost(),
routeDefinition.getUri().getHost()));
}

Expand All @@ -67,4 +67,10 @@ String convertPathPrefix(Map<String, String> predicateArgs) {
public int order() {
return GatewayRouterStrategy.CONFIG.getOrder();
}

@Override
Knife4jGatewayProperties.Discover getDiscover() {
return this.knife4jGatewayProperties.getDiscover();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void process(ServiceRouterHolder holder) {
discoveryClientRouteDefinitionLocator.getRouteDefinitions()
.filter(routeDefinition -> ServiceUtils.startLoadBalance(routeDefinition.getUri()))
.filter(routeDefinition -> ServiceUtils.includeService(routeDefinition.getUri(), holder.getService(), holder.getExcludeService()))
.subscribe(routeDefinition -> parseRouteDefinition(holder, this.knife4jGatewayProperties.getDiscover(), routeDefinition.getPredicates(), routeDefinition.getUri().getHost(),
.subscribe(routeDefinition -> parseRouteDefinition(holder, routeDefinition.getPredicates(), routeDefinition.getUri().getHost(),
routeDefinition.getUri().getHost()));
}

Expand All @@ -71,4 +71,10 @@ String convertPathPrefix(Map<String, String> predicateArgs) {
public int order() {
return GatewayRouterStrategy.REACTIVE.getOrder();
}

@Override
Knife4jGatewayProperties.Discover getDiscover() {
return this.knife4jGatewayProperties.getDiscover();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void process(ServiceRouterHolder holder) {
routeDefinitionRepository.getRouteDefinitions()
.filter(routeDefinition -> ServiceUtils.startLoadBalance(routeDefinition.getUri()))
.filter(routeDefinition -> ServiceUtils.includeService(routeDefinition.getUri(), holder.getService(), holder.getExcludeService()))
.subscribe(routeDefinition -> parseRouteDefinition(holder, this.knife4jGatewayProperties.getDiscover(), routeDefinition.getPredicates(), routeDefinition.getUri().getHost(),
.subscribe(routeDefinition -> parseRouteDefinition(holder, routeDefinition.getPredicates(), routeDefinition.getUri().getHost(),
routeDefinition.getUri().getHost()));
}

Expand All @@ -65,4 +65,10 @@ String convertPathPrefix(Map<String, String> predicateArgs) {
public int order() {
return GatewayRouterStrategy.DYNAMIC.getOrder();
}

@Override
Knife4jGatewayProperties.Discover getDiscover() {
return this.knife4jGatewayProperties.getDiscover();
}

}

0 comments on commit b3b2a7e

Please sign in to comment.