SpringGateway配置使⽤(⼀)
参考⽂档: ,
1.Spring Gateway简介
Spring Cloud Gateway是Spring官⽅基于Spring 5.0,Spring Boot 2.0和Project Reactor等技术开发的⽹关,Spring Cloud Gateway旨在为微服务架构提供⼀种简单⽽有效的统⼀的
API路由管理⽅式。Spring Cloud Gateway作为Spring Cloud⽣态系中的⽹关,⽬标是替代Netflix ZUUL,其不仅提供统⼀的路由⽅式,并且基于Filter链的⽅式提供了⽹关基本
的功能,例如:安全,监控/埋点,和限流等。
2.Spring Gateway⼯作原理
Spring Gateway⼯作结构图
spring_cloud_gateway_diagram.png
3.Spring Gateway配置和使⽤
由于项⽬使⽤maven模块化配置,Springboot版本为:2.0.3.RELEASE, SpringCloud版本为:Finchley.RELEASE
pom配置:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</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-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-contract-stub-runner</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependencyManagement</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependencies</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>depend
ency</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>org.springframework.cloud<span class="token tag"><span class=" <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-dependencies<span class="token tag"><span class= <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>${spring-cloud.version}<span class="token tag"><span class="toke <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>type</span><span class="token punctuation">></span></span>pom<span class="token tag"><span class="token tag"><span class= <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>scope</span><span class="token punctuation">></span></span>import<span class="token tag"><span class="token tag"><span cla <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span>
springcloud和springboot
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="t <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-dependencies<span class="token tag"><span class= <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>${spring-boot.version}<span class="token tag"><span class="token <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>type</span><span class="token punctuation">></span></span>pom<span class="token tag"><span class="token tag"><span class= <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>scope</span><span class="token punctuation">></span></span>import<span class="token tag"><span class="token tag"><span cla <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependencies</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependencyManagement</span><span class="token punctuation">></span></span>
请注意:不要引⼊spring-boot-starter-web包,会导致Gateway启动抛出异常
Spring Gateway⽀持两种⽅式提供路由服务,其⼀是配置⽂件启⽤,其⼆则是通过代码达到⽬的
spring:
application:
name: xxxx
cloud:
consul:
host: localhost
port: 8500
discovery:
enabled: true
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
service-name: xxxx
prefer-ip-address: true
gateway:
discovery:
locator:
enabled: true
routes:
# This route rule used to forward request to activity server
- id: activity-route
uri: lb://activity
predicates:
- Path=/activity/**
filters:
- StripPrefix=1
代码实现
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
utes()
.route(r -> r.path("/activity/**")
.filters(f -> f.stripPrefix(1).filter(new TestGetWayFilter()).addResponseHeader("X-Response-Default-Foo", "Default-Bar")) .uri("lb://activity")
.order(0)
.id("activity-route")
)
.build();
}
原⽂地址:cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.0.RC2/reference/html/
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论