ZUUL source code analysis <1

1. Turn on the ZUUL support logo

Joining zuul is generally as follows

@EnableZuulProxy
@SpringCloudApplication
public class Application {
  
  public static void main(String[] args) {
    new SpringApplicationBuilder(Application.class).web(true).run(args);
  }
  
}

 

Among them, @EnableZuulProxy is to enable the function support of zuul.

Track into EnableZuulProxy, the source code is as follows

@EnableCircuitBreaker
@EnableDiscoveryClient
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(ZuulProxyMarkerConfiguration.class)
public @interface EnableZuulProxy {
}

 

In addition to introducing discoveryClient to register eureka, the main thing is to introduce ZuulProxyMarkerConfiguration, @Import(ZuulProxyMarkerConfiguration.class), ZuulProxyMarkerConfiguration creates a new Marker bean return.

as follows:

@Configuration
public class ZuulProxyMarkerConfiguration {
	@Bean
	public Marker zuulProxyMarkerBean() {
		return new Marker();
	}

	class Marker {
	}
}

 

The following zuul configuration will make the configuration take effect according to whether there is this maker object

@ConditionalOnBean(ZuulServerMarkerConfiguration.Marker.class)

 

 

 

Second, zuul initialization configuration

For more initialization specific configuration, zuul uses the SPI method for

SPI entry

All registrations are managed with SPI, spring.factories are as follows:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration,\
org.springframework.cloud.netflix.feign.ribbon.FeignRibbonClientAutoConfiguration,\
org.springframework.cloud.netflix.feign.FeignAutoConfiguration,\
org.springframework.cloud.netflix.feign.encoding.FeignAcceptGzipEncodingAutoConfiguration,\
org.springframework.cloud.netflix.feign.encoding.FeignContentGzipEncodingAutoConfiguration,\
org.springframework.cloud.netflix.hystrix.HystrixAutoConfiguration,\
org.springframework.cloud.netflix.hystrix.security.HystrixSecurityAutoConfiguration,\
org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration,\
org.springframework.cloud.netflix.rx.RxJavaAutoConfiguration,\
org.springframework.cloud.netflix.metrics.servo.ServoMetricsAutoConfiguration,\
org.springframework.cloud.netflix.zuul.ZuulServerAutoConfiguration,\
org.springframework.cloud.netflix.zuul.ZuulProxyAutoConfiguration
org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker=\
org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerConfiguration
org.springframework.boot.env.EnvironmentPostProcessor=\
org.springframework.cloud.netflix.metrics.ServoEnvironmentPostProcessor

Among them, the initialization related to zuul is mainly

org.springframework.cloud.netflix.zuul.ZuulServerAutoConfiguration

and

org.springframework.cloud.netflix.zuul.ZuulProxyAutoConfiguration

 

1. ZuulServerAutoConfiguration

@Configuration
@EnableConfigurationProperties({ ZuulProperties.class })
@ConditionalOnClass(ZuulServlet.class)
@ConditionalOnBean(ZuulServerMarkerConfiguration.Marker.class)   //这里用到上面的@EnableZuulProxy的引入做判断

// Make sure to get the ServerProperties from the same place as a normal web app would
@Import(ServerPropertiesAutoConfiguration.class)

public class ZuulServerAutoConfiguration {

@Autowired
protected ZuulProperties zuulProperties;    
@Autowired
protected ServerProperties server;   

@Autowired(required = false)
private ErrorController errorController;

... ...

 

attribute related

ZuulProperties zuulProperties; // The configuration in yml, the relevant configuration at the beginning of zuul is injected here

ServerProperties server; // The configuration in yml, the relevant configuration at the beginning of zuul is injected here

ErrorController errorController; //Controller for handling errors, custom implementation

 

Several important beans:

ZuulController takes over all requests, and hands all requests to zuulServlet for processing, all requests will go to zuulcontroler first and then go to zuulServlet

ZuulRefreshListener listens for heartbeat messages by listening to spring Context publishing mechanism events

 

Several important Filters:

Sort by order from smallest to largest

ServletDetectionFilter -- PRE -3. Determine whether to go spring MVC or go directly to zuulservlet, and assign IS_DISPATCHER_SERVLET_REQUEST_KEY

Servlet30RequestWrapper -PRE -2. 把RequestServlet转换Servlet30RequestWrapper

FormBodyWrapperFilter -PRE -1. Encapsulated as FormBodyRequestWrapper

DebugFilter -PRE 1. Used to dynamically enable debug logs, zuul.debug.request=true enables debug mode

Also several core flter are registered at org.springframework.cloud.netflix.zuul.ZuulProxyAutoConfiguration

PreDecorationFilter PRE 5, PreDecorationFilter add header, serviceId information processing, etc.

 

2, ZuulProxyAutoConfiguration

@Configuration
@Import({ RibbonCommandFactoryConfiguration.RestClientRibbonConfiguration.class,
		RibbonCommandFactoryConfiguration.OkHttpRibbonConfiguration.class,
		RibbonCommandFactoryConfiguration.HttpClientRibbonConfiguration.class })
@ConditionalOnBean(ZuulProxyMarkerConfiguration.Marker.class)
public class ZuulProxyAutoConfiguration extends ZuulServerAutoConfiguration {

	@SuppressWarnings("rawtypes")
	@Autowired(required = false)
	private List<RibbonRequestCustomizer> requestCustomizers = Collections.emptyList();

	@Autowired
	private DiscoveryClient discovery;

	@Autowired
	private ServiceRouteMapper serviceRouteMapper;

 

Import Ribbon related classes

attribute related

DiscoveryClient discovery; // eureka client, eureka related operations, such as: get the list of all machines of the instance according to serviceId

 

Important Filter

PreDecorationFilter // where the request header, serviceId and other information are processed

RibbonRoutingFilter. //Real filter for forwarding, which encapsulates the place where the nitfilx family bucket

SimpleHostRoutingFilter // Forward requests without eureka

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324981451&siteId=291194637