Spring Cloud Gateway是基于Spring5、SpringBoot2.0和Project Reactor等技术开发的网关,目的是为微服务架构系统提供高性能,且简单易用的api路由管理方式。
优点:
1.性能强劲,是第一代网关Zuul的1.6倍。
2.功能强大,内置很多使用功能,例如:路由、过滤、限流、监控等。
3.易于扩展。
代码示例:
1、使用IDEA新建模块cloud-gw
pom文件添加如下引用:
<?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>cloud-demo01</artifactId> <groupId>org.yas</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-gw</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <!-- 端点监控场景依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- nacos场景依赖 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> </project>
2、application.yml配置文件:
server: port: 8040 spring: application: name: cloud-gateway cloud: nacos: discovery: server-addr: localhost:8848 gateway: routes: - id: cloud-goods # uri: http://localhost:9901/ uri: lb://cloud-goods/ #使用Ribbon调用服务 predicates: #谓词,断言 - Path=/goods/** # - After=2021-11-11T16:02:30.200+08:00[Asia/Shanghai] # - Before=2022-11-11T16:02:30.200+08:00[Asia/Shanghai] # - Cookie=age,18 # - Header=token,123 # - Host=localhost,192.168.0.*,127.0.0.1 # - Method=GET filters: #过滤器 - AddRequestHeader=tonken,123 - id: baidu #路由表,从上到下,按顺序匹配 uri: http://www.baidu.com predicates: - Path=/**
3、启动类:
1 package com.yas; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 7 @SpringBootApplication 8 @EnableDiscoveryClient//开启服务注册与发现功能 9 public class GWApp { 10 public static void main(String[] args) { 11 SpringApplication.run(GWApp.class,args); 12 } 13 }
测试:
使用postman请求地址:localhost:8040/goods/findById/1
即可转发到cloud-goods服务对应的方法。
补充:跨域访问,添加如下配置类
1 package com.yas.config; 2 3 import org.springframework.context.annotation.Bean; 4 import org.springframework.context.annotation.Configuration; 5 import org.springframework.web.cors.CorsConfiguration; 6 import org.springframework.web.cors.reactive.CorsWebFilter; 7 import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource; 8 import org.springframework.web.util.pattern.PathPatternParser; 9 10 @Configuration 11 public class CorsConfig { 12 13 //解决跨域问题 14 @Bean 15 public CorsWebFilter corsFilter(){ 16 CorsConfiguration config = new CorsConfiguration(); 17 config.addAllowedHeader("*"); 18 config.addAllowedMethod("*"); 19 config.addAllowedOrigin("*"); 20 21 UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser()); 22 source.registerCorsConfiguration("/**",config); 23 return new CorsWebFilter(source); 24 } 25 }