javascript
SpringCloudGateway 集成 nacos 整合实现动态路由_04
接上一篇:SpringCloud Gateway 集成 oauth2 實現(xiàn)統(tǒng)一認(rèn)證授權(quán)
文章目錄
- 一、目前存在的問題
- 1. 問題簡述
- 2. 集成nacos前配置
- 3. 前言簡述
- 二、網(wǎng)關(guān)模塊改造集成nacos
- 2.1. 引入依賴
- 2.2. 創(chuàng)建bootstrap.yaml
- 2.3. 在nacos配置中心添加配置
- 2.4. 啟動服務(wù)
- 2.5. 訪問產(chǎn)品模塊
- 2.6. 獲取toeken
- 2.7. 攜帶toekn訪問產(chǎn)品模塊
- 2.8. 怎樣證明配置動態(tài)刷新呢
- 三、利用注冊中心動態(tài)路由
- 3.1. 查看服務(wù)列表
- 3.2. 應(yīng)用名稱替換ip和端口
- 3.3. 重新請求
一、目前存在的問題
1. 問題簡述
- SpringCloudGateway的路由規(guī)則寫死在配置文件中,無法支持動態(tài)更新
- 路由規(guī)則如何與服務(wù)注冊中心聯(lián)動
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集成授權(quán)認(rèn)證中心的oauth2的因此,發(fā)起請求之前需要先通過網(wǎng)關(guān)訪問認(rèn)證授權(quán)中心auth-serv獲取token才可以訪問后面的模塊。
二、網(wǎng)關(guān)模塊改造集成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><!--服務(wù)注冊發(fā)現(xiàn)--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!--安全認(rèn)證框架--><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><!--網(wǎng)關(guān)--><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. 創(chuàng)建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# 動態(tài)刷新refresh: true將以前application.yml文件的內(nèi)容,添加到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. 啟動服務(wù)
啟動Gateway-Serv模塊服務(wù)
啟動auth-serv認(rèn)證授權(quán)服務(wù)
啟動product-serv服務(wù)
2.5. 訪問產(chǎn)品模塊
不請求auth-serv模塊獲取otken,直接通過網(wǎng)關(guān)訪問產(chǎn)品模塊
從上圖可以看出訪問需要認(rèn)證授權(quán)
2.6. 獲取toeken
http://localhost:8081/oauth/token
通過認(rèn)證授權(quán)中心獲取toekn
2.7. 攜帶toekn訪問產(chǎn)品模塊
攜帶toekn通過網(wǎng)關(guān)服務(wù)訪問產(chǎn)品模塊
http://product.gblfy.com:8081/product/1
從圖中可以看出,獲取token后,通過網(wǎng)關(guān)服務(wù)可以正常請求產(chǎn)品模塊,并有響應(yīng)報文。
2.8. 怎樣證明配置動態(tài)刷新呢
修改配置
再次通過網(wǎng)關(guān)請求產(chǎn)品服務(wù)模塊服務(wù)
從圖中可以看出訪問請求拒接了,因為沒有9200端口的服務(wù)應(yīng)用。我們現(xiàn)在基于nacos-config配置動態(tài)將我們的路由規(guī)則管理起來了。
三、利用注冊中心動態(tài)路由
3.1. 查看服務(wù)列表
有3臺應(yīng)用
3.2. 應(yīng)用名稱替換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
從上圖可以看出可以通過網(wǎng)關(guān)服務(wù)正常請求產(chǎn)品模塊!
總結(jié)
以上是生活随笔為你收集整理的SpringCloudGateway 集成 nacos 整合实现动态路由_04的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 企业实战_15_MySql主从复制到My
- 下一篇: SpringBoot2 整合 AXIS2