Zuul是在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架。我们通过Zuul实现动态的路由切换及对服务访问前的一些复杂处理。
目前SpringCloud的版本分为1.x.x和2.xx两种版本。其中1.x.x对应的版本为spring-cloud-starter-zuul,2.x.x版本对应的依赖为spring-cloud-starter-netflix-zuul。由于我的环境用的是2.0.6.RELEASE这个版本,因此需要在pom.xml文件中加入如下依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
增加完成后的整体pom文件内容如下:
<?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.qlys.gateway</groupId>
<artifactId>service-gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>service-gateway</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.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>Finchley.SR1</spring-cloud.version>
</properties>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>
spring-cloud-starter-netflix-eureka-client
</artifactId>
</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>
由于我是基于第四天的Eureka服务做的,因此会有Eureka客户端的依赖和相关的配置,请大家注意。
依赖增加完成后,修改启动类ServiceGatewayApplication,增加启动Zuul的相关注解@EnableZuulProxy。代码如下:
package com.qlys.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
public class ServiceGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceGatewayApplication.class, args);
}
}
代码中的@EnableDiscoveryClient注解为Eureka的客户端注解。
然后修改application.yml配置文件,增加路由的设置,代码如下:
server:
port: 8081
spring:
application:
name: gateway
eureka:
instance:
appname: gateway
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:8761/eureka/
zuul:
routes:
security:
path: /security/**
url: http://localhost:8888/security/
logs:
path: /logs/**
url: http://localhost:9999/logs/
以上配置中,zuul为网关相关的配置。zuul.routes参数接收一个map对象,而且要求这个map对象的key值必须为{name}.path和{name}.url。其中{name}为我们自定义的名称,用于配对path和url组合。如果是application.properties文件的配置,内容应该是下面的格式:
zuul.routes.security.path=/security/**
zuul.routes.security.url=http://localhost:8888/security/
配置完成后即可启动程序进行测试。