Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand grayscale publishing

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to understand grayscale publishing". The content in the article is simple and clear, and it is easy to learn and understand. please follow the editor's ideas to study and learn "how to understand grayscale publishing".

Define

Grayscale publishing has been released in a smooth transition way, gradually switching from the old version to the new version by switching the route weight between the new and old versions online; for example, to launch the new function, first only update a small number of service nodes, through routing weights, let a small number of users experience the new version, and then update all service nodes if there is no problem In this way, the influence area can be reduced to a minimum when problems arise, and the stability of the system is ensured.

Grayscale release

A system often has access layers such as nginx (Openresty), gateway layers such as zuul, and service layers such as various rpc frameworks; there are routing functions in these layers, that is, all these layers can do grayscale; the access layer can use nginx+lua to achieve grayscale, gateway layer zuul can be combined with ribbon to achieve grayscale, and rpc frameworks such as dubbo itself provide routing functions that can be processed directly. Let's take a look at how to implement it.

Grayscale of access layer

In the access layer, we use a more powerful Openresty here, and then use lua for routing and forwarding. The relevant routing policies can be configured in the distributed cache redis or persisted to the database.

Prepare for

Prepare an Openresty, two web server tomcat (port is 8081, respectively), and redis; configure the whitelist in redis to facilitate simulation. If you are in the whitelist, go 8082 if you are not there, then go 8081.

Openresty configuration

You need to configure a lua script in Openresty that supports lua and related routes. The nginx.conf configuration is as follows:

Http {... lua_package_path "/ lualib/?.lua;;"; # lua module lua_package_cpath "/ lualib/?.so;;"; # c module upstream tomcat1 {server 127.0.0.1 server 8081;} upstream tomcat2 {listen 127.0.1 lua_package_path 8082;} server {listen 8010 serverSecretname localhost;location / {content_by_lua_file lua/gray.lua;} location @ tomcat1 {proxy_pass module } location @ tomcat2 {proxy_pass http://tomcat2;}}...}

If all requests are configured, they will go through the gray.lua script in the lua directory, as follows:

Local redis = require "resty.redis"; local redis_obj = redis:new (); redis_obj:set_timeout (2000); local ok,err = redis_obj:connect ("127.0.0.1", 6379); if not ok then ngx.say ("failed to connect redis", err); return;end-- get request iplocal_ip = ngx.var.remote_addr;--redis get whitelist local whitelist = redis_obj:get ("whitelist") -determine whether it is on the whitelist and then go to the corresponding service if string.find (whitelist,local_ip) = = nil then ngx.exec ("@ tomcat1"); else ngx.exec ("@ tomcat2"); endlocal ok,err = redis_obj:close ()

The built-in function module of Openresty can be directly connected to redis, and then take out the whitelist from redis to see whether the current request ip is in the whitelist, and then do simple routing function. You can dynamically modify the whitelist in redis and update it in real time.

Localhost:0 > set whitelist 127.0.0.1 "OK" localhost:0 > get whitelist "127.0.0.1"

Start the test

Launch tomcat1,tomcat2 and Openresty, respectively, and visit http://localhost. You can dynamically modify the whitelist in redis, and then access to view the result verification.

Grayscale of gateway layer

The gateway layer has zuul as an example. The grayscale of zuul needs to modify the load policy of ribbon, that is, customize the metadata according to the metadata of eureka, and then modify the policy rules of ribbon.

Prepare for

Two ports are prepared for the test service: 8765, 8766, and application.yml is configured as follows:

Server: port: 8765eureka: instance:metadata-map: route: 1

At the same time, prepare the request address / hiGray, and the return value is route1

Server: port: 8766eureka: instance:metadata-map: route: 2

At the same time, prepare the request address / hiGray, and the return value is route2; to distinguish whether you have gone to the grayscale server; then you need to introduce a plug-in on the Zulu side:

Io.jmnarlochribbon-discovery-filter-spring-cloud-starter2.1.0

Then you need to prepare a filter of type pre, as follows:

@ Configurationpublic class GrayFilter extends ZuulFilter {@ Overridepublic Object run () {RequestContext ctx = RequestContext.getCurrentContext (); HttpServletRequest request = ctx.getRequest (); String ip = request.getRemoteAddr (); / / ipv6 local address, that is 127.0.0.1if ("0 RibbonFilterContextHolder.getCurrentContext () .equals (ip)) {RibbonFilterContextHolder.getCurrentContext () .add (" route "," 1 ") } else {RibbonFilterContextHolder.getCurrentContext () .add ("route", "2");} return null;}...}

The whitelist is also used as an example. For convenience, the whitelist is not configured in redis. The configured whitelist address is ipv6:0:0:0:0:0:0:0:1. If it is a whitelist address, it will be routed to port 8765, otherwise it will serve port 8766.

test

Start eureka-server, two eureka-client, and zuul gateway, respectively, and access the gateway address; you can test through 127.0.0.1 and local ip access respectively.

Service layer grayscale

The server has rpc framework dubbo as an example, and dubbo itself provides a variety of routing rules, including conditional routing, script routing, etc. Here, script routing is also used as an example. Script routing rules support all scripts of JDK scripting engine, such as javascript, jruby, groovy, etc. Here the default JavaScript is used as an example

Prepare for

Registry zookeeper, two Provider can specify ports 20881 and 20882 locally, respectively, for consumers, as well as routing scripts highlighted below:

Function gray_rule (invokers, context) {var tag = context.getAttachment ("tag"); var result = new java.util.ArrayList (invokers.size ()); if (tag = = "gray") {for (I = 0; I < invokers.size (); I + +) {if (invokers.get (I). GetUrl (). GetPort () = 20881) {result.add (invokers.get (I)) } else {for (I = 0; I < invokers.size (); I + +) {if (invokers.get (I). GetUrl (). GetPort () = 20882) {result.add (invokers.get (I));}} return result;} (invokers,context)

When dubbo runs the script, three parameters are passed in: invokers,Invocation and RpcContext.getContext (); by setting tag in the RpcContext on the consumer side:

RpcContext.getContext (. SetAttachment ("tag", "gray")

In this way, we can judge in the script that tag is the consumer side of gray and only goes to the server side of port 20881, while the rest goes to the server side of 20882.

The above script needs to be registered in zookeeper. The manual registration code is as follows. Of course, you can also use the dubbo-admin provided by dubbo to set the routing script:

URL registryUrl = URL.valueOf ("zookeeper://127.0.0.1:2181"); ZookeeperRegistryFactory zookeeperRegistryFactory = new ZookeeperRegistryFactory (); zookeeperRegistryFactory.setZookeeperTransporter (new CuratorZookeeperTransporter ()); Registry zookeeperRegistry = (ZookeeperRegistry) zookeeperRegistryFactory.createRegistry (registryUrl); URL routerURL = URL.valueOf ("script://0.0.0.0/com.dubboApi.DemoService?category=routers&dynamic=false"); routerURL = routerURL.addParameter ("rule", URL.encode ("(JavaScript script..)"); zookeeperRegistry.register (routerURL); / / Registration

For details, please refer to the official document: old routing rules

test

Start zookeeper, and then start two producers respectively. When starting the consumer, modify the tag and then observe the route.

Thank you for your reading, the above is the content of "how to understand grayscale publishing". After the study of this article, I believe you have a deeper understanding of how to understand grayscale publishing, and the specific use needs to be verified in practice. 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report