Zuul of Spring Cloud Gateway

Microservices have mutual calls between services, and client requests will also request different services. If you want to count the website pv, uv and other data at this time, it will become very complicated. If there is a unified request entry, the request will be processed Forwarding will greatly simplify the business. For this reason, spring cloud zuul came into being.
Zuul is Netflix's open source microservice gateway, through which request filtering, forwarding, identity authentication, unified traffic entry, etc. can be completed. According to the configuration, zuul can forward the corresponding request to the corresponding service, and at the same time, zuul is also registered in eureka as a client.
Create a zuul service spring-cloud-zuul
pom.xml content is 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>spring-cloud-zuul</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-cloud-zuul</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Edgware.SR2</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-config</artifactId>
        </dependency>
        -->
        <!-- eureka服务发现客户端依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <!-- zuul依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-start-webflux</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>

Add the @EnableZuulServer @EnableDiscoveryClient annotation to the SpringCloudZuulApplication automatic class, @EnableZuulServer indicates that this function is a zuul server.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulServer;

@SpringBootApplication
@EnableZuulServer
@EnableDiscoveryClient
public class SpringCloudZuulApplication {

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

Configuration file:

#应用名称
spring.application.name=spring-cloud-zuul
#应用端口号,默认为8080
server.port=8889

#配置中心
#如果需要读取配置中心的配置,添加此配置项
#spring.cloud.config.discovery.enabled=true
#spring.cloud.config.discovery.service-id=spring-cloud-zuul

#eureka服务发现相关
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port}

#api路由配置
zuul.routes.api-a.path=/api-a/**
zuul.routes.api-a.service-id=service-A
zuul.routes.api-a.sensitive-headers=*
zuul.routes.api-b.path=/api-b/**
zuul.routes.api-b.service-id=service-B

Note: If you need to read the configuration of the configuration center, use bootstrap.yml or bootstrap.properties for the configuration file name, because bootstrap* is loaded before application*.
According to the routing configuration, the request of http://ip:port/api-a/ will be forwarded to the service-A service, and service-A is the instance name of the service registered to the eureka registration center. Similarly, requests for http://ip:port/api-b/ will be forwarded to the service-B service.
If you want to do some business processing under the zuul gateway, such as special character filtering, identity authentication, etc., you can integrate ZuulFilter.

import com.netflix.zuul.ZuulFilter;
import org.springframework.stereotype.Component;

@Component
public class RequestFilter extends ZuulFilter {
    
    

    /**
     * 过滤器类型
     * pre:路由之前;routing:路由之时;post:路由之后;error:发送错误调用;
     * @return
     */
    @Override
    public String filterType() {
        return "pre";
    }

    /**
     * 过滤的顺序
     * @return
     */
    @Override
    public int filterOrder() {
        return 0;
    }

    /**
     *可写逻辑判断,是否过滤,true代表永久过滤
     * @return
     */
    @Override
    public boolean shouldFilter() {
        return false;
    }

    /**
     * 过滤器的具体逻辑
     * @return
     */
    @Override
    public Object run() {
        return null;
    }
}

Guess you like

Origin blog.csdn.net/x1172031988/article/details/80045168