Today, I want to share with you an idea that I want to use Nacos to do grayscale publishing.

What is gray publishing please see the following link: baike.baidu.com/item/%E7%81…

Getting to the point, the Ribbon component has held up well since Netflix went cold, including in Spring Cloud Alibaba Nacos. In fact, to do grayscale publishing based on Nacos is also a trick on the Ribbon.

The first step is to extend the Metadata Predicate

public abstract class DiscoveryEnabledPredicate extends AbstractServerPredicate { @Override public boolean Apply (@nullable PredicateKey Input) {// Since NacosServer is based on the Server of the Ribbon, return input! = null && input.getServer() instanceof NacosServer && apply((NacosServer) input.getServer()); } protected abstract boolean apply(NacosServer nacosServer); }Copy the code
public class MetadataAwarePredicate extends DiscoveryEnabledPredicate{ @Override protected boolean apply(NacosServer NacosServer) {// Filter according to the version number passed in by the client, HttpServletRequest Request = ((ServletRequestAttributes)RequestContextHolder .getRequestAttributes()).getRequest(); String versionNo = request.getHeader("version"); Map<String,String> versionMap = new HashMap<>(); versionMap.put("version",versionNo); final Set<Map.Entry<String,String>> attributes = Collections.unmodifiableSet(versionMap.entrySet()); final Map<String,String> metadata = nacosServer.getInstance().getMetadata(); return metadata.entrySet().containsAll(attributes); }}Copy the code

The second step is to extend the Metadata Rule

public abstract class DiscoveryEnabledRule extends PredicateBasedRule { private final CompositePredicate predicate; public DiscoveryEnabledRule(DiscoveryEnabledPredicate discoveryEnabledPredicate) { Assert.notNull(discoveryEnabledPredicate, "Parameter 'discoveryEnabledPredicate' can't be null"); this.predicate = createCompositePredicate(discoveryEnabledPredicate,new AvailabilityPredicate(this,null)); } @Override public AbstractServerPredicate getPredicate() { return this.predicate; } private CompositePredicate createCompositePredicate(DiscoveryEnabledPredicate discoveryEnabledPredicate, AvailabilityPredicate availabilityPredicate) { return CompositePredicate.withPredicates(discoveryEnabledPredicate, availabilityPredicate) .build(); }}Copy the code
public class MetadataAwareRule extends DiscoveryEnabledRule{ public MetadataAwareRule(){ this(new MetadataAwarePredicate()); } public MetadataAwareRule(DiscoveryEnabledPredicate predicate) { super(predicate); }}Copy the code

Step 3 drop it into the Spring container

@Configuration public class RibbonDiscoveryRuleAutoConfiguration { @Bean public DiscoveryEnabledRule metadataAwareRule(){ return new MetadataAwareRule(); }}Copy the code

The last

Grayscale release is actually a very complex system, the above code is just to provide you with a lost ideas, this dish is also learning.

At the same time to recommend a very good grayscale publishing framework (junge’s work), github.com/Nepxion/Dis…