Source code address: gitee.com/fighter3/es…

Continuously updated…

Hi, everybody. I’m a bad guy.

In the previous chapter, we have completed the calls between services, unified configuration, and so on. In this chapter, we will introduce an important component of microservices architecture, the gateway.

Summary of the gateway

Why introduce gateways

As we all know, each service invocation on the server side pulls the list of services from the service registry and invokes the corresponding service provider by load balancing policy.

So, how does our client, PC, mobile, etc., access our server without doing anything?

What’s the problem with that?

  • The client needs to maintain the address of the back-end service. If we cluster deployment, a service has tens or hundreds of nodes?

  • Logging, authentication, and so on, we have to have one for each service.

  • The services on the server side have to be accessible to clients, so they need external IP, but IP resources are too precious.

At this time, it is necessary to add a unified entrance between the client and the server, and in the microservice system, the gateway is to assume this role.

We added a gateway as a unified access for requests. We just need to configure the gateway’s machine IP to DNS, or access layer load, so that the client service finally passes through our gateway, and then forwards to the corresponding server service.

Common microservice gateways

There are probably some gateways on the market, depending on the implementation of the technology stack:

A brief description of these gateways:

  • Nginx: Nginx consists of a kernel and modules. The design of the kernel is very small and concise. The work done by the kernel is very simple.
  • Kong: Kong is a highly available, easily extensible API Gateway project written by Mashape based on OpenResty (Nginx + Lua module). Built on NGINX and Apache Cassandra or PostgreSQL, Kong provides easy-to-use RESTful apis to operate and configure API management systems, so it can scale horizontally across multiple Kong servers. Requests are evenly distributed to each Server through pre-configured load balancing configuration to cope with a large number of network requests.
  • **Netfilx Zuul: **Zuul is Netflix’s open source microservices gateway component that works with Eureka, Ribbon, Hystrix, and more. The community is active and integrated into the complete ecology of SpringCloud, which is one of the best models to build the front gateway service of micro-service system.
  • **Spring Cloud GetWay: **Spring Cloud Gateway is a new API Gateway project for Spring Cloud, designed to replace Zuul1. Gateway can be used together with Spring Cloud Discovery Client (such as Eureka), Ribbon, Hystrix and other components to realize routing and forwarding, load balancing, and fusing. Moreover, Gateway also has a built-in flow limiting filter to realize the function of flow limiting.

The Spring Cloud GetWay practice

Now that we’ve briefly covered the various common microservice gateways, we’ll introduce our hero of the day, the SpringCloud Gateway.

Creating a Gateway Service

  • Create a gateway submoduleesop-gateway

  • Introducing dependencies. Note that since the gateway service as a service also requires configuration of the center and registry, we also introduced dependencies
<! --Spring Cloud Alibaba--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>0.22..RELEASE</version> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId>  </dependency> <! --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <! - gateway gateway - > < the dependency > < groupId > org. Springframework. Cloud < / groupId > <artifactId>spring-cloud-starter-gateway</artifactId> </dependency>Copy the code
  • Bootstap. yml: In addition to the application name in the configuration file, we also configure the relevant configuration of Nacos, for those who are not familiar with the previous section.
Spring: application: name: gateway-service # profiles: active: dev # Cloud: nacos: config: enabled:trueIf you don't want to use Nacos for configuration management, set it tofalseCan server - addr:127.0. 01.:8848# Nacos Server address group: DEFAULT_GROUP # group, default DEFAULT_GROUP file-extension: yaml # config content data format, default: properties namespace: Dev_space # specifies the namespace, default ispublic
Copy the code

The routing configuration

We use NACOS to configure the gateway route.

server:
  port: 9000
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0. 01.:8848
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
      - id: user-service
        uri: lb://user-service
        predicates:
          - Path=/user/** filters: - StripPrefix=1Copy the code

Add routes (” routes “); add routes (” routes “);

  • Id:The routingThe onlyIdentifier to distinguish it from other routes
  • Uri: The address to which the request is forwarded, lb refers to the microservice obtained by name from NACOS, following a load balancing policy
  • Predicates:The condition that the route needs to satisfy is also an array (here it isorThe relationship between)
  • Filters: through which requests can be modified as they are passed

In this configuration item, we define requests beginning with user that are distributed to the user-service service.

Now let’s see the effect!

Routing and forwarding effect

Recall that in the user service there is a get request to get the user by ID interface, access:

OK, now instead of accessing the user service interface directly, we can access the gateway service instead. Let’s see what happens:

We visit the address is: http://localhost:9000/user/shop-user/user/get-by-id? Id =1;


At this point, we have introduced the Spring Cloud Gateway as the microservices Gateway and completed the basic routing and forwarding functions.

In addition to basic routing and forwarding, the service gateway can also complete functions such as permission verification, traffic limiting, API verification, etc. We will continue to in-depth, please look forward to it!


“Do simple things repeatedly, do repetitive things carefully, and do serious things creatively!” –

I am three points evil, can call me old three/three minutes/three elder brother/three son, a full stack of literary and military development, let’s see next period!



Reference:

[1] : SpringCloud Cloud Gateway official document

[2] : How to choose the gateway under micro-service

[3] : Small column SpringCloudAlibaba micro-service practice

[4] : SpringCloud Gateway (most comprehensive in history)

[5] : SpringCloud Alibaba micro-service combat ten – service gateway

[6] : Understand micro Services – Gateway of service Gateway