Spring Boot 简明教程
Spring Boot - Zuul Proxy Server and Routing
Zuul 服务器是一个网关应用程序,处理所有请求并对微服务应用程序进行动态路由。Zuul 服务器也被称为边缘服务器。
Zuul Server is a gateway application that handles all the requests and does the dynamic routing of microservice applications. The Zuul Server is also known as Edge Server.
例如, /api/user 映射到用户服务,/api/products 映射到产品服务,Zuul 服务器将动态地将请求路由到各自的后端应用程序。
For Example, /api/user is mapped to the user service and /api/products is mapped to the product service and Zuul Server dynamically routes the requests to the respective backend application.
在本章中,我们将详细地了解如何在 Spring Boot 中创建 Zuul 服务器应用程序。
In this chapter, we are going to see in detail how to create Zuul Server application in Spring Boot.
Creating Zuul Server Application
Zuul 服务器与 Spring Cloud 依赖项捆绑在一起。你可以从 Spring Initializer 页面 https://start.spring.io/ 下载 Spring Boot 项目,并选择 Zuul 服务器依赖项。
The Zuul Server is bundled with Spring Cloud dependency. You can download the Spring Boot project from Spring Initializer page https://start.spring.io/ and choose the Zuul Server dependency.
在你的 Spring Boot 主应用程序中添加 @EnableZuulProxy 注释。@EnableZuulProxy 注释用于让你的 Spring Boot 应用程序充当 Zuul 代理服务器。
Add the @EnableZuulProxy annotation on your main Spring Boot application. The @EnableZuulProxy annotation is used to make your Spring Boot application act as a Zuul Proxy server.
package com.tutorialspoint.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 依赖项。
You will have to add the Spring Cloud Starter Zuul dependency in our build configuration file.
Maven 用户必须在你的 pom.xml 文件中添加以下依赖项:
Maven users will have to add the following dependency in your pom.xml file −
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
对于 Gradle 用户,在你的 build.gradle 文件中添加以下依赖项:
For Gradle users, add the below dependency in your build.gradle file
compile('org.springframework.cloud:spring-cloud-starter-zuul')
对于 Zuul 路由,在你的 application.properties 文件或 application.yml 文件中添加以下属性。
For Zuul routing, add the below properties in your application.properties file or application.yml file.
spring.application.name = zuulserver
zuul.routes.products.path = /api/demo/**
zuul.routes.products.url = http://localhost:8080/
server.port = 8111
这意味着对 /api/demo/ 的 http 调用被转发到产品服务。例如, /api/demo/products 被转发到 /products 。
This means that http calls to /api/demo/ get forwarded to the products service. For example, /api/demo/products is forwarded to /products.
yaml 文件用户可以使用下面所示的 application.yml 文件:
yaml file users can use the application.yml file shown below −
server:
port: 8111
spring:
application:
name: zuulserver
zuul:
routes:
products:
path: /api/demo/**
url: http://localhost:8080/
Note - 在通过 Zuul 代理路由之前, http://localhost:8080/ 应用程序应该已经运行。
Note − The http://localhost:8080/ application should already be running before routing via Zuul Proxy.
完整的构建配置文件如下所示。
The complete build configuration file is given below.
Maven 用户可以使用下面给出的 pom.xml 文件:
Maven users can use the pom.xml file given below −
<?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.tutorialspoint</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 文件,如下所示 −
Gradle users can use the build.gradle file given below −
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.tutorialspoint'
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 应用程序:
You can create an executable JAR file, and run the Spring Boot application by using the Maven or Gradle commands given below −
对于 Maven,你可以使用下面给出的命令 −
For Maven, you can use the command given below −
mvn clean install
“BUILD SUCCESS”之后,您可以在目标目录中找到 JAR 文件。
After “BUILD SUCCESS”, you can find the JAR file under the target directory.
对于 Gradle,你可以使用下面给出的命令:
For Gradle, you can use the command given below −
gradle clean build
“BUILD SUCCESSFUL”之后,您可以在 build/libs 目录中找到 JAR 文件。
After “BUILD SUCCESSFUL”, you can find the JAR file under the build/libs directory.
现在,使用如下所示的命令运行 JAR 文件:
Now, run the JAR file by using the command shown below −
java –jar <JARFILE>
你可以在此处看到应用程序已经启动,端口为 Tomcat 端口 8111。
You can find the application has started on the Tomcat port 8111 as shown here.
现在,在你的网页浏览器中点击 URL http://localhost:8111/api/demo/products ,你可以看到 /products REST 端点的输出,如下所示:
Now, hit the URL http://localhost:8111/api/demo/products in your web browser and you can see the output of /products REST Endpoint as shown below −