使用spring cloud gateway作为服务网关

Spring Cloud Gateway是Spring Cloud官方推出的第二代网关框架,取代Zuul网关。网关作为流量的,在微服务系统中有着非常作用,网关常见的功能有路由转发、权限校验、限流控制等作用。

gateway需要注册到nacos中去,需要引入以下的依赖:

java 复制代码
<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>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
        

在配置文件application.pom文件:

java 复制代码
spring:
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true
      routes:
        - id: nacos-provider
          uri: http://localhost:8762
          predicates:
            - Path=/nacos-provider/**
          filters:
            - StripPrefix=1
        - id: order-domain
          # uri: lb://order-domain
          uri: http://localhost:8763
          predicates:
            - Path=/order-domain/**
          filters:
            - StripPrefix=1

在工程的启动文件加上相关注解

java 复制代码
@SpringBootApplication
@EnableDiscoveryClient
public class gatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(gatewayApplication.class,args);
    }
}

成功

相关推荐
Pedantic4 分钟前
为什么 Swift 字符串不能用 `myString[3]` 随便取字符?
前端·后端
Apifox8 分钟前
提交代码后如何自动触发 Apifox 的自动化测试?
前端·后端·测试
程序员NEO11 分钟前
Spring AI 实现让你的 AI “三思而后行”
后端
天下一般11 分钟前
go入门 - day1 - 环境搭建
开发语言·后端·golang
程序员NEO12 分钟前
Spring AI 骚操作:让大模型乖乖听话,直接返回 Java 对象!
人工智能·后端
星辰大海的精灵13 分钟前
FastAPI开发AI应用,多厂商模型使用指南
人工智能·后端·架构
国家不保护废物14 分钟前
前端存储与后端服务的奇妙冒险:一个Node.js服务器的诞生记(cookie实现用户登入)
前端·javascript·后端
绅士玖17 分钟前
🍪 前后端相连小项目实战:Cookic的作用
前端·后端
易元25 分钟前
设计模式-模板方法模式
后端·设计模式
uhakadotcom26 分钟前
刚刚,Golang更新了, 1.24.5 与 Go 1.23.11有啥新能力?
后端·面试·架构