Implement grayscale publishing based on Spring Cloud ribbon

Source: Internet
Author: User
Tags extend

In the previous article, "Multi-versioning based on the spring Cloud Ribbon", describes how to extend the Spring cloud Ribbon to implement Interface multi-versioning project –fm-cloud-bambbo, the process of developing this project has given me a lot of ideas and ideas , found that as long as some expansion, you can achieve grayscale management, and then have a fm-cloud-graybunny. Grayscale Publishing

Gray-scale publishing is a further extension of the project----Fm-cloud-graybunny, which is based on multi-versioning, abstracting gray-scale service, gray-scale service instance, gray-scale strategy, gray-scale decision and so on. Support for A/b test, Canary Test. The grayscale strategy can be created from request IP, request Patameter, request header, etc. It is also possible to extend the gray-level strategy and gray-level decision based on the bamboo Loadbalancerequesttrigger combined with the Graybuanny interface. Scene

There are two services, a total of four service instances, namely ServiceA-1, ServiceA-2, ServiceB-1. Where ServiceA-2 is a grayscale instance.

Scenario 1
All request Headers usertype:old,ip:10.217. . Request or request header usertype:test, the URL parameter action:create request, will be forwarded to the grayscale service ServiceA-2.

Scenario 2
ServiceA-2 through a period of observation, to determine the stability of the operation, began to ServiceA-2 delete gray-level marks, beginning and ServiceA-1 will join the normal load balancing rules.

Scenario 3
Service SERVICEB release new version, ServiceB-2 need grayscale registration, after successful registration all requests cannot forward to ServiceB-2, after setting the grayscale policy for ServiceB-2, the request that conforms to policy will be forwarded to ServiceB-2. Ideas

From the above scenario analysis, we can sum up two objects: service instance and call request; The Gray-level management of service instance is the base, and how to make decision-making route when calling request is judged according to the Gray policy of service instance. Since there is the concept of grayscale management, then from the functional points, there will be client-server points, so you can also from the graybunny-client and Graybunny-server analysis. The next step is to analyze these four aspects. Grayscale Instances

Instance Registration
The service instance is added to grayscale management.

Instance Downline
Service instance offline, removed from grayscale management.

Grayscale switch
Adjusts the grayscale state of the service instance, has enabled, disabled two states, and disabled instances are not included in the grayscale list.

Grayscale Policy
Whether the request can be forwarded to the condition of the service instance, and only if it is passed, the request may be forwarded to that instance. Call Request

Grayscale decision
Depending on the requested information to match the grayscale policy of the Grayscale service instance, if the match is on, the service instance is added to the through list. If none are matched, the bamboo routing rule is filtered to filter non-grayscale service instances for forwarding. Grayscale Client

The service consumer that invokes the request, and the service provider that provides the service, can all be grayscale clients, because most of the service instances in MicroServices are both service providers and service consumers.

Grayscale Service Registration
When the service instance starts, it initiates a request to the grayscale server and opens the grayscale switch of the instance itself.

Grayscale Services Offline
Before the service instance is offline, a hook is triggered to initiate a request to the grayscale server to remove the instance itself from the grayscale list.

Receive Grayscale instance tuning messages
Receive Grayscale list update messages pushed by the grayscale server, such as adding grayscale instances, deleting grayscale instances, and maintaining a grayscale list that is cached on the instance.

Timed Pull Grayscale List
Periodically pull the latest grayscale list from the grayscale server to maintain a grayscale list of the instance's own cache. Grayscale service-side

Gray Service side Negative table maintains a grayscale list, you can add, delete, edit grayscale information.

Editing grayscale Instances
New Grayscale instance, delete grayscale instance, modify instance grayscale state.

Edit Grayscale Policy
New instance grayscale policy, delete instance grayscale policy, modify gray policy status.

Push Grayscale Service Tuning message
Push gray list Change message to gray client, such as adding grayscale instance, deleting grayscale instance, modifying instance grayscale state, etc.

Check if the service instance is offline at regular intervals
Check whether the Grayscale service instance is offline at timed intervals, and the instances of the downline will be removed from the grayscale list. Code Design

According to the above ideas, design the following objects and interfaces. A total of 6 interfaces, 4 model objects.

Object:

Grayservice
Grayscale Services

Grayinstance
Grayscale instances, stateful properties

