zuul具备路由转发和过滤的功能,相似于nginx的功能。首先新建一个项目service-zuul来实现路由转发的功能。java
1.路由转发功能nginx
build.gradle文件web
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文件spring
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
主方法api
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注册中心是这样:app
此时分别访问http://localhost:8798/api-a/hi;eclipse
以及http://localhost:8798/api-b/hi;maven
其响应页面都是:ide
以上结果代表,zuul实现了路由转发的功能,只需加上注解@EnableZuulProxy以及路由转发相关策略便可。spring-boot
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; } }
运行结果比较:
以上结果代表,过滤器已经产生做用了。