Architecture design: realize the gray release mode under the microservice mode

Source code of this article: GitHub·click here || GitEE·click here

1. Basic logic

The request passes the 8001 service. In the gray rule, the service list of the next request will be read, and the routed service will be selected according to the version number parameter rule.

Configure the version number, distinguish the gray version and the default normal version;
customize the interceptor, manage the version number or other identification parameters to be passed in the request;
customize the service selection strategy, and identify the routing service based on the version;
if the gray service does not exist, then Select the default service based on rules;

Two, version configuration

Configure two services in the node12-server cluster: configure version v7.0.0 on port 8002 and configure version 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:

Three, parameter transfer

Microservices manage the Feign request interceptor between services by implementing the RequestInterceptor interface. Before the request is routed to the service, some processing operations can be performed on the request. Common operations such as passing the version number, user token and other request attributes.

/**
 * 请求拦截器
 */
@Component
public class GrayReqInterceptor implements RequestInterceptor {
    
    

    private static final String VERSION_KEY = "versionId" ;

    /**
     * 处理请求头参数携带问题
     */
    @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 for the next request routing service.

Fourth, the gray scale rule

Add the version number to be accessed in the Header of the request header. If there is a matching service, route all requested gray-scale services, if not, return to the default service.

@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);

        // 服务匹配
        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, service selection is very complicated. If there is no gray service, service matching rules need to be formulated according to the actual situation, such as response time or default polling.

One more thing to note is that once the secondary encapsulation of the underlying API is used, the overall project will be affected by the framework version upgrade, and it is necessary to continue to pay attention to the framework environment.

Five, test process

1. Start related services and observe the list of registry services;

2. Request the interface of 8001 service and bring the version number;

3. Observe routing services of different version numbers;

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

Six, source code address

GitHub地址:知了一笑
https://github.com/cicadasmile/spring-cloud-base
GitEE地址:知了一笑
https://gitee.com/cicadasmile/spring-cloud-base

Recommended reading: finishing programming system

Serial number project name GitHub address GitEE address Recommended
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 Detailed explanation of SpringCloud microservice basic component case GitHub·click here GitEE·Click here ☆☆☆
04 SpringCloud microservice architecture actual combat comprehensive case GitHub·click here GitEE·Click here ☆☆☆☆☆
05 Getting started with SpringBoot framework basic application to advanced GitHub·click here GitEE·Click here ☆☆☆☆
06 SpringBoot framework integrates and develops common middleware GitHub·click here GitEE·Click here ☆☆☆☆☆
07 Basic case of data management, distribution, architecture design GitHub·click here GitEE·Click here ☆☆☆☆☆
08 Big data series, storage, components, computing and other frameworks GitHub·click here GitEE·Click here ☆☆☆☆☆

Guess you like

Origin blog.csdn.net/cicada_smile/article/details/109830130
Recommended