隨著業務和架構的發展,微服務的開發部署模式已經成為當今技術發展的趨勢,微服務流量入口的控制基本通過網關實現。網關的功能:路由轉發、融合、限流、安全認證、日志監控等。,之前也發表過一篇關于它的文章。有興趣可以了解一下網關的基本介紹。
后臺目前我們的業務流量入口已經由網關統一轉發,路由信息在配置文件中定義。但是,這種方法有一個缺點,即必須重新啟動服務才能修改路由信息。網關作為支付所有流量的入口,需要保證其高可用性,因此需要在不重啟服務的情況下修改路由信息?,F在動態路由的配置是結合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技術感興趣,可以“關注”我。讓我們一起學習,一起進步!