Graypolicygroup
Grayscale Policy Group, stateful properties

Graypolicy
Grayscale Policy

Interface:

Graymanager
Grayscale Client Manager, maintains a grayscale list, maintains its own grayscale state, and creates a grayscale decision object. The abstract implementation class Abstractgraymanager implements the basic ability to obtain a grayscale list and create a grayscale decision object. Basegraymanger is extended on the basis of the period, caches the grayscale list, and periodically updates the grayscale list from the gray service side.

Informationclient
This interface is mainly responsible for communicating with the gray service side, obtaining the grayscale list, editing the grayscale instance and other abilities. Its implementation class Httpinformationclient by default uses HTTP to access the grayscale server side.
Subclass Informationclientdecorator is an adapter class, Retryableinformationclient inherits the Informationclientdecorator class and implements the retry function.

Graydecision
This interface is a grayscale decision that is used to determine whether the request matches the grayscale policy. The matching capabilities of IP matching, request parameter matching, request header matching, parameter matching in Bamboorequestcontext and merge matching are realized.

Graydecisionfactory
The default implementation class for Gray-scale decision-making supports the creation of several grayscale decisions.

Grayservicemanager
Grayscale service Management class, which belongs to the service-side class. Mainly editing service instances, editing grayscale policies, and maintaining the latest grayscale lists.

Graybunnyserverevictor
If the Grayscale service instance is offline and, because of an unexpected situation, it does not send a delete request to the grayscale server, the server calls the interface's methods at intervals, checks whether the instance in the Grayscale list is offline, and removes it from the grayscale list if the instance is offline. Code Implementation

After the model is abstracted into interface and object design, the realization idea is clear.

Grayscale Routing
Gray-level routing is the ability that clients must implement, Graybunny is extended on a bamboo basis, so Graybunny's routing Rule Object grayloadbalancerule inherits Bamboozoneavoidancerule,
The logic is this:
1, judge whether the target service has grayscale instance.
2. 1. If not, perform the parent class logic. End.
2.2, there are grayscale instances, the first grayscale instance and non-grayscale instance filter out.
3. Select the grayscale instance and filter the policy that calls the request to match the grayscale instance.
4.1. If there is no matching grayscale instance, pass the non-grayscale instance list past the filter logic that executes the parent class. End.
4. 2, if there is a matching grayscale instance, from which the polling method to pick out an instance. End.

Grayscale decision execution code in Graydecisionpredicate

Grayscale Management
Gray-scale management is the function of gray-scale service, mainly to maintain the grayscale list. Its implementation class Defaultgrayservicemanger has a map to maintain that Grayservice,key is the service ID. and call Eurekagraybunnyserverevictor every once in a while to check if the instances in the list are offline and remove the offline service from the grayscale list.

Eurekagraybunnyserverevictor is dependent on eurekaclient to check if the service instance is offline.
Use guide

The configuration of grayscale management is the same as the configuration of bamboo, and the configuration method is not very different. Let's start with the Gray-server configuration.

Gray-server:
In the project Pom.xml to add spring-boot related dependencies, then add Bamboo-start, Graybunny-server-starter, and then start on it.

In the startup class, you need to hire service discovery.

Once started, you can access the Http://localhost:10202/swagger-ui.html#/service-gray-resouce to view the list of interfaces, or you can call the interfaces in them.

The above describes the Gray-server configuration, the following look at the gray-client configuration.

gray-client

Add Gm-cloud-graybunny to the Pom.xml.

Add a grayscale configuration to the Application.yaml.

Add a grayscale client annotation to the startup class @enablegraybunny

This gray-level server and client are configured, as long as the grayscale server to turn on grayscale instance and grayscale policy, the gray-scale client will be automatically grayscale routing. Project Address

Insufficient Https://github.com/saleson/fm-cloud/tree/master/fm-cloud/fm-cloud-plugins/fm-cloud-graybunny

Graybunny currently only the basic functions of grayscale management, such as data persistence, high availability, push grayscale adjustment messages, etc., are not implemented. There is no interface, just a list of interfaces. Extended Thinking

Graybunny currently only supports Spring Cloud Eureka, but in spring Cloud, Eureka just as one of the registries, if you want to do the grayscale management of spring cloud, you need to be compatible with the registry, such as zookeeper, consul and so on.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.