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

What is the Spring Cloud gateway gateway service like?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article shows you what the Spring Cloud gateway gateway service is like, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Today, let's talk about spring cloud gateway as a gateway service for spring cloud's son. Many ideas refer to zuul, which provides a convenient condition to consider the migration of zuul to gateway.

Gateway his core function is also similar to zuul. But his implementation is a little different from that of zuul, and his core is based on Spring Boot 2.x, Spring WebFlux and Project Reactor.

Spring WebFlux responsive Web framework.

Spring WebFlux is based on responsive flows, so it can be used to build asynchronous, non-blocking, event-driven services. It uses Reactor as the preferred implementation library for responsive streams, but it also provides support for RxJava. Due to the nature of responsive programming, the underlying layers of Spring WebFlux and Reactor need to support asynchronous running environments. For example, Netty and Undertow; can also run on those that support asynchronous IBO

Above the containers of Servlet 3.1, such as Tomcat (8.0.23 and above) and Jetty (9.0.4 and above).

The upper layer of spring-webflux supports two development modes:

Annotation-based development model similar to Spring WebMVC (@ Controller, @ RequestMapping)

Functional development mode in Java 8 lambda style.

Spring WebFlux also supports responsive Websocket server development.

So spring cloud gateway is not based on blocking web development. He is in conflict with the traditional Servlet. The traditional Servlet jar package reference working principle should be excluded when creating the feature.

The client sends a request to the Spring Cloud Gateway. If the gateway handler map determines that the request matches the route, it is sent to the gateway Web handler. The handler runs to send the request through a request-specific filter chain. The reason filters are separated by dotted lines is that filters can execute logic before or after sending proxy requests. Execute all "front" filter logic, and then issue a proxy request. After the proxy request is made, the publish filter logic is executed.

Note: URI defined in a route without ports will set the default ports for HTTP and HTTPS URI to 80 and 443, respectively.

Predicate asserts that this is a Java 8 Predicate. The input type is a ServerWebExchange. We can use it to match anything from the HTTP request, such as headers or parameters.

Route route forwarding is defined by a serverID, a destination URI, a set of assertions, and a set of filters. If the assertion is true, the route matches.

Filter request filtering intercepts web resources, does some processing, and then gives it to the processor for processing

Before modification, we have a spring-boot-starter-web project reference in the total pom of the pom file of the project. Delete it. Rely solely on the service. As mentioned above, the problem of jar package conflicts in traditional Servlet.

Add to the service consumer and service provider, respectively

Org.springframework.boot spring-boot-starter-web

We create the project cloud-gateway and modify the pom

Com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery com.alibaba.cloud spring-cloud-alibaba-nacos-config org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-starter-gateway Org.springframework.boot spring-boot-maven-plugin

Create bootstrap.yml

Server: port: 9000spring: profiles: active: dev application: name: cloud-gateway-demo jackson: date-format: yyyy-MM-dd HH:mm:ss time-zone: GMT+8 default-property-inclusion: non_null cloud: nacos: discovery: server-addr: 47.99.209.72 non_null cloud 8848 # ${prefix}-${spring.profile.active}. ${file-extension} config: Server-addr: 47.99.209.72 yaml gateway 8848 file-extension: yaml gateway: discovery: locator: # whether it is combined with the service discovery component Forwarded to a specific service instance via serviceId. The default false, # enables routing rules based on service discovery for true representatives. Enabled: true # access after configuration does not need to uppercase lower-case-service-id: true routes:-id: cloud-discovery-server uri: lb://cloud-discovery-server predicates: # path matching, starting with api, direct configuration is not valid See filters configuration-Path=/server/** filters: # prefix filtering. By default, our request path is http://localhost:9000/myshop-service-consumer-item/** and will be routed to the specified service. # remove a path prefix here, and then configure the Path=/api/** above. You can access-StripPrefix=1-id: cloud-discovery-client uri: lb://cloud-discovery-client predicates: # path matching as http://localhost:9000/api/**, starting with api. Direct configuration does not take effect. See filters configuration-Path=/client/** filters: # prefix filtering By default, our request path is http://localhost:9000/myshop-service-consumer-item/**, which will be routed to the specified service. # remove 1 path prefix here, and then configure the above Path=/api/**, to access-StripPrefix=1 in the http://localhost:9000/api/** way.

Create a main startup class

Package com.xian.cloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;/** @ Author: xlr * @ Date: Author 11:08 2019-11-4 * / @ EnableDiscoveryClient@SpringBootApplicationpublic class GatewayServerApplication {public static void main (String [] args) {SpringApplication.run (GatewayServerApplication.class, args);}}

Start the service command line curl http://localhost:9000/client/client/test

The services have been integrated. Routing function forwarding has been implemented. Descriptions of some of the fields in the configuration file are also described in the comments. The next article is about Spring Cloud Gateway assertions.

The above is what the Spring Cloud gateway gateway service is like. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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: 0

*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