Java Code Examples for org.springframework.cloud.netflix.zuul.filters.ZuulProperties#ZuulRoute

The following examples show how to use org.springframework.cloud.netflix.zuul.filters.ZuulProperties#ZuulRoute . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JdbcRouteLocator.java    From open-cloud with MIT License 6 votes vote down vote up
/**
 * 加载路由配置
 *
 * @return
 */
@Override
protected Map<String, ZuulProperties.ZuulRoute> locateRoutes() {
    LinkedHashMap<String, ZuulProperties.ZuulRoute> routesMap = Maps.newLinkedHashMap();
    routesMap.putAll(super.locateRoutes());
    //从db中加载路由信息
    routesMap.putAll(loadRoutes());
    //优化一下配置
    LinkedHashMap<String, ZuulProperties.ZuulRoute> values = Maps.newLinkedHashMap();
    for (Map.Entry<String, ZuulProperties.ZuulRoute> entry : routesMap.entrySet()) {
        String path = entry.getKey();
        // Prepend with slash if not already present.
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        if (StringUtils.hasText(this.properties.getPrefix())) {
            path = this.properties.getPrefix() + path;
            if (!path.startsWith("/")) {
                path = "/" + path;
            }
        }
        values.put(path, entry.getValue());
    }
    return values;
}
 
Example 2
Source File: DynamicRouteLocator.java    From Taroco with Apache License 2.0 6 votes vote down vote up
/**
 * 重写路由配置
 *
 * @return 路由表
 */
@Override
protected LinkedHashMap<String, ZuulProperties.ZuulRoute> locateRoutes() {
    //读取properties配置、eureka默认配置
    LinkedHashMap<String, ZuulProperties.ZuulRoute> routesMap = new LinkedHashMap<>(super.locateRoutes());
    log.debug("初始默认的路由配置完成");
    routesMap.putAll(locateRoutesFromCache());
    LinkedHashMap<String, ZuulProperties.ZuulRoute> values = new LinkedHashMap<>();
    for (Map.Entry<String, ZuulProperties.ZuulRoute> entry : routesMap.entrySet()) {
        String path = entry.getKey();
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        if (StrUtil.isNotBlank(this.properties.getPrefix())) {
            path = this.properties.getPrefix() + path;
            if (!path.startsWith("/")) {
                path = "/" + path;
            }
        }
        values.put(path, entry.getValue());
    }
    return values;
}
 
Example 3
Source File: NacosDynRouteLocator.java    From microservices-platform with Apache License 2.0 6 votes vote down vote up
@Override
public Map<String, ZuulProperties.ZuulRoute> loadDynamicRoute() {
    Map<String, ZuulRoute> routes = new LinkedHashMap<>();
    if (zuulRouteEntities == null) {
        zuulRouteEntities = getNacosConfig();
    }
    for (ZuulRouteEntity result : zuulRouteEntities) {
        if (StrUtil.isBlank(result.getPath()) || !result.isEnabled()) {
            continue;
        }
        ZuulRoute zuulRoute = new ZuulRoute();
        BeanUtil.copyProperties(result, zuulRoute);
        routes.put(zuulRoute.getPath(), zuulRoute);
    }
    return routes;
}
 
Example 4
Source File: DynamicRouteLocator.java    From pig with MIT License 6 votes vote down vote up
/**
 * 重写路由配置
 * <p>
 * 1. properties 配置。
 * 2. eureka 默认配置。
 * 3. DB数据库配置。
 *
 * @return 路由表
 */
@Override
protected LinkedHashMap<String, ZuulProperties.ZuulRoute> locateRoutes() {
    LinkedHashMap<String, ZuulProperties.ZuulRoute> routesMap = new LinkedHashMap<>();
    //读取properties配置、eureka默认配置
    routesMap.putAll(super.locateRoutes());
    log.debug("初始默认的路由配置完成");
    routesMap.putAll(locateRoutesFromDb());
    LinkedHashMap<String, ZuulProperties.ZuulRoute> values = new LinkedHashMap<>();
    for (Map.Entry<String, ZuulProperties.ZuulRoute> entry : routesMap.entrySet()) {
        String path = entry.getKey();
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        if (StrUtil.isNotBlank(this.properties.getPrefix())) {
            path = this.properties.getPrefix() + path;
            if (!path.startsWith("/")) {
                path = "/" + path;
            }
        }
        values.put(path, entry.getValue());
    }
    return values;
}
 
