小白网-奉贤部落-奉贤免费信息发布平台

查看: 24|回复: 0
打印 上一主题 下一主题

nacos网关(nacos动态路由)

[复制链接]

2万

主题

2万

帖子

7万

积分

论坛元老

Rank: 8Rank: 8

积分
78182
跳转到指定楼层
楼主
发表于 2025-7-26 12:32 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
随着业务和架构的发展,微服务的开发部署模式已经成为当今技术发展的趋势,微服务流量入口的控制基本通过网关实现。网关的功能:路由转发、融合、限流、安全认证、日志监控等。,之前也发表过一篇关于它的文章。有兴趣可以了解一下网关的基本介绍。
后台目前我们的业务流量入口已经由网关统一转发,路由信息在配置文件中定义。但是,这种方法有一个缺点,即必须重新启动服务才能修改路由信息。网关作为支付所有流量的入口,需要保证其高可用性,因此需要在不重启服务的情况下修改路由信息。现在动态路由的配置是结合Nacos实现的,转发服务需要在Nacos注册表中注册,网关直接在注册表中选择机器进行转发。下面是具体转换的代码。
访问网关Nacos以动态刷新网关路由配置表。如果要实现动态路由配置,需要做两件事。
  • Nacos可以监控客户端前台配置文件的修改;
  • 将修改后的配置文件更改为网关路由;针对以上两点,我们需要通过代码监控Nacos配置文件的变化,同时需要利用Spring事件发布将配置文件变化信息动态刷新到路由配置表中。核心代码如下:
    @ Component@ Slf4jpublic class NacosDynamicRouteService实现applicationeventpublisherware {@ Value(" $ { spring . cloud . nacos . config . server-addr } "private String server addr;@ Value(" $ { nacos . group } "私有字符串组;@ Value(" $ { nacos . dataid } "私有字符串routeConfigDataId@ Value(" $ { nacos . namespace } "私有字符串命名空间;@ Resourceprivate RouteDefinitionWriter RouteDefinitionWriter;private ApplicationEventPublisher ApplicationEventPublisher;私有静态最终列表ROUTE _ LIST = new ArrayList  lt gt();@ Overridepublic void setApplicationEventPublisher(@ not null ApplicationEventPublisher ApplicationEventPublisher){this . ApplicationEventPublisher = ApplicationEventPublisher;}@ post constructpublic void dynamicRouteListener(){try {Properties Properties = new Properties();properties . put(" server addr ",server addr);properties.put("namespace ",命名空间);config service config service = nacosfactory . createconfigservice(properties);//程序首次启动加载初始路由配置string initconfiginfo = config service . getconfig(RouteConfigDataID,Group,5000);addandpublishbatchrroute(initConfigInfo);config service . add Listener(routeConfigDataId,group,new Listener(){@ Overridepublic void receive config info(String config info){addAndPublishBatchRoute(config info);}@ Overridepublic Executor get Executor(){返回null}});} catch(Exception e){e . printstacktrace();}} Private void添加和发布Batch route(string config)List gatewaytroutedefinitions = JSON object . parse array(configInfo,route definition . class);for(route definition route definition:gatewayroutededefinitions){add route(route definition);}publish();log . info("  gt; gt gt gt gt gt gt gt gt gt gt gt gt gt gt gt gtNacos动态配置网关路由完成| {} ",JSON . tojsonstring(gatewayRouteDefinitions));} catch(Exception e){e . printstacktrace();}}Private Void Publish(){This。ApplicationEventPublishers。publish event(New RefreshRouteEvent(This。RoutedE }}
    配置文件yml
    nacos:GROUP:DEFAULT _ GROUPdataId:gateway-outer-nacos-betanamespace:beta
    Nacos控制台配置路由信息路由转发JSON
    具体JSON配置如下:注意my-service注册了Nacos的服务名,用于转发服务。
    [{"id": "service ","order": 0," args ":[{" pattern ":"/service/* * "/h/]},"name": "Path"}]," filters ":[{" args ":{" _ genkey _ 0 ":" 1 "[/h
    将服务转发到Nacos registry的第一步是介绍Nacos registry和spring-cloud依赖。
    2 . 1 . 4 . releasecom . Alibaba . cloudspring-cloud-starter-Alibaba-nacos-config$ { Alibaba . nacos . version }com . Alibaba . cloudspring-cloud-starter-Alibaba-nacos-discovery$ { Alibaba . nacos . version }SR6POMimport
    步骤2: SpringBootApplication添加服务发现注释@ enabledDiscovery Client
    @ spring boot Application@ EnableDiscoveryClientpublic class Application {public static void main(String[]args){spring Application . run(Application . class,args);}}
    第三步:添加bootstrap.yml注册服务中心名称,转发网关负载。
    spring:应用:名称:我的服务
    步骤4:将Nacos注册表配置地址添加到配置文件中。
    http://192.168 . x . 1:8848/nacos/index . html是后台登录管理界面。
    spring:cloud:nacos:config:server-addr:192.168 . x . 1:8848,192.168.x.2:8848,192.168.x.3: 8848 命名空间:beta # naming 空用于环境隔离发现:server-addr:192.192
    Gray scale,Nacos权重设置首先,您需要在权重设置生效之前向网关项目添加一个负载平衡策略。代码如下:
    @ Configurationpublic class Configuration {@ Bean@ Scope(value = " prototype ")public I rule loadBalanceRule(){return new NacosRule();}}
    接下来可以在Nacos控制台界面设置权重,完成流量倾斜转发。
    注册服务列表
    服务详细信息操作页面
    服务流量转发权重设置
    在滚动服务发布的过程中,可以通过点击服务离线按钮停止在线服务的转发,避免服务重启导致的数据问题。即网关(SpringCloud Gateway)结合Nacos完成网关路由动态配置、灰度发布、线上线下优雅服务等功能。
    不断分享开发过程中使用的技术和访谈中的常见问题。如果你也对IT技术感兴趣,可以“关注”我。让我们一起学习,一起进步!
  • 回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    Archiver|手机版|小黑屋|小白网-奉贤部落-奉贤免费信息发布平台  

    GMT+8, 2025-8-22 09:26 , Processed in 0.063448 second(s), 21 queries .

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

    快速回复 返回顶部 返回列表