2021-04-22 18:51:53 +08:00
|
|
|
|
package ink.wgink.gateway.component;
|
|
|
|
|
|
2021-04-23 21:53:45 +08:00
|
|
|
|
import ink.wgink.gateway.consts.ISystemConst;
|
2021-04-22 18:51:53 +08:00
|
|
|
|
import ink.wgink.gateway.dao.route.IRouteDao;
|
|
|
|
|
import ink.wgink.gateway.handler.route.RouteHandler;
|
2021-04-23 21:53:45 +08:00
|
|
|
|
import ink.wgink.gateway.pojo.route.Route;
|
2021-04-22 18:51:53 +08:00
|
|
|
|
import ink.wgink.gateway.util.UUIDUtil;
|
|
|
|
|
import org.springframework.cloud.client.ServiceInstance;
|
|
|
|
|
import org.springframework.cloud.client.loadbalancer.*;
|
2021-04-23 21:53:45 +08:00
|
|
|
|
import org.springframework.cloud.gateway.filter.FilterDefinition;
|
|
|
|
|
import org.springframework.cloud.gateway.filter.factory.RewritePathGatewayFilterFactory;
|
2021-04-22 18:51:53 +08:00
|
|
|
|
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
|
2021-04-23 21:53:45 +08:00
|
|
|
|
import org.springframework.cloud.gateway.handler.predicate.RoutePredicateFactory;
|
2021-04-22 18:51:53 +08:00
|
|
|
|
import org.springframework.cloud.gateway.route.RouteDefinition;
|
|
|
|
|
import org.springframework.cloud.gateway.route.RouteDefinitionRepository;
|
|
|
|
|
import org.springframework.cloud.gateway.route.RouteLocator;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
import org.springframework.web.util.UriComponents;
|
|
|
|
|
import org.springframework.web.util.UriComponentsBuilder;
|
|
|
|
|
import reactor.core.publisher.Flux;
|
|
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.net.URI;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* When you feel like quitting. Think about why you started
|
|
|
|
|
* 当你想要放弃的时候,想想当初你为何开始
|
|
|
|
|
*
|
|
|
|
|
* @ClassName: RouteDefinitionRepository
|
|
|
|
|
* @Description: 路由定义
|
|
|
|
|
* @Author: wanggeng
|
|
|
|
|
* @Date: 2021/4/22 4:04 下午
|
|
|
|
|
* @Version: 1.0
|
|
|
|
|
*/
|
|
|
|
|
@Component
|
|
|
|
|
public class MongoRouteDefinitionRepository implements RouteDefinitionRepository {
|
|
|
|
|
|
2021-04-23 21:53:45 +08:00
|
|
|
|
public IRouteDao routeDao;
|
2021-04-22 18:51:53 +08:00
|
|
|
|
|
2021-04-23 21:53:45 +08:00
|
|
|
|
public MongoRouteDefinitionRepository(IRouteDao routeDao) {
|
|
|
|
|
this.routeDao = routeDao;
|
2021-04-22 18:51:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Flux<RouteDefinition> getRouteDefinitions() {
|
2021-04-23 21:53:45 +08:00
|
|
|
|
return this.routeDefinitionFlux();
|
2021-04-22 18:51:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Mono<Void> save(Mono<RouteDefinition> route) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Mono<Void> delete(Mono<String> routeId) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2021-04-23 21:53:45 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 路由定义
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private Flux<RouteDefinition> routeDefinitionFlux() {
|
|
|
|
|
return routeDao.findAll().flatMap(route -> {
|
|
|
|
|
// 定义路由
|
|
|
|
|
RouteDefinition routeDefinition = new RouteDefinition();
|
|
|
|
|
routeDefinition.setId(UUIDUtil.getUUID());
|
|
|
|
|
routeDefinition.setUri(UriComponentsBuilder.fromUriString(route.getUri()).build().toUri());
|
|
|
|
|
routeDefinition.setPredicates(this.listPredicateDefinition(route));
|
|
|
|
|
// 过滤器
|
|
|
|
|
routeDefinition.setFilters(this.listFilterDefinition(route));
|
|
|
|
|
return Flux.just(routeDefinition);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 路由断言列表
|
|
|
|
|
*
|
|
|
|
|
* @param route
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private List<PredicateDefinition> listPredicateDefinition(Route route) {
|
|
|
|
|
// 定义断言
|
|
|
|
|
List<PredicateDefinition> predicateDefinitions = new ArrayList<>();
|
|
|
|
|
// 路由断言
|
|
|
|
|
PredicateDefinition pathPredicateDefinition = new PredicateDefinition();
|
|
|
|
|
Map<String, String> pathPredicateDefinitionMap = new HashMap<>(2);
|
|
|
|
|
pathPredicateDefinitionMap.put(RoutePredicateFactory.PATTERN_KEY, ISystemConst.ROUTE_PREFIX + route.getPath());
|
|
|
|
|
pathPredicateDefinition.setName("Path");
|
|
|
|
|
pathPredicateDefinition.setArgs(pathPredicateDefinitionMap);
|
|
|
|
|
// 添加路由断言
|
|
|
|
|
predicateDefinitions.add(pathPredicateDefinition);
|
|
|
|
|
return predicateDefinitions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 过滤器列表
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private List<FilterDefinition> listFilterDefinition(Route route) {
|
|
|
|
|
// 定义过滤器
|
|
|
|
|
List<FilterDefinition> filterDefinitions = new ArrayList<>();
|
|
|
|
|
FilterDefinition rewritePathFilterDefinition = new FilterDefinition();
|
|
|
|
|
Map<String, String> rewritePathFilterDefinitionMap = new HashMap<>(2);
|
|
|
|
|
rewritePathFilterDefinitionMap.put(RewritePathGatewayFilterFactory.REGEXP_KEY, ISystemConst.ROUTE_PREFIX + "/?(?<segment>.*)");
|
|
|
|
|
rewritePathFilterDefinitionMap.put(RewritePathGatewayFilterFactory.REPLACEMENT_KEY, "/$\\{segment}");
|
|
|
|
|
rewritePathFilterDefinition.setArgs(rewritePathFilterDefinitionMap);
|
|
|
|
|
rewritePathFilterDefinition.setName("RewritePath");
|
|
|
|
|
// 添加重写过滤
|
|
|
|
|
filterDefinitions.add(rewritePathFilterDefinition);
|
|
|
|
|
return filterDefinitions;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 18:51:53 +08:00
|
|
|
|
}
|