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: 123456
3. 前言简述
首先咱们这篇基于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: 123456
2.3. 在nacos配置中心添加配置
新建配置
2.4. 启动服务
启动Gateway-Serv模块服务
启动auth-serv认证授权服务
启动product-serv服务
2.5. 访问产品模块
不请求auth-serv模块获取otken,直接通过网关访问产品模块
从上图可以看出访问需要认证授权
2.6. 获取toeken
http://localhost:8081/oauth/token
通过认证授权中心获取toekn
grant_type:password
client_id:app
client_secret:app
username:ziya
password:111111
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: 123456
3.3. 重新请求
http://product.gblfy.com:8081/product/1
Authorization: Bearer d364c6cc-3c60-402f-b3d0-af69f6d6b73e
从上图可以看出可以通过网关服务正常请求产品模块!
SpringCloudGateway 集成 nacos 整合实现动态路由_04相关推荐
- nacos windows部署_Sentinel-Go 集成 Nacos 实现外部动态数据源
导读:2020年,Sentinel 推出 Go 原生版本Sentinel-Golang,在云原生领域继续突破.本文将从实际出发 结合案例说明 在Sentinel-Golang中如何集成Nacos,使其 ...
- Sentinel-Go 集成 Nacos 实现外部动态数据源
**导读:**2020年,Sentinel 推出 Go 原生版本Sentinel-Golang,在云原生领域继续突破.本文将从实际出发 结合案例说明 在Sentinel-Golang中如何集成Naco ...
- 基于Nacos配置中心实现Spring Cloud Gateway的动态路由管理
前面我们了解过了Sentinel 网关流量控制之Spring Cloud Gateway实战,今天带给大家是基于Nacos配置中心实现Spring Cloud Gateway的动态路由管理. 1.为什 ...
- spring cloud gateway nacos搭建动态路由
spring cloud gateway nacos搭建动态路由 一.环境 开发工具:IntelliJ Idea JDK 1.8 Spring boot 2.3.12.RELEASE spring c ...
- Nacos + Spring Cloud Gateway动态路由配置
前言 Nacos最近项目一直在使用,其简单灵活,支持更细粒度的命令空间,分组等为麻烦复杂的环境切换提供了方便:同时也很好支持动态路由的配置,只需要简单的几步即可.在国产的注册中心.配置中心中比较突出, ...
- nacos动态路由配置(二)-微服务启动自动配置网关路由
经过上一篇我们发现nacos通过配置动态路由routes-api-gateway.yaml配置json,监听动态路由变化即可实现动态路由,非常的银杏化. 那么有的小伙伴发现配置json也比较麻烦,有没 ...
- kong 网关 结合 nacos 动态路由,服务上下线,加载插件
kong 怎么根据nacos的服务自动路由,加载服务呢 先梳理逻辑 kong路由动态控制服务的说明 逻辑说明 核心流程说明: 此服务中存在的冲突及问题 插件的加载 附上部分代码 先梳理逻辑 本文参考: ...
- Nacos整合Gateway实现动态路由
往期回顾 Nacos的安装与配置 Spring Cloud集成Nacos作为注册中心 LoadBalacer集成Nacos实现负载均衡 常见的负载均衡策略分析 Spring Cloud集成Dubbo实 ...
- angularjs 让当前路由重新加载_Spring Cloud Gateway的动态路由怎样做?集成Nacos实现很简单...
一.说明 网关的核心概念就是路由配置和路由规则,而作为所有请求流量的入口,在实际生产环境中为了保证高可靠和高可用,是尽量要避免重启的,所以实现动态路由是非常有必要的:本文主要介绍 Spring Clo ...
最新文章
- 接近开关的初步测试 : DF-11N
- NopCommerce架构分析之八------多语言
- AngularJS table 按照表头字段排序功能(升序和降序)
- 【Redis】详细基础命令 - 学习笔记
- 判断子序列不同的子序列两个字符串的删除操作编辑距离
- sink xxx does not exist
- 第四章 生命周期函数--35 vue-resource发起get、post、jsonp请求
- android与js交互
- 计算机二级excel数据有效性,原来Excel数据有效性还可以这样做——制作二级下拉菜单...
- android 自定义属性 双向绑定,如何解决:“在使用自定义视图实现双向数据绑定时,找不到属性’android:text’”的getter?...
- Spring事务管理全面分析
- 一个获取随机字符串的函数
- python在线题库推荐_Python题库.docx
- springboot集成knife4j2.0.8实现自定义md文档及权限控制
- 毕业设计 基于51单片机老人防跌倒GSM短信报警系统
- 阿里云Ubuntu 18.04安装图形界面
- CentOS7搭建私有化Docker仓库Harbor
- 【RDMA】RDMA编程 和相关资料
- 前端利器——炫酷的CodePen
- Windows服务器——网络负载平衡
热门文章
- 复数可以阐释的如此优雅
- 博士当中学老师是“人才浪费”?
- 数学的威力有多大?足以震慑全球......
- 文案月薪3w?月薪10w的设计大神笑而不语
- java 正则 最后一个字符_正则表达式怎么匹配字符串中最后一串数字?
- ubuntu给pip换源,给conda换源
- IndexError: invalid index of a 0-dim tensor. Use `tensor.item()` in Python or `tensor.item<T>()` in
- 【链接保存】十分钟上手sklearn:特征提取,常用模型,交叉验证
- 【kaggle入门题一】Titanic: Machine Learning from Disaster
- opencv 常用操作 c++