SpringBoot中使用Zuul

Zuul提供了服務網關的功能,能夠實現負載均衡、反向代理、動態路由、請求轉發等功能。
Zuul大部分功能是經過過濾器實現的,除了標準的四種過濾器類型,還支持自定義過濾器。java

使用@EnableZuulProxy註解,Spring容器初始化時,會將Zuul的相關配置初始化,其中包含一個Spring Boot的Bean:ServletRegistrationBean,該類主要用於註冊Servlet。在Servlet的service方法中,執行各類Zuul過濾器。下圖爲HTTP請求在ZuulServlet中的生命週期。web

Spring Boot Web項目中整合Zuul:spring

1、建立hello源服務項目apache

一、建立項目瀏覽器

開發工具:IntelliJ IDEA 2019.2.3
IDEA中建立一個新的SpringBoot項目,名稱爲「hello-server」,SpringBoot版本選擇2.1.10,在選擇Dependencies(依賴)的界面勾選Web -> Spring Web。app

二、修改啓動類代碼負載均衡

添加一個hello服務maven

package com.example.helloserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class HelloServerApplication {

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

    @GetMapping("/hello/{name}")
    public String hello(@PathVariable String name){
        return "hello " + name;
    }
}

三、修改配置application.yml,指定端口號8090ide

server:
  port: 8090

2、測試路由功能spring-boot

一、建立項目

IDEA中建立一個新的SpringBoot項目,名稱爲「zuul-router」,SpringBoot版本選擇2.1.10,在選擇Dependencies(依賴)的界面勾選Web -> Spring Web,Spring Cloud Routing -> Zuul。
主要添加了spring-boot-starter-web和spring-cloud-starter-netflix-zuul兩個依賴項。
pom.xml完整內容以下:

<?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.1.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>zuul-router</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>zuul-router</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR4</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-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>
View Code

二、修改啓動類代碼

增長註解@EnableZuulProxy

package com.example.zuulrouter;

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

@SpringBootApplication
@EnableZuulProxy
public class ZuulRouterApplication {

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

}
View Code

三、修改配置application.yml

zuul:
  routes:
    test:
      url: http://localhost:8090

加入以上配置後,發送給http://localhost:8080/test的全部請求會被轉發到8090端口。

在瀏覽器訪問http://localhost:8080/test/hello/lc,頁面輸出:hello lc

上面路由配置省略了path,默認狀況下用routeId「test」做爲path。
修改成:

zuul:
  routes:
    test:
      path: /a/**
      url: http://localhost:8090
    b:
      url: https://www.cnblogs.com/gdjlc

如今瀏覽器訪問http://localhost:8080/a/hello/lc,頁面輸出:hello lc  
訪問http://localhost:8080/b,頁面顯示https://www.cnblogs.com/gdjlc的內容

相關文章
相關標籤/搜索