Zuul Server是一个网关应用程序,可处理所有请求并执行微服务应用程序的动态路由。 Zuul服务器也称为边缘服务器。
例如,/api/user
映射到用户服务,/api/products
映射到产品服务,Zuul Server将请求动态路由到相应的后端应用程序。
在本章中,将详细介绍如何在Spring Boot中创建Zuul Server应用程序。
创建Zuul服务器应用程序
Zuul Server捆绑了Spring Cloud依赖项。可以从Spring Initializer页面https://start.spring.io /下载Spring Boot项目,然后选择Zuul Server依赖项。
在主Spring Boot应用程序中添加@EnableZuulProxy
注解。 @EnableZuulProxy
注释用于使Spring Boot应用程序充当Zuul代理服务器。
package com.zaixian.zuulserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableZuulProxy
public class ZuulserverApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulserverApplication.class, args);
}
}
必须在构建配置文件中添加Spring Cloud Starter Zuul依赖项。
Maven用户需要在pom.xml 文件中添加以下依赖项 -
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
对于Gradle用户,请在build.gradle 文件中添加以下依赖项 -
compile('org.springframework.cloud:spring-cloud-starter-zuul')
对于Zuul路由,请在application.properties 文件或application.yml 文件中添加以下属性。
spring.application.name = zuulserver
zuul.routes.products.path = /api/demo/**
zuul.routes.products.url = http://localhost:8080/
server.port = 8111
它表示 http 调用/api/demo/get
转发到产品服务。 例如,/api/demo/products
被转发到/products
。
yaml文件用户可以使用如下所示的 application.yml 文件 -
server:
port: 8111
spring:
application:
name: zuulserver
zuul:
routes:
products:
path: /api/demo/**
url: http://localhost:8080/
注 - 在通过Zuul Proxy进行路由之前,
http://localhost:8080/
应用程序应该已经运行。
完整的构建配置文件如下所示。
Maven用户可以使用下面给出的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
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zaixian</groupId>
<artifactId>zuulserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>zuulserver</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.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.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-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>
Gradle用户可以使用下面给出的build.gradle 文件 -
buildscript {
ext {
springBootVersion = '1.5.9.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
group = 'com.zaixian'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
ext {
springCloudVersion = 'Edgware.RELEASE'
}
dependencies {
compile('org.springframework.cloud:spring-cloud-starter-zuul')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
创建一个可执行的JAR文件,并使用下面给出的Maven或Gradle命令运行Spring Boot应用程序 -
对于Maven,可以使用下面给出的命令 -
mvn clean install
在“BUILD SUCCESS”之后,可以在target
目录下找到JAR文件。
对于Gradle,可以使用下面给出的命令 -
gradle clean build
在“BUILD SUCCESSFUL” 之后,可以在build/libs
目录下找到JAR文件。
现在,使用下面显示的命令运行JAR文件 -
java –jar <JARFILE>
在Tomcat端口8111上找到已启动的应用程序。
现在,在Web浏览器访问URL => http://localhost:8111/api/demo/products
,可看到/products
REST端点的输出,如下所示 -