关于SpringBoot中Nacos动态路由的配置

首先。。。
我自己有个服务器。在无聊之时写的代码,主要是通过网关来调用接口所以有了下面的代码。
1.作为一个动态路由维护管理的类
@Service
public class DynamicRouteServiceImpl  implements ApplicationEventPublisherAware {
    /**
     *
     */
    @Autowired
    private RouteDefinitionWriter routeDefinitionWriter;

    private ApplicationEventPublisher publisher;


    /**
     * 增加路由
     * @param definition
     * @return
     */
    public String add(RouteDefinition definition) {
        routeDefinitionWriter.save(Mono.just(definition)).subscribe();
        this.publisher.publishEvent(new RefreshRoutesEvent(this));
        return "success";
    }


    /**
     * 更新路由
     * @param definition
     * @return
     */
    public String update(RouteDefinition definition) {
        try {
            this.routeDefinitionWriter.delete(Mono.just(definition.getId()));
        } catch (Exception e) {
            return "update fail,not find route  routeId: "+definition.getId();
        }
        try {
            routeDefinitionWriter.save(Mono.just(definition)).subscribe();
            this.publisher.publishEvent(new RefreshRoutesEvent(this));
            return "success";
        } catch (Exception e) {
            return "update route  fail";
        }


    }
    /**
     * 删除路由
     * @param id
     * @return
     */
    public String delete(String id) {
        try {
            this.routeDefinitionWriter.delete(Mono.just(id));
            return "delete success";
        } catch (Exception e) {
            e.printStackTrace();
            return "delete fail";
        }

    }

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.publisher = applicationEventPublisher;
    }
2.基于Nacos动态配置路由服务
@Component
public class DynamicRouteServiceImplByNacos implements CommandLineRunner {
    private static final Logger logger = LoggerFactory.getLogger(DynamicRouteServiceImplByNacos.class);
    @Autowired
    private DynamicRouteServiceImpl dynamicRouteService;

    @Value("${spring.cloud.nacos.discovery.server-addr}")
    private String address;
    @Value("${config.dataId}")
    private String dataId;
    @Value("${config.groupId}")
    private String groupId;
    @Value("${config.timeout}")
    private Long timeout;
   /* @Value("${config.ignore}")
    private String ignore;*/

    /**
     * 监听Nacos Server下发的动态路由配置
     */
    public void dynamicRouteByNacosListener() {
        try {
            ConfigService configService = NacosFactory.createConfigService(address);


            configService.addListener(dataId, groupId, new Listener() {
                @Override
                public void receiveConfigInfo(String configInfo) {
                    try {
                        logger.info("================Nacos 配置中心路由配置信息已修改================\n" + configInfo + "\n\n");
                        List<RouteDefinition> list = JSON.parseArray(configInfo, RouteDefinition.class);
                        list.forEach(definition -> {
                            dynamicRouteService.update(definition);
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public Executor getExecutor() {
                    return null;
                }
            });


            /*configService.addListener(ignore, groupId, new Listener() {
                @Override
                public void receiveConfigInfo(String configInfo) {
                    try {
                        logger.info("================Nacos 配置中心忽略URL配置已修改================\n" + configInfo + "\n\n");
                        logger.info("\n" + configInfo + "\n\n");
                        List<String> ignoreList = JSON.parseArray(configInfo, String.class);
                        IgnoreRouteConfig.setIgnoreRouteArr(ignoreList);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public Executor getExecutor() {
                    return null;
                }
            });*/

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run(String... args) throws Exception {
        dynamicRouteByNacosListener();

    }

}
3.yml配置

4B2F6EFF-8D45-BC96-E951-FF2FA52F1E46.png

4. nacos网关配置

1D726772-35D5-4ECD-1269-A870F7268242.png

5.最后:我建的是父子工程两个服务发到服务器后
注意注意注意!!!:一定要开启防火墙,登上阿里云自己的服务增加端口,然后再linux中也要增加端口
linux防火墙相关命令

删除 firewall-cmd --zone= public --remove-port=80/tcp --permanent

开放 firewall-cmd --zone=public --add-port=1935/tcp --permanent

查看状态 systemctl status firewalld

启动 systemctl start firewalld

查看已开启端口 firewall-cmd --list-ports

重启 firewall-cmd --reload

28DDB584-2E34-3E17-7E52-5260B1161E0F.png

yml配置文件中 有一个这个端口 这个也要在阿里云上面还有服务器开启

这个就是接口访问时的网关端口号

ABA63C8F-A113-BEE4-F4D8-B7B06B315DDA.png

收藏 (0)
评论列表
正在载入评论列表...
我是有底线的
为您推荐
    暂时没有数据