com.spring.boxes.gateway.locator.MemoryRouteDefinitionLocator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-boxes-gateway-starter Show documentation
Show all versions of spring-boxes-gateway-starter Show documentation
spring-cookie-boxes-boot-starter
The newest version!
package com.spring.boxes.gateway.locator;
import com.google.common.collect.Lists;
import com.spring.boxes.dollar.CollectionUtils;
import com.spring.boxes.dollar.JSONUtils;
import com.spring.boxes.dollar.enums.OnlineTypeEnum;
import com.spring.boxes.dollar.support.Endpoint;
import com.spring.boxes.dollar.support.gateway.GatewayMetadata;
import com.spring.boxes.gateway.refresh.GatewayRoutesRefresher;
import com.spring.boxes.gateway.routes.GatewayDefinition;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.gateway.filter.FilterDefinition;
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
import org.springframework.cloud.gateway.route.InMemoryRouteDefinitionRepository;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.support.NameUtils;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Mono;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@NoArgsConstructor
@AllArgsConstructor
public class MemoryRouteDefinitionLocator {
private GatewayRoutesRefresher gatewayRoutesRefresher;
private InMemoryRouteDefinitionRepository inMemoryRouteDefinitionRepository;
public void refresh(GatewayMetadata route){
// 断言
List predicates = Lists.newArrayList();
PredicateDefinition predicateDefinition = GatewayDefinition.path(route.getPath());
predicates.add(predicateDefinition);
// 过滤器
List filters = Lists.newArrayList();
if (route.getStripPrefix() > 0) {
FilterDefinition stripPrefixFilterDefinition = GatewayDefinition.stripPrefix(String.valueOf(route.getStripPrefix()));
filters.add(stripPrefixFilterDefinition);
}
// 默认过滤器
if (StringUtils.isNotEmpty(route.getFilters())) {
List customFilters = JSONUtils.fromJSON(route.getFilters(), List.class, FilterDefinition.class);
this.reloadArgs(customFilters);
filters.addAll(customFilters);
}
// 路径重写
if (StringUtils.isNotBlank(route.getPathTo())) {
FilterDefinition pathToFilterDefinition = GatewayDefinition.pathTo(route.getPath(), route.getPathTo());
filters.add(pathToFilterDefinition);
}
// 元数据
Map metadata = MapUtils.emptyIfNull(route.getMetadata());
// 代理路径
URI uri = Endpoint.isHttpOrHttps(route.getUri()) ? UriComponentsBuilder.fromHttpUrl(route.getUri()).build().toUri() : URI.create(route.getUri());
// 构建路由信息
RouteDefinition routeDefinition = new RouteDefinition();
routeDefinition.setId(String.valueOf(route.getId()));
routeDefinition.setPredicates(predicates);
routeDefinition.setUri(uri);
routeDefinition.setFilters(filters);
routeDefinition.setMetadata(metadata);
// 在线的追加 反之下线
if (OnlineTypeEnum.ONLINE.getValue() == route.getStatus()) {
this.inMemoryRouteDefinitionRepository.save(Mono.just(routeDefinition)).subscribe();
} else {
this.inMemoryRouteDefinitionRepository.delete(Mono.just(String.valueOf(route.getId()))).subscribe();
}
this.gatewayRoutesRefresher.refreshRoutes();
}
private void reloadArgs(List filterDefinitions) {
if (CollectionUtils.isEmpty(filterDefinitions)) {
return;
}
for (FilterDefinition definition : filterDefinitions) {
Map args = new HashMap<>();
int i = 0;
for (Map.Entry entry : definition.getArgs().entrySet()) {
args.put(NameUtils.generateName(i), entry.getKey());
args.put(NameUtils.generateName(i + 1), entry.getValue());
i += 2;
}
definition.setArgs(args);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy