docs/Spring全家桶/SpringCloud/SpringCloudZuul.md
Spring Cloud Zuul Spring Cloud Netflix Ŀĺ֮һΪܹеAPIʹãֶ֧̬·˹ܣĽ÷ϸܡ
APIΪܹеķṩͳһķڣͻͨAPIططAPIصĶģʽеģʽ൱ܹе棬пͻ˵ķʶͨ·ɼˡʵ·ɡؾ⡢Уˡݴۺϵȹܡ
#һzuul-proxyģ
Ǵһzuul-proxyģʾzuulijùܡ
#pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
spring-cloud-starter-netflix-eureka-client
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
spring-cloud-starter-netflix-zuul
</dependency>
server:
port: 8801
spring:
application:
name: zuul-proxy
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8001/eureka/
#@EnableZuulProxyעZuulAPIع
@EnableZuulProxy
@EnableDiscoveryClient
@SpringBootApplication
public class ZuulProxyApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulProxyApplication.class, args);
}
}
ͨeureka-serveruser-servicefeign-servicezuul-proxyʾZuulijùܣעʾ¡
ǿͨapplication.ymlе·ɹǽƥ/userService/·ɵuser-serviceȥƥ/feignService/·ɵfeign-serviceȥ
zuul: routes: #· user-service: path: /userService/** feign-service: path: /feignService/**
http://localhost:8801/userService/user/1open in new windowԷ·ɵuser-serviceˣ
http://localhost:8801/feignService/user/1open in new windowԷ·ɵfeign-serviceˡ
ZuulEurekaʹãʵ·ɵԶãԶõ·ԷΪƥ·൱ã
zuul: routes: #· user-service: path: /user-service/** feign-service: path: /feign-service/**
http://localhost:8801/user-service/user/1open in new windowͬ·ɵuser-serviceˣ
http://localhost:8801/feign-service/user/1open in new windowͬ·ɵfeign-serviceˡ
ʹĬϵ·ɹĬ·ã
zuul: ignored-services: user-service,feign-service #رĬ·
εhttp://localhost:8801/user-service/user/1open in new windowвԣԷ82018202user-serviceӡϢ
2019-10-05 10:31:58.738 INFO 11520 --- [nio-8202-exec-5] c.macro.cloud.controller.UserController : idȡûϢûΪmacro
2019-10-05 10:32:00.356 INFO 11520 --- [nio-8202-exec-6] c.macro.cloud.controller.UserController : idȡûϢûΪmacro
ǿͨ·ǰ˴/proxyǰҪhttp://localhost:8801/proxy/user-service/user/1open in new windowܷʵuser-serviceеĽӿڡ
zuul:
prefix: /proxy #·ǰ
Zuul·ʱĬϻ˵һЩеͷϢÿԷֹ·ʱCookieAuthorizationĶʧ
zuul: sensitive-headers: Cookie,Set-Cookie,Authorization #ùеͷϢΪվͲ
Zuul·ʱhostͷϢÿԽ
zuul: add-host-header: true #Ϊtrueضǻhostͷ
ǿͨSpringBoot Actuator鿴Zuulе·Ϣ
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> spring-boot-starter-actuator </dependency>application.yamlļ鿴·ɵĶ˵㣺
management: endpoints: web: exposure: include: 'routes'
ͨhttp://localhost:8801/actuator/routesopen in new window鿴·Ϣ
ͨhttp://localhost:8801/actuator/routes/detailsopen in new window鿴ϸ·Ϣ
·ZuulĹܣ·ɹܸⲿתķʵȥʵͳһڵĻ˹ܸ̽жĴУ˼ۺϵĻ
Zuul¼ֵ͵Ĺ͡
ͼһHTTPAPIغڸֲͬ͵ĹתĹ̡
Զһʾ¹á
һǰù·ɵĿǰӡ־
/**
* Created by macro on 2019/9/9.
*/
@Component
public class PreLogFilter extends ZuulFilter {
private Logger LOGGER = LoggerFactory.getLogger(this.getClass());
/**
* ͣpreroutingposterror֡
*/
@Override
public String filterType() {
return "pre";
}
/**
* ִ˳ֵԽСȼԽߡ
*/
@Override
public int filterOrder() {
return 1;
}
/**
* Ƿйˣtrueִйˡ
*/
@Override
public boolean shouldFilter() {
return true;
}
/**
* ԶĹshouldFilter()trueʱִС
*/
@Override
public Object run() throws ZuulException {
RequestContext requestContext = RequestContext.getCurrentContext();
HttpServletRequest request = requestContext.getRequest();
String host = request.getRemoteHost();
String method = request.getMethod();
String uri = request.getRequestURI();
LOGGER.info("Remote host:{},method:{},uri:{}", host, method, uri);
return null;
}
}
ӹǷhttp://localhost:8801/user-service/user/1open in new window£ӡ־
2019-10-05 15:13:10.232 INFO 11040 --- [nio-8801-exec-7] com.macro.cloud.filter.PreLogFilter : Remote host:0:0:0:0:0:0:0:1,method:GET,uri:/user-service/user/1
ȼ
ServletDetectionFilter pre -3 ǰͨDispatcherServletеĻZuulServletдġ
Servlet30WrapperFilter pre -2 ԭʼHttpServletRequestаװ
FormBodyWrapperFilter pre -1 Content-TypeΪapplication/x-www-form-urlencodedmultipart/form-dataװFormBodyRequestWrapper
DebugFilter route 1 zuul.debug.requestǷӡdebug־
PreDecorationFilter route 5 ԵǰԤԱִк
RibbonRoutingFilter route 10 ͨRibbonHystrixʵзء
SimpleHostRoutingFilter route 100 ֻrouteHostĽдֱʹHttpClientrouteHostӦַת
SendForwardFilter route 500 ֻforward.toĽдбת
SendErrorFilter post 0 ڲ쳣ʱĻдӦ
SendResponseFilter post 1000 ĵӦϢ֯ɹӦݡ
ǿԶԹнõãøʽ£
zuul: filterClassName: filter: disable: true
ǽPreLogFilterʾã
zuul: PreLogFilter: pre: disable: true
#RibbonHystrix֧
ZuulԶRibbonHystrixZuulиؾͷݴǿͨRibbonHystrixZuulеӦܡ
ʹHystrix��תʱHystrixCommandִгʱʱ䣺
hystrix: command: #ڿHystrixCommandΪ default: execution: isolation: thread: timeoutInMilliseconds: 1000 #HystrixCommandִеijʱʱ䣬ִгʱз
ʹRibbon·תʱӼijʱʱ䣺
ribbon: #ȫ ConnectTimeout: 1000 #ӳʱʱ䣨룩 ReadTimeout: 3000 #ʱʱ䣨룩
zuul:
routes: #·
user-service:
path: /userService/**
feign-service:
path: /feignService/**
ignored-services: user-service,feign-service #رĬ·
prefix: /proxy #·ǰ
sensitive-headers: Cookie,Set-Cookie,Authorization #ùеͷϢΪվͲ
add-host-header: true #Ϊtrueضǻhostͷ
retryable: true # رԻ
PreLogFilter:
pre:
disable: false #Ƿù
#ʹõģ
springcloud-learning
eureka-server -- eurekaע
user-service -- ṩUserCRUDӿڵķ
feign-service -- feignòԷ
zuul-proxy -- zuulΪصIJԷ
#ĿԴַ
https://github.com/macrozheng/springcloud-learning
https://lijunyi.xyz/docs/SpringCloud/SpringCloud.html#_2-2-x-%E5%88%86%E6%94%AF https://mp.weixin.qq.com/s/2jeovmj77O9Ux96v3A0NtA https://juejin.cn/post/6931922457741770760 https://github.com/D2C-Cai/herring http://c.biancheng.net/springcloud