Fork me on GitHub

版权声明 本站原创文章 由 萌叔 发表
转载请注明 萌叔 | https://vearne.cc

1. 前言

说到Golang的Redis库,用到最多的恐怕是
redigogo-redis。其中 redigo 不支持对集群的访问。
本文想聊聊go-redis 2个高级用法

2. 开启对Cluster中Slave Node的访问

在一个负载比较高的Redis Cluster中,如果允许对slave节点进行读操作将极大的提高集群的吞吐能力。

开启对Slave 节点的访问,受以下3个参数的影响

type ClusterOptions struct {
    // Enables read-only commands on slave nodes.
    ReadOnly bool
    // Allows routing read-only commands to the closest master or slave node.
    // It automatically enables ReadOnly.
    RouteByLatency bool
    // Allows routing read-only commands to the random master or slave node.
    // It automatically enables ReadOnly.
    RouteRandomly bool
    ... 
}

go-redis 选择节点的逻辑如下

func (c *ClusterClient) cmdSlotAndNode(cmd Cmder) (int, *clusterNode, error) {
    state, err := c.state.Get()
    if err != nil {
        return 0, nil, err
    }

    cmdInfo := c.cmdInfo(cmd.Name())
    slot := cmdSlot(cmd, cmdFirstKeyPos(cmd, cmdInfo))

    if c.opt.ReadOnly && cmdInfo != nil && cmdInfo.ReadOnly {
        if c.opt.RouteByLatency {
            node, err := state.slotClosestNode(slot)
            return slot, node, err
        }

        if c.opt.RouteRandomly {
            node := state.slotRandomNode(slot)
            return slot, node, nil
        }

        node, err := state.slotSlaveNode(slot)
        return slot, node, err
    }

    node, err := state.slotMasterNode(slot)
    return slot, node, err
}
  • 如果ReadOnly = true,只选择Slave Node
  • 如果ReadOnly = true 且 RouteByLatency = true 将从slot对应的Master NodeSlave Node选择,选择策略为: 选择PING 延迟最低的节点
  • 如果ReadOnly = true 且 RouteRandomly = true 将从slot对应的Master NodeSlave Node选择,选择策略为:随机选择

3. 在集群模式下使用pipeline功能

Redis的pipeline功能的原理是 Client通过一次性将多条redis命令发往Redis Server,减少了每条命令分别传输的IO开销。同时减少了系统调用的次数,因此提升了整体的吞吐能力。

我们在主-从模式的Redis中,pipeline功能应该用的很多,但是Cluster模式下,估计还没有几个人用过。
我们知道 redis cluster 默认分配了 16384 个slot,当我们set一个key 时,会用CRC16算法来取模得到所属的slot,然后将这个key 分到哈希槽区间的节点上,具体算法就是:CRC16(key) % 16384。如果我们使用pipeline功能,一个批次中包含的多条命令,每条命令涉及的key可能属于不同的slot

go-redis 为了解决这个问题, 分为3步
源码可以阅读 defaultProcessPipeline
1) 将计算command 所属的slot, 根据slot选择合适的Cluster Node
2)将同一个Cluster Node 的所有command,放在一个批次中发送(并发操作)
3)接收结果

注意:这里go-redis 为了处理简单,每个command 只能涉及一个key, 否则你可能会收到如下错误

err CROSSSLOT Keys in request don't hash to the same slot

也就是说go-redis不支持类似 MGET 命令的用法

一个简单的例子

package main

import (
    "github.com/go-redis/redis"
    "fmt"
)

func main() {
    client := redis.NewClusterClient(&redis.ClusterOptions{
        Addrs: []string{"192.168.120.110:6380", "192.168.120.111:6380"},
        ReadOnly: true,
        RouteRandomly: true,
    })

    pipe := client.Pipeline()
    pipe.HGetAll("1371648200")
    pipe.HGetAll("1371648300")
    pipe.HGetAll("1371648400")
    cmders, err := pipe.Exec()
    if err != nil {
        fmt.Println("err", err)
    }
    for _, cmder := range cmders {
        cmd := cmder.(*redis.StringStringMapCmd)
        strMap, err := cmd.Result()
        if err != nil {
            fmt.Println("err", err)
        }
        fmt.Println("strMap", strMap)
    }
}

后记:

2022年09月16日 需要注意的点
主从模式下,一组只读副本(多个slave实例)如果使用的是高可用域名,由于域名解析的结果在进程中会缓存,因此所有请求可能都被发往了同一个实例。

