Microservices have become the mainstream technology architecture pursued by major manufacturers, and the prospect of learning microservices is very promising, and SpringCloud has become the mainstream microservice technology stack. This series of articles will focus on the SpringCloud technology stack, and comprehensively analyze and explain the actual application of the SpringCloud technology stack in the microservice scenario.
Spring Cloud Gateway
Knowledge Index
- Introduction to Gateway
- Getting Started
- routing prefix
- filter
1 Introduction to Gateway
Spring Cloud Gateway
It is Spring Cloud
a brand new project of the team, a gateway based on technologies such as Spring 5.0
, , SpringBoot2.0
, Project Reactor
etc. It aims to provide a simple, effective and unified REST
request routing management method for microservice architecture.
Spring Cloud Gateway
As SpringCloud
a gateway in the ecosystem, the goal is to replace Netflix Zuul
. Gateway
It not only provides a unified routing method, but also Filter
provides the basic functions of the gateway based on the chain method. For example: security, monitoring/metrics, and throttling.
2 Getting Started
spring_cloud_demos
Create gateway_demo
submodules in the project
2.1 Introducing dependencies
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.2 Startup class
/**
* Copyright (c) 2022 itmentu.com, All rights reserved.
*
* @Author: yang
*/
@SpringBootApplication
public class GateWayDemoApplication {
public static void main(String[] args) {
SpringApplication.run(GateWayDemoApplication.class, args);
}
}
2.3 Configuration
server:
port: 8502
spring:
application:
name: gateway
cloud:
consul:
host: 192.168.184.128
port: 8500
discovery:
service-name: ${
spring.application.name}
register: false
2.4 Routing configuration
gateway:
# 路由si(集合)
routes:
# id唯一标识
- id: consumer-service-route
# 路由服务地址
uri: lb://service-consumer
# 断言
predicates:
- Path=/**
Code Description:
1:routes:id表示路由标识,唯一即可,当前案例中表示消费者服务对应的路由
2:uri: lb://service-consumer表示uri使用负载均衡模式,lb表示loadbalance,”lb:“后跟注册中心对应的服务名
2.5 Transform the original service-consumer service
Add @EnableDiscoveryClient
annotation registration to open the registration center
/**
* Copyright (c) 2022 itmentu.com, All rights reserved.
*
* @Author: yang
*/
@SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker
@EnableHystrixDashboard
@EnableDiscoveryClient
public class ServiceConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceConsumerApplication.class, args);
}
@Bean
@LoadBalanced//开启负载均衡
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
Modify the configuration to register with the registry
spring:
cloud:
consul:
discovery:
register: true
2.6 Testing
Access the consumer service interface by http://localhost:8502/consumer/hello-feign
address
3 Routing prefix
3.1 Add prefix
Prefix addition in the mapped path can gateway
be achieved by configuring the route's filter in . PrefixPath
It can play the role of hiding the interface address and avoid the exposure of the interface address.
Configure the request address to add a path prefix filter
spring:
gateway:
routes:
filters:
- PrefixPath=/consumer
test
Restart routing service access
http://localhost:8502/hello-feign
The result is as follows, indicating that the route to
http://localhost:8002/consumer/hello-feign
3.2 Remove prefix
By gateway
configuring a route filter in StripPrefix
, the addresses in the mapped path can be removed. Pass StripPrefix=1
to specify the number of prefixes to be removed from the route. For example: the path /api/consumer/hello-feign
will be routed to/consumer/hello-feign
Configure remove path prefix filter
spring:
gateway:
routes:
filters:
- StripPrefix=1
test
Restart routing service access
http://localhost:8502/api/consumer/hello-feign
The result is as follows, indicating that it is routed to
/consumer/hello-feign
4 filters
One of the important functions of a filter as a gateway is to implement request authentication. The functions in the previous 路由前缀
chapters are also implemented using filters.
4.1 Common Filters
Gateway
There are dozens of built-in filters. Common built-in filters include:
filter name | illustrate |
---|---|
StripPrefix | Remove prefix from matching request path |
PrefixPath | Prefix the matching request path |
AddRequestHeader | Add Header to matching request |
AddRequestParameter | Add parameters to matching requests |
AddResponseHeader | Add Header to the response returned from the gateway |
4.2 Common Scenarios
- Request authentication: if you do not have access rights, intercept directly
- Exception handling: record exception logs
- Service call duration statistics
4.3 Filter Configuration Instructions
Gateway
There are two filters
局部过滤器:只作用在当前配置的路由上,上面我们配置的路由前缀过滤器就是局部过滤器
全局过滤器:作用在所有路由上。
4.4 Demonstration of global filter configuration
Global configuration filterAddResponseHeader
spring:
cloud:
gateway:
# 配置全局默认过滤器
default-filters:
# 往响应过滤器中加入信息
- AddResponseHeader=key1,value1
test
Response headers can be seen in the browser