Gateway Service Gateway
网关功能
Gateway is the unified entrance for all microservices. The core functional characteristics of the gateway are mainly reflected in请求路由,权限控制,限流
three parts
- Routing: Since the gateway cannot process the business, the gateway needs to forward the request to the matching host or interface according to a certain method
规则(断言)
. This forwarding The process is called routing 负载均衡
: When the routing target microservice has multiple instances, you also need to select one from the multiple service instances through load balancing rules.身份认证(鉴权)
: As the entrance to microservices, the gateway needs to verify whether the user is qualified to request or has the permission to perform the operation. If not, it will be intercepted.访问控制
: Set up black and white lists, such as restricting IP addresses for DDOS attacks请求限流
: When the request volume is too high, the gateway will release the request at the speed that the microservice can accept to avoid excessive service pressure.- Release control: For example, when a new interface is launched, 20% of the traffic will be allocated to the new interface first, and 80% will be allocated to the old interface, and the proportion will be adjusted slowly later
- [Traffic coloring](https://docs.spring.io/spring-cloud-gateway/docs/current/refer
ence/html/#the-addrequestheader-gatewayfilter -factory): Distinguish user sources and add some identifiers to requests, usually by adding new request headers,global dyeing - Unified handling of cross-domain: The gateway handles cross-domain in a unified manner, without having to handle each project separately
- Unified business processing: Put the common logic required in each project into the upper gateway for unified processing
- Unified document knife4j: It is recommended to aggregate the documents of downstream projects on one page for unified viewing
统一日志
: Unified request and response information records接口保护
: Restriction request,Information desensitization,< /span>Set the timeout and interrupt when it times out,Request current limit (token bucket algorithm, leaky bucket algorithm, RedisLimitHandler),Downgrade circuit breaker for cover-up
网关的分类
Classification of gateways and their technology selection
-
全局网关(接入层网关)
: Mainly implements load balancing, request logs, etc., and is not bound to business logic code -
业务网关(微服务网关)
: Forward requests to different businesses/projects/interfaces/services, including some business logic code
There are mainly four types of gateway implementations in Spring Cloud, which are designed to provide a simple and effective unified API routing management method for the microservice framework.
Spring Cloud Gateway
: A brand new project of SpringCloud, a gateway developed based on reactive programming and event streaming technologies such as Spring 5.0, SpringBoot2.0 and ProjectReactor.Zuul
: It is a blocking programming implementation based on Servlet, and SpringCloudGateway is a responsive programming implementation based on WebFlux provided in Spring 5, so it has better performance.其他实现
: Nginx (global gateway),Kong (API gateway, programming cost is relatively high)
网关的实现
Step 1: Create a new SpringBoot project such as gateway module, introduce gateway dependency and nacos service discovery dependency
<!--网关依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!--nacos服务发现依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
Step 2: Write the startup class ingateway模块
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class,args);
}
}
编程式(更灵活)
: Add the corresponding gateway code to the project startup class to implement https://yupi.icu/yupi to http://yupi.icu/
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("toyupi", r -> r.path("/yupi")
.uri("http://yupi.icu/"))
.route("路由Id", r -> r.path("路由规则")
.uri("路由的目标地址"))
.route.......
.build();
}
配置式(更直观)
:Write basic configuration and routing rules
路由名称(id)
: The unique representation of the route, user-defined only needs to be unique路由目标地址(uri)
: You can route to a fixed address, or you can route to a service and then select a service instance according to the load balancing rules.路由断言(predicates)
: Determine whether the request meets the conditions of the routing rules, and forward it to the routing destination if it does.路由过滤器(filters)
: Perform some processing operations on the request or response
# 快捷配置方式
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org # 路由到固定地址
uri: lb://Nacos中注册的服务名称 # 路由到某个服务
predicates:
- Cookie=mycookie,mycookievalue # cookie里必须有mycookie且值是mycookievalue
# 完全展开配置方式
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- name: Cookie
args:
name: mycookie
regexp: mycookievalue
网关的工作流程
Step 1: The client initiates a request to the gateway. If the request matches the route defined by Handler Mapping
, the request will be forwarded to Web Handler
Step 2: Filter user requests through the defined filter chain. The filter can be executed before or after the proxy request.