springcloud -zuul(1-zuul的简单使用)

1.maven引入包

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

2.配置文件

zuul:
          host:
            socket-timeout-millis: 180000
            connect-timeout-millis: 180000
            max-total-connections: 1000
          #忽略所有服务
          ignoredServices: '*'
          #添加主机头
          addHostHeader: true
          #是否默认支持重试
          retryable: true
          #路由前缀
          prefix: /webmicroservice
          #自定义路由
          routes:
            leaguer:
              path: /leaguer/**
              serviceId: leaguer
              customSensitiveHeaders: true

3.自定义过滤器,都继承zuulFilter,实现其方法

3.1
public class AuthFilter extends ZuulFilter {
                @Override
                public String filterType() {
                    //类型为pre,前置
                    return FilterConstants.PRE_TYPE;
                }

                @Override
                public int filterOrder() {
                    //顺序为数字为6
                    return FilterConstants.PRE_DECORATION_FILTER_ORDER + 1;
                }

                @Override
                public boolean shouldFilter() {
                    //无条件执行
                    return true;
                }

                @Override
                public Object run() {
                    RequestContext context = RequestContext.getCurrentContext();
                    //TODO 实现自己需要执行的逻辑如,鉴定权限相关.....
                    log.info("authFilter----->pre_type----->order=6");
                    return null;
                }
            }
public class LoginPostFilter extends ZuulFilter {
                @Override
                public String filterType() {
                    //类型为post,后置
                    return FilterConstants.POST_TYPE;
                }

                @Override
                public int filterOrder() {
                    //顺序为数字为999
                    return FilterConstants.SEND_RESPONSE_FILTER_ORDER - 1;
                }


                @Override
                public boolean shouldFilter() {
                    //无条件执行
                    return true;
                }

                @Override
                public Object run() {
                    //TODO 实现自己需要执行的逻辑如,登录后做些什么
                    log.info("loginFilter----->post_type----->order=999");
                    return null;
                }
            }
3.2实例化,注入spring容器
@Bean
            public AuthFilter getAuthFilter(){
                return new AuthFilter();
            }

            @Bean
            public LoginPostFilter getLoginPostFilter(){
                return new LoginPostFilter();
            }

4.启动服务且发送请求

1:39:14.266 DEBUG 12920 --- [nio-8116-exec-2] o.s.b.w.f.OrderedRequestContextFilter    : Bound request context to thread: org.apache.catalina.connector.RequestFacade@fa6901d
11:39:14.266 DEBUG 12920 --- [nio-8116-exec-2] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing POST request for [/webmicroservice/leaguer/leaUser/createImge]
11:39:14.266 DEBUG 12920 --- [nio-8116-exec-2] o.s.c.n.zuul.filters.SimpleRouteLocator  : route matched=ZuulRoute{id='leaguer', path='/leaguer/**', serviceId='leaguer', url='null', stripPrefix=true, retryable=null, sensitiveHeaders=[], customSensitiveHeaders=true, }
11:39:14.266 DEBUG 12920 --- [nio-8116-exec-2] o.s.c.n.zuul.web.ZuulHandlerMapping      : Matching patterns for request [/webmicroservice/leaguer/leaUser/createImge] are [/webmicroservice/leaguer/**]
11:39:14.266 DEBUG 12920 --- [nio-8116-exec-2] o.s.c.n.zuul.web.ZuulHandlerMapping      : URI Template variables for request [/webmicroservice/leaguer/leaUser/createImge] are {}
11:39:14.266 DEBUG 12920 --- [nio-8116-exec-2] o.s.c.n.zuul.web.ZuulHandlerMapping      : Mapping [/webmicroservice/leaguer/leaUser/createImge] to HandlerExecutionChain with handler [org.springframework.cloud.netflix.zuul.web.ZuulController@1f78d415] and 1 interceptor
11:39:14.267 DEBUG 12920 --- [nio-8116-exec-2] c.n.zuul.http.HttpServletRequestWrapper  : Path = null
11:39:14.267 DEBUG 12920 --- [nio-8116-exec-2] c.n.zuul.http.HttpServletRequestWrapper  : Transfer-Encoding = null
11:39:14.267 DEBUG 12920 --- [nio-8116-exec-2] c.n.zuul.http.HttpServletRequestWrapper  : Content-Encoding = null
11:39:14.267 DEBUG 12920 --- [nio-8116-exec-2] c.n.zuul.http.HttpServletRequestWrapper  : Content-Length header = 12
11:39:14.267 DEBUG 12920 --- [nio-8116-exec-2] c.n.zuul.http.HttpServletRequestWrapper  : Length of contentData byte array = 12
11:39:14.267 DEBUG 12920 --- [nio-8116-exec-2] o.s.c.n.zuul.filters.SimpleRouteLocator  : Finding route for path: /webmicroservice/leaguer/leaUser/createImge
11:39:14.267 DEBUG 12920 --- [nio-8116-exec-2] o.s.c.n.zuul.filters.SimpleRouteLocator  : servletPath=/
11:39:14.267 DEBUG 12920 --- [nio-8116-exec-2] o.s.c.n.zuul.filters.SimpleRouteLocator  : zuulServletPath=/zuul
11:39:14.267 DEBUG 12920 --- [nio-8116-exec-2] o.s.c.n.zuul.filters.SimpleRouteLocator  : RequestUtils.isDispatcherServletRequest()=true
11:39:14.267 DEBUG 12920 --- [nio-8116-exec-2] o.s.c.n.zuul.filters.SimpleRouteLocator  : RequestUtils.isZuulServletRequest()=false
11:39:14.267 DEBUG 12920 --- [nio-8116-exec-2] o.s.c.n.zuul.filters.SimpleRouteLocator  : adjustedPath=/webmicroservice/leaguer/leaUser/createImge
11:39:14.267 DEBUG 12920 --- [nio-8116-exec-2] o.s.c.n.zuul.filters.SimpleRouteLocator  : Matching pattern:/webmicroservice/leaguer/**
11:39:14.267 DEBUG 12920 --- [nio-8116-exec-2] o.s.c.n.zuul.filters.SimpleRouteLocator  : route matched=ZuulRoute{id='leaguer', path='/leaguer/**', serviceId='leaguer', url='null', stripPrefix=true, retryable=null, sensitiveHeaders=[], customSensitiveHeaders=true, }
11:39:14.268  INFO 12920 --- [nio-8116-exec-2] c.e.springbootzuul.filters.AuthFilter    : authFilter----->pre_type----->order=6
11:39:14.268 DEBUG 12920 --- [nio-8116-exec-2] o.s.b.f.s.DefaultListableBeanFactory     : Returning cached instance of singleton bean 'ribbonLoadBalancingHttpClient'
11:39:14.268 DEBUG 12920 --- [nio-8116-exec-2] o.s.b.f.s.DefaultListableBeanFactory     : Returning cached instance of singleton bean 'ribbonLoadBalancer'
11:39:14.268 DEBUG 12920 --- [nio-8116-exec-2] o.s.b.f.s.DefaultListableBeanFactory     : Returning cached instance of singleton bean 'ribbonClientConfig'
11:39:14.269 DEBUG 12920 --- [nio-8116-exec-2] c.n.loadbalancer.ZoneAwareLoadBalancer   : Zone aware logic disabled or there is only one zone
11:39:14.269 DEBUG 12920 --- [nio-8116-exec-2] c.n.loadbalancer.LoadBalancerContext     : leaguer using LB returned Server: 10.0.126.14:8085 for request /leaUser/createImge
11:39:14.270 DEBUG 12920 --- [nio-8116-exec-2] o.a.h.client.protocol.RequestAuthCache   : Auth cache not set in the context
11:39:14.270 DEBUG 12920 --- [nio-8116-exec-2] h.i.c.PoolingHttpClientConnectionManager : Connection request: [route: {}->http://10.0.126.14:8085][total kept alive: 1; route allocated: 1 of 50; total allocated: 1 of 200]
11:39:14.271 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 << "[read] I/O error: Read timed out"
11:39:14.271 DEBUG 12920 --- [nio-8116-exec-2] h.i.c.PoolingHttpClientConnectionManager : Connection leased: [id: 0][route: {}->http://10.0.126.14:8085][total kept alive: 0; route allocated: 1 of 50; total allocated: 1 of 200]
11:39:14.271 DEBUG 12920 --- [nio-8116-exec-2] h.i.c.DefaultManagedHttpClientConnection : http-outgoing-0: set socket timeout to 0
11:39:14.271 DEBUG 12920 --- [nio-8116-exec-2] h.i.c.DefaultManagedHttpClientConnection : http-outgoing-0: set socket timeout to 1000
11:39:14.271 DEBUG 12920 --- [nio-8116-exec-2] o.a.http.impl.execchain.MainClientExec   : Executing request POST /leaUser/createImge HTTP/1.1
11:39:14.271 DEBUG 12920 --- [nio-8116-exec-2] o.a.http.impl.execchain.MainClientExec   : Target auth state: UNCHALLENGED
11:39:14.271 DEBUG 12920 --- [nio-8116-exec-2] o.a.http.impl.execchain.MainClientExec   : Proxy auth state: UNCHALLENGED
11:39:14.271 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.headers                  : http-outgoing-0 >> POST /leaUser/createImge HTTP/1.1
11:39:14.271 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.headers                  : http-outgoing-0 >> accept-language: zh-CN,zh;q=0.9
11:39:14.271 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.headers                  : http-outgoing-0 >> referer: http://localhost:9200/vip/login
11:39:14.271 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.headers                  : http-outgoing-0 >> content-type: application/x-www-form-urlencoded
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.headers                  : http-outgoing-0 >> user-agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.headers                  : http-outgoing-0 >> origin: http://localhost:9200
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.headers                  : http-outgoing-0 >> accept: application/json, text/plain, */*
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.headers                  : http-outgoing-0 >> x-forwarded-host: localhost:9200
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.headers                  : http-outgoing-0 >> x-forwarded-proto: http
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.headers                  : http-outgoing-0 >> x-forwarded-prefix: /webmicroservice/leaguer
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.headers                  : http-outgoing-0 >> host: localhost:9200
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.headers                  : http-outgoing-0 >> x-forwarded-port: 9200
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.headers                  : http-outgoing-0 >> x-forwarded-for: 10.0.126.14
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.headers                  : http-outgoing-0 >> Accept-Encoding: gzip
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.headers                  : http-outgoing-0 >> Content-Length: 12
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.headers                  : http-outgoing-0 >> Connection: Keep-Alive
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 >> "POST /leaUser/createImge HTTP/1.1[\r][\n]"
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 >> "accept-language: zh-CN,zh;q=0.9[\r][\n]"
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 >> "referer: http://localhost:9200/vip/login[\r][\n]"
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 >> "content-type: application/x-www-form-urlencoded[\r][\n]"
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 >> "user-agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36[\r][\n]"
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 >> "origin: http://localhost:9200[\r][\n]"
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 >> "accept: application/json, text/plain, */*[\r][\n]"
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 >> "x-forwarded-host: localhost:9200[\r][\n]"
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 >> "x-forwarded-proto: http[\r][\n]"
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 >> "x-forwarded-prefix: /webmicroservice/leaguer[\r][\n]"
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 >> "host: localhost:9200[\r][\n]"
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 >> "x-forwarded-port: 9200[\r][\n]"
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 >> "x-forwarded-for: 10.0.126.14[\r][\n]"
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 >> "Accept-Encoding: gzip[\r][\n]"
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 >> "Content-Length: 12[\r][\n]"
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 >> "[\r][\n]"
11:39:14.272 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 >> "param=e30%3D"
11:39:14.330 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 << "HTTP/1.1 200 OK[\r][\n]"
11:39:14.330 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 << "Connection: keep-alive[\r][\n]"
11:39:14.330 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 << "Transfer-Encoding: chunked[\r][\n]"
11:39:14.330 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 << "Content-Type: application/json;charset=UTF-8[\r][\n]"
11:39:14.330 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 << "X-Application-Context: leaguer[\r][\n]"
11:39:14.330 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 << "Date: Wed, 03 Jul 2019 03:39:14 GMT[\r][\n]"
11:39:14.330 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 << "[\r][\n]"
11:39:14.330 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 << "045d[\r][\n]"
11:39:14.330 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 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MS05N2NmLTBiZThiMzc2NjQ0NCJ9LCJnZW5lcmFsQnVzaW5lc3NFeGNlcHRpb24iOm51bGx9"}[\r][\n]"
11:39:14.330 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.headers                  : http-outgoing-0 << HTTP/1.1 200 OK
11:39:14.330 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.headers                  : http-outgoing-0 << Connection: keep-alive
11:39:14.330 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.headers                  : http-outgoing-0 << Transfer-Encoding: chunked
11:39:14.330 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.headers                  : http-outgoing-0 << Content-Type: application/json;charset=UTF-8
11:39:14.330 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.headers                  : http-outgoing-0 << X-Application-Context: leaguer
11:39:14.333 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.headers                  : http-outgoing-0 << Date: Wed, 03 Jul 2019 03:39:14 GMT
11:39:14.333 DEBUG 12920 --- [nio-8116-exec-2] o.a.http.impl.execchain.MainClientExec   : Connection can be kept alive indefinitely
11:39:14.334  INFO 12920 --- [nio-8116-exec-2] c.e.s.filters.LoginPostFilter            : loginFilter----->post_type----->order=999
11:39:14.334 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 << "0[\r][\n]"
11:39:14.334 DEBUG 12920 --- [nio-8116-exec-2] org.apache.http.wire                     : http-outgoing-0 << "[\r][\n]"
11:39:14.334 DEBUG 12920 --- [nio-8116-exec-2] h.i.c.PoolingHttpClientConnectionManager : Connection [id: 0][route: {}->http://10.0.126.14:8085] can be kept alive indefinitely
11:39:14.334 DEBUG 12920 --- [nio-8116-exec-2] h.i.c.DefaultManagedHttpClientConnection : http-outgoing-0: set socket timeout to 0
11:39:14.334 DEBUG 12920 --- [nio-8116-exec-2] h.i.c.PoolingHttpClientConnectionManager : Connection released: [id: 0][route: {}->http://10.0.126.14:8085][total kept alive: 1; route allocated: 1 of 50; total allocated: 1 of 200]
11:39:14.335 DEBUG 12920 --- [nio-8116-exec-2] o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
11:39:14.335 DEBUG 12920 --- [nio-8116-exec-2] o.s.web.servlet.DispatcherServlet        : Successfully completed request    

 

转载于:https://www.cnblogs.com/lantuanqing/p/11139555.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值