Spring Cloud implements several filters in the routing phase of Zuul, which determine how the routing works.
- Simple Routing (simplehostroutingfilter)
After the filter is run, all HTTP requests are forwarded to the "source server", and the simple route is configured as follows:
#zuul路由配置
Zuul:
Routes
#表示http://localhost:9100/person/speaks address, routing to Http://localhost:8080/speaks
Person
Path: /person/**
URL: http://localhost:8080
The configuration access Http://localhost:9200/person/speak forwards the request to http://localhost:8080/speak, and in order to configure simplification, the path attribute can be omitted, simplifying the configuration as follows:
#zuul路由配置
Zuul:
Routes
#表示http://localhost:9100/person/speaks address, routing to Http://localhost:8080/speaks
Person
URL: http://localhost:8080
The value of the URL must begin with the http: and https: strings, or the simple route will not be triggered.
Simple routing uses HttpClient for forwarding, which converts the HttpServletRequest related data (HTTP method, parameter, request prime) to the HttpClient request entity, and then uses closeablehttpclient To ensure performance, the HttpClient connection pooling feature is used, so you can configure the properties of the HttpClient connection pool when using simple routing:
- Zuul.host.maxTotalConnections: The maximum number of connections to the target host, the default value of 200. This property is configured fairly with the Setmaxtotal method that called Poolinghttpclientconnectionmanager.
- Zuul.host.maxPerRouteConnections: The initial number of connections per host, the default value of 20. This property is configured fairly with the Setdefaultmaxperroute method that called Poolinghttpclientconnectionmanager.
??
- Jump Route (sendforwardfilter)
Jump route when the external access to the gateway's a address, will jump to the B address, processing the jump filter for Sendforwardfilter, the configuration of the jump route is as follows:
#zuul路由配置
Zuul:
Routes
Testroute:
Path: /test/**
URL: Forward:/hello
Indicates that access to the Http://localhost:9100/test address will automatically jump to the Http://localhost:9100/hello address, note that this is not a client-side jump, the browser can not see the address changes, only based on the return information to verify, And can not cross the domain name, only at the current site to jump, in verifying the jump route must also create a simple REST service, as follows:
Package Org.lixue.zuul;
??
Import org.springframework.web.bind.annotation. pathvariable;
Import org.springframework.web.bind.annotation. requestmapping;
Import Org.springframework.web.bind.annotation.RequestMethod;
Import org.springframework.web.bind.annotation. Restcontroller;
??
@RestController
Public class helloworldcontroller{
@RequestMapping(value="/hello/{name}", Method=requestmethod. GET )
Public Stringhello (@PathVariable("name") stringname) {
return"Hello" +name;
}
}
Jump routing implementation is relatively simple, actually called the RequestDispatcher forward method to jump.
??
- Ribbon Routing (ribbonroutingfilter)
when the gateway acts as When the Eureka client registers with the Eureka server, the Ribbon route filter can be executed by configuring the ServiceId to forward the request to the cluster's service, using the following configuration :
#zuul路由配置
Zuul:
Routes
#表示http://localhost:9100/hello/speaks address, routed to Http://HELLOWORLD-PROVIDER/speaks, where the specific address is obtained through Eureka
Hello
Path: /hello/**
#或者使用url: Helloworld-provider means the same
serviceId: Helloworld-provider
As with simple routes, serviceId can also be omitted, and when omitted, the Routeid is used as the serviceId, which simplifies configuration as follows:
#zuul路由配置
Zuul:
Routes
#表示http://localhost:9100/hello/speaks address, routed to Http://HELLOWORLD-PROVIDER/speaks, where the specific address is obtained through Eureka
Helloworld-provider:
Path: /hello/**
If a configuration item configured with ServiceId or a URL is not a simple routing format (not at http: or https:), nor is it a jump routing format (forward:), then the Ribbon route filter is executed with the following configuration:
#zuul路由配置
Zuul:
Routes
#表示http://localhost:9100/hello/speaks address, routed to Http://HELLOWORLD-PROVIDER/speaks, where the specific address is obtained through Eureka
Hello
Path: /hello/**
#或者使用url: Helloworld-provider means the same
URL: Helloworld-provider
??
If there are up to more than 10 back-end services, each of these configurations can be cumbersome, and spring cloud Zuul has already done the default configuration for us, by default, Zuul proxies all the microservices registered to Eureka Server, and the Zuul routing rules are as follows:
http://zuul_host:port/serviceid/**
If you want one or more services to be not routed, you can use the Zuul.ignoredservices property to configure it, and if setting ' * ' means that all services are ignored, configure the following:
Zuul:
#不被路由的serviceId, multiple partitions using commas
ignored-services: Helloworld-provider
??
- Ignore Route
In addition to using zuul.ignoredservices to ignore the routing service, you can also use Zuul.ignoredpatterns to set URLs that are not routed, configured as follows:
#zuul路由配置
Zuul:
Routes
#表示http://localhost:9100/hello/speaks address, routed to Http://HELLOWORLD-PROVIDER/speaks, where the specific address is obtained through Eureka
Hello
Path: /hello/**
#或者使用url: Helloworld-provider means the same
serviceId: Helloworld-provider
Ignored-patterns: /hello/noroute
??
Access/hello will be routed to the Helloworld-provider service, but/hello/noroute will not be routed.
??
Request Header Configuration
There is no problem sharing the request header between the services of the cluster, but if the request is forwarded to another system, the sensitive request header information needs to be processed. By default, the Cookie, Setcookie, Authorization property of the HTTP request header is not passed to the source service, and you can use the Sensitiveheaders property to configure the sensitive request header, which is globally valid for the following configuration:
#zuul路由配置
Zuul:
#HTTP请求头不进行转发
sensitive-headers: cookie,set-cookie,authorization
If you only want to take effect on one route, you can configure it under routing, as follows:
#zuul路由配置
Zuul:
Routes
#表示http://localhost:9100/hello/speaks address, routed to Http://HELLOWORLD-PROVIDER/speaks, where the specific address is obtained through Eureka
Hello
Path: /hello/**
#或者使用url: Helloworld-provider means the same
serviceId: Helloworld-provider
#HTTP请求头不进行转发
sensitive-headers: cookie,set-cookie,authorization
??
If you need to configure a few additional sensitive headers for each route, you can set the HTTP request header to be ignored by zuul.ignoredheaders, which is not configured by default, if Spring Security is introduced into the project , Spring Security automatically adds this configuration, with the default values: Pragma, Cache-control, X-frame-options, x-content-type-options , X-xss-protection, Expries.
??
??
Spring Cloud (DALSTON.SR5)--zuul Gateway-routing configuration