天天看点

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: product
          uri: http://localhost:9000
          predicates:
            - Host=product.gblfy.com**
        - id: auth
          uri: http://localhost:5000
          predicates:
            - Path=/oauth/token
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/auth-serv?characterEncoding=UTF-8&serverTimezone=GMT%2B8
    username: root
    password: 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-serv
      server-addr: localhost:8848
      config:
        server-addr: localhost:8848
        file-extension: yaml
        shared-configs[0]:
          dataId: gateway.yaml
          # 动态刷新
          refresh: true      

将以前application.yml文件的内容,添加到nacos控制台的​

​gateway.yaml​

spring:
  cloud:
    gateway:
      routes:
        - id: product
          uri: http://localhost:9000
          predicates:
            - Host=product.gblfy.com**
        - id: auth
          uri: http://localhost:5000
          predicates:
            - Path=/oauth/token
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/auth-serv?characterEncoding=UTF-8&serverTimezone=GMT%2B8
    username: root
    password: 123456      

2.3. 在nacos配置中心添加配置

新建配置

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

2.4. 启动服务

启动Gateway-Serv模块服务

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

启动auth-serv认证授权服务

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

启动product-serv服务

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

2.5. 访问产品模块

不请求auth-serv模块获取otken,直接通过网关访问产品模块

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

从上图可以看出访问需要认证授权

2.6. 获取toeken

​​http://localhost:8081/oauth/token​​ 通过认证授权中心获取toekn

grant_type:password
client_id:app
client_secret:app
username:ziya
password:111111      
SpringCloudGateway 集成 nacos 整合实现动态路由_04

2.7. 携带toekn访问产品模块

携带toekn通过网关服务访问产品模块

​​http://product.gblfy.com:8081/product/1​​

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

从图中可以看出,获取token后,通过网关服务可以正常请求产品模块,并有响应报文。

2.8. 怎样证明配置动态刷新呢

修改配置

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

再次通过网关请求产品服务模块服务

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

从图中可以看出访问请求拒接了,因为没有9200端口的服务应用。我们现在基于nacos-config配置动态将我们的路由规则管理起来了。

三、利用注册中心动态路由

3.1. 查看服务列表

有3台应用

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

3.2. 应用名称替换ip和端口

原配置

spring:
  cloud:
    gateway:
      routes:
        - id: product
          uri: http://localhost:9200
          predicates:
            - Host=product.gblfy.com**
        - id: auth
          uri: http://localhost:5000
          predicates:
            - Path=/oauth/token
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/auth-serv?characterEncoding=UTF-8&serverTimezone=GMT%2B8
    username: root
    password: 123456      

改造后配置

spring:
  cloud:
    gateway:
      routes:
        - id: product
          uri: lb://product-serv
          predicates:
            - Host=product.gblfy.com**
        - id: auth
          uri: lb://auth-serv
          predicates:
            - Path=/oauth/token
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/auth-serv?characterEncoding=UTF-8&serverTimezone=GMT%2B8
    username: root
    password: 123456      

3.3. 重新请求

​​http://product.gblfy.com:8081/product/1​​

Authorization: Bearer d364c6cc-3c60-402f-b3d0-af69f6d6b73e      
SpringCloudGateway 集成 nacos 整合实现动态路由_04

继续阅读