本文介绍了Spymemcache- Memcache/Membase Faileover的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

平台:64位Windows操作系统,spymemcached-2.7.3.jar,J2EE

我们要使用两个memcache/membase服务器作为缓存解决方案.我们想为每个memcache/membase服务器分配1 GB内存,因此总共可以缓存2 GB数据.我们正在使用spymemcached java客户端从memcache设置和获取数据.我们没有在两个membase服务器之间使用任何复制.

We want to use two memcache/membase servers for caching solution. We want to allocate 1 GB memory to each memcache/membase server so total we can cache 2 GB data.We are using spymemcached java client for setting and getting data from memcache. We are not using any replication between two membase servers.

我们在J2EE应用程序启动时加载memcacheClient对象.

We loading memcacheClient object at the time of our J2EE application startup.

    URI server1 = new URI("http://192.168.100.111:8091/pools");
    URI server2 = new URI("http://127.0.0.1:8091/pools");
    ArrayList<URI> serverList = new ArrayList<URI>();
    serverList.add(server1);
    serverList.add(server2);
    client = new MemcachedClient(serverList, "default", "");

之后,我们使用memcacheClient在memcache/membase服务器中获取并设置值.

After that we are using memcacheClient to get and set value in memcache/membase server.

Object obj = client.get("spoon");
client.set("spoon", 50, "Hello World!");

好像memcacheClient正在设置,并且仅从server1获取值.

Looks like memcacheClient is setting and getting and value only from server1.

如果我们停止server1,则它无法获取/设置值.如果server1宕机,是否应该不使用server2?请让我知道我们在这里做错了什么...

If we stop server1, it fails to get/set value. Should it not use server2 in case of server1 down? Please let me know if we are doing anything wrong here...

推荐答案

aspymemcached Java客户端不处理特定节点的membase故障转移.

aspymemcached java client dos not handle membase failover for particular node.

Ref: https://blog.serverdensity.com/handling-memcached-failover/我们需要手动处理(通过我们的代码)

Ref : https://blog.serverdensity.com/handling-memcached-failover/We need to handle it manually(by our code)

我们可以使用ConnectionObserver这是我的代码:

We can do this by using ConnectionObserverHere is my code :

public static void main(String a[]) throws InterruptedException{
            try {
                URI server1 = new URI("http://192.168.100.111:8091/pools");
                URI server2 = new URI("http://127.0.0.1:8091/pools");
                final  ArrayList<URI> serverList = new ArrayList<URI>();
                serverList.add(server1);
                serverList.add(server2);
               final MemcachedClient client = new MemcachedClient(serverList, "bucketName", "");
                client.addObserver(new ConnectionObserver() {

                    @Override
                    public void connectionLost(SocketAddress arg0) {
                        //method call when connection lost
                        for(MemcachedNode node : client.getNodeLocator().getAll()){
                            if(!node.isActive()){
                                client.shutdown();
                                //re init your client here, and after re-init it will connect to your secodry node
                                break;
                            }
                        }
                    }
                    @Override
                    public void connectionEstablished(SocketAddress arg0, int arg1) {
                        //method call when connection established
                    }
                });
                Object obj = client.get("spoon");
                client.set("spoon", 50, "Hello World!");
            }  catch (Exception e) {
            }
    }

这篇关于Spymemcache- Memcache/Membase Faileover的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 20:42