Spring Cloud implements several filters in Zuul's routing phase, which determine how routing works.
-
Simple Routing (SimpleHostRoutingFilter)
After the filter runs, it will forward all HTTP requests to the "origin server". The configuration of the simple route is as follows:
#zuul routing configuration
zuul:
routes:
#represents the http://localhost:9100/person/speaks address, routed to http://localhost:8080/speaks
person:
path:/person/**
url:http://localhost:8080
The configuration access http://localhost:9200/person/speak will forward the request to http://localhost:8080/speak . To simplify the configuration, the path attribute can be omitted. The simplified configuration is as follows:
#zuul routing configuration
zuul:
routes:
#represents the http://localhost:9100/person/speaks address, routed to http://localhost:8080/speaks
person:
url:http://localhost:8080
The value of url must start with http: and https: strings, otherwise simple routing will not be triggered.
Simple routing uses HttpClient for forwarding. The filter converts the relevant data of HttpServletRequest (HTTP methods, parameters, request headers, etc.) into the request entity of HttpClient, and then uses CloseableHttpClient for forwarding. In order to ensure performance, the connection pool function of HttpClient is used. , so when using simple routing, you can configure the properties of the HttpClient connection pool:
- zuul.host.maxTotalConnections: The maximum number of connections to the target host, the default value is 200. Configuring this property is equivalent to calling the setMaxTotal method of PoolingHttpClientConnectionManager.
-
zuul.host.maxPerRouteConnections: The initial number of connections per host, the default value is 20. Configuring this property is equivalent to calling the setDefaultMaxPerRoute method of PoolingHttpClientConnectionManager.
-
Jump routing (SendForwardFilter)
When jumping the route, when the external access to the gateway's A address, it will jump to the B address. The filter that handles the jump is SendForwardFilter. The configuration of the jump route is as follows:
#zuul routing configuration
zuul:
routes:
testRoute:
path:/test/**
url:forward:/hello
Indicates that when accessing the http://localhost:9100/test address, it will automatically jump to the http://localhost:9100/hello address. Note that this is not a client-side jump, and the address change cannot be seen on the browser. It can only be verified based on the returned information, and it cannot cross domain names. It can only be jumped at the current site. To verify the jump route, a simple REST service must be created, 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)
publicStringhello(@PathVariable("name")Stringname){
return"hello"+name;
}
}
The implementation of jump routing is relatively simple. In fact, the forward method of RequestDispatcher is called to jump.
-
Ribbon Routing (RibbonRoutingFilter)
When the gateway is registered to the Eureka server as an Eureka client, the request can be forwarded to the cluster service by configuring the serviceId. With the following configuration, the Ribbon route filter can be executed:
#zuul routing configuration
zuul:
routes:
#Represents the http://localhost:9100/hello/speaks address, routed to http://HELLOWORLD-PROVIDER/speaks, where the specific address is obtained through eureka
hello:
path:/hello/**
#Or use url:HELLOWORLD-PROVIDER with the same meaning
serviceId:HELLOWORLD-PROVIDER
Like simple routes, serviceId can also be omitted. When omitted, routeId will be used as serviceId. The simplified configuration is as follows:
#zuul routing configuration
zuul:
routes:
#Represents the 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 the configuration item configured with serviceId or url is not a simple routing format (not starting with http: or https:), nor is it a jump routing format (starting with forward:), then the ribbon routing filter will be executed, and the configuration is as follows:
#zuul routing configuration
zuul:
routes:
#Represents the http://localhost:9100/hello/speaks address, routed to http://HELLOWORLD-PROVIDER/speaks, where the specific address is obtained through eureka
hello:
path:/hello/**
#Or use url:HELLOWORLD-PROVIDER with the same meaning
url:HELLOWORLD-PROVIDER
If there are more than a dozen back-end services, it is very troublesome to configure each one like this. Spring cloud zuul has already made the default configuration for us. By default, Zuul will proxy all microservices registered to Eureka Server, and Zuul's routing rules are as follows:
http://ZUUL_HOST:PORT/serviceId/**
If you want one or more services not to be routed, you can use the zuul.ignoredServices property to configure. If you set '*', it means that all services are ignored. The configuration is as follows:
zuul:
#不被路由的serviceId,多个使用逗号分割
ignored-services:HELLOWORLD-PROVIDER
-
忽略路由
除了使用 zuul.ignoredServices 来忽略路由服务外,还可以使用 zuul.ignoredPatterns 来设置不进行路由的 Url,配置如下:
#zuul路由配置
zuul:
routes:
#表示http://localhost:9100/hello/speaks地址,路由到http://HELLOWORLD-PROVIDER/speaks,其中具体地址是通过eureka获取
hello:
path:/hello/**
#或者使用url:HELLOWORLD-PROVIDER含义一样
serviceId:HELLOWORLD-PROVIDER
ignored-patterns:/hello/noRoute
访问/hello会被路由到HELLOWORLD-PROVIDER服务,但是/hello/noRoute不会被路由。
请求头配置
在集群的服务间共享请求头并没有什么问题,但是如果请求会被转发到其他系统,那么对于敏感的请求头信息,就需要进行处理。在默认情况下,HTTP 请求头的 Cookie、SetCookie、Authorization 属性不会传递到"源服务",可以使用 sensitiveHeaders 属性来配置敏感请求头,下面的配置全局生效:
#zuul路由配置
zuul:
#HTTP请求头不进行转发
sensitive-headers:Cookie,Set-Cookie,Authorization
如果只希望针对一个路由生效,可以在路由下进行配置,如下:
#zuul路由配置
zuul:
routes:
#表示http://localhost:9100/hello/speaks地址,路由到http://HELLOWORLD-PROVIDER/speaks,其中具体地址是通过eureka获取
hello:
path:/hello/**
#或者使用url:HELLOWORLD-PROVIDER含义一样
serviceId:HELLOWORLD-PROVIDER
#HTTP请求头不进行转发
sensitive-headers:Cookie,Set-Cookie,Authorization
如果每一个路由都需要配置一些额外的敏感Header时,那你可以通过 zuul.ignoredHeaders 来统一设置需要忽略的HTTP请求头,默认情况下,是没有配置的,如果项目中引入了Spring Security,那么Spring Security会自动加上这个配置,默认值为: Pragma、Cache-Control、X-Frame-Options、X-Content-Type-Options、X-XSS-Protection、Expries。