2019独角兽企业重金招聘Python工程师标准>>>
zuul具有路由转发和过滤的功能,类似于nginx的功能。首先新建一个项目service-zuul来实现路由转发的功能。
1.路由转发功能
build.gradle文件
buildscript {
ext {
springBootVersion = '2.0.4.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
ext {
springCloudVersion = 'Finchley.SR1'
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
compile('org.springframework.cloud:spring-cloud-starter-netflix-zuul')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
application.yml文件
server:
port: 8798
spring:
application:
name: service-zuul
eureka:
client:
service-url:
defaultZone: http://localhost:8791/eureka/
zuul:
routes:
api-a:
path: /api-a/*
serviceId: service-ribbon
api-b:
path: /api-b/*
serviceId: service-feign
主方法
package com.example.servicezuul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication
public class ServiceZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceZuulApplication.class, args);
}
}
启动该项目,以及service-ribbon,service-feign项目,以及eureka-server项目,以及eureka-client-say-hi项目,此时的eureka注册中心是这样:
此时分别访问http://localhost:8798/api-a/hi;
以及http://localhost:8798/api-b/hi;
其响应页面都是:
以上结果表明,zuul实现了路由转发的功能,只需加上注解@EnableZuulProxy以及路由转发相关策略即可。
2.过滤功能
在上面项目的基础上添加一个自定义的过滤组件
MyFilter.java
package com.example.servicezuul;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@Component
public class MyFilter extends ZuulFilter {
@Override
public String filterType() { //过滤器类型,一共有4种不同周期的类型,pre:路由之前;routing:路由之时;post:代表路由之后;error:错误时调用
return "pre";
}
@Override
public int filterOrder() { //过滤器使用顺序
return 0;
}
@Override
public boolean shouldFilter() { //是否允许过滤,为true,即始终过滤
return true;
}
@Override
public Object run() throws ZuulException { //过滤器的具体逻辑,以下的内容大致是,有test参数允许访问,没有test参数不允许访问
RequestContext requestContext = RequestContext.getCurrentContext();
HttpServletRequest request = requestContext.getRequest();
Object o = request.getParameter("test");
if (o == null) {
requestContext.setSendZuulResponse(false);
requestContext.setResponseStatusCode(401);
try {
requestContext.getResponse().getWriter().write("test is empty");
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
运行结果比较:
以上结果表明,过滤器已经产生作用了。