Back to Javatutorial

Zuul

docs/Spring全家桶/SpringCloud/SpringCloudZuul.md

1.0.07.4 KB
Original Source

Spring Cloud Zuul Spring Cloud Netflix Ŀĺ֮һΪ΢ܹеAPIʹãֶ֧̬·˹ܣĽ÷ϸܡ

Zuul

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>

application.ymlн

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ùܣעʾ¡

·ɹ

Ĭ·ɹ

ؾ⹦

ε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 #·ǰ׺

Header˼ضHost

  • Zuul·ʱĬϻ˵һЩеͷϢÿԷֹ·ʱCookieAuthorizationĶʧ

    zuul: sensitive-headers: Cookie,Set-Cookie,Authorization #ùеͷϢΪվͲ

  • Zuul·ʱhostͷϢÿԽ

    zuul: add-host-header: true #Ϊtrueضǻhostͷ

鿴·Ϣ

ǿͨSpringBoot Actuator鿴Zuulе·Ϣ

·ZuulĹܣ·ɹܸⲿתķʵȥʵͳһڵĻ˹ܸ̽жĴУ˼ۺϵĻ

Zuul¼ֵ͵Ĺ͡

  • pre·ɵĿǰִУȨУ顢ӡ־ȹܣ
  • routing·ɵĿʱִУʹApache HttpClientNetflix RibbonͷԭʼHTTPĵط
  • post·ɵĿִУĿӦͷϢռͳݵȹܣ
  • error׶ηʱִС

ͼһHTTP󵽴APIغڸֲͬ͵ĹתĹ̡

Զ

Զһʾ¹á

PreLogFilter̳ZuulFilter

һǰù·ɵĿǰӡ־

/**
 * 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