日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringCloudGateway 集成 nacos 整合实现动态路由_04

發(fā)布時間:2024/9/27 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringCloudGateway 集成 nacos 整合实现动态路由_04 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

接上一篇: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: 123456
3. 前言簡述

首先咱們這篇基于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: 123456
2.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

grant_type:password client_id:app client_secret:app username:ziya password:111111

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: 123456
3.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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。