Zuul is the front door for all requests from devices and web sites to the backend of the Netflix streaming application. As an edge service application, Zuul is built to enable dynamic routing, monitoring, resiliency and security. It also has the ability to route requests to multiple Amazon Auto Scaling Groups as appropriate.
Zuul 是在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架。Zuul 相当于是设备和 Netflix 流应用的 Web 网站后端所有请求的前门。
https://github.com/andyChenHuaYing/spring-cloud-demo
通过tag切换git tag -d v1.0,若想修改,可根据此tag创建新的分支。
与Spring Cloud 之服务发现与调用-Ribbon#2.3 eureka-server-singleton 没有任何区别
与Spring Cloud 之服务发现与调用-Ribbon#2.4 eureka-service 没有任何区别
与五:Spring Cloud 之服务发现与调用-Feign#2.5 eureka-service-feign没有任何区别
与四:Spring Cloud 之服务发现与调用-Ribbon#2.5 eureka-service-ribbon 没有任何区别
spring-cloud-starter-netflix-eureka-client
和spring-cloud-starter-netflix-zuul
依赖/api-ribbon/**
请求转发到eureka-service-ribbon
处理/api-feign/**
请求转发到eureka-serivce-feign
处理@EnableZuulProxy
ZuulFilter
的Filter,并指定filterType实现请求过滤
spring-cloud-finchley-demo
org.oscar.scd
1.0.0
4.0.0
eureka-service-zuul
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-zuul
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8768
spring:
application:
name: eureka-serivce-zuul
zuul:
routes:
api-ribbon:
path: /api-ribbon/**
serviceId: eureka-service-ribbon
api-feign:
path: /api-feign/**
serviceId: eureka-serivce-feign
package org.oscar.scd.eureka.service.ribbon.zuul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@EnableZuulProxy
@EnableEurekaClient
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaServiceZuulApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServiceZuulApplication.class, args);
}
}
使用了lombok插件,IDEA需要安装一下lombok插件。见补充部分
package org.oscar.scd.eureka.service.ribbon.zuul.filter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
@Slf4j
@Service
public class PreFilter extends ZuulFilter {
@Override
public String filterType() {
return "pre";
}
@Override
public int filterOrder() {
return 0;
}
@Override
public boolean shouldFilter() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
return "yes".equals(request.getParameter("shouldFilter"));
}
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
String token = request.getParameter("token");
log.info("Token:{}", token);
return null;
}
}
与四:Spring Cloud 之服务发现与调用-Ribbon#3.1.1 EurekaServerSingletonApplication 完全一致
与四:Spring Cloud 之服务发现与调用-Ribbon#3.1.2 EurekaServiceApplication-8762 完全一致
与四:Spring Cloud 之服务发现与调用-Ribbon#3.1.3 EurekaServiceApplication-8763完全一致
最简单的方式添加一个SpringBoot启动类型的启动类就行。
最简单的方式添加一个SpringBoot启动类型的启动类就行。
###3.1.6 EurekaServiceZuulApplication
最简单的方式添加一个SpringBoot启动类型的启动类就行。
EurekaServerSingletonApplication
EurekaServiceApplication-8762
EurekaServiceApplication-8763
EurekaServiceFeignApplication
EurekaServiceRibbonApplication
EurekaServiceZuulApplication
访问:http://localhost:8768/api-ribbon/ribbon/print?name=oscar&shouldFilter=no
访问:http://localhost:8768/api-ribbon/ribbon/print?name=oscar&shouldFilter=yes