Using Spring Boot and Spring Cloud to implement the API gateway under the microservice architecture: Simplify the interaction process between the client and multiple microservices

Using Spring Boot and Spring Cloud to implement the API gateway under the microservice architecture: Simplify the interaction process between the client and multiple microservices

1. Introduction

1 Development trend of microservice architecture

With the rapid growth and iteration of business, traditional monolithic applications can no longer meet the needs of today's large-scale distribution. As a new service architecture pattern, microservice architecture improves development efficiency and scalability by splitting applications into smaller units. Each unit can be developed, tested and deployed independently, enabling rapid response to business changes and requirements.

2 Challenges under the microservice architecture

The microservice architecture also faces some challenges. One of the important challenges is how to manage and protect the open API interface. Since there are a large number of service instances and interface resources in the microservice architecture, a common entrance is required to provide services externally, and authentication and authorization are required to protect service resources.

3 Function and value of API gateway

In order to meet the challenges under the microservice architecture, the API gateway came into being. The API gateway is a single entry point for services, providing functions such as routing, forwarding, security, monitoring, and protocol conversion. The API gateway can manage and protect the API interface, uniformly access and process the requests of various microservices, and at the same time improve the availability and reliability of the service.

2. Introduction to Spring Boot and Spring Cloud

1 Features and advantages of Spring Boot

Spring Boot is a framework for quickly building independent Spring applications. Through automatic configuration and convention over configuration, Spring applications can be quickly developed and deployed. Spring Boot has the characteristics and advantages of self-contained, easy to deploy, no need for xml configuration, automatic assembly, and multiple starter packages.

2 Main functions and components of Spring Cloud

Spring Cloud is a Spring Boot-based microservice framework that provides numerous components and functions, including Spring Cloud Config, Spring Cloud Netflix, Spring Cloud Bus, Spring Cloud Sleuth, Spring Cloud Stream, etc. Among them, Spring Cloud Netflix includes many practical microservice components, such as Netflix Eureka, Netflix Hystrix, Netflix Zuul, etc., which make the development of microservice architecture easier and more convenient.

3 Basic principles and characteristics of Spring Cloud Gateway

As a new generation of API gateway, Spring Cloud Gateway uses a filter chain-based approach for routing and forwarding. It can integrate a variety of service discovery components and load balancing strategies, and supports functions such as dynamic routing, circuit breakers, current limiting, and request retrying. In addition, Spring Cloud Gateway also has the characteristics of concise code, easy expansion and high performance.

@RestController
public class ApiController {
    
    

    @RequestMapping("/hello/{name}")
    public String hello(@PathVariable String name) {
    
    
        return "Hello, " + name + " from API";
    }

}

The above is a simple Spring Boot REST interface. API Gateway can expose this interface to external clients by implementing routing and proxy, and provides high-performance and high-availability services.

3. Implementation of API gateway under microservice architecture

1 Outline the role and advantages of API gateways under the microservice architecture

In the microservice architecture, each microservice has its own REST API, which is called by other microservices or front-end applications. However, with the increase in the number and diversity of services, the way clients call REST APIs becomes more and more complicated. At this time, a centralized entrance is needed to manage these APIs. This centralized entrance is API gateway.

As a centralized entrance, the API gateway can manage and protect the API interface, and can perform functions such as flow control, access control, fuse and current limit on the request, so that the client's request only needs to call the API gateway without knowing the specific conditions of the underlying microservices .

The advantage of the microservice architecture is that it can easily divide services, which can effectively achieve agile development, fast iteration, scalability, high availability and performance optimization, while the API gateway can further improve system security, stability and Observability.

2 Technical points of implementing API gateway under microservice architecture

2.1 Service discovery and registration

In the microservice architecture, the number of services is large and unstable, so a mechanism is needed to track the status and location of services, which is service discovery and registration. Service discovery and registration can be implemented using tools such as Consul, Zookeeper, Etcd, etc. When the service starts, it will register its own metadata (such as IP address, port, health status, etc.) to the service registry, and other services can pass the service Discovery mechanism to obtain service information.

2.2 Fusing and current limiting

Invoking a problematic service in a distributed system may cause the entire system to crash, so a mechanism is needed to control the access of the service, which is circuit breaker and current limit. The fuse mechanism will close the service call until it is re-enabled when the service is abnormal, and the current limit will limit the service request to achieve the purpose of controlling the flow.

2.3 Authentication and Identity Authentication

In an open system, request authentication and identity authentication are required to ensure the security of the system. Authentication and identity authentication can be implemented using popular authentication mechanisms such as OAuth2, JWT, etc., to authorize users to access APIs by verifying user identities and permissions.

2.4 Unified routing and load balancing

In the microservice architecture, services may be deployed on different hosts and accessed using different ports. Therefore, a unified routing method is required so that clients can access the corresponding services. Using load balancing technology can ensure the health, high performance and scalability of services.

4. Build an API gateway based on Spring Boot and Spring Cloud

1 Build a simple microservice application

We first build a simple microservice application that contains two services: user-service and order-service. user-service is used to process user-related requests, and order-service is used to process order-related requests.

@SpringBootApplication
public class UserServiceApplication {
    
    

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

}

@RestController
public class UserController {
    
    

    @GetMapping("/users/{userId}")
    public String getUser(@PathVariable String userId) {
    
    
        return "User " + userId;
    }

}

@SpringBootApplication
public class OrderServiceApplication {
    
    

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

}

@RestController
public class OrderController {
    
    

    @GetMapping("/orders/{orderId}")
    public String getOrder(@PathVariable String orderId) {
    
    
        return "Order " + orderId;
    }

}

The above are the codes of the two microservices to process user and order requests respectively.

2 Build a service discovery and registration center

