(SSM+SE)SSO(4.1 gateway网关)

本文介绍了如何使用Spring Cloud Gateway创建一个模块项目,配置Nacos服务发现与负载均衡,实现路由、权限认证与限流。通过实例演示了如何设置路由规则和网关过滤器,以及如何利用注册中心调用服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

网关本质上要提供一个各种服务访问的入口,并提供服务接收并转发所有内外部的客户端调用,还有就是权限认证,限流控制等等。Spring Cloud Gateway是Spring公司基于Spring 5.0,Spring Boot 2.0 和 等技术开发的一个网关组件,它旨在为微服务架构提供一种简单有效的统一的 API入口,负责服务请求路由、组合及协议转换,并且基于 Filter 链的方式提供了权限认证,监控、限流等功能。

新建一个module项目,叫gateway

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>HandSomeAq_SSO</artifactId>
        <groupId>com.hd</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>gateway</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </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.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

    </dependencies>
</project>

yml文件:

server:
  port: 9000

spring:
  application:
    name: sso-gateway
  cloud:
    nacos:
      config:
        server-addr: localhost:8848
        file-extension: yml
      discovery:
        server-addr: localhost:8848
    gateway:
      discovery:
        locator:
          enabled: true #开启通过服务中心的serviceId 创建路由的功能
      routes:
        - id: route01  #路由的id 唯一即可
          uri: http://localhost:8081/ #路由帮我们转发的url
          predicates:
            - Path=/auth/**  #断言,匹配请求规则
          filters: #网关过滤器 
            - StripPrefix=1 # 1代表过滤一层url 即auth
        - id: route02
          uri: http://localhost:8082/
          predicates:
            - Path=/resource/**
          filters:
            - StripPrefix=1

路由(Route) 是 gateway 中最基本的组件之一,表示一个具体的路由信息载体。主要定义了下面的几个信息:

id,路由标识符,区别于其他 Route。
uri,路由指向的目的地 uri,即客户端请求最终被转发到的微服务。
predicate,断言(谓词)的作用是进行条件判断,只有断言都返回真,才会执行路由。
filter,过滤器用于修改请求和响应信息。

用postman访问 http://localhost:9000/auth/login?username=admin&password=123 测试:
在这里插入图片描述
携带token访问 http://localhost:9000/resource/logs/getLogs/admin

在这里插入图片描述
负载均衡,应该有多个auth和resource服务器提供服务来构建负载均衡。

1、网关在注册中心注册。
2、auth和resource在注册中心注册。
3、网关通过DataId来进行服务的调用。

修改gateway的yml文件,将

 #uri: http://localhost:8081/
         uri: lb://sso-auth  #从注册中心调用服务
 #uri: http://localhost:8082/
  uri: lb://sso-resource
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值