你需要使用Dialer参数,它会在每次创建连接时被调用。

    options := redis.Options{
        Addr:     "movie.vearne.redis:18429",
        Password: "xxxxxx",
        // Dialer creates new network connection and has priority over
        // Network and Addr options.
        Dialer: func(ctx context.Context, network, addr string) (net.Conn, error) {
            r := &net.Resolver{}
            host, port, err := net.SplitHostPort(addr)
            if err != nil {
                return nil, err
            }
            ips, err := r.LookupHost(context.Background(), host)
            if err != nil {
                return nil, err
            }
            ip := ips[rand.Intn(len(ips))]
            netDialer := &net.Dialer{
                Timeout:   10 * time.Second,
                KeepAlive: 1 * time.Minute,
            }
            fmt.Println("addr", net.JoinHostPort(ip, port))
            return netDialer.DialContext(ctx, network, net.JoinHostPort(ip, port))
        },
    }
    client := redis.NewClient(&options)

微信公众号

26 对 “聊聊go-redis的一些高级用法”的想法;

  1. On this platform, you can find lots of online slots from famous studios.
    Users can enjoy traditional machines as well as modern video slots with high-quality visuals and bonus rounds.
    Even if you’re new or a casino enthusiast, there’s always a slot to match your mood.
    casino
    The games are available 24/7 and optimized for laptops and mobile devices alike.
    All games run in your browser, so you can jump into the action right away.
    The interface is easy to use, making it convenient to browse the collection.
    Sign up today, and discover the thrill of casino games!

  2. Here, you can access lots of online slots from leading developers.
    Users can enjoy classic slots as well as modern video slots with vivid animation and interactive gameplay.
    If you’re just starting out or a casino enthusiast, there’s a game that fits your style.
    slot casino
    Each title are instantly accessible round the clock and designed for laptops and smartphones alike.
    All games run in your browser, so you can jump into the action right away.
    Platform layout is easy to use, making it convenient to explore new games.
    Join the fun, and enjoy the world of online slots!

  3. Here, you can access a wide selection of slot machines from famous studios.
    Players can try out retro-style games as well as new-generation slots with stunning graphics and exciting features.
    If you’re just starting out or an experienced player, there’s something for everyone.
    casino games
    Each title are available 24/7 and compatible with desktop computers and tablets alike.
    No download is required, so you can jump into the action right away.
    The interface is easy to use, making it convenient to find your favorite slot.
    Join the fun, and enjoy the excitement of spinning reels!

  4. On this site showcases disc player alarm devices by trusted manufacturers.
    You can find premium CD devices with FM/AM reception and dual alarms.
    Most units include auxiliary inputs, charging capability, and backup batteries.
    The selection covers value picks to luxury editions.
    best clock radios with cd player
    Each one offer snooze functions, auto-off timers, and LED screens.
    Order today are available via Amazon with free delivery.
    Choose your ultimate wake-up solution for office or office use.

  5. On this platform, you can find a wide selection of online slots from top providers.
    Users can try out retro-style games as well as feature-packed games with vivid animation and bonus rounds.
    If you’re just starting out or a casino enthusiast, there’s a game that fits your style.
    money casino
    Each title are instantly accessible 24/7 and designed for PCs and tablets alike.
    All games run in your browser, so you can jump into the action right away.
    Site navigation is easy to use, making it simple to explore new games.
    Join the fun, and enjoy the thrill of casino games!

  6. Наличие страхового полиса при выезде за границу — это разумное решение для обеспечения безопасности туриста.
    Страховка обеспечивает неотложную помощь в случае обострения болезни за границей.
    К тому же, документ может включать компенсацию на возвращение домой.
    carbox30.ru
    Некоторые государства настаивают на предоставление документа для пересечения границы.
    Без наличия документа лечение могут быть финансово обременительными.
    Получение сертификата заблаговременно

  7. This platform allows you to connect with workers for occasional risky projects.
    Visitors are able to securely schedule services for particular situations.
    All listed individuals have expertise in executing critical activities.
    assassin for hire
    The website provides discreet communication between clients and specialists.
    Whether you need immediate help, this platform is the right choice.
    List your task and find a fit with an expert now!

  8. Il nostro servizio consente il reclutamento di lavoratori per compiti delicati.
    I clienti possono selezionare candidati qualificati per incarichi occasionali.
    Gli operatori proposti sono valutati con attenzione.
    sonsofanarchy-italia.com
    Con il nostro aiuto è possibile ottenere informazioni dettagliate prima della scelta.
    La professionalità è la nostra priorità.
    Sfogliate i profili oggi stesso per ottenere aiuto specializzato!

  9. На данной странице вы можете найти актуальное зеркало 1xBet без ограничений.
    Систематически обновляем доступы, чтобы облегчить свободное подключение к порталу.
    Используя зеркало, вы сможете пользоваться всеми функциями без рисков.
    1xbet-official.live
    Наш сайт обеспечит возможность вам быстро найти новую ссылку 1хБет.
    Мы заботимся, чтобы каждый пользователь был в состоянии использовать все возможности.
    Следите за обновлениями, чтобы не терять доступ с 1xBet!

  10. Данный ресурс — настоящий цифровой магазин Боттега Вэнета с доставлением по всей России.
    У нас вы можете оформить заказ на эксклюзивные вещи Боттега Венета напрямую.
    Любая покупка подтверждены сертификатами от бренда.
    bottega veneta italy
    Отправка осуществляется в кратчайшие сроки в любое место России.
    Бутик онлайн предлагает выгодные условия покупки и комфортные условия возврата.
    Выбирайте официальном сайте Bottega Veneta, чтобы наслаждаться оригинальными товарами!

  11. 在此页面,您可以找到专门从事单次的危险工作的专家。
    我们汇集大量技能娴熟的任务执行者供您选择。
    不管是何种高风险任务,您都可以快速找到胜任的人选。
    chinese-hitman-assassin.com
    所有执行者均经过严格甄别,维护您的机密信息。
    平台注重专业性,让您的任务委托更加无忧。
    如果您需要详细资料,请与我们取得联系!

  12. Through this platform, you can find trusted websites for CS:GO betting.
    We list a selection of betting platforms centered around Counter-Strike: Global Offensive.
    These betting options is carefully selected to provide reliability.
    best cs 2 betting sites
    Whether you’re a CS:GO enthusiast, you’ll conveniently find a platform that meets your expectations.
    Our goal is to guide you to enjoy the top-rated CS:GO gaming options.
    Start browsing our list today and elevate your CS:GO playing experience!

  13. Here, you can explore different platforms for CS:GO gambling.
    We offer a selection of gaming platforms dedicated to CS:GO.
    Each site is thoroughly reviewed to guarantee trustworthiness.
    skin betting
    Whether you’re new to betting, you’ll quickly select a platform that meets your expectations.
    Our goal is to guide you to enjoy the top-rated CS:GO gambling websites.
    Check out our list right away and elevate your CS:GO betting experience!

  14. Here, you can find a wide selection of online slots from top providers.
    Players can experience traditional machines as well as new-generation slots with vivid animation and bonus rounds.
    Whether you’re a beginner or an experienced player, there’s something for everyone.
    casino
    Each title are available anytime and compatible with desktop computers and mobile devices alike.
    No download is required, so you can get started without hassle.
    Platform layout is easy to use, making it convenient to browse the collection.
    Register now, and dive into the thrill of casino games!

  15. В этом источнике вы сможете найти всю информацию о программе лояльности: 1win партнерская программа.
    Представлены все особенности взаимодействия, условия участия и потенциальные вознаграждения.
    Любой блок детально описан, что позволяет легко усвоить в особенностях функционирования.
    Также доступны вопросы и ответы и практические указания для начинающих.
    Материалы поддерживаются в актуальном состоянии, поэтому вы можете быть уверены в достоверности предоставленных материалов.
    Этот ресурс станет вашим надежным помощником в исследовании партнёрской программы 1Win.

  16. This website offers you the chance to get in touch with professionals for temporary high-risk tasks.
    Visitors are able to securely request support for particular situations.
    All listed individuals are experienced in managing complex jobs.
    killer for hire
    This service provides discreet arrangements between employers and contractors.
    For those needing a quick solution, this platform is ready to help.
    Create a job and match with the right person instantly!

  17. Questo sito permette la selezione di lavoratori per compiti delicati.
    Gli interessati possono ingaggiare candidati qualificati per operazioni isolate.
    Tutti i lavoratori sono valutati secondo criteri di sicurezza.
    sonsofanarchy-italia.com
    Sul sito è possibile ottenere informazioni dettagliate prima della scelta.
    La sicurezza continua a essere al centro del nostro servizio.
    Esplorate le offerte oggi stesso per trovare il supporto necessario!

  18. Seeking to hire reliable workers ready for one-time hazardous jobs.
    Need a specialist for a hazardous job? Find vetted laborers via this site to manage urgent dangerous operations.
    hire a killer
    This website links businesses to skilled professionals prepared to accept unsafe short-term positions.
    Employ verified contractors to perform perilous jobs safely. Perfect when you need last-minute assignments requiring specialized labor.

  19. Here, you can find a wide selection of online slots from famous studios.
    Visitors can try out classic slots as well as new-generation slots with vivid animation and interactive gameplay.
    If you’re just starting out or a casino enthusiast, there’s a game that fits your style.
    slot casino
    All slot machines are instantly accessible 24/7 and designed for PCs and mobile devices alike.
    All games run in your browser, so you can start playing instantly.
    The interface is user-friendly, making it simple to find your favorite slot.
    Sign up today, and enjoy the world of online slots!

  20. Humans think about ending their life due to many factors, commonly arising from intense psychological suffering.
    A sense of despair may consume someone’s will to live. Often, isolation is a major factor in this decision.
    Psychological disorders can cloud judgment, preventing someone to find other solutions for their struggles.
    how to kill yourself
    Life stressors could lead a person closer to the edge.
    Lack of access to help may leave them feeling trapped. Keep in mind getting help makes all the difference.

  21. 欢迎光临,这是一个成人网站。
    进入前请确认您已年满18岁,并同意接受相关条款。
    本网站包含不适合未成年人观看的内容,请自行判断是否适合进入。 色情网站
    若不符合年龄要求,请立即关闭窗口。
    我们致力于提供合法合规的娱乐内容。

  22. Looking for someone to handle a one-time risky job?
    Our platform specializes in linking customers with freelancers who are willing to tackle high-stakes jobs.
    Whether you’re dealing with urgent repairs, hazardous cleanups, or risky installations, you’re at the right place.
    All available professional is vetted and certified to guarantee your security.
    rent a hitman
    We provide transparent pricing, comprehensive profiles, and safe payment methods.
    Regardless of how difficult the situation, our network has the expertise to get it done.
    Begin your quest today and find the ideal candidate for your needs.

  23. Here useful materials about how to become a cyber specialist.
    Information is provided in a easily digestible manner.
    You may acquire different tactics for bypassing protection.
    What’s more, there are concrete instances that show how to utilize these capabilities.
    how to become a hacker
    Full details is often renewed to stay current with the newest developments in cybersecurity.
    Unique consideration is focused on functional usage of the developed competencies.
    Consider that every action should be applied lawfully and with moral considerations only.

  24. Our website you can find unique promo codes for 1x Bet.
    The promo codes make it possible to acquire supplementary benefits when placing bets on the site.
    Available special codes are regularly updated to maintain their usability.
    Using these promocodes it is possible to significantly increase your opportunities on the betting platform.
    https://usounds.com/wp/wp-inc/sovety_po_prohoghdeniyu_metal_gear_solid_v_the_phantom_pain_osnovnye_zadaniya_033.html
    Furthermore, full explanations on how to implement bonus codes are offered for maximum efficiency.
    Remember that some promocodes may have expiration dates, so check them before activating.

  25. In this place you can easily find particular promo codes for one of the leading betting services.
    The set of bonus opportunities is often modified to secure that you always have opportunity to use the most recent suggestions.
    Via these coupons, you can economize considerably on your gambling activities and amplify your potential of success.
    All promo codes are diligently inspected for validity and execution before showing up.
    https://abtechtechnologies.com/wp-content/pgs/lechenie_moghet_byty_vkusnym_i_lechenie_ot_tuberkuleza_blokiruet_lechenie_ot_vich.html
    In addition, we deliver extensive details on how to utilize each special promotion to heighten your gains.
    Keep in mind that some deals may have special provisions or expiration dates, so it’s essential to examine thoroughly all the information before applying them.

  26. Welcome to our platform, where you can discover special content created exclusively for adults.
    All the resources available here is appropriate for individuals who are of legal age.
    Make sure that you are eligible before continuing.
    threesome
    Enjoy a one-of-a-kind selection of age-restricted content, and immerse yourself today!

回复 casino slots 取消回复

您的邮箱地址不会被公开。 必填项已用 * 标注