Example 5
Source File: CassandraZuulRouteStoreTest.java    From zuul-route-cassandra-spring-cloud-starter with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRetrieveEmptyRouteList() {

    // given
    cassandraTemplate.truncate("zuul_routes");

    // when
    final List<ZuulProperties.ZuulRoute> routes = instance.findAll();

    // then
    assertTrue(routes.isEmpty());
}
 
Example 6
Source File: StoreProxyRouteLocator.java    From zuul-route-cassandra-spring-cloud-starter with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected void addConfiguredRoutes(Map<String, ZuulProperties.ZuulRoute> routes) {
    for (ZuulProperties.ZuulRoute route : store.findAll()) {
        routes.put(route.getPath(), route);
    }
}
 
Example 7
Source File: LoadBalancedProxyTargetResolver.java    From spring-cloud-netflix-zuul-websocket with Apache License 2.0 5 votes vote down vote up
@Override
public URI resolveTarget(ZuulWebSocketProperties.WsBrokerage wsBrokerage) {
    ZuulProperties.ZuulRoute zuulRoute = resolveRoute(wsBrokerage);
    if (zuulRoute == null || StringUtils.isEmpty(zuulRoute.getServiceId())) return null;

    ServiceInstance serviceInstance = loadBalancerClient.choose(zuulRoute.getServiceId());
    return serviceInstance != null ? resolveUri(serviceInstance) : null;
}
 
Example 8
Source File: TrieRouteMatcherTest.java    From zuul-trie-matcher-spring-cloud-starter with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotMatchExactRoute() {
    // given
    final String path = "/uaa";

    // when
    final ZuulProperties.ZuulRoute result = instance.getMatchingRoute(path);

    // then
    assertNull(result);
}
 
Example 9
Source File: UrlProxyTargetResolver.java    From spring-cloud-netflix-zuul-websocket with Apache License 2.0 5 votes vote down vote up
@Override
public URI resolveTarget(ZuulWebSocketProperties.WsBrokerage wsBrokerage) {
    ZuulProperties.ZuulRoute zuulRoute = resolveRoute(wsBrokerage);
    if (zuulRoute == null || StringUtils.isEmpty(zuulRoute.getUrl())) return null;

    try {
        return new URI(zuulRoute.getUrl());
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
}
 
Example 10
Source File: CustomRouteLocator.java    From oneplatform with Apache License 2.0 5 votes vote down vote up
@Override  
  protected Map<String, ZuulProperties.ZuulRoute> locateRoutes() {  
      LinkedHashMap<String, ZuulProperties.ZuulRoute> routesMap = new LinkedHashMap<>();  
      
      routesMap.putAll(super.locateRoutes());  
      
      ModuleEntityMapper entityMapper = null;
      try {entityMapper = InstanceFactory.getInstance(ModuleEntityMapper.class);} catch (Exception e) {}
      if(entityMapper == null)return routesMap;
      
      List<ModuleEntity> serviceModules = entityMapper.findAllEnabled();
      
      String path = null;
      ZuulProperties.ZuulRoute zuulRoute = null;
      for (ModuleEntity module : serviceModules) {
      	if(module.getEnabled() == false 
      			|| module.getServiceId().equalsIgnoreCase(GlobalContants.MODULE_NAME) 
      			|| ModuleType.plugin.name().equals(module.getModuleType()))continue;
      	path = String.format("/%s/**", module.getRouteName());
      	
      	zuulRoute = new ZuulProperties.ZuulRoute();  
      	zuulRoute.setPath(path);
      	zuulRoute.setId(module.getRouteName());
      	zuulRoute.setServiceId(module.getServiceId());
      	
      	routesMap.put(path, zuulRoute);
      	registryRouteIds.add(module.getRouteName());
      	
      	logger.info("add new Route:{} = {}",path,JsonUtils.toJson(zuulRoute));
}

      return routesMap;  
  }
 
Example 11
Source File: TrieRouteMatcher.java    From zuul-trie-matcher-spring-cloud-starter with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void setRoutes(Map<String, ZuulProperties.ZuulRoute> routes) {

    final Trie<ZuulRouteEntry> trie = createTrie();
    for (Map.Entry<String, ZuulProperties.ZuulRoute> route : routes.entrySet()) {
        trie.put(
                path(route.getKey()),
                new ZuulRouteEntry(route.getKey(), route.getValue(), isWildcard(route.getKey()))
        );
    }
    this.trie.lazySet(trie);
}
 
Example 12
Source File: TrieRouteMatcher.java    From zuul-trie-matcher-spring-cloud-starter with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public ZuulProperties.ZuulRoute getMatchingRoute(String path) {
    final ZuulRouteEntry matching = trie.get().prefix(path);
    if (matching == null
            || !matching.isWildcard() && !matchesExact(path, matching.getPath())) {
        return null;
    } else {
        return matching.getRoute();
    }
}
 
Example 13
Source File: CassandraZuulRouteStoreTest.java    From zuul-route-cassandra-spring-cloud-starter with Apache License 2.0 5 votes vote down vote up
private Map<String, ZuulProperties.ZuulRoute> asMap(List<ZuulProperties.ZuulRoute> routes) {
    final Map<String, ZuulProperties.ZuulRoute> map = new HashMap<String, ZuulProperties.ZuulRoute>();
    for(ZuulProperties.ZuulRoute route : routes) {
        map.put(route.getId(), route);
    }
    return map;
}
 
Example 14
Source File: MatcherProxyRouteLocator.java    From zuul-trie-matcher-spring-cloud-starter with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@link org.springframework.cloud.netflix.zuul.filters.ProxyRouteLocator.ProxyRouteSpec} out of
 * {@link org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute}.
 *
 * @param route the zuul route
 * @return the route spec
 */
