目录

Zuul代理服务器和路由(Zuul Proxy Server and Routing)

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依赖项。

创建Zuul服务器应用程序

在主Spring Boot应用程序中添加@EnableZuulProxy批注。 @EnableZuulProxy注释用于使Spring Boot应用程序充当Zuul代理服务器。

package com.iowiki.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/

Note - 在通过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.iowiki</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.iowiki'
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”之后,您可以在目标目录下找到JAR文件。

对于Gradle,您可以使用下面给出的命令 -

gradle clean build

在“BUILD SUCCESSFUL”之后,您可以在build/libs目录下找到JAR文件。

现在,使用下面显示的命令运行JAR文件 -

java –jar <JARFILE> 

您可以在Tomcat端口8111上找到已启动的应用程序,如下所示。

在Tomcat Port_8111上启动应用程序

现在,在Web浏览器中点击URL http://localhost:8111/api/demo/products ,您可以看到/products REST Endpoint的输出,如下所示 -

产品REST端点
↑回到顶部↑
WIKI教程 @2018