创建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整合的更多相关文章

  1. springboot和Redis集群版的整合

    此篇接上一个文章springboot和Redis单机版的整合 https://www.cnblogs.com/lin530/p/12019023.html 下面接着介绍和Redis集群版的整合. 1. ...

  2. RabbitMQ 3.9.7 镜像模式集群与Springboot 2.5.5 整合

    1. 概述 老话说的好:做人要懂得变通,善于思考,有时稍微转个弯,也许问题就解决了. 言归正传,之前我们聊了 RabbitMQ 3.9.7 镜像模式集群的搭建,今天我们来聊聊 RabbitMQ 3.9 ...

  3. docker搭建redis主从集群和sentinel哨兵集群,springboot客户端连接

    花了两天搭建redis主从集群和sentinel哨兵集群,讲一下springboot客户端连接测试情况 redis主从集群 从网上查看说是有两种方式:一种是指定配置文件,一种是不指定配置文件 引用地址 ...

  4. springboot+shiro+redis(集群redis版)整合教程

    相关教程: 1. springboot+shiro整合教程 2. springboot+shiro+redis(单机redis版)整合教程 3.springboot+shiro+redis(单机red ...

  5. 如何在Windows上部署Redis集群和SpringBoot进行整合

    一.安装Redis的Windows版本并进行配置 (1)下载链接 https://github.com/microsoftarchive/redis/releases (2)将下载后的Redis复制成 ...

  6. RocketMQ 4.7.1 环境搭建、集群、MQ整合SpringBoot

    导读 之前学过ActiveMQ但是并发量不是很大点我直达,所以又学阿里开源的RocketMQ,据说队列可以堆积亿级别.下面是网上找的消息队列对比图,仅供参考 部署 官网 点我直达 前置条件 推荐使用6 ...

  7. Elasticsearch 分片集群原理、搭建、与SpringBoot整合

    单机es可以用,没毛病,但是有一点我们需要去注意,就是高可用是需要关注的,一般我们可以把es搭建成集群,2台以上就能成为es集群了.集群不仅可以实现高可用,也能实现海量数据存储的横向扩展. 新的阅读体 ...

  8. 04.redis集群+SSM整合使用

    redis集群+SSM整合使用 首先是创建redis-cluster文件夹: 因为redis最少需要6个节点(三主三从),为了更好的理解,我这里创建了两台虚拟机(192.168.0.109 192.1 ...

  9. jedis集群版应用

    1.pom文件添加依赖: 2.创建配置文件 <!-- jedis集群版配置(JedisCluster通过构造传参(2个参数)) --> <bean id="redisCli ...

随机推荐

  1. Nginx设置禁止通过IP访问服务器并且只能通过指定域名访问

    为了避免别人把未备案的域名解析到自己的服务器IP而导致服务器被断网,需要在nginx上设置禁止通过IP访问服务器,只能通过域名访问. 最关键的一点是,在server的设置里面添加这么一行: liste ...

  2. postgrepSQL psql基础操作

    1.登录postgrepSQL psql 2.退出postgrepSQL \q 3.查看postgrepSQL里面的数据库 1)psql -l 2)\ l 4.切换DB \c db_name 5.查看 ...

  3. SpringBoot项目中,Redis的初次使用

    1.引入Redis依赖包,在application.yml中配置redis <dependency> <groupId>org.springframework.boot< ...

  4. learning scala Function Composition andThen

    Ordering using andThen: f(x) andThen g(x) = g(f(x)) Ordering using compose: f(x) compose g(x) = f(g( ...

  5. 2017.11.7 Noip2017 考前模拟赛

    ----------------------------------T1---------------------------------- ——>数学老师的报复 题目描述 11 班数学大佬 Y ...

  6. Centos 7.x 设置Lvs+ Keepalived

    [实验环境] Centos 7.2 Nginx  以下为本次试验所使用的地址: VIP:192.168.136.100 LVS-1:192.168.136.170 LVS-2:192.168.136. ...

  7. 阿里云服务器linux系统上 安装node环境

    今天将阿里云服务器重windows系统重置成了linux系统, 点击上面的实例名称,进入到 点击  更换系统盘   选择linux 的centos版本(操作前需要  停止实例) 弄好后,启动实例 下面 ...

  8. ROSservice 通信方式

    操作演示,对 service 通信的理解请看:点击打开链接 1. 使用 rosservice 1.1  rosservice list 假设小乌龟节点仍在运行  rosrun turtlesim tu ...

  9. SSH dao层异常 org.hibernate.HibernateException: No Session found for current thread

    解决方法: 在 接口方法中添加 事务注解 即可. public interface IBase<PK extends Serializable, T> { @Transactional v ...

  10. 在OpenFOAM中做用户自定义库——编译library【转载】

    转载自:http://openfoam.blog.sohu.com/22041538.html OpenFOAM自己提供的标准类都是以库的形式提供的,并且利用头文件给出了库的应用接口.这样一来,用户的 ...