springcloud-gateway-nacos
介紹
springcloud gateway+nacos 實現動態路由 demo,參考了網上一些文章 博客,然後調試完成
軟件架構
軟件架構說明
首先參考nacos官方文檔,安裝nacos的server服務
nacos 配置截圖
一. gateway-server.yml配置
spring:
cloud:
nacos:
discovery:
server-addr: ${spring.cloud.nacos.config.server-addr}
locator:
enabled: true
lowerCaseServiceId: true
nacos:
dataId: gateway_config_json
group: refresh_config_json
server:
port: 5008
# 配置輸出日誌
logging:
level:
org.springframework.cloud.gateway: TRACE
org.springframework.http.server.reactive: DEBUG
org.springframework.web.reactive: DEBUG
reactor.ipc.netty: DEBUG
#開啓端點
management:
endpoints:
web:
exposure:
include: '*'
security:
enabled: false
二. gateway_config_json, 這是動態路由規則配置,注意,這裏是json格式的,詳細可看項目中的代碼
[{
"filters": [{
"args": {
"parts": "1"
},
"name": "StripPrefix"
}],
"id": "hello-service",
"order": 0,
"predicates": [{
"args": {
"pattern": "/hello-service/**"
},
"name": "Path"
}],
"uri": "lb://hello-service"
},{
"filters": [{
"args": {
"parts": "1"
},
"name": "StripPrefix"
}],
"id": "eat-service",
"order": 0,
"predicates": [{
"args": {
"pattern": "/eat-service/**"
},
"name": "Path"
}],
"uri": "lb://eat-service"
}]
三.啓動測試
這裏啓動之後,項目中會有對nacos配置的監聽,每次更新路由配置後,會動態的刷新到項目中