Integration of springcloud components and Zuul implementation of service gateway

Posted by pacmon on Sat, 19 Feb 2022 04:57:32 +0100

1, What is Zuul?

Zuul is an open source API Gateway server of netflix. It is essentially a web servlet application.
The core of Zuul is a series of filters, which can be compared with the Filter of Servlet framework or AOP.
Zuul can realize the following functions by loading the dynamic filtering mechanism:
Verification and security:
Identify the verification requirements for various resources and reject those requests that do not meet the requirements.
Review and monitoring:
Track meaningful data and statistical results at the edge position, so as to bring us accurate production status conclusions.
Dynamic routing:
Dynamically route requests to different back-end clusters as needed.
Pressure test:
Gradually increase the load traffic to the cluster to calculate the performance level.
Load distribution:
Allocate the corresponding capacity for each load type and discard requests that exceed the limit.
Static response processing:
Part of the response is directly established at the edge to prevent it from flowing into the internal cluster.
Multi area elasticity:
Request routing across AWS area aims to diversify the use of ELB and ensure that the edge position is as close to the user as possible.

2, Create project implementation service gateway

(1) Project description

The project is in Integration of spring cloud components Feign for load balancing The project feign created in this paper is modified on the basis of feign.
There are three module s under the original project: Eureka server, provider and consumer.
This paper creates a new module: zuul proxy to implement the service gateway.
Project directory structure:

(2) Create module zuul proxy


1. Add dependency

Add dependencies. If you have followed the screenshot, you need to POM Add zuul dependency to the XML file. The complete contents are as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.chaoyue</groupId>
    <artifactId>zuul-proxy</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>zuul-proxy</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR12</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2. Add configuration

application. Add the following configuration to the YML file:

server:
  port: 8085  #Service port
eureka:
  client:
    serviceUrl: #Registered address of Registration Center
      defaultZone: http://127.0.0.1:8080/eureka/
spring:
  application:
    name: zuul-proxy #Service name

3. Add comments to the startup class

Add annotation @ EnableEurekaClient and @ EnableZuulProxy to startup class

package com.chaoyue.zuulproxy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class ZuulProxyApplication {

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

}

4. Start service

Start Eureka server, provider, consumer and zuul proxy services, and enter: http://localhost:8080/

(3) Function example

1. Configure routing rules

(1) Add configuration

application. Add configuration in YML to route / provider / * * requests to server provider service and / consumer / * * requests to service consumer service

zuul:
  routes: #Configure service routing
    service-provider:
      path: /provider/**
    service-consumer:
      path: /consumer/**

(2) Run test

Post startup access http://localhost:8085/provider/sayHi And http://localhost:8085/consumer/sayHi

2. Default routing rules

Zuul and Eureka can be used together to realize automatic route configuration without adding configuration in the configuration file. The automatically configured route takes the service name as the matching path and input http://localhost:8085/service-provider/sayHi and http://localhost:8085/service-consumer/sayHi can access:

3. Load balancing

Start two provider services (service ports 8081 and 8083) and call them multiple times http://localhost:8085/service-provider/sayHi, it can be found that the display information is switched between 8081 and 8083.

4. Configure access prefix

application. Add prefix configuration to YML:

zuul:
  routes: #Configure service routing
    service-provider:
      path: /provider/**
    service-consumer:
      path: /consumer/**
  prefix: /proxy  #Gateway routing prefix

Because the prefix / proxy is added, the prefix is required when accessing:
http://localhost:8085/proxy/service-provider/sayHi

5. Filter sensitive header information

application. Add sensitive headers configuration to YML:

zuul:
  sensitive-headers: Cookie,Set-Cookie,Authorization  #Filter sensitive request header information

6. Add host header information

application. Add add host header configuration in YML:

zuul:
  add-host-header: true  #Add host header information

7. View routing information

(1) Add dependency

pom. Add spring boot starter actuator dependency to the XML file:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

(2) Add configuration

application. Add the configuration in the YML file and enable the endpoint to view the route:

management:
  endpoints:
    web:
      exposure:
        include: routes

(3) View basic routing information

visit: http://localhost:8085/actuator/routes

(4) View detailed routing information

visit: http://localhost:8085/actuator/routes/details

8. Filter

To be continued, see the follow-up blog.

Topics: Java Spring Boot Spring Cloud eureka zuul