Redis cluster on Java to enable scalable and highly available

Learn more about the Java build on Redis more information cluster - for scalable and highly available. This Share sharp lesson to learn from [excellent].


What is Redis cluster?

Scalability and availability are two of any enterprise-level database of the most important quality.

You can accurately predict the maximum amount of resources consumed by the database, which is very unusual, therefore, in response to unusually high demand periods, must be scalable. However, in the absence of the availability of scalability situation it is useless, which ensures that users always have access to information in the database when needed.

Redis is a data structure stored in memory, the key may be used to implement non-relational database. However, Redis barebones installation does not provide the best performance immediately.

In order to improve Redis scalability and availability deployment, you can use Redis the Cluster , which is a different Redis method for automatically sliced data between nodes. The Cluster Redis a great Redis database into smaller horizontal partitions, these partitions called slices, are stored on a separate server.

This makes Redis database to accommodate more requests, therefore having a greater scalability. Moreover, even if some of the nodes in the cluster fails, the database can continue to run, so availability can be improved.

In 3.0 before version, Redis Cluster using asynchronous replication. In practice, this means that if Redis Cluster the master server crashes before all write operations sent from the server to which, you can not receive a write operation from the primary server to upgrade equipment, which will result in the loss of a write operation .

From the 3.0 version of the beginning, Redis Cluster also has a WAIT command in the form of synchronous replication option, which prevents current clients until the successful completion of all write commands so far. Although this is not enough to guarantee strong consistency, but it does make the process more secure data transmission.


How to run Redis cluster

There are two ways to start and run Redis Cluster : an easy way and difficult method.

The method involves the use of a simple create-clusterbash script, you can Redis installed utils/create-clusterto locate the script directory. The following two commands will have to create a 6 -node, 3 master node and 3 Ge from the default cluster nodes:

create-cluster start
create-cluster create

 

After you create a cluster, you can interact with it. By default, the first node in the cluster from port 30001 to start. Stop the cluster using the following command:

create-cluster stop

 

运行Redis Cluster的困难方法包括为集群设置自己的配置文件。Redis Cluster的所有实例必须至少包含三个主节点。

以下是单个节点的示例配置文件:

port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes

 

顾名思义,启用集群的选项启用集群模式。cluster-config-file选项包含给定节点的配置文件路径。

要创建具有三个主节点和三个从节点的测试Redis群集实例,请在终端中执行以下命令:

mkdir cluster-test
cd cluster-test
mkdir 7000 7001 7002 7003 7004 7005

 

在这六个目录中的每个目录中,使用上面给出的示例配置文件创建redis.conf配置文件。然后,将你的redis-server可执行文件复制到cluster-test目录中,并使用它在终端的六个不同选项卡中启动六个不同的节点。


Java上连接到Redis集群

与基本的Redis安装一样,Redis Cluster无法立即使用Java编程语言。好消息是,有一些框架使你可以轻松地将Redis ClusterJava一起使用。

RedissonRedisJava客户端,它包括Java中的许多常见构造,包括各种对象,集合,锁和服务。由于Redisson以分布式方式重新实现这些构造,因此它们可以在多个应用程序和服务器之间共享,从而使它们可以与Redis Cluster等工具一起使用。

以下代码演示了Redis集群与Redis集群的用法:

package redis.demo;
import org.redisson.Redisson;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
/**
 * Redis Sentinel Java example
 *
 */
public class Application
{
    public static void main( String[] args )
    {
        Config config = new Config();
        config.useClusterServers()
              .addNodeAddress("redis://127.0.0.1:6379", "redis://127.0.0.1:6380");
        RedissonClient redisson = Redisson.create(config);
        // operations with Redis based Lock
        // implements java.util.concurrent.locks.Lock
        RLock lock = redisson.getLock("simpleLock");
        lock.lock();
        try {
           // do some actions
        } finally {
           lock.unlock();
        }
        // operations with Redis based Map
        // implements java.util.concurrent.ConcurrentMap
        RMap<String, String> map = redisson.getMap("simpleMap");
        map.put("mapKey", "This is a map value");
        String mapValue = map.get("mapKey");
        System.out.println("stored map value: " + mapValue);
        redisson.shutdown();
    }
}

 

Redisson是一个开放源代码客户端,它使Java程序员能够以最小的压力和复杂性来使用Redis,从而极大地简化了开发过程并使之更加熟悉。


谢谢阅读!

更深入的探讨欢迎留言或私信,还可以和你分享更多Java学习资料!


抽丝剥茧 细说架构那些事——【优锐课】


Guess you like

Origin blog.51cto.com/14631216/2462297