“This is the 28th day of my participation in the November Gwen Challenge.The final text challenge in 2021”.

preface

Why do we introduce gateways in a microservices scenario? Or what role does the gateway play in the microservices architecture?

Problems faced

Under the micro-service architecture, the carrying capacity of our project has been improved, but the relative maintenance of multiple services working together and the management of cluster have also brought us some new problems.

  • The client needs to know the port number of each service when calling different services, such as callAservicehttp://127.0.0.1:8000/addproduct, the callBThe servicehttp://127.0.0.1:8001/adduserEach service port number is different, which makes our front-end code difficult to maintain.
  • When multiple nodes are deployed on the same service and the front-end code specifies a specific port, load balancing cannot be achieved
  • The client and server are accessed through a specific port, and this strong coupling leads to later splitting of an existing service, notifying the client, and making changes to the corresponding front-end code.
  • For communication between services, authentication is required when the client calls service A and calls service B, and authentication and authorization codes are repeatedly developed.
  • Too much information is exposed, and the system security deteriorates

Microservice Gateway

Based on the above problems, we can introduce the service gateway, the gateway is exposed outside the action of a single port, to maintain the back-end each service routing information, corresponding to the user, all request directly to the gateway, and then the gateway, routing to the corresponding service, this time we can do load balancing, unified authorization to intercept processing, and so on.

Spring Cloud Gateway

Spring Cloud Gateway:

Spring Cloud Gateway is the second generation of Spring Clould Gateway. The first generation of Netflix zuul has been discontinued. Built based on Netty(NewlO), Reactor (Responsive Programming) and WebFlux (new WebMVC framework)

Advantages of Spring Cloud Gateway:

  • Performance is strong and execution efficiency is traditionally basedServletSynchronous communicationZuulThe efficiency of the1.6
  • Powerful, withSpring CloudNatural integration of the system, there is no compatibility problem, built-in forwarding/limiting/monitoring
  • Elegant design, simple to use and easy to expand

Disadvantages of Spring Cloud Gateway:

  • Based on theNIO,J2EESystem, learning source code is by a certain threshold
  • Are not compatibleServlet, the application is incompatibleTomcatTo playWar
  • Only supportSpring Boot 2.xVersion, for unfamiliarSpringBootFriends have learning barriers

Spring Cloud Gateway fast integration

  • Create a basic SpringBoot application
  • Introduction of depend on
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
Copy the code
  • Writing configuration Information
spring:
  application:
    name: gateway
  cloud:
    Let the Gateway implement automatic routing and forwarding via NACOS
    gateway:
      discovery:
        locator:
          enabled: true
    nacos:
      discovery:
        server-addr: localhost:8848
        username: nacos
        password: nacos
server:
  port: 9001
Copy the code
  • Start the project

Looking at our gateway service registered in NACOS, we can now access the two previously registered test services through the gateway.

  • Access test service

Erp service 9002 System service 9000

  • A successful response