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

How to configure SpringCloud GateWay Gateway

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces "how to configure the SpringCloud GateWay gateway" in detail. The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to configure the SpringCloud GateWay gateway" can help you solve your doubts. Let's follow the editor's ideas to learn new knowledge.

First, the basic concept of gateway 1. Introduction of API gateway

The reason for the emergence of API gateway is the emergence of micro-service architecture. Different micro-services generally have different network addresses, and external clients may need to call the interfaces of multiple services to complete a business requirement. If the client is allowed to communicate with each micro-service directly, there will be the following problems:

(1) the client requests different micro-services for many times, which increases the complexity of the client.

(2) there are cross-domain requests, and it is relatively complex to process them in certain scenarios.

(3) Authentication is complex, and each service needs independent authentication.

(4) it is difficult to restructure, and as the project iterates, it may be necessary to repartition micro-services. For example, multiple services may be merged into one or split into multiple services. If the client communicates directly with the microservice, refactoring will be difficult to implement.

(5) some microservices may use firewall / browser-unfriendly protocols, so it will be difficult to access them directly.

The above problems can be solved with the help of API gateway. The API gateway is the middle layer between the client and the server, and all external requests will pass through the API gateway first. In other words, the implementation of API pays more attention to business logic, while security, performance and monitoring can be done by the API gateway, which not only improves business flexibility but also has no lack of security.

2 、 Spring Cloud Gateway

Spring cloud gateway is an official gateway developed by spring based on Spring 5.0, Spring Boot2.0 and Project Reactor. Spring Cloud Gateway aims to provide simple, effective and unified API routing management for micro-service architectures. Spring Cloud Gateway, as a gateway in Spring Cloud ecosystem, aims to replace Netflix Zuul. It not only provides a unified routing method, but also provides the basic functions of the gateway based on Filer chain. For example: safety, monitoring / burying point, current restriction and so on.

3. The core concepts of Spring Cloud Gateway

The gateway provides API fully managed services and rich API management functions to assist enterprises in managing large-scale API to reduce management costs and security risks, including protocol adaptation, protocol forwarding, security policy, anti-brushing, traffic, monitoring logs and so on. Generally speaking, the URL or interface information exposed by the gateway is collectively referred to as routing information. If you have developed gateway middleware or used Zuul, you will know that the core of the gateway is Filter and Filter Chain (Filter chain of responsibility). Sprig Cloud Gateway also has the concept of routing and Filter.

Here are a few important concepts in Spring Cloud Gateway.

(1) routing. Routing is the most basic part of the gateway, and the routing information consists of an ID, a destination URL, a set of assertions, and a set of Filter. If you assert that the route is true, the requested URL matches the configuration

(2) assertion. Assertion function in Java8. The input type of the assertion function in Spring Cloud Gateway is ServerWebExchange in the Spring5.0 framework. The assertion function in Spring Cloud Gateway allows developers to define matches from http

Any information in the request, such as request headers and parameters.

(3) filter. A standard Spring webFilter. Filter in Spring cloud gateway is divided into two types of Filter, Gateway Filter and Global Filter. The filter Filter will modify the request and response

As shown in the figure above, Spring cloud Gateway makes a request. The route matching the request is then found in Gateway Handler Mapping and sent to Gateway web handler. Handler then sends the request to our actual service to execute the business logic through the specified filter chain, and then returns.

Spring cloud Gateway has many powerful functions, so let's simply implement load balancing and service discovery.

Introduce dependency

Org.springframework.cloud spring-cloud-starter-alibaba-nacos-discovery org.springframework.cloud spring-cloud-starter-gateway com.google.code.gson gson org.springframework.cloud spring-cloud-starter-openfeign

Add Profil

# Service port server.port=8222# service name spring.application.name=service-gateway# nacos service address spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848# uses service discovery routing spring.cloud.gateway.discovery.locator.enabled=true# setting routing id 0 represents the first service spring.cloud.gateway.routes [0] .id=service-edu# setting routed uri lb://nacos registered service name spring.cloud.gateway.routes [0] .uri=lb://service-edu# sets routing assertions The proxy servicerId sets the routing idspring.cloud.gateway.routes [1] for the / auth/ path spring.cloud.gateway.routes [0] .principates = Path=/eduservice/**# of the auth-service. The service name registered by the uri lb://nacos of the id=service-msm# setting route spring.cloud.gateway.routes [1] .uri=lb://service-msm# sets the routing assertion, and the proxy servicerId is the / auth/ path spring.cloud.gateway.routes [1] .formulates = Path=/edumsm/** of auth-service.

Remember to add @ EnableDiscoveryClient annotation to the startup class because gateway needs to be used in conjunction with nacos

Make sure that the gateway gateway module and other modules are registered in nacos, such as

Originally, the port number of the edu module is 8001, but now you can also access the services of edu when you access port 8222 of the gateway module, thus realizing the service discovery load balancer (gateway can automatically help us with load balancing without any other configuration). Does this make you feel like you can abandon nginx?

In fact, most of the actual projects are used in combination of nginx and gateway, mainly because gateway is equivalent to a part of the business system, and the whole system will be inaccessible in the event of a downtime. Be sure to add nginx before gateway. Nginx backend corresponds to multiple gateway,nginx, but proxy traffic forwarding does not have any business logic, which can not only ensure stability, but also achieve horizontal load balancing.

That is, Nginx first balances the request load of the client to SpringGateway, and then SpringGateway balances the request load to each business micro-service through service discovery.

In addition, the combination of nignx and gateway can also realize the separation of motion and motion.

In actual development, some requests need to be processed in the background, while others are not. Files that need to be processed in the background are called dynamic resources, and those that do not need background processing are called static resources. Then we can deploy static resources on nginx and dynamic resources on micro-service clusters, which can improve the response speed of resources.

After reading this, the article "how to configure SpringCloud GateWay Gateway" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, 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: 286

*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

Development

Wechat

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

12
Report