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 ...
随机推荐
- postgrepSQL psql基础操作
1.登录postgrepSQL psql 2.退出postgrepSQL \q 3.查看postgrepSQL里面的数据库 1)psql -l 2)\ l 4.切换DB \c db_name 5.查看 ...
- python3 中的bytes类型
- unbuntu16.04安装geoserver运行环境
1.下载并上传 在windows下载geoserver 2.15.1Platform Independent Binary版本, 是zip文件,然后使用xfile将zip上传到/usr/geoserv ...
- GitLab获取人员参与项目-贡献项目列表
目录 前言 获取token 登录 获取用户参与项目 完整代码 前言 最近在做的统计报表项目包含人员代码提交量. 要获取人员代码提交量首先要知道人员参与的项目.GitLab个人页面中有Contribut ...
- LOJ#2977. 「THUSCH 2017」巧克力(斯坦纳树+随机化)
题目 题目 做法 考虑部分数据(颜色较少)的: 二分中位数\(mid\),将\(v[i]=1000+(v[i]>mid)\) 具体二分操作:然后求出包含\(K\)种颜色的联通快最小的权值和,判断 ...
- 更加方便的使用git上传自己的代码
经过以上的培训,同学们肯定对git的基本使用没有什么问题了.但是每次代码有更改后,依旧需要 git add * git commit * git 打开vim编辑器,编辑提交信息 或者 git ...
- ajax默认是异步的
jquery中的ajax 默认情况下为异步请求,即 async:true,可以通过设置参数 asycn:false 到使其同步 $.ajax({ url: 'www.test.com/test/tes ...
- 使用 Nexus3 Repository Manager 搭建 npm 私服
公司里一般都有自己的私服,用于管理封装的工具插件等,Nexus2主要是用于maven/gralde仓库的统一管理,Nexus3则添加了npm插件,可以对npm提供支持,其实用于npm仓库管理的还有一个 ...
- linux服务器执行cd和ls等等命令都有卡顿现象问题记录
一 执行命令free -gh 1. free 命令显示系统内存的使用情况,包括物理内存.交换内存(swap)和内核缓冲区内存. 2. -g 表示以G单位显示, -m以兆单位显示, h会在数字后面 ...
- Java核心复习——synchronized
一.概念 利用锁机制实现线程同步,synchronized关键字的底层交由了JVM通过C++来实现 Java中的锁有两大特性: 互斥性 同一时间,只允许一个线程持有某个对象锁. 可见性 锁释放前,线程 ...