• java连接couchbase的memcache


    没有参考代码的时候重新写还是有点累的,这是重温了,第一次写的时候参考了别的一些乱七八糟的代码,虽然不是和这个一样的,不过在不懂的情况下还是很快写出来了,反而是这次光是找需要哪些jar都搞了半天,最后发现extern的用一个jar就够了,addresses和auth之前也是糊里糊涂的,这次总算搞清楚了

    maven引入

    <dependency>
        <groupId>com.googlecode.xmemcached</groupId>
        <artifactId>xmemcached</artifactId>
        <version>1.4.3</version>
     </dependency>

    先按照在前面的一篇《记一次couchbase(memcached)安装以及使用》的步骤安装好,类型选择couchbase就行了,
    测试代码如下:

    package Test.test;
    
    import java.io.IOException;
    import java.util.concurrent.TimeoutException;
    
    import net.rubyeye.xmemcached.MemcachedClient;
    import net.rubyeye.xmemcached.MemcachedClientBuilder;
    import net.rubyeye.xmemcached.XMemcachedClientBuilder;
    import net.rubyeye.xmemcached.auth.AuthInfo;
    import net.rubyeye.xmemcached.command.BinaryCommandFactory;
    import net.rubyeye.xmemcached.exception.MemcachedException;
    import net.rubyeye.xmemcached.utils.AddrUtil;
    public class App 
    {
        public static void main( String[] args )
        {
            try {
                MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("192.168.92.128:11211")) ;
                builder.addAuthInfo(AddrUtil.getOneAddress("192.168.92.128:11211"), AuthInfo.plain("azul", "xxxx"));
                builder.setCommandFactory(new BinaryCommandFactory());
                builder.setFailureMode(true);
                builder.setConnectionPoolSize(10); 
                MemcachedClient client = builder.build();
                client.set("lwk", 0, "hello");
                String value = client.get("key");
                System.out.println(value);
                client.shutdown();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (TimeoutException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (MemcachedException e) {
                e.printStackTrace();
            }
            System.out.println( "end..." );
        }
    }

    执行结果:

    SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
    SLF4J: Defaulting to no-operation (NOP) logger implementation
    SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
    hello
    end...

    管理界面上可以看到:

    它是密文存储的

    如果不用sasl验证的话先把服务器上的验证去掉然后这样写

    package Test.test;
    
    import net.spy.memcached.AddrUtil;
    import net.spy.memcached.MemcachedClient;
    
    public class App 
    {
        public static void main( String[] args )
        {
            try {
                MemcachedClient client = new MemcachedClient(AddrUtil.getAddresses("192.168.92.128:11211"));
                client.set("lwk", 0, "hello");
                Object value = client.get("lwk");
                System.out.println(value);
                client.shutdown();
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println( "end..." );
        }
    }

    结果:

    2015-03-01 23:26:38.268 INFO net.spy.memcached.MemcachedConnection:  Added {QA sa=/192.168.92.128:11202, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
    2015-03-01 23:26:38.271 INFO net.spy.memcached.MemcachedConnection:  Connection state changed for sun.nio.ch.SelectionKeyImpl@be7667
    2015-03-01 23:26:38.276 WARN net.spy.memcached.MemcachedConnection:  Could not redistribute to another node, retrying primary node for lwk.
    hello
    end...
    2015-03-01 23:26:38.284 INFO net.spy.memcached.MemcachedConnection:  Shut down memcached client

    这里是通过java client访问,一会儿用命令行试一下。

     服务器上起了一个memcached服务之后,

    如果是auth-less的,可以用telnet客户端直接去连接

    C:UsersAdministrator>telnet 192.168.92.128 11200

    输入stats,得到一系列的服务器上的信息

    stats
    STAT delete_misses 0
    STAT rejected_conns 0
    STAT connection_structures 5000
    STAT reclaimed 0
    STAT limit_maxbytes 67108864
    STAT decr_hits 0
    STAT curr_conns_on_port_11209 12
    STAT curr_conns_on_port_11210 5
    STAT curr_connections 19
    STAT rusage_system 17.695309
    STAT cas_misses 0
    STAT bytes 64
    STAT get_misses 1
    STAT bytes_read 380638
    STAT get_hits 4
    STAT decr_misses 0
    STAT rusage_user 1093.362783
    STAT bucket_conns 9
    STAT cmd_flush 0
    STAT tcp_nodelay enable
    STAT uptime 25500
    STAT max_conns_on_port_11209 1000
    STAT total_items 2
    STAT incr_hits 0
    STAT max_conns_on_port_11210 9000
    STAT time 1425174744
    STAT daemon_connections 4
    STAT pointer_size 64
    STAT version 2.1.0r
    STAT engine_maxbytes 104857600
    STAT evictions 0
    STAT total_connections 58
    STAT curr_items 1
    STAT delete_hits 0
    STAT cas_hits 0
    STAT auth_errors 0
    STAT threads 4
    STAT pid 1465
    STAT auth_cmds 4
    STAT cas_badval 0
    STAT cmd_set 2
    STAT bucket_active_conns 1
    STAT cmd_get 5
    STAT conn_yields 0
    STAT listen_disabled_num 0
    STAT bytes_written 8718605
    STAT libevent 2.0.11-stable
    STAT accepting_conns 1
    STAT incr_misses 0
    END

    现在可以add、set、get

  • 相关阅读:
    Spring注解@Resource和@Autowired区别对比
    Http请求中Content-Type讲解以及在Spring MVC中的应用
    解决SpringMVC的@ResponseBody返回中文乱码
    JVM之类加载器下篇
    JVM之类加载器中篇
    JVM之类加载器上篇
    HashMap的resize和Fail-Fast机制
    HashMap的实现原理
    Tomcat中JVM内存溢出及合理配置
    redis的主从复制,读写分离,主从切换
  • 原文地址:https://www.cnblogs.com/azul0906/p/4307742.html
Copyright © 2020-2023  润新知