Spring Cloud Gateway接入记录

依赖注意

Spring Cloud Gateway是使用netty+webflux实现因此不需要再引入spring-boot-starter-web包,这里我们引入Eureka,是为了直接通过Eureka 获取注册服务并发送请求。

Spring Cloud Gateway切记不可添加spring-boot-starterspring-boot-starter-web依赖,否则报404错误

1
2
3
4
<!--    <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter</artifactId>-->
<!-- </dependency>-->

基本依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<dependencies>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter</artifactId>-->
<!-- </dependency>-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-gateway -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<!--https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

开启调试日志

1
2
3
logging:
level:
org.springframework.cloud.gateway: debug

基本配置(Java)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@Configuration
public class RouteLocatorConfig {

@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
// .route("path_route", r -> r.path("/about") //http://127.0.0.1:9000/about -> http://www.appblog.cn/about
// .uri("http://www.appblog.cn"))
.route("path_route", r -> r.path("/about") //http://127.0.0.1:9000/about -> http://www.appblog.cn
.filters(f -> f.stripPrefix(1))
.uri("http://www.appblog.cn"))
.route(r -> r.path("/test/**") //http://127.0.0.1:9000/test/hello -> http://127.0.0.1:9001/hello
.filters(f -> f.stripPrefix(1))
.uri("http://127.0.0.1:9001")
)
.route("service_route", r -> r.path("/consumer/**")
.filters(f -> f.stripPrefix(1)
.addResponseHeader("X-AnotherHeader", "test"))
.uri("lb://spring-cloud-consumer")
)
.route(r -> r.path("/test/rateLimit")
.filters(f -> f.rewritePath("test", "hello")
.filter(new GatewayRateLimitByIpFilter(10, 1, Duration.ofSeconds(1))))
.uri("http://127.0.0.1:9001")
)
.build();
}
}

基本配置(yml)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
spring:
cloud:
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
routes:
# http://127.0.0.1:9000/about -> http://www.appblog.cn
- id: appblog
uri: http://www.appblog.cn
predicates:
- Path=/about
filters:
- StripPrefix=1
# http://127.0.0.1:9000/consumer/hello -> http://spring-cloud-consumer/hello
- id: consumer-service
uri: lb://spring-cloud-consumer
predicates:
- Path=/consumer/**
filters:
- StripPrefix=1
- id: producer-service
uri: lb://spring-cloud-producer
predicates:
- Path=/producer/**
filters:
- StripPrefix=1

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2021 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :