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="" xmlns:xsi=""
        <relativePath/> <!-- lookup parent from repository -->
    <description>Demo project for Spring Boot</description>




2. Add configuration

application. Add the following configuration to the YML file:

  port: 8085  #Service port
    serviceUrl: #Registered address of Registration Center
    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;

public class ZuulProxyApplication {

    public static void main(String[] args) {, 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

  routes: #Configure service routing
      path: /provider/**
      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:

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

Because the prefix / proxy is added, the prefix is required when accessing:

5. Filter sensitive header information

application. Add sensitive headers configuration to YML:

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

6. Add host header information

application. Add add host header configuration in YML:

  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:


(2) Add configuration

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

        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