Java Code Examples for org.springframework.cloud.gateway.route.RouteDefinition#setId()

The following examples show how to use org.springframework.cloud.gateway.route.RouteDefinition#setId() . 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: RouteController.java    From spring-cloud-gateway-nacos with Apache License 2.0 6 votes vote down vote up
private RouteDefinition assembleRouteDefinition(GatewayRouteDefinition gwdefinition) {
    RouteDefinition definition = new RouteDefinition();
    List<PredicateDefinition> pdList=new ArrayList<>();
    definition.setId(gwdefinition.getId());
    List<GatewayPredicateDefinition> gatewayPredicateDefinitionList=gwdefinition.getPredicates();
    for (GatewayPredicateDefinition gpDefinition: gatewayPredicateDefinitionList) {
        PredicateDefinition predicate = new PredicateDefinition();
        predicate.setArgs(gpDefinition.getArgs());
        predicate.setName(gpDefinition.getName());
        pdList.add(predicate);
    }
    definition.setPredicates(pdList);
    URI uri = UriComponentsBuilder.fromHttpUrl(gwdefinition.getUri()).build().toUri();
    definition.setUri(uri);
    return definition;
}
 
Example 2
Source File: GatewayRouteService.java    From SpringCloud with Apache License 2.0 6 votes vote down vote up
/**
 * 将数据库中json对象转换为网关需要的RouteDefinition对象
 *
 * @param gatewayRoute
 * @return RouteDefinition
 */
private RouteDefinition gatewayRouteToRouteDefinition(GatewayRoute gatewayRoute) {
    RouteDefinition routeDefinition = new RouteDefinition();
    routeDefinition.setId(gatewayRoute.getRouteId());
    routeDefinition.setOrder(gatewayRoute.getOrders());
    routeDefinition.setUri(URI.create(gatewayRoute.getUri()));
    ObjectMapper objectMapper = new ObjectMapper();
    try {
        routeDefinition.setFilters(objectMapper.readValue(gatewayRoute.getFilters(), new TypeReference<List<FilterDefinition>>() {
        }));
        routeDefinition.setPredicates(objectMapper.readValue(gatewayRoute.getPredicates(), new TypeReference<List<PredicateDefinition>>() {
        }));
    } catch (IOException e) {
        log.error("网关路由对象转换失败", e);
    }
    return routeDefinition;
}
 
Example 3
Source File: GatewayUtils.java    From jeecg-cloud with Apache License 2.0 5 votes vote down vote up
/**
 *  redis中的信息需要处理下 转成RouteDefinition对象
 *         - id: login
 *           uri: lb://cloud-jeecg-system
 *           predicates:
 *             - Path=/jeecg-boot/sys/**,
 * @param array
 * @return
 */

public static List<RouteDefinition> getRoutesByJson(JSONArray array) throws URISyntaxException {
    List<RouteDefinition> ls = new ArrayList<>();
    for(int i=0;i<array.size();i++) {
        JSONObject obj = array.getJSONObject(i);
        RouteDefinition route = new RouteDefinition();
        route.setId(obj.getString("id"));
        Object uri = obj.get("uri");
        if(uri==null){
            route.setUri(new URI("lb://"+obj.getString("name")));
        }else{
            route.setUri(new URI(obj.getString("uri")));
        }
        Object predicates = obj.get("predicates");
        if (predicates != null) {
            JSONArray predicatesArray = JSONArray.parseArray(predicates.toString());
            List<PredicateDefinition> predicateDefinitionList =
                    predicatesArray.toJavaList(PredicateDefinition.class);
            route.setPredicates(predicateDefinitionList);
        }

        Object filters = obj.get("filters");
        if (filters != null) {
            JSONArray filtersArray = JSONArray.parseArray(filters.toString());
            List<FilterDefinition> filterDefinitionList
                    = filtersArray.toJavaList(FilterDefinition.class);
            route.setFilters(filterDefinitionList);
        }
        ls.add(route);
    }
    return ls;
}
 
Example 4
Source File: DynamicRouteServiceImpl.java    From spring-cloud-gateway-nacos with Apache License 2.0 5 votes vote down vote up
/**
 * spring:
 cloud:
 gateway:
 routes: #当访问http://localhost:8080/baidu,直接转发到https://www.baidu.com/
 - id: baidu_route
 uri: http://baidu.com:80/
 predicates:
 - Path=/baidu
 * @param args
 */

