Categories
Uncategorized

How Spring Cloud service developers to resolve conflicts and instances scurrying?

I. Background

When we developed the micro-service architecture system, although each micro services are isolated can be developed separately, but in fact not the case, you want to debug and test your service requires not only your micro service up and running, but also its context of the service, base on which services should be run; but if your system service number and more dependent on it, that is a more difficult problem! There is no way it can improve the efficiency of development?

As shown above, we can not use the server to all the services are deployed together, and then develop their own service run only responsible for the development of local, because of the need to rely on other local services so startup services also need to be registered in the public registry ;

Examples of business services B has three instances registered with the Registration Center
    They are: server, development A and B to develop their own native started

But doing so would give rise to a new problem: the service will be scurrying conflict, meaning that developers debug their business at A service B service request may be time to jump on the instance of others (server, development B)

 

Second, Solutions

Services scurrying to solve this problem there is a more elegant way to customize load balancing rules, to achieve the following objectives:

    When ordinary users to access pages on the server, the server instances on all routes requests only call

    A developing access, priority call routing request all examples of development A native boot, if there is no instance on the server is called

    Above, all requests priority call routing to develop this instance B-initiated the development of B access, if there is no instance on the server is called

 

Third, the specific implementation

To achieve the above objectives are the two key issues need to be addressed

    Examples of differentiated services for different users

    Implementing custom load balancing rules

3.1. Examples of differentiated services for different users

Direct use registry metadata (metadata) to distinguish it

Mainstream registration centers with metadata management
    To Nacos, for example, only need to add in the configuration file

spring:
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
        metadata:
          version: zlt

the next version of metadata is metadata I added the key for the version, value is zlt

启动服务后元数据就会注册上去,如下图

After the metadata to distinguish the following cases is present

    Instances of the server version is empty

  • 开发人员自己本地启动的实例version为唯一标识(自己的名字)

 

3.2. Custom load balancing rules

First in Spring Cloud service framework instance micro load balancing is the responsibility of the Ribbon.
    CustomIsolationRule detailed class information can be viewed: CustomIsolationRule.java

public class CustomIsolationRule extends RoundRobinRule {
    /**
     * 优先根据版本号取实例
     */
    @Override
    public Server choose(ILoadBalancer lb, Object key) {
        if (lb == null) {
            return null;
        }
        String version = LbIsolationContextHolder.getVersion();
        List targetList = null;
        List upList = lb.getReachableServers();
        if (StrUtil.isNotEmpty(version)) {
            //取指定版本号的实例
            targetList = upList.stream().filter(
                    server -> version.equals(
                            ((NacosServer) server).getMetadata().get(CommonConstant.METADATA_VERSION)
                    )
            ).collect(Collectors.toList());
        }

        if (CollUtil.isEmpty(targetList)) {
            //只取无版本号的实例
            targetList = upList.stream().filter(
                    server -> {
                        String metadataVersion = ((NacosServer) server).getMetadata().get(CommonConstant.METADATA_VERSION);
                        return StrUtil.isEmpty(metadataVersion);
                    }
            ).collect(Collectors.toList());
        }

        if (CollUtil.isNotEmpty(targetList)) {
            return getServer(targetList);
        }
        return super.choose(lb, key);
    }

    /**
     * 随机取一个实例
     */
    private Server getServer(List upList) {
        int nextInt = RandomUtil.randomInt(upList.size());
        return upList.get(nextInt);
    }
}

RoundRobinRule polling inheritance rules to implement the main logic

    The version number of the version of the upstream input, has a value, then the service instance of the meta-information as the version value is taken

    Upstream version number version or the version number is not a value not match any of the services, the service instance element taking only the version information is empty

And the load is turned on custom rules by configuring the switching control

@Configuration
@ConditionalOnProperty(value = "zlt.ribbon.isolation.enabled", havingValue = "true")
@RibbonClients(defaultConfiguration = {RuleConfigure.class})
public class LbIsolationConfig {

}

 

IV Summary

Examples of differentiated services mentioned above and load custom rules as the core point of the whole idea of ​​solving the basic realization of the quarantine service instance, left to do is upstream version of how to pass it?, I offer two ideas

    Developers start their own front-end engineering, through the configuration parameters, unified version passed in front-end engineering

  • 通过postman调用接口的时候在header参数中添加

 
参考
https://github.com/Nepxion/Discovery

Recommended Reading

    Logs to troubleshoot difficult problems? Distributed link tracking log to help you

    zuul latest Sentinel integrated gateway flow control assembly

    Ali registry Nacos production deployment

    Spring Boot custom configuration item inside the IDE automatically prompted

    How dynamic routing Spring Cloud Zuul do? Nacos very simple to achieve integration

 

Leave a Reply