javascript
SpringCloudGateway 集成 nacos 整合实现动态路由_04
接上一篇:SpringCloud Gateway 集成 oauth2 實現統一認證授權
文章目錄
- 一、目前存在的問題
- 1. 問題簡述
- 2. 集成nacos前配置
- 3. 前言簡述
 
- 二、網關模塊改造集成nacos
- 2.1. 引入依賴
- 2.2. 創建bootstrap.yaml
- 2.3. 在nacos配置中心添加配置
- 2.4. 啟動服務
- 2.5. 訪問產品模塊
- 2.6. 獲取toeken
- 2.7. 攜帶toekn訪問產品模塊
- 2.8. 怎樣證明配置動態刷新呢
 
- 三、利用注冊中心動態路由
- 3.1. 查看服務列表
- 3.2. 應用名稱替換ip和端口
- 3.3. 重新請求
 
 
 
 
 
一、目前存在的問題
1. 問題簡述
- SpringCloudGateway的路由規則寫死在配置文件中,無法支持動態更新
- 路由規則如何與服務注冊中心聯動
2. 集成nacos前配置
spring:cloud:gateway:routes:- id: producturi: http://localhost:9000predicates:- Host=product.gblfy.com**- id: authuri: http://localhost:5000predicates:- Path=/oauth/tokendatasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/auth-serv?characterEncoding=UTF-8&serverTimezone=GMT%2B8username: rootpassword: 1234563. 前言簡述
首先咱們這篇基于SpringCloudGateway集成授權認證中心的oauth2的因此,發起請求之前需要先通過網關訪問認證授權中心auth-serv獲取token才可以訪問后面的模塊。
二、網關模塊改造集成nacos
2.1. 引入依賴
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><spring.cloud-version>Hoxton.SR9</spring.cloud-version></properties><dependencies><!--配置中心--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency><!--服務注冊發現--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!--安全認證框架--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><!--security-oauth2整合--><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-oauth2-resource-server</artifactId></dependency><!--oauth2--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-oauth2</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!--網關--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency></dependencies><dependencyManagement><!--https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E--><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring.cloud-version}</version><type>pom</type><scope>import</scope></dependency><!--spring-cloud-alibaba 版本控制--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2.2.6.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>2.2. 創建bootstrap.yaml
server:port: 8081 spring:cloud:nacos:discovery:service: gateway-servserver-addr: localhost:8848config:server-addr: localhost:8848file-extension: yamlshared-configs[0]:dataId: gateway.yaml# 動態刷新refresh: true將以前application.yml文件的內容,添加到nacos控制臺的gateway.yaml
spring:cloud:gateway:routes:- id: producturi: http://localhost:9000predicates:- Host=product.gblfy.com**- id: authuri: http://localhost:5000predicates:- Path=/oauth/tokendatasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/auth-serv?characterEncoding=UTF-8&serverTimezone=GMT%2B8username: rootpassword: 1234562.3. 在nacos配置中心添加配置
新建配置
 
 
2.4. 啟動服務
啟動Gateway-Serv模塊服務
 
 啟動auth-serv認證授權服務
 
啟動product-serv服務
 
2.5. 訪問產品模塊
不請求auth-serv模塊獲取otken,直接通過網關訪問產品模塊
 從上圖可以看出訪問需要認證授權
2.6. 獲取toeken
http://localhost:8081/oauth/token
 通過認證授權中心獲取toekn
2.7. 攜帶toekn訪問產品模塊
攜帶toekn通過網關服務訪問產品模塊
 http://product.gblfy.com:8081/product/1
 
 從圖中可以看出,獲取token后,通過網關服務可以正常請求產品模塊,并有響應報文。
2.8. 怎樣證明配置動態刷新呢
修改配置
 
再次通過網關請求產品服務模塊服務
 
 
從圖中可以看出訪問請求拒接了,因為沒有9200端口的服務應用。我們現在基于nacos-config配置動態將我們的路由規則管理起來了。
三、利用注冊中心動態路由
3.1. 查看服務列表
有3臺應用
 
3.2. 應用名稱替換ip和端口
原配置
spring:cloud:gateway:routes:- id: producturi: http://localhost:9200predicates:- Host=product.gblfy.com**- id: authuri: http://localhost:5000predicates:- Path=/oauth/tokendatasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/auth-serv?characterEncoding=UTF-8&serverTimezone=GMT%2B8username: rootpassword: 123456改造后配置
spring:cloud:gateway:routes:- id: producturi: lb://product-servpredicates:- Host=product.gblfy.com**- id: authuri: lb://auth-servpredicates:- Path=/oauth/tokendatasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/auth-serv?characterEncoding=UTF-8&serverTimezone=GMT%2B8username: rootpassword: 1234563.3. 重新請求
http://product.gblfy.com:8081/product/1
Authorization: Bearer d364c6cc-3c60-402f-b3d0-af69f6d6b73e
 從上圖可以看出可以通過網關服務正常請求產品模塊!
總結
以上是生活随笔為你收集整理的SpringCloudGateway 集成 nacos 整合实现动态路由_04的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 企业实战_15_MySql主从复制到My
- 下一篇: uniapp 用户登录
