Memcached的Java客户端目前有三个:
- Memcached Client for Java 比 SpyMemcached更稳定、更早、更广泛;
- SpyMemcached 比 Memcached Client for Java更高效;
- XMemcached 比 SpyMemcache并发效果更好;
本文以Maven来构建项目,简单展示XMemcached的使用。
- 首先使用Maven的骨架简单构造一个项目;
mvn archetype:generate
我这里给出默认都是序号为16的“ org.apache.maven.archetypes:maven-archetype-quickstart”;回车;
然后依次输入groupId:net.oseye,artifactId:DemoXMemcached,默认的版本号,默认的packeage,最后“Y”,创建项目。 - 修改Pom.xml,加入:
<dependency> <groupId>com.googlecode.xmemcached</groupId> <artifactId>xmemcached</artifactId> <version>1.4.3</version> </dependency>
chaed & mvn ccd DemoXMemompile
- 这时项目就构建好了,可以生成Eclipse项目,来用功能强大的Eclipse来编辑项目:
mvn eclipse:eclipse
- 使用eclipse把DemoXMemcached项目Import进来:
- 因为XMemcached使用了SLF4J框架,而从上面的应用类库来看,显然没有绑定日志框架,由SLF4J知识,我们知道需要在Pom.xml在添加一个依赖:
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.5</version> </dependency>
- 再重新生成eclipse项目,就能看到引用类库如:
现在虽然依赖库都齐全了,就需要添加log4j的配置文件了,关于Log4j的配置文件请参考之前的文章,简单配置文件“log4j.properties”内容:log4j.rootLogger=INFO,CONSOLE log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.Threshold=WARN log4j.appender.CONSOLE.Target=System.out log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=[%-5p]%d{yyyy-MM-dd HH:mm:ssS} %c %m%n
- 关于“log4j.properties”放在哪里呢?这又是Maven的约定优先配置的原则了,其实我们打开pom.xml,切换至于"Effective POM"面板,就能发现:
PS:只有安装了Eclipse的Maven插件才能看到上面的图示,- Overview:显示maven项目的一些基本信息.
- Dependencies:添加jar包的页面,很重要!
- Plugins:添加maven插件的页面.比如tomcat-maven-plugin等.
- Dependency Hierarchy:用于显示jar包的依赖关系.没事的时候可以看看jar包的依赖关系.
- Effective POM:显示maven的编译路径,plugin之类的.可以无视.
Log4j默认加载根目录的“log4j.properties”,如果你的路径不是这样的,则需要另行配置,这里不再赘述。 - XMemcached示例Demo:
package net.oseye; import java.io.IOException; import net.rubyeye.xmemcached.MemcachedClient; import net.rubyeye.xmemcached.MemcachedClientBuilder; import net.rubyeye.xmemcached.XMemcachedClientBuilder; import net.rubyeye.xmemcached.utils.AddrUtil; public class App { public static void main( String[] args ) throws IOException { MemcachedClientBuilder builder = new XMemcachedClientBuilder( AddrUtil.getAddresses("168.192.0.75:11211")); MemcachedClient memcachedClient = builder.build(); try { memcachedClient.set("hello", 0, "Hello,xmemcached"); String value = memcachedClient.get("hello"); System.out.println("hello 值:"+value); }catch (Exception e) { } try { memcachedClient.shutdown(); } catch (IOException e) { System.err.println("Shutdown MemcachedClient fail"); e.printStackTrace(); } } }
[WARN ]2014-03-18 15:32:50515 net.rubyeye.xmemcached.XMemcachedClient XMemcachedClient use Text protocol
[INFO ]2014-03-18 15:32:50687 com.google.code.yanf4j.nio.impl.SelectorManager Creating 4 reactors...
[WARN ]2014-03-18 15:32:50750 com.google.code.yanf4j.core.impl.AbstractController The Controller started at localhost/127.0.0.1:0 ...
[WARN ]2014-03-18 15:32:50765 com.google.code.yanf4j.core.impl.AbstractController Add a session: 192.168.0.75:11211
hello 值:Hello,xmemcached
[INFO ]2014-03-18 15:32:50796 com.google.code.yanf4j.core.impl.AbstractController Controller has been stopped.
[WARN ]2014-03-18 15:32:50796 com.google.code.yanf4j.core.impl.AbstractController Remove a session: 192.168.0.75:11211log4j.rootLogger=INFO,CONSOLE log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.Threshold=WARN log4j.appender.CONSOLE.Target=System.out log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=[%-5p]%d{yyyy-MM-dd HH:mm:ssS} %c %m%n log4j.logger.com.google=ERROR log4j.logger.net.rubyeye=ERROR
hello 值:Hello,xmemcached
PS:最好使用eclipse的maven插件,应用使用Maven生成的eclipse工程,有的时候不知道啥原因,找不到Maven命令按钮,只能Maven和Eclipse来回切换,也蛮麻烦的。(发现了,其实只要在pom.xml文件上右键的Run As就能出现Maven命令,或许pom.xml就是maven的标志原因吧)