We use Consul as a service discovery and registry. Building Consul is very simple, just download the corresponding binary package, and then start it by executing the command line.

3 Integrate Spring Cloud Gateway

We use Spring Cloud Gateway as API Gateway. Spring Cloud Gateway is a lightweight, high-performance, and easily extensible API gateway that can be easily integrated with Spring Boot and Spring Cloud.

Add Spring Cloud Gateway dependency in the pom.xml file:

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

4 Realize the key functions of the API gateway

4.1 Authentication and Identity Authentication

We can use Spring Security and OAuth2 to implement API gateway authentication and authentication.

Add Spring Security and OAuth2 dependencies in the pom.xml file:

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-oauth2-resource-server</artifactId>
</dependency>

Configure Spring Security and OAuth2 in the application.yml file:

spring:
  security:
    oauth2:
      resourceserver:
        jwt:
          jwk-set-uri: <auth-server-url>/auth/realms/<realm-name>/protocol/openid-connect/certs

Then you need to authenticate each API interface, for example:

@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
    
    
    return builder.routes()
        .route(r -> r.path("/users/**")
            .filters(f -> f.stripPrefix(1))
            .uri("lb://user-service")
            .filters(f -> f.oauth2ResourceServer().jwt()))
        .route(r -> r.path("/orders/**")
            .filters(f -> f.stripPrefix(1))
            .uri("lb://order-service")
            .filters(f -> f.oauth2ResourceServer().jwt()))
        .build();
}

4.2 Unified routing and load balancing

We can use Spring Cloud LoadBalancer to achieve unified routing and load balancing of API Gateway.

Add Spring Cloud LoadBalancer dependency in the pom.xml file:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-loadbalancer</artifactId>
    <version>2.2.1.RELEASE</version>
</dependency>

Then you can add LoadBalancerClientFilter to achieve unified routing and load balancing, for example:

@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder, LoadBalancerClient loadBalancerClient) {
    
    
    return builder.routes()
        .route(r -> r.path("/users/**")
            .filters(f -> f.stripPrefix(1))
            .uri("lb://user-service")
            .filters(f -> f.filter(loadBalancerClientFilter(loadBalancerClient))))
        .route(r -> r.path("/orders/**")
            .filters(f -> f.stripPrefix(1))
            .uri("lb://order-service")
            .filters(f -> f.filter(loadBalancerClientFilter(loadBalancerClient))))
        .build();
}

private GatewayFilter loadBalancerClientFilter(LoadBalancerClient loadBalancerClient) {
    
    
    return (exchange, chain) -> loadBalancerClient.choose(((URI) exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR)).getHost())
            .map(serviceInstance -> {
    
    
                URI uri = exchange.getRequest().getURI();

                // 使用选择的服务实例替换URI原始路径中的服务名称
                URI newUri = loadBalancerClient.reconstructURI(
                    new DelegatingServiceInstance(serviceInstance),
                    uri);

                // 重新定义请求的URI
                ServerHttpRequest request = exchange.getRequest().mutate()
                    .uri(newUri)
                    .build();

                return chain.filter(exchange.mutate().request(request).build());
            }).orElse(chain.filter(exchange));
}

4.3 Fusing and current limiting

We can use Sentinel to realize circuit breaking and current limiting of API gateway. Sentinel is a traffic control system open sourced by Alibaba, which can cope with high concurrent traffic and perform circuit breaker protection.

Add Sentinel dependencies in the pom.xml file:

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
    <version>2.2.1.RELEASE</version>
</dependency>

Then, configure Sentinel in the application.yml file:

spring:
  cloud:
    sentinel:
      transport:
        dashboard: <sentinel-dashboard-url>
      gateway:
        enabled: true

The above is the construction and implementation of the API gateway based on Spring Boot and Spring Cloud, which can well support traffic control, routing and security issues under the microservice architecture, and can provide high availability and performance optimization.

5. Effects and limitations of implementing API gateway

In this paper, the concept, function, implementation and key functions of the API gateway are introduced, and then the implementation effect and limitations of the API gateway are reviewed.

1 Realize the effect of API gateway

By implementing an API gateway, the following effects can be obtained:

1.1 Unified management API interface

The API gateway can manage the API interfaces of each microservice in a unified way, avoiding the client from calling the microservice directly, strengthening the security of the service, and improving the observability and maintainability of the system.

1.2 Unified Authentication and Identity Authentication

The API gateway can handle user authentication and identity authentication in a unified manner, avoiding repeated authentication work for each microservice, and implementing security protection for services to ensure user data security.

1.3 Fuse and current limiting protection

The API gateway can protect the access of microservices through the fuse and current limiting mechanism, prevent server downtime caused by excessive traffic, and improve the stability and availability of the system.

1.4 Unified routing and load balancing

The API gateway can complete the functions of unified routing and load balancing, effectively schedule and balance traffic, avoid single point of failure due to excessive traffic, and improve the disaster tolerance and scalability of the system.

2 Limitations of implementing an API gateway

Although API gateways have many advantages, implementing API gateways also has the following limitations:

2.1 Single point of failure

As a centralized service, the API gateway may have a single point of failure, causing the entire system to crash.

2.2 Performance bottleneck

There may be performance bottlenecks in the process of API gateway processing requests, resulting in request delays or processing failures.

2.3 Dependency management

API gateway needs to rely on many open source frameworks and components to achieve dependency management and version control, otherwise there will be compatibility and stability problems.

2.4 Technical Complexity

Implementing an API gateway requires technical knowledge in multiple fields such as distributed, microservices, network, security, and performance, and requires professional technicians to maintain and upgrade it.

Guess you like

Origin blog.csdn.net/u010349629/article/details/130700505