使用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);
    }
}

成功

相关推荐
Codelinghu1 小时前
LangSmith Evaluate实战评估Agent,不要在玩Demo了
后端
xingren1 小时前
人体模型部位分割:从 SCHP平面解析 映射到 3D 模型
后端
董员外1 小时前
RAG 系统进化论(五):Corrective RAG 与 Self-RAG,让系统发现并纠正错误
人工智能·后端·设计模式
暗黑小白1 小时前
路由策略与引擎可替换性
后端·python·ai agent
zzzll11113 小时前
SpringBoot 入门与实践指南
java·spring boot·后端
newerp3 小时前
Go :结构体嵌入、sort.Interface 与 io.Reader/Writer
后端
SamDeepThinking3 小时前
如何维持缓存的一致性?
后端·面试·程序员
泡海椒3 小时前
还在手动把 Postman 和浏览器的 curl 转成 Java 代码?这个框架让你一键粘贴直接用
后端
用户8181870627463 小时前
第13章 死锁诊断实战
后端
晨曦中的暮雨3 小时前
Redis 有哪些常见数据结构?它们的底层实现和使用场景是什么?
java·后端·八股