Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Spring Cloud Gateway Gateway Taste

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Gateway

Spring Cloud Gateway is an official Spring gateway based on technologies such as Spring 5.0, Spring Boot 2.0 and Project Reactor. Spring Cloud Gateway aims to provide a simple and effective way to manage API routing for microservices architecture. Spring Cloud Gateway, as a gateway in the Spring Cloud ecosystem, aims to replace Netflix ZUUL, which not only provides a unified routing method, but also provides basic gateway functions based on Filter Chain, such as security, monitoring/burying points, and current limiting.

Maybe some students will ask, isn't there already Zuul, why did you make a gateway, this update rhythm is really fast ha, no energy really can't learn over.

The reason for a new gateway is because Zuul is based on servlet 2.5 (works with 3.x) and uses a blocking API. It does not support any long-term connections, such as websocket.

Gateway builds on Spring Framework 5, Project Reactor, and Spring Boot 2, using non-blocking APIs. Websockets is supported, because it is tightly integrated with Spring, so it will be a framework for developers to have a better experience. Of course, the performance improvement is certain, otherwise there is no need to make a new ah, but Zuul2 came out too late, I have already made one, so it is unlikely to integrate Zuul2 into Spring Cloud.

For comparison of performance, please refer to my brother Zhou's article "Error correction post: Zuul & Spring Cloud Gateway & Linkerd performance comparison"

working principle

As shown above, the client sends a request to Spring Cloud Gateway, Gateway Handler Mapping determines that the request matches the route, and the request is handed over to Gateway Web Handler for processing.

Multiple filters can be implemented before and after the proxy. Finally, the agent goes to specific services.

Project Integration Gateway

The first and most basic step is to create a Maven project and add the dependency information required by Gateway:

org.springframework.bootgroupId>

spring-boot-starter-parentartifactId>

2.0.1.RELEASEversion>

parent>

org.springframework.cloudgroupId>

spring-cloud-dependenciesartifactId>

Finchley.RELEASEversion>

pomtype>

importscope>

dependency>

dependencies>

dependencyManagement>

org.springframework.cloudgroupId>

spring-cloud-starter-gatewayartifactId>

dependency>

dependencies>

Write startup classes:

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

/**

* gateway activation portal

*

* @author yinjihuan

*

* @about http://cxytiandi.com/about

*

*/

@SpringBootApplication

public class GatewayApplication {

public static void main(String[] args) {

SpringApplication.run(GatewayApplication.class, args);

}

}

simple route listing

Here is how to implement one of the simplest forwarding functions, Path-based matching forwarding.

Create an application.yml file under resources with the following content:

server:

port: 8084

spring:

cloud:

gateway:

routes:

- id: path_route

uri: http://cxytiandi.com

predicates:

- Path=/course

When you visit http://localhost:8084/course, it will be forwarded to http://cxytiandi.com/course, and the effect is as follows:

We will introduce routing rules later. This chapter is just to experience the functions of Spring Cloud Gateway first. You can create a new project and start it successfully. Step by step.

If your project includes spring-cloud-starter-gateway, but you don't want to start gateway, you can disable it by configuring it as follows:

application.properties

spring.cloud.gateway.enabled=false.

application.yml

spring:

cloud:

gateway:

enabled: false

The above explanation is based on the configuration of the way to achieve routing, there is also a way to route through the code, such as:

@Bean

public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {

return builder.routes()

.route(r -> r.path("/course").uri("http://cxytiandi.com"))

.build();

}

Listing code: github.com/yinjihuan/spring-cloud/tree/master/fangjia-gateway

Original link: mp.weixin.qq.com/s/92JDqjRcv452FfQUJxuRhw

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 241

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report