private ProxyRouteSpec toProxyRouteSpec(final String path, final ZuulProperties.ZuulRoute route) {
    if(route == null) {
        return null;
    }

    String targetPath = getRequestPath(path);
    String prefix = properties.getPrefix();
    final Boolean retryable = isRetryable(route);

    if (properties.isStripPrefix() && targetPath.startsWith(prefix)) {
        targetPath = path.substring(prefix.length());
    }

    if (route.isStripPrefix()) {
        int index = route.getPath().indexOf("*") - 1;
        if (index > 0) {
            String routePrefix = route.getPath().substring(0, index);
            targetPath = targetPath.replaceFirst(routePrefix, "");
            prefix = prefix + routePrefix;
        }
    }

    return new ProxyRouteSpec(
            route.getId(),
            targetPath,
            route.getLocation(),
            prefix,
            retryable
    );
}
 
Example 15
Source File: ApimlRouteLocatorStub.java    From api-layer with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected LinkedHashMap<String, ZuulProperties.ZuulRoute> locateRoutes() {
    return applicationRegistry.getRoutes();
}
 
Example 16
Source File: AbstractProxyTargetResolver.java    From spring-cloud-netflix-zuul-websocket with Apache License 2.0 4 votes vote down vote up
protected ZuulProperties.ZuulRoute resolveRoute(ZuulWebSocketProperties.WsBrokerage wsBrokerage) {

        ZuulProperties.ZuulRoute zuulRoute = zuulProperties.getRoutes().get(wsBrokerage.getRouteId());
        zuulRoute = zuulRoute == null ? zuulProperties.getRoutes().get(wsBrokerage.getId()) : zuulRoute;
        return zuulRoute;
    }
 
Example 17
Source File: ZuulRouteStorage.java    From heimdall with Apache License 2.0 2 votes vote down vote up
/**
* Gets a List of {@link ZuulRoute}.
* 
* @return		The List of {@link ZuulRoute} 
*/
  List<ZuulProperties.ZuulRoute> findAll();
 
Example 18
Source File: ZuulRouteStore.java    From zuul-route-cassandra-spring-cloud-starter with Apache License 2.0 2 votes vote down vote up
/**
 * Retrieves the list of all stored Zuul routes from the persistence storage.
 *
 * @return the list of zuul routes
 */
List<ZuulProperties.ZuulRoute> findAll();
 
Example 19
Source File: RouteMatcher.java    From zuul-trie-matcher-spring-cloud-starter with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the map of routes.
 *
 * @param routes the routes map
 */
void setRoutes(Map<String, ZuulProperties.ZuulRoute> routes);
 
Example 20
Source File: TrieRouteMatcher.java    From zuul-trie-matcher-spring-cloud-starter with Apache License 2.0 2 votes vote down vote up
/**
 * Retrieves the route spec
 *
 * @return the route spec
 */
public ZuulProperties.ZuulRoute getRoute() {
    return route;
}