Spring cloud integrated Gateway gateway

Posted by samscripts on Fri, 14 Jan 2022 21:15:55 +0100

1. Service 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 directly with each micro service, there will be the following problems:

(1) The client will request different microservices many times, which increases the complexity of the client.
(2) There are cross domain requests, which are relatively complex to process in certain scenarios.
(3) Authentication is complex, and each service needs independent authentication.
(4) It is difficult to refactor. With the iteration of the project, it may be necessary to re divide the microservices. For example, you might merge multiple services into one or split a service into multiple. If the client communicates directly with the microservice, the refactoring will be difficult to implement.
(5) Some micro services may use firewall / browser unfriendly protocols, and direct access will be difficult.

These problems can be solved by API gateway. API gateway is an intermediate layer between client and server. All external requests will pass through the API gateway layer first. In other words, business logic is more considered in the implementation of API, and security, performance and monitoring can be done by API gateway, which not only improves business flexibility but also does not lack security.

In a sense, it can replace the proxy port of nginx to distribute requests. At the same time, configuration in the service gateway can also solve the cross domain problem of front and back-end requests.

2. Spring cloud gateway

Spring Cloud gateway is the official spring gateway based on Spring 5.0 and Spring Boot2.0 0 and Project Reactor. Spring Cloud gateway aims to provide a simple, effective and unified API routing management method for microservice architecture. As a gateway in Spring Cloud ecosystem, Spring Cloud gateway 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 / embedding point, current limiting, etc

3. Service construction

3.1 build server gateway

Create a new module in the project

3.2 modify the configuration POM xml

Modify POM xml



    <!-- Service registration -->

3.3 add configuration file under resources


# Service port
# service name

# nacos service address

#Using service discovery routing

#Set routing id
#Set the uri of the route
#Set the route assertion. The agent servicerId is the / auth / path of auth service
spring.cloud.gateway.routes[0].predicates= Path=/*/hosp/**

#Set routing id
#Set the uri of the route
#Set the route assertion. The agent servicerId is the / auth / path of auth service
spring.cloud.gateway.routes[1].predicates= Path=/*/cmn/**

3.4 add startup class

package com.atguigu.yygh;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class ServerGatewayApplication {

public static void main(String[] args) {
      SpringApplication.run(ServerGatewayApplication.class, args);

3.5 cross domain processing

Cross domain: Browser restrictions on javascript's homology policy.
The following situations are cross domain:
Cross domain cause description example

Different domain names www.jd.com COM and www.taobao.com com
Same domain name, different ports www.jd.com COM: 8080 and www.jd.com com:8081
The secondary domain name is different from item jd. COM and Miaosha jd. com
If the domain name and port are the same, but the request path is different, it does not belong to cross domain, such as:
http and https are also cross domain
Access localhost:8888 from localhost:1000, which belongs to different ports and cross domains.

3.5.1 why is there a cross domain problem?

Cross domain problems do not always exist.
Because the cross domain problem is a security limitation of the browser for ajax requests: the ajax request initiated by a page can only be the same path as the current page domain name, which can effectively prevent cross site attacks.
Therefore: cross domain issues are a limitation of ajax.
But this has brought inconvenience to our development, and in the actual production environment, there must be many servers interacting with each other, and the addresses and ports may be different. What should we do?

3.5.2 solving cross domain problems

Global configuration class implementation
CorsConfig class

public class CorsConfig {
    public CorsWebFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        source.registerCorsConfiguration("/**", config);

        return new CorsWebFilter(source);

3.6 service adjustment

At present, we have done cross domain processing in the gateway, so the service does not need to do cross domain processing. Remove the @ CrossOrigin tag previously added to the controller class to prevent program exceptions

Topics: Java Back-end Spring Cloud gateway