public static void main(String[] args) {
    GatewayRouteDefinition definition = new GatewayRouteDefinition();
    GatewayPredicateDefinition predicate = new GatewayPredicateDefinition();
    Map<String, String> predicateParams = new HashMap<>(8);
    definition.setId("jd_route");
    predicate.setName("Path");
    predicateParams.put("pattern", "/jd");
    predicate.setArgs(predicateParams);
    definition.setPredicates(Arrays.asList(predicate));
    String uri="http://www.jd.com";
    //URI uri = UriComponentsBuilder.fromHttpUrl("http://www.baidu.com").build().toUri();
    definition.setUri(uri);
    System.out.println("definition:"+JSON.toJSONString(definition));


    RouteDefinition definition1 = new RouteDefinition();
    PredicateDefinition predicate1 = new PredicateDefinition();
    Map<String, String> predicateParams1 = new HashMap<>(8);
    definition1.setId("baidu_route");
    predicate1.setName("Path");
    predicateParams1.put("pattern", "/baidu");
    predicate1.setArgs(predicateParams1);
    definition1.setPredicates(Arrays.asList(predicate1));
    URI uri1 = UriComponentsBuilder.fromHttpUrl("http://www.baidu.com").build().toUri();
    definition1.setUri(uri1);
    System.out.println("definition1:"+JSON.toJSONString(definition1));

}
 
Example 5
Source File: DiscoveryClientRouteDefinitionLocator.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
protected RouteDefinition buildRouteDefinition(Expression urlExpr,
		ServiceInstance serviceInstance) {
	String serviceId = serviceInstance.getServiceId();
	RouteDefinition routeDefinition = new RouteDefinition();
	routeDefinition.setId(this.routeIdPrefix + serviceId);
	String uri = urlExpr.getValue(this.evalCtxt, serviceInstance, String.class);
	routeDefinition.setUri(URI.create(uri));
	// add instance metadata
	routeDefinition.setMetadata(new LinkedHashMap<>(serviceInstance.getMetadata()));
	return routeDefinition;
}
 
Example 6
Source File: DynamicRouteConfigurationService.java    From wecube-platform with Apache License 2.0 4 votes vote down vote up
protected void buildRouteDefinition(String context, DynamicRouteItemInfo itemInfo) {
    RouteDefinition rd = new RouteDefinition();
    rd.setId(context + ROUTE_ID_SUFFIX);
    String urlStr = String.format("%s://%s:%s", itemInfo.getHttpScheme(), itemInfo.getHost(), itemInfo.getPort());
    URI uri = UriComponentsBuilder.fromHttpUrl(urlStr).build().toUri();
    rd.setUri(uri);

    PredicateDefinition pd = new PredicateDefinition();
    pd.setName("Path");
    Map<String, String> predicateParams = new HashMap<>(8);
    predicateParams.put("pattern", String.format("/%s/**", context));
    pd.setArgs(predicateParams);
    rd.setPredicates(Arrays.asList(pd));

    List<FilterDefinition> filters = new ArrayList<>();

    FilterDefinition fdDynamicRoute = new FilterDefinition();
    fdDynamicRoute.setName("DynamicRoute");
    fdDynamicRoute.addArg("enabled", "true");

    filters.add(fdDynamicRoute);

    if (dynamicRouteProperties.isEnableRetry()) {

        FilterDefinition fdRetry = new FilterDefinition();
        fdRetry.setName("ExRetry");
        fdRetry.addArg("retries", "10");
        fdRetry.addArg("series", "SERVER_ERROR");
        // fdRetry.addArg("statuses", "NOT_FOUND");
        fdRetry.addArg("methods", "GET,POST,PUT,DELETE");
        fdRetry.addArg("exceptions", "java.io.IOException,java.net.ConnectException");

        filters.add(fdRetry);
    }

    rd.setFilters(filters);

    add(rd);

    log.debug("### route added:{} {} {}", itemInfo.getContext(), itemInfo.getHost(), itemInfo.getPort());
}