什么是zuul
在微服务架构中,如何把拆分好的大量微服务服务对外提供统一的访问接口,从而将APP的具体实现细节对使用透明化是一个共同的挑战,Netflix zuul可以完美的解决这个问题。可以把它看做是微服务架构的门户,把请求代理到对应的后台应用,能够使浏览器、手机APP、或者是应用接口访问分布在不同主机的服务而不用考虑跨域访问问题( CORS)。zuul能够和其他的Netflix组件结合使用,如Hystrix一起提供熔断机制,利用Eureka做服务发现,也能够用来做路由过滤,负载均衡等。
应用
1. API网关模式
特点
- 通过增加API网关使得客户端和具体的实现隔离开来
- 客户端不用关心具体的服务部署的位置
- 可以把合并逻辑放在API网关层来减少客户端复杂性
- 引入网关层增加了系统的复杂性
- 相比直接访问,因为追加了一层路由跳跃,响应时间会增加
简单路由
zuul:
routes:
get:
path: /portal/**
url: http://portal
links:
path: /bd/**
url: http://bdip
images:
path: /sd/**
url: http://sd
ribbon:
eureka:
enabled: false
上面的配置中包含三个命名路由,每个都包含一个path和一个url,其中path定义了在zull server中匹配的路径,path定义了所代理的服务的URL。
追加负载均衡
因为spring cloud zuul已经和客户端负载均衡组件ribbon,所以可以很方便的实现集成,我们只需要把它们启用就可以了。
zuul:
routes:
portal:
path: /**
serviceId: portal
portal:
ribbon:
listOfServers: http://portal1.http://portal2
ribbon:
eureka:
enabled: false
利用服务发现
zuul:
routes:
portal:
path: /portal/**
serviceId: portal
eureka:
client:
serviceUrl:
defaultZone: ${EUREKA_URL:http://user:password@localhost:5000}/eureka/
其中portal就是真正的服务在eureka中的名字
2. 动态路由
zuul内置framework支持动态的read, compile, and run filters,
private void initGroovyFilterManager() {
FilterLoader.getInstance().setCompiler(new GroovyCompiler());
String scriptRoot = System.getProperty("zuul.filter.root", "");
if (scriptRoot.length() > 0) scriptRoot = scriptRoot + File.separator;
try {
FilterFileManager.setFilenameFilter(new GroovyFileFilter());
FilterFileManager.init(5, scriptRoot + "pre", scriptRoot + "route", scriptRoot + "post");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
package filters.pre
import com.netflix.zuul.ZuulFilter
import com.netflix.zuul.context.RequestContext
/**
* @author mhawthorne
*/
class PreDecorationFilter extends ZuulFilter {
@Override
int filterOrder() {
return 5
}
@Override
String filterType() {
return "pre"
}
@Override
boolean shouldFilter() {
return true;
}
@Override
Object run() {
RequestContext ctx = RequestContext.getCurrentContext()
// sets origin
ctx.setRouteHost(new URL("http://httpbin.org"));
// sets custom header to send to the origin
ctx.addOriginResponseHeader("cache-control", "max-age=3600");
}
}
3. zuul filter
4.配置重试:
@EnableZuulProxy->ZuulProxyConfiguration->HttpClientRibbonConfiguration(默认)->HttpClientRibbonCommandFactory创建HttpClientRibbonCommand(利用SpringClientFactory创建RetryableRibbonLoadBalancingHttpClient(调用execute方法是委托给RetryTemplate,若是发生Hystrix times out,则直接返回500,所以The hystrix timeout needs to be slightly greater than the aggregate timeouts of the original request and all retries(hsytrix timeout 需要比 原始请求时间*单个server的总请求次数*总请求server个数 的结果要长,总的请求次数为1+最大重试次数:MaxAutoRetries,总请求server个数为1+最大请求server个数:MaxAutoRetriesNextServer);在executeWithRetry方法中调用loadBalancedRetryPolicyFactory.create方法创建重试策略LoadBalancedRetryPolicy,此类的registerThrowable方法中,当同一个instance重试的次数达到最大值,并且能重试下一个instance时(nextServerCount<=设定的最大重试次数,如果成立LoadBalancedRetryPolicy类重新调用RetryableRibbonLoadBalancingHttpClient.choose()方法,委托调用ZoneAwareLoadBalancer.chooseServer(serverId)方法,在不分zone的时候,调用BaseLoadBalancer.chooseServer(serverId),之后委托调用PredicateBasedRule.choose(key),采用RoundRobin的方法获取下一个实例;否则调用LoadBalancedRetryContext.setExhaustedOnly()方法将RetryContextSupport中的terminate设置成true,retryTemplate退出doExecute的while循环,整个重试结束),配置信息如下
spring:
application:
name: zuul-gateway
server:
port: 8989
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
leaseRenewalIntervalInSeconds: 10
metadataMap:
instanceId: ${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${server.port}}}
cluster: MAIN
zuul:
routes:
mcsas_bd:
path: /UserServer/**
serviceId: UserServer
retryable: true
management:
security:
enabled: false
ribbon:
MaxAutoRetriesNextServer: 1
MaxAutoRetries: 1
ReadTimeout: 3000
OkToRetryOnAllOperations: true #false to only allow get to retry
hystrix:
command:
mcsas_bd:
execution:
isolation:
strategy: THREAD
thread:
timeoutInMilliseconds: 13000
5.配置信息对应的类ZuulProperties,例如修改zuulServlet对应的path,可以在application.yml 文件中设置 zuul.servletPath=/
- 大小: 26.5 KB
- 大小: 13.7 KB
分享到:
相关推荐
springcloud zuul网关服务,可直接运行,实现了网关的过滤,以及多重认证功能
Springcloud Zuul config eureka ribbon实例 ,用于入门学习
Zuul的规则引擎允许通过任何JVM语言来编写规则和过滤器, 支持基于Java和Groovy的构建。(spring cloud zuul整合例子包能运行)
SpringCloud zuul 组件的jar包,可以用作联调SpringCloud测试使用。
springcloud zuul gateway 服务网关 http://knight-black-bob.iteye.com/blog/2357683
spring cloud 客户端和spring cloud zuul ,和前面的两个是一个系列,先打开cloud service 的服务,然后打开 product ,在打开客户端,最后可以打开zuul 可以实现eurake的负载均衡,zuul的负载均衡。
springcloud zuul 网关开发实践,模拟了在Spring Cloud微服务系统中,客户端的请求首先经过负载均衡(zuul、Ngnix),再到达服务网关(zuul集群),然后再到具体的服的实现过程。
06Spring Cloud Zuul:API网关服务1
Spring Cloud Zuul示例, 支持服务自动发现,注册中心为Consul
《深入理解Spring Cloud与微服务构建》学习笔记(十六)~路由网关Spring Cloud Zuul
springcloud zuul
《深入理解Spring Cloud与微服务构建》学习笔记(十七)~路由网关Spring Cloud Zuul~负载均衡
基于Spring Cloud Zuul的动态路由实现demo,实现动态配置,实时刷新
第六章 SpringCloud Zuul网关.pdf
《深入理解Spring Cloud与微服务构建》学习笔记(十八)~路由网关Spring Cloud Zuul~熔断器
使用SpringCloud Zuul Proxy构建API网关.docx
主要介绍了SpringCloud Zuul网关功能实现解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
资源用springcloud搭的一个微服务框架,涉及Zuul知识点,适合初学者理解微服务,大神绕过。