简体   繁体   English

如何在应用程序节点之间同步令牌(字符串值)?

[英]How to synchronize token(string value) between application nodes?

We have a Spring 3 + REST application in which we are using a token to identify source of request coming in. This token is kept in-memory(in a hashmap) and used to identify the request. 我们有一个Spring 3 + REST应用程序,我们在其中使用令牌来标识传入的请求源。此令牌保留在内存中(在哈希图中)并用于标识请求。 When we are multiple instance of our app(deployed on multiple tomcat instances on different machines), how can we share/sync this token between different app nodes? 当我们是应用程序的多个实例时(部署在不同机器上的多个tomcat实例上),我们如何在不同的应用程序节点之间共享/同步此令牌? Our only requirement is to sync this token among different nodes. 我们唯一的要求是在不同节点之间同步此令牌。

I browsed and found few apis'/software like Redis, memcached, zookeeper. 我浏览并发现很少的api /软件,例如Redis,memcached,zookeeper。 I am not able to decide which one to select. 我无法决定选择哪一个。

Any kind of suggestion/comments is helpful. 任何类型的建议/意见都是有帮助的。

Regards, Pramod 问候,普拉莫德

I've never used zookeeper so I cannot say anything about it. 我从未使用过Zookeeper,所以我对此一无所知。 Both Redis (database) and memcached (cache) will work for you. Redis(数据库)和memcached(缓存)都将为您工作。 Which one is better depends on how you use tokens. 哪种更好取决于您如何使用令牌。

Choose Redis if: 如果满足以下条件,则选择Redis:

  • tokens may be stored for a long time 令牌可能会存储很长时间
  • no need to expire tokens 无需使令牌过期
  • want to handle more tokens than can be stored in memory 想要处理比存储在内存中更多的令牌
  • want to replicate tokens to other servers so if one goes down other will provide tokens 想要将令牌复制到其他服务器,因此如果其中一个发生故障,其他服务器将提供令牌
  • tokens have to survive server restart 令牌必须在服务器重启后才能幸免

Choose memcached if: 如果满足以下条件,则选择内存缓存:

  • tokens are valid only for certain amount of time and should expire 令牌仅在一定时间内有效,并且应该过期
  • amount of tokens exeed memory capacity the least recently used token will be removed 令牌数量超过内存容量最近使用最少的令牌将被删除
  • all tokens may be stored in memory 所有令牌都可以存储在内存中
  • no need to replicate tokens to other server 无需将令牌复制到其他服务器
  • tokens don't have to survive server restart 令牌不必在服务器重启后幸存下来
  • want to use Spring Cache abstraction 想要使用Spring Cache抽象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM