All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.spring.boxes.gateway.locator.RedisKvRouteDefinitionLocator Maven / Gradle / Ivy

The newest version!
package com.spring.boxes.gateway.locator;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
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.lang3.StringUtils;
import org.springframework.cloud.gateway.filter.FilterDefinition;
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
import org.springframework.cloud.gateway.route.RedisRouteDefinitionRepository;
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 RedisKvRouteDefinitionLocator {

    private GatewayRoutesRefresher gatewayRoutesRefresher;

    private RedisRouteDefinitionRepository redisRouteDefinitionRepository;

    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 = this.buildMetadata(route);

        // 代理路径
        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.redisRouteDefinitionRepository.save(Mono.just(routeDefinition)).subscribe();
        } else {
            this.redisRouteDefinitionRepository.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);
        }
    }

    private Map buildMetadata(GatewayMetadata route) {
        Map metadata = Maps.newHashMap();
        metadata.put("authType", route.getAuthType());
        return metadata;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy