SpringCloud项目中搭建网关

网关概述
当我们请求进入某个服务中之前,会经过网关的处理。网关会进行授权,限流,登录,日志等一系列的操作。最终会让我们的请求进入服务。

引入依赖

点击查看代码
<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
     <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt</artifactId>
    </dependency>

编写启动类

编写bootstrap.yml配置

点击查看代码
server:
  port: 51601
spring:
  application:
    name: leadnews-app-gateway #网关微服务名称,nacos中的配置DataId要与该名称一致
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.200.130:8848  #nacos地址
      config:
        server-addr: 192.168.200.130:8848
        file-extension: yml

在nacos的配置中心创建DataId为leadnews-app-gateway的yml配置

点击查看代码
spring:
  cloud:
    gateway:
      globalcors:
        add-to-simple-url-handler-mapping: true
        corsConfigurations:
          '[/**]':
            allowedHeaders: "*"
            allowedOrigins: "*"
            allowedMethods:
              - GET
              - POST
              - DELETE
              - PUT
              - OPTION
      routes:
        # 平台管理
        - id: user
          uri: lb://leadnews-user  #这个是要路由到具体某个微服务的名称。
          predicates:
            - Path=/user/**
          filters:
            - StripPrefix= 1
posted @   wzh_Official  阅读(29)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示