SpringCloud integrated Gateway gateway

1. Service Gateway

The reason for the emergence of API gateway is the emergence of microservice architecture. Different microservices 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 directly communicates with each microservice Service communication, there will be the following problems:

(1) The client will request different microservices multiple times, which increases the complexity of the client.
(2) There are cross-domain requests, which are relatively complicated to handle in certain scenarios.
(3) Authentication is complex, and each service requires independent authentication.
(4) Difficult to refactor, with the iteration of the project, microservices may need to be re-divided. For example, multiple services may be combined into one or one service may be split into multiple. If the client communicates directly with the microservice, the refactoring will be difficult to implement.
(5) Some microservices may use firewall/browser unfriendly protocols, and direct access will be difficult.

These 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 go through the API gateway first. That is to say, API implementation takes more business logic into consideration, while security, performance, and monitoring can be handled by API gateways, which not only improves business flexibility but also does not lack security.

In a sense, it can replace the function of nginx's proxy port distribution request. And at the same time, configuring in the service gateway can also solve the problem of cross-domain requests between front-end and back-end.

2. Spring cloud gateway

Spring cloud gateway is an official gateway developed by spring based on technologies such as Spring 5.0, Spring Boot2.0 and Project Reactor. Spring Cloud Gateway aims to provide a simple, effective and unified API routing management method for microservice architecture. Spring Cloud Gateway is used as Spring Cloud The gateway in the ecosystem aims to replace Netflix Zuul, which not only provides a unified routing method, but also provides basic functions of the gateway based on the Filer chain, such as: security, monitoring/buying, 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

<dependencies>
    <dependency>
        <groupId>com.atguigu.yygh</groupId>
        <artifactId>common-util</artifactId>
        <version>1.0</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>

    <!-- 服务注册 -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
</dependencies>

3.3 Add a configuration file under resources

1、application.properties

# 服务端口
server.port=80
# 服务名
spring.application.name=service-gateway

# nacos服务地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

#使用服务发现路由
spring.cloud.gateway.discovery.locator.enabled=true

#设置路由id
spring.cloud.gateway.routes[0].id=service-hosp
#设置路由的uri
spring.cloud.gateway.routes[0].uri=lb://service-hosp
#设置路由断言,代理servicerId为auth-service的/auth/路径
spring.cloud.gateway.routes[0].predicates= Path=/*/hosp/**

#设置路由id
spring.cloud.gateway.routes[1].id=service-cmn
#设置路由的uri
spring.cloud.gateway.routes[1].uri=lb://service-cmn
#设置路由断言,代理servicerId为auth-service的/auth/路径
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;

@SpringBootApplication
public class ServerGatewayApplication {
    
    

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

3.5 Cross-domain processing

Cross-origin: browser restrictions on JavaScript's same-origin policy.
The following situations are cross-domain:
Examples of cross-domain reasons

Different domain names www.jd.com
and www.taobao.com have the same domain name, different ports It is the same as the port, but the request path is different and does not belong to cross-domain, such as: www.jd.com/item www.jd.com/goods http and https also belong to cross-domain from localhost:1000 to localhost:8888, which belongs to The ports are different, cross-domain.





3.5.1 Why are there cross-domain issues?

Cross-domain does not necessarily have cross-domain problems.
Because the cross-domain problem is a security restriction of browsers for Ajax requests: Ajax requests initiated by a page can only be in the same path as the domain name of the current page, which can effectively prevent cross-site attacks.
So: the cross domain problem is a kind of limitation for ajax .
But this brings inconvenience to our development, and in the actual production environment, there will definitely be many interactions between servers, and the addresses and ports may be different. What should we do?

3.5.2 Solving cross-domain problems

The global configuration class implements
the CorsConfig class

@Configuration
public class CorsConfig {
    
    
    @Bean
    public CorsWebFilter corsFilter() {
    
    
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedMethod("*");
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");

        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 on the gateway, so the service service does not need to do cross-domain processing. Remove the @CrossOrigin tag added to the controller class before to prevent program exceptions.

Guess you like

Origin blog.csdn.net/david2000999/article/details/122490865