Xmemcached集群与SpringBoot整合
创建SpringBoot项目xmemcached_springboot,添加开发需要的包名和类名,项目的目录结构如下:

添加XMemcached依赖:
<dependency>
<groupId>com.googlecode.xmemcached</groupId>
<artifactId>xmemcached</artifactId>
<version>2.4.0</version>
</dependency>
添加XMemcached相关配置到属性文件application.yml
################### memcached配置 ###################
memcached:
servers: 192.168.84.128:2222 192.168.84.128:6666
poolSize: 10
sanitizeKeys: false
openCache: true # true为启动缓存 false为标准实现
编写读取上述属性的读取类:XMemcachedProperties.java
package com.czhappy.xmemcached_springboot.properties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; @Component
@ConfigurationProperties(prefix = "memcached")
public class XMemcachedProperties { private String servers;
private int poolSize;
private boolean sanitizeKeys;
private boolean openCache; public boolean isOpenCache() {
return openCache;
} public void setOpenCache(boolean openCache) {
this.openCache = openCache;
} public String getServers() {
return servers;
} public void setServers(String servers) {
this.servers = servers;
} public int getPoolSize() {
return poolSize;
} public void setPoolSize(int poolSize) {
this.poolSize = poolSize;
} public boolean isSanitizeKeys() {
return sanitizeKeys;
} public void setSanitizeKeys(boolean sanitizeKeys) {
this.sanitizeKeys = sanitizeKeys;
}
}
编写XMemcachedConfig.java,构建builder和client
package com.czhappy.xmemcached_springboot.config; import com.czhappy.xmemcached_springboot.properties.XMemcachedProperties;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.command.BinaryCommandFactory;
import net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class XMemcachedConfig { @Autowired
private XMemcachedProperties xMemcachedProperties; // 构建builder
@Bean
public MemcachedClientBuilder getXMBuilder(){
MemcachedClientBuilder memcachedClientBuilder = null;
try{
String servers = xMemcachedProperties.getServers();
System.out.println("servers="+servers);
memcachedClientBuilder = new XMemcachedClientBuilder(servers);
// 开启/关闭failure模式
memcachedClientBuilder.setFailureMode(false);
memcachedClientBuilder.setSanitizeKeys(xMemcachedProperties.isSanitizeKeys());
memcachedClientBuilder.setConnectionPoolSize(xMemcachedProperties.getPoolSize());
memcachedClientBuilder.setCommandFactory(new BinaryCommandFactory());
memcachedClientBuilder.setOpTimeout(3000);
memcachedClientBuilder.setSessionLocator(new KetamaMemcachedSessionLocator()); // 诸多XMemcached配置
return memcachedClientBuilder;
}catch(Exception e){
e.printStackTrace();
}
return null;
} // client
@Bean
public MemcachedClient getXMClient(MemcachedClientBuilder memcachedClientBuilder){
MemcachedClient memcachedClient = null;
try{
memcachedClient = memcachedClientBuilder.build();
return memcachedClient;
}catch(Exception e){
e.printStackTrace();
}
return null; }
}
编写ShowApi.java文件,此处仅实现新增和查询方法,其他方法可参考添加
package com.czhappy.xmemcached_springboot.utils; import net.rubyeye.xmemcached.MemcachedClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class ShowApi { @Autowired
private MemcachedClient memcachedClient; /**
* 新增
* @param key
* @param value
*/
public void showAdd(String key, String value){
try {
memcachedClient.set(key, 0, value);
}catch (Exception e){
e.printStackTrace();
}
} public String showQuery(String key){
try {
return memcachedClient.get(key);
}catch (Exception e){
e.printStackTrace();
}
return "";
}
}
编写测试类,添加数据并打印查询的结果:
package com.czhappy.xmemcached_springboot; import com.czhappy.xmemcached_springboot.utils.ShowApi;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest
public class XmemcachedSpringbootApplicationTests { @Autowired
private ShowApi showApi; @Test
public void contextLoads() { String key = "goods_name";
String value = "apple";
showApi.showAdd(key, value);
System.out.println(showApi.showQuery(key)); } }
为了测试memcached集群和数据存储,我们分别启动端口为2222和6666的memcached服务端


运行测试类,可以看到后台打印出存储的结果

说明数据保存成功,我们也可以去服务端直接查询
2222端口:

6666端口:

