First, what can gateways do for us?

It can do routing and forwarding, black and white lists, filtering requests, and much more

The essence of a gateway is actually a series of filters. I remember when we first learned filters, the doFilter of the servlet, did the DNA of its friends move? Haha

What we’re going to talk about today is using the essence of the gateway, the filter, to do grayscale publishing

Say before, first talk about what is gray release, often hear this noun, is do not know is what

Today, it’s got to be clean for him

Gray release, also known as canary release, is A way to release the version with smooth transition. A/B testing can be carried out on it, that is, some users continue to use product feature A, some users start to use product feature B, if users have no objection to B, then gradually expand the scope. Migrate all users to B.

What are the benefits?

It can ensure the stability of the whole system, and problems can be found and adjusted at the initial gray level to ensure its impact.

What other publishing methods are available besides grayscale publishing?

Blue-green publishing, scrolling publishing, and so on

A brief description of the principle of blue-green publishing:

Three additional servers need to be opened (to host the new service)

Rolling releases:

Replace one of the services with a new service, which is free to load another new service, one by one

Grayscale release:

Start the same service set the version of the configuration file, for example: start 3 same services, one version is V2, the remaining two are V1(version can be dynamically changed using the Eureka registry API)

eureka:
 instance:
  metadata-map:
  # Custom attribute version V1, V2
   version: v2
Copy the code

All ready, back to Zuul gateway, Zuul gateway filters, there are several types: Pre, Route, POST, error

The sequence is pre – > Route – > Post

If you want to customize a gateway filter, inherit ZuulFilter and implement its methods

Use the @Component annotation to be managed by the Spring container

There are four methods: fileType, filterOrder, shouldFilter, and run

  • The fileType method specifies the type of the filter, pre, route, post, error

  • The filterOrder method determines the order of execution, and the smaller the number, the earlier the execution

  • The shouldFilter method determines whether this filter should be executed, true, false not

  • The run method, the execution logic of the filter

Custom gateway filters, such as this grayscale distribution, try to use pre type filters

When making a request, the request header carries the userId(based on the userId to distinguish whether it is accessing the new service).

@override public Object run() throws ZuulException {// obtain RequestContext RequestContext currentContext = RequestContext.getCurrentContext(); / / get it it request = currentContext. GetRequest (); UserId = integer.parseint (request.getheader ("userId")); If (userId >= 5000) {// if (userId >= 5000) { Let him access/experience new service RibbonFilterContextHolder getCurrentContext (). The add (" version ", "v2"); } else {// otherwise, Continue to let the user experience of the old service / / because V1 we launched two load balance can continue to use RibbonFilterContextHolder. GetCurrentContext (). The add (" version ", "V1"); } return null; }Copy the code

RibbonFilter dependency is used here and prepared for you

<dependency> <groupId>io.jmnarloch</groupId> <artifactId>ribbon-discovery-filter-spring-cloud-starter</artifactId> The < version > 2.1.0 < / version > < / dependency >Copy the code

It is also important to note that the gateway may lose your request header data when filtering requests. Yes, the gateway does eat it for you

It is not over-intentional. It wants to ensure security. It will erase some special keys for you, such as Authorization and Cookie

// If the gateway is configured with this, you don't need to fill in anything to prevent the network management from erring the request header parameters.Copy the code

Hey hey, you like can pay attention to my micro channel public number oh, I heard that now pay attention to, later are honorable old powder