In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the characteristics of Spring Cloud Gateway extension". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the characteristics of Spring Cloud Gateway extension"?
Grayscale release
What is grayscale release? please refer to the concept. Let's simply look at the following figure. In popular terms, in order to ensure a smooth transition in the service upgrade process and improve the customer experience, some users will be updated progressively. In this way, multiple versions of the client will appear at the same time in production. In order to ensure the availability of multiple versions of the client, multiple versions of the server version are required. Grayscale publishing is to ensure that multiple versions of clients and servers can correspond correctly through a certain strategy.
The so-called grayscale publishing, that is, when there are multiple instances of a service and the versions of the instances are not consistent, through the
Implementation scheme nginx + lua (openresty)
Netflix Zuul
You only need to customize the assertion of ribbon. The core is to obtain the version number in the upper and lower request header through TTL.
@ Slf4jpublic class MetadataCanaryRuleHandler extends ZoneAvoidanceRule {@ Override public AbstractServerPredicate getPredicate () {return new AbstractServerPredicate () {@ Override public boolean apply (PredicateKey predicateKey) {String targetVersion = RibbonVersionHolder.getContext (); RibbonVersionHolder.clearContext () If (StrUtil.isBlank (targetVersion)) {log.debug ("client is not configured with target version direct routing"); return true;} DiscoveryEnabledServer server = (DiscoveryEnabledServer) predicateKey.getServer (); final Map metadata = server.getInstanceInfo () .getMetadata () If (StrUtil.isBlank (metadata.get (SecurityConstants.VERSION) {log.debug ("current microservice {} version is not configured for direct routing"); return true;} if (metadata.get (SecurityConstants.VERSION) .equals (targetVersion)) {return true } else {log.debug ("current microservice {} version is {}, target version {} failed to match", server.getInstanceInfo () .getAppName (), metadata.get (SecurityConstants.VERSION), targetVersion); return false;};}}
Maintain the version number in the request
Public class RibbonVersionHolder {private static final ThreadLocal context = new TransmittableThreadLocal (); public static String getContext () {return context.get ();} public static void setContext (String value) {context.set (value);} public static void clearContext () {context.remove ();}} Spring Cloud Gateway
In the first reaction, refer to the implementation of zuul, customize the assertion, and then get the version information from the top and bottom. However, because spring cloud gateway is reactive programming based on webflux, traditional TTL or RequestContextHolder can not maintain context requests correctly.
Let's first take a look at spring clou's gateway default lb policy to implement LoadBalancerClientFilter.
Public class LoadBalancerClientFilter implements GlobalFilter, Ordered {@ Override public int getOrder () {return LOAD_BALANCER_CLIENT_FILTER_ORDER;} @ Override @ SuppressWarnings ("Duplicates") public Mono filter (ServerWebExchange exchange, GatewayFilterChain chain) {return chain.filter (exchange) } protected ServiceInstance choose (ServerWebExchange exchange) {return loadBalancer.choose (URI) exchange.getAttribute (GATEWAY_REQUEST_URL_ATTR)) .getHost ();}}
We just need to override the choose method to pass the context request to the routing assertion, as follows
@ Overrideprotected ServiceInstance choose (ServerWebExchange exchange) {HttpHeaders headers = exchange.getRequest () .getHeaders (); return loadBalancer.choose (URI) exchange.getAttribute (GATEWAY_REQUEST_URL_ATTR)) .getHost (), headers);}
Then you can get it through PredicateKey in the route assertion
Public abstract class AbstractDiscoveryEnabledPredicate extends AbstractServerPredicate {/ * * {@ inheritDoc} * / @ Override public boolean apply (@ Nullable PredicateKey input) {return input! = null & & input.getServer () instanceof NacosServer & & apply ((NacosServer) input.getServer (), (HttpHeaders) input.getLoadBalancerKey ()) }}
Finally, it is calculated according to the version.
Public class GrayMetadataAwarePredicate extends AbstractDiscoveryEnabledPredicate {@ Override protected boolean apply (NacosServer server, HttpHeaders headers) {PigxRibbonRuleProperties ribbonProperties = SpringContextHolder.getBean (PigxRibbonRuleProperties.class); if (! ribbonProperties.isGrayEnabled ()) {log.debug ("gray closed,GrayMetadataAwarePredicate return true"); return true } final Map metadata = server.getMetadata (); String version = metadata.get (CommonConstants.VERSION); / / determine whether the Nacos service has a version tag if (StrUtil.isBlank (version)) {log.debug ("nacos server tag is blank, GrayMetadataAwarePredicate return true") Return true;} / / determine whether there is a version in the request String target = headers.getFirst (CommonConstants.VERSION); if (StrUtil.isBlank (target)) {log.debug ("request headers version is blank,GrayMetadataAwarePredicate return true"); return true } log.debug ("request version: {}, current service version: {}", target, version); return target.equals (version);}} integrate nacos
Combined with the dynamic configuration of nacos, it is very convenient to realize grayscale.
Thank you for your reading, the above is the "what are the characteristics of Spring Cloud Gateway extension" content, after the study of this article, I believe you have a deeper understanding of the characteristics of Spring Cloud Gateway extension, the specific use of the need for you to practice and verify. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.