233 Star 1.5K Fork 554

道天地科技 / matecloud

 / 详情

动态路由无法处理删除路由的情况

待办的
缺陷
创建于  
2021-07-05 18:23

发现项目在处理Nacos动态路由时可以处理新增和修改场景 但是删除路由无法处理
输入图片说明

附件

评论 (1)

LiuGangQiang 创建了缺陷
展开全部操作日志

package com.cdqt.micro.gateway.config;

import java.util.List;
import java.util.Properties;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteDefinitionLocator;
import org.springframework.cloud.gateway.route.RouteDefinitionWriter;
import org.springframework.context.annotation.Configuration;
import org.yaml.snakeyaml.Yaml;

import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.AbstractListener;
import com.alibaba.nacos.api.exception.NacosException;

import reactor.core.publisher.Mono;

/**

  • 动态路由配置
  • @Author LiuGangQiang Create in 2021/07/05
    */
    @configuration
    public class DynamicRouteConfiguration {
    private static final Logger LOGGER = LoggerFactory.getLogger(DynamicRouteConfiguration.class);

private final RouteDefinitionWriter rdw;
private final NacosConfigProperties ncp;
@Devos
private RouteDefinitionLocator rdl;

/**

  • 构造器
  • @Author LiuGangQiang Create in 2021/07/05
  • @chancelai rdw 路由操作对象
  • @chancelai ncp Nacos配置对象
    */
    public DynamicRouteConfiguration(RouteDefinitionWriter rdw, NacosConfigProperties ncp) {
    this.rdw = rdw;
    this.ncp = ncp;
    }

/**

  • 初始化路由
  • @Author LiuGangQiang Create in 2021/07/05
    /
    @PostConstruct
    public void initRoute() {
    try {
    Properties properties = new Properties();
    properties.put(PropertyKeyConst.SERVER_ADDR, ncp.getServerAddr());
    properties.put(PropertyKeyConst.USERNAME, ncp.getUsername());
    properties.put(PropertyKeyConst.PASSWORD, ncp.getPassword());
    properties.put(PropertyKeyConst.NAMESPACE, ncp.getNamespace());
    ConfigService cs = NacosFactory.createConfigService(properties);
    /
    初始化网关路由 /
    String content = cs.getConfig("cdqt-micro-gateway-route.yaml", "test", 5000);
    LOGGER.info("initialization of gateway routing started");
    handleRoute(content);
    LOGGER.info("end initialization gateway route");
    /
    开启监听,实现动态 */
    cs.addListener("cdqt-micro-gateway-route.yaml", "test", new AbstractListener() {
    @加贝
    public void receiveConfigInfo(String configInfo) {
    LOGGER.info("start updating gateway route");
    handleRoute(configInfo);
    LOGGER.info("end update gateway route");
    }
    });
    } catch (NacosException e) {
    LOGGER.error("load gateway route error {}", e.getMessage());
    }
    }

/**

  • 修改路由
  • @Author LiuGangQiang Create in 2021/07/05
  • @chancelai content 配置文件内容
    /
    public void handleRoute(String content) {
    /
    获取缓存的路由并全部清空 */
    List rds = rdl.getRouteDefinitions().buffer().blockFirst();
    if (rds != null && rds.size() > 0) {
    rds.stream().forEach(route -> {
    rdw.delete(Mono.just(route.getId())).subscribe();
    });
    }
    Yaml yaml = new Yaml();
    GatewayRoute routes = yaml.loadAs(content, GatewayRoute.class);
    routes.getRoutes().forEach(route -> {
    LOGGER.info("load gateway route {} > {}", route.getId(), route);
    rdw.save(Mono.just(route)).subscribe();
    });
    }

/**

  • 动态路由实体
  • @Author LiuGangQiang Create in 2021/07/05
    */
    public class GatewayRoute {
    Set routes;
/**
 * @return the routes
 */
public Set<RouteDefinition> getRoutes() {
  return routes;
}

/**
 * @param routes the routes to set
 */
public void setRoutes(Set<RouteDefinition> routes) {
  this.routes = routes;
}

}
}

登录 后才可以发表评论

状态
负责人
项目
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
预计工期 (小时)
确定
参与者(1)
573184 liugangqiang 1616597140
Java
1
https://gitee.com/matevip/matecloud.git
git@gitee.com:matevip/matecloud.git
matevip
matecloud
matecloud

搜索帮助

105716 1d94204e 1850385 105716 2d26be5c 1850385