Architecture design: realize gray publishing mode in micro service mode

Time:2021-3-8

Source code:GitHub. Click here || Gitee. Click here

1、 Basic logic

Request through 8001 service, in the gray rule, will read the next request service list, according to the version number parameter rule, select the routing service.

Architecture design: realize gray publishing mode in micro service mode

Configure version number to distinguish gray version and default normal version;
The user-defined interceptor manages the passing of version number or other identification parameters in the request;
User defined service selection strategy and routing service identification based on version;
If the gray service does not exist, the default service is selected based on the rules;

2、 Version configuration

Two services are configured in the node12 server cluster: v7.0.0 on port 8002 and v7.0.1 on port 8003 to test the gray version selection.

8002 service

eureka:
    metadata-map:
      version: v7.0.0

8003 service

eureka:
    metadata-map:
      version: v7.0.1

Eureka registry, service list:

Architecture design: realize gray publishing mode in micro service mode

3、 Parameter transfer

Through the implementation of requestinterceptor interface in microservice, the feign request interceptor between services can be managed. Before the request is routed to the service, some processing operations can be performed on the request, such as passing version number, user token and other request header attributes.

/**
 *Request interceptor
 */
@Component
public class GrayReqInterceptor implements RequestInterceptor {

    private static final String VERSION_KEY = "versionId" ;

    /**
     *Handling the problem of carrying request header parameters
     */
    @Override
    public void apply(RequestTemplate requestTemplate) {
        HttpServletRequest request =
                ((ServletRequestAttributes)
                RequestContextHolder.getRequestAttributes()).getRequest();

        String versionId = request.getHeader(VERSION_KEY);

        if (StringUtils.isNotEmpty(versionId)){
            requestTemplate.header(VERSION_KEY,versionId);
        }
    }
}

Here, a versionid parameter is passed as the core identifier of the next request routing service.

4、 Gray rule

Add the version number to be accessed in the header of the request header. If there is a matching service, all the requested gray services will be routed. If there is no matching service, the default service will be returned.

@Configuration
public class GrayRule extends ZoneAvoidanceRule {

    @Bean
    public GrayReqInterceptor grayReqInterceptor(){
        return new GrayReqInterceptor();
    }

    private static final String VERSION_KEY = "versionId" ;

    @Override
    public Server choose(Object key) {

        HttpServletRequest request =
                ((ServletRequestAttributes)
                RequestContextHolder.getRequestAttributes()).getRequest();

        String versionId = request.getHeader(VERSION_KEY);

        //Service matching
        List<Server> serverList = this.getPredicate().getEligibleServers(this.getLoadBalancer().getAllServers(), key);
        Server toServer = getServer(serverList,versionId);
        if (toServer != null){
            return toServer ;
        } else {
            return getServer(serverList,GrayConstant.VERSION_DEF);
        }
    }

    private Server getServer (List<Server> serverList,String version){
        Server toServer = null ;
        for (Server server : serverList) {
            Map<String, String> metadata = ((DiscoveryEnabledServer) server).getInstanceInfo().getMetadata();

            String metaVersion = metadata.get("version");
            if (!StringUtils.isEmpty(metaVersion)) {
                if (metaVersion.equals(version)) {
                    toServer = server;
                }
            }
        }
        return toServer ;
    }
}

In the actual process, the selection of services is very complex. If there is no gray service, we need to make service matching rules according to the actual situation, such as response time or default polling.

More importantly, once the underlying API is used for secondary encapsulation, the overall project will be affected by the Framework version upgrade, so we need to continue to pay attention to the framework environment.

5、 Test process

1. Start related services and observe the service list of the registration center;

2. Request 8001 service interface with version number;

3. Observe the routing services with different version numbers;

4. Do not carry the version number and observe the default service selection;

6、 Source code address

GitHub address: cicada smile
https://github.com/cicadasmile/spring-cloud-base
Gitee address: cicada smile
https://gitee.com/cicadasmile/spring-cloud-base

Recommended reading: programming system arrangement

Serial number entry name GitHub address Gitee address Recommended index
01 Java describes design patterns, algorithms, and data structures GitHub. Click here Gitee. Click here ☆☆☆☆☆
02 Java foundation, concurrency, object oriented, web development GitHub. Click here Gitee. Click here ☆☆☆☆
03 Spring cloud microservice basic component case explanation GitHub. Click here Gitee. Click here ☆☆☆
04 A case study of springcloud microservice architecture GitHub. Click here Gitee. Click here ☆☆☆☆☆
05 Basic application of springboot framework GitHub. Click here Gitee. Click here ☆☆☆☆
06 Integration and development of common middleware based on springboot framework GitHub. Click here Gitee. Click here ☆☆☆☆☆
07 Basic cases of data management, distributed and architecture design GitHub. Click here Gitee. Click here ☆☆☆☆☆
08 Big data series, storage, components, computing and other frameworks GitHub. Click here Gitee. Click here ☆☆☆☆☆