数据具体存储在哪个memcached上我们是无法预知的,它是由客户端XMemcached内部hash算法判断实现,此处我们可以知道数据是存储成功了,通过查询最终结果能知道存在端口为6666的服务端上。
Xmemcached集群与SpringBoot整合的更多相关文章
- springboot和Redis集群版的整合
此篇接上一个文章springboot和Redis单机版的整合 https://www.cnblogs.com/lin530/p/12019023.html 下面接着介绍和Redis集群版的整合. 1. ...
- RabbitMQ 3.9.7 镜像模式集群与Springboot 2.5.5 整合
1. 概述 老话说的好:做人要懂得变通,善于思考,有时稍微转个弯,也许问题就解决了. 言归正传,之前我们聊了 RabbitMQ 3.9.7 镜像模式集群的搭建,今天我们来聊聊 RabbitMQ 3.9 ...
- docker搭建redis主从集群和sentinel哨兵集群,springboot客户端连接
花了两天搭建redis主从集群和sentinel哨兵集群,讲一下springboot客户端连接测试情况 redis主从集群 从网上查看说是有两种方式:一种是指定配置文件,一种是不指定配置文件 引用地址 ...
- springboot+shiro+redis(集群redis版)整合教程
相关教程: 1. springboot+shiro整合教程 2. springboot+shiro+redis(单机redis版)整合教程 3.springboot+shiro+redis(单机red ...
- 如何在Windows上部署Redis集群和SpringBoot进行整合
一.安装Redis的Windows版本并进行配置 (1)下载链接 https://github.com/microsoftarchive/redis/releases (2)将下载后的Redis复制成 ...
- RocketMQ 4.7.1 环境搭建、集群、MQ整合SpringBoot
导读 之前学过ActiveMQ但是并发量不是很大点我直达,所以又学阿里开源的RocketMQ,据说队列可以堆积亿级别.下面是网上找的消息队列对比图,仅供参考 部署 官网 点我直达 前置条件 推荐使用6 ...
- Elasticsearch 分片集群原理、搭建、与SpringBoot整合
单机es可以用,没毛病,但是有一点我们需要去注意,就是高可用是需要关注的,一般我们可以把es搭建成集群,2台以上就能成为es集群了.集群不仅可以实现高可用,也能实现海量数据存储的横向扩展. 新的阅读体 ...
- 04.redis集群+SSM整合使用
redis集群+SSM整合使用 首先是创建redis-cluster文件夹: 因为redis最少需要6个节点(三主三从),为了更好的理解,我这里创建了两台虚拟机(192.168.0.109 192.1 ...
- jedis集群版应用
1.pom文件添加依赖: 2.创建配置文件 <!-- jedis集群版配置(JedisCluster通过构造传参(2个参数)) --> <bean id="redisCli ...
随机推荐
- jq time 控件
当有时需要调用到添加时间是 就会用到时间控件 My97DatePicker是一款非常灵活好用的日期控件.使用非常简单. My97DatePicker使用方法 1.下载My97DatePicker组件包 ...
- MySQL 事务 MVCC 版本链
版本链 对于使用InnoDB存储引擎的表来说,它的聚簇索引记录中都包含两个必要的隐藏列(row_id并不是必要的,我们创建的表中有主键或者非NULL唯一键时都不会包含row_id列): 1) ...
- [JXOI2017]颜色
\(Orz\) 各位题解大佬,我来膜拜一发 还有单调栈实在没弄懂 法一:线段树+堆 首先,讨论区间的个数的题目,我们可以想到枚举一个端点\(r\),找到所有的\(l\) 我们不妨设:\(ml[i]\) ...
- K Simple question (第十届山东理工大学ACM网络编程擂台赛 正式赛)
题解:素数筛+唯一分解定理 可以把素数筛那部分放到while之外,减小时间复杂度. #include <stdio.h> #include <stdlib.h> #includ ...
- Ubuntu 14.04 indigo 安装 cartographer 1.0.0
安装依赖(cmake 版本为2.8,我的是自带的)sudo apt-get updatesudo apt-get install -y g++ git google-mock libboost-all ...
- 【SPOJ】Longest Common Substring II
[SPOJ]Longest Common Substring II 多个字符串求最长公共子串 还是将一个子串建SAM,其他字符串全部跑一边,记录每个点的最大贡献 由于是所有串,要对每个点每个字符串跑完 ...
- 2.线程--线程安全(synchronized)
同步锁:synchronized,(缺点:不能手动开锁 解锁) 1.同步代码块 public void run(){ synchronized(this){/使用this关键字表示同一把锁,非静态 ...
- BZOJ1941Hide and Seek
做KD_tree的入门题. 问题就是求出任意一个点距其他点的最大曼哈顿距离和最小曼哈顿距离差,然后对其取min即可. 这个东西就是KD_tree可以轻松解决的了. 下面总结一下做KD_tree(不带修 ...
- OpenResty之ngx.var.VARIABLE
1. ngx.var.VARIABLE syntax: ngx.var.VAR_NAME context: set_by_lua*, rewrite_by_lua*, access_by_lua*, ...
- Linux如何使用shell命令检测PHP木马防止脚本木马的破坏
1.一句话查找PHP木马 代码如下 # find / -name "*.php" |xargs egrep "phpspy|c99sh|milw0rm|eval\(gun ...