一、zuul和nginx都是网关吗?
. Zuul is a JVM based router and server side load balancer by Netflix.
二、网关使⽤场景
• 监控( Monitoring)
nginx accesslog,监控请求的状态,参数,返回值,url,body的content-length,都可以写入日志。
• 测试( Testing)
压力测试:权重压力测试,nginx
• 动态路由( Dynamic Routing)
zuul的常用功能
• 服务整合( Service Integration)
• 负荷减配( Load Shedding)
客户端的服务短路其实就是一种负荷减配,放弃一些请求。
• 安全( Security)
Auth2
• 静态资源处理( Static Resources handling)
委派给当前服务器做的
• 活跃流量管理( Active traffic management)
三、数据来源
与ribbon结合:ribbon的数据来源有两种方式:
1,白名单的方式
2,服务发现
• 服务发现
与eureka结合
• 服务注册
与eureka结合
• 通讯⽅式
• 协议:⼆进制、本⽂
• ⽅式:同步、异步
四、服务接⼝
• 平台⽆关
• XML、 JSON、 HTML
• 平台相关
• IDL(可能是二进制可能是文本)、 RMI、 Hession
五、spring cloud zuul
1,增加依赖
org.springframework.cloud
spring-cloud-starter-netflix-zuul
2,annotation具有传递性
@SpringCloudApplication = @SpringBootApplication+@EnableDiscoveryClient+@EnableCircuitBreaker
@EnableZuulProxy = @EnableCircuitBreaker+ZuulProxyMarkerConfiguration
3,application.properties
spring.application.name = zuul-proxy
management.security.enabled = false
server.port = 8084
##路由规则配置在zuul-config.properties
####配置ribbon白名单,访问localhost:8084/user-service/* ->localhost:8081/*
provider.service.host = localhost
provider.service.port = 8081
#user-service-provider.ribbon.listOfServers = \
# http://${provider.service.host}:${provider.service.port}
###注册到eureka
#eureka.server.hostname = localhost
#eureka.server.port = 8083
#eureka.client.enabled = true;
#eureka.client.serviceUrl.defaultZone=\
# http://{eureka.server.hostname}:{eureka.server.port}/eureka
六、zuul里面引了ribbon和Hystrix
• 路路由设置
• 配置模式
• 服务-映射:zuul.routes.{url-pattern}
• 路路径模式
• 当前层级匹配:/*
• 递归层级匹配:/**
七、HTTP 客户端
• HttpClient(默认)
• 装配: HttpClientRibbonConfiguration
注意,这里不是自动装配。
• OkHttp(条件)
• 激活配置: ribbon.okhttp.enabled = true
• 装配: OkHttpRibbonConfiguration
配置HttpClient客户端:可以切换HttpClient来提高性能
实际是配置Ribbon底层HTTP调用客户端,并发zuul独享此功能。
http,json,xml也是一种RPC。是一种特殊的RPC
八、
• 端点( Endpoint)
• 实现: RoutesEndpoint
• 路径: /routes
• 过滤器: ZuulFilter
ZuulFilter不是传统的servlet的filter,它是自己的一个组件.
filter type :pre,route,post,error
设计模式是责任链模式。
ZuulFilter调用链:
ZuulServletFilter是servlet filter,按照servlet filter的规范,调用dofilter()
ZuulServlet是servlet,调用service()
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
try {
this.init((HttpServletRequest)servletRequest, (HttpServletResponse)servletResponse);
RequestContext context = RequestContext.getCurrentContext();
context.setZuulEngineRan();
try {
this.preRoute();
} catch (ZuulException var12) {
this.error(var12);
this.postRoute();
return;
}
try {
this.route();
} catch (ZuulException var13) {
this.error(var13);
this.postRoute();
return;
}
try {
this.postRoute();
} catch (ZuulException var11) {
this.error(var11);
}
} catch (Throwable var14) {
this.error(new ZuulException(var14, 500, "UNHANDLED_EXCEPTION_" + var14.getClass().getName()));
} finally {
RequestContext.getCurrentContext().unset();
}
}
ZuulServle#service()->
ZuulServlet#preRoute()
ZuulServlet#routing()
ZuulServlet#postRoute()->
FilterProcessor#preRoute()
FilterProcessor#route()
FilterProcessor#postRoute()->
FilterProcessor#runFilters(String sType)->FilterProcessor#processZuulFilter()->ZuulFilter#runFilter()->ZuulFilter#run()
九、spring cloud 整合
1,启动顺序:
eureka-server
user-service-provider(数据源先启动)
config-server(配置源后启动)
user-service-client
zuul-proxy
十、zuul 注册到 eureka
(1)激活服务发现
(2)在config-server中配置zuul-config.properties
##指定user-service-provider
zuul.routes.user-service-provider = /user-service/**
zuul.routes.user-service-client =/user-client/**
(3)zuul-proxy作为配置客户端,
1,增加config-client依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
2,配置bootstrap.properties
spring.config.client.name = zuul-config
##关联profile
spring.cloud.config.profile = default
spring.cloud.config.label = master
###激活config server 服务发现,通过服务发现service去发现(这里的实现是Eureka,也可以是consul等)
spring.cloud.config.discovery.enabled=true
##配置config-server的applicationname,否则eureka不知道去找谁
spring.cloud.config.discovery.serviceId = config-server
##因为bootstrap比application先加载,所以它先获取eureka的地址,所以配在bootstrap这里,application就不需要配置了
eureka.server.hostname = localhost
eureka.server.port = 8083
eureka.client.serviceUrl.defaultZone = \
http://${eureka.server.hostname}:${eureka.server.port}/eureka
在config-server/src/main/resources/configs下执行
git init
git add .
git commit
十一、
所有你用zuul.routes.${serviceId}配置的信息都是属于静态信息staticServices。
十二、
网友评论