Spring Cloud cognitive learning: the use of Zuul gateway

Keywords: Java Spring network snapshot Jetty

catalog

💡 The previous article introduced a new component, hystrix. Hystrix is a fuse, which can be used to solve the problem of service fuse and service degradation sent in microservice call. Spring Cloud cognitive learning (4): the use of fuse Hystrix

💡 This article introduces a new component Zuul, which is a gateway component. It receives Api requests uniformly. Based on the gateway, we can process all requests, log records, request authorization and other operations.

zuul


  • zuul can be used as a micro service gateway. The so-called gateway, in fact, means that the service consumer accesses the service consumer through the gateway, just as we all leave the request to the gateway in the network environment, and then the gateway requests outward.
  • zuul is a member of Netfilx OSS, and can work well with other Netfilx OSS brothers, such as Eureka, ribbon, and hystrix.
  • In order to cope with zuul's update problem, the spring team also developed a gateway component, Spring Cloud Gateway. But as I said before, on the one hand, learning this is to understand a variety of knowledge, on the other hand, to avoid the need to take over zuul project or change zuul project to create a gateway without zuul knowledge. Zuul is not cool yet, is it

effect:

💡 The gateway is used to hide the specific service URL. When the API interface is called externally, the gateway URL is used to avoid the disclosure of sensitive information of the service.
💡 Since all requests are through the gateway, identity authentication and authority authentication can be done in the gateway.
💡 Unified logging can be done in the gateway.
💡 It can do unified monitoring processing in the gateway.
💡 It can be combined with eureka, ribbon, etc. to achieve load balancing of requests.


Simple example:

The following code can be referred to zuul integrated use experiment

0. Create module

0. Create a new module spring-cloud-zuul-10001

1. Import dependency:

    <dependencies>
        <!--Introduce public dependency package start-->
        <dependency>
            <groupId>com.progor.study</groupId>
            <artifactId>spring-cloud-common-data</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--Introduce public dependency package end-->
        <!--Import zuul-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
        <!--Import eureka-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--Import actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--Import hystrix-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <!--Import web relevant-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

2. Add notes to the main program:

@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient // You need to add eureka, because he also needs to pull the service list
public class SpringCloudZuul10001Application {

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

}

3. Configure application.yml:

server:
  port: 10001

spring:
  application:
    name: spring-cloud-zuul

# For load balancing and pull services, you need to configure eureka
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka
  instance:
    instance-id: zuul-10001.com
    prefer-ip-address: true

# Configure zuul
zuul:
  routes: # Configure routing
    UserService.serviceId: USERSERIVE # service name
    UserService.path: /myuser/**  # What is used to map locally? For example, / user/list will map to / myuser/user/list locally

4. test

🔴 Start spring-cloud-user-service-8001, spring-cloud-zuul-10001, spring-cloud-eureka-server-7001,
🔴 Visit http://localhost:10001/myuser/user/list, you will find that the effect is the same as that of calling http://localhost:8001/user/list, so the above configuration is successful. We call the 8001 service through Zul's 10001 as a gateway, so later consumers can call the gateway interface to indirectly call the service interface.


Configuration syntax:

route

zuul:
  routes: # Configure routing
    UserService.serviceId: USERSERIVE # serviceId is used for service name. The UserService. Prefix is used for group mapping and can be a custom prefix.
    UserService.path: /myuser/**  # What is used to map locally? For example, / user/list will map to / myuser/user/list locally

It can also be:

zuul:
  routes: # Configure routing
    UserService:
      serviceId: USERSERIVE # service name
      path: /myuser/**  # What is used to map locally? For example, / user/list will map to / myuser/user/list locally
    MessageService:
      serviceId: MESSAGESERIVE # service name
      path: /mymsg/**  #

💡 Ignored services: by default, zuul will image all services that can be pulled. When there is a MessageService in eureka, but we have not configured it, you can also access the MessageService by visiting http: / / localhost: 10001 / MessageService / MSG / list, because the default mapping is http://ip:port / service name. If you don't need the default mapping, just use the mapping you configured manually, you need to configure zuul. Ignored services = '*'.
💡 Prefix: used to specify a global prefix, for example, / mymsg / * *. If you have configured zuul.prefix=/api, you need to call / api/mymsg / * * later to access the interface of messageservice.

# Configure zuul
zuul:
  prefix: /api
  routes: # Configure routing
    UserService:
      serviceId: USERSERIVE # service name
      path: /myuser/**  # What is used to map locally? For example, / user/list will map to / myuser/user/list locally
  ignored-services: '*'

Add:

  • As a gateway, you can do a lot of functions in the gateway layer. More content will be explained in a separate chapter zuul. Here is just a simple explanation of the most basic role of a gateway.

Posted by BradZynda on Fri, 15 May 2020 00:58:16 -0700