spring boot整合memcache
1.导入memcached客户端jar包
<dependency>
<groupId>com.whalin</groupId>
<artifactId>Memcached-Java-Client</artifactId>
<version>3.0.2</version>
</dependency>
2.在application.yml设置memcache连接池配置属性值
memcache:
servers: 127.0.0.1:11211
failover: true
initConn: 100
minConn: 20
maxConn: 1000
maintSleep: 50
nagel: false
socketTO: 3000
aliveCheck: true
3.设置项目启动读取application.yml中memcache属性值
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; @Component
@ConfigurationProperties(prefix = "memcache")
public class SockIOPoolConfig { private String[] servers; private Integer[] weights; private int initConn; private int minConn; private int maxConn; private long maintSleep; private boolean nagle; private int socketTO; public String[] getServers() {
return servers;
} public void setServers(String[] servers) {
this.servers = servers;
} public Integer[] getWeights() {
return weights;
} public void setWeights(Integer[] weights) {
this.weights = weights;
} public int getInitConn() {
return initConn;
} public void setInitConn(int initConn) {
this.initConn = initConn;
} public int getMinConn() {
return minConn;
} public void setMinConn(int minConn) {
this.minConn = minConn;
} public int getMaxConn() {
return maxConn;
} public void setMaxConn(int maxConn) {
this.maxConn = maxConn;
} public long getMaintSleep() {
return maintSleep;
} public void setMaintSleep(long maintSleep) {
this.maintSleep = maintSleep;
} public boolean isNagle() {
return nagle;
} public void setNagle(boolean nagle) {
this.nagle = nagle;
} public int getSocketTO() {
return socketTO;
} public void setSocketTO(int socketTO) {
this.socketTO = socketTO;
}
4.初始化memcache客户端
import com.idelan.iot.client.Memcache;
import com.whalin.MemCached.MemCachedClient;
import com.whalin.MemCached.SockIOPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class MemcacheConfig { @Autowired
private SockIOPoolConfig sockIOPoolConfig; @Bean
public SockIOPool sockIOPool(){ //获取连接池的实例
SockIOPool pool = SockIOPool.getInstance();
//服务器列表及其权重
String[] servers = sockIOPoolConfig.getServers();
Integer[] weights = sockIOPoolConfig.getWeights();
//设置服务器信息
pool.setServers(servers);
pool.setWeights(weights);
//设置初始连接数、最小连接数、最大连接数、最大处理时间
pool.setInitConn(sockIOPoolConfig.getInitConn());
pool.setMinConn(sockIOPoolConfig.getMinConn());
pool.setMaxConn(sockIOPoolConfig.getMaxConn());
//设置连接池守护线程的睡眠时间
pool.setMaintSleep(sockIOPoolConfig.getMaintSleep());
//设置TCP参数,连接超时
pool.setNagle(sockIOPoolConfig.isNagle());
pool.setSocketConnectTO(sockIOPoolConfig.getSocketTO());
//初始化并启动连接池
pool.initialize();
return pool;
} @Bean
public Memcache memCachedClient(){
return new Memcache(new MemCachedClient());
}
5.编写Memcache类,实现memcache基本操作
import com.whalin.MemCached.MemCachedClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils; public class Memcache { private final static Logger logger = LoggerFactory.getLogger(Memcache.class); private MemCachedClient memCachedClient; public Memcache(MemCachedClient memCachedClient) {
this.memCachedClient = memCachedClient;
} /**
* 将netty客户端信息存入memcached
* @param obj
* @return
*/
public boolean addChannel(String key, Object obj) {
try {
if (obj == null) {
return false;
}
return memCachedClient.set(key, obj);
} catch (Exception e) {
logger.error("netty客户端插入memcache出错", e);
return false;
}
} /**
* 将netty客户端信息从memcached中移除
* @param key
* @return
*/
public boolean removeChannel(String key) {
try {
if (StringUtils.isEmpty(key)) {
return false;
} return memCachedClient.delete(key);
} catch (Exception e) {
logger.error("netty客户端移除memcache出错", e);
return false;
}
} /**
* 从memcache中获取netty客户端
* @param key
* @return
*/
public Object getChannel(String key) {
try {
if (StringUtils.isEmpty(key)) {
return null;
}
return memCachedClient.get(key);
} catch (Exception e) {
logger.error("从memcache中获取客户端出错", e);
return null;
}
} /**
* 检测memcache中设备是否登录过
* @param key
* @return
*/
public boolean exist(String key) {
try {
if (StringUtils.isEmpty(key)) {
return false;
} return memCachedClient.keyExists(key);
} catch (Exception e) {
logger.error("检测memcache中是否存在某个设备出错", e);
return false;
}
}
6.方法调用
@Autowired
private Memcache memcache;
通过@Autowired注入Memcache类来调用memcach方法
spring boot整合memcache的更多相关文章
- Spring Boot 整合 Elasticsearch,实现 function score query 权重分查询
摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢! 『 预见未来最好的方式就是亲手创造未来 – <史蒂夫·乔布斯传> 』 运行环境: ...
- spring boot整合jsp的那些坑(spring boot 学习笔记之三)
Spring Boot 整合 Jsp 步骤: 1.新建一个spring boot项目 2.修改pom文件 <dependency> <groupId>or ...
- spring boot 系列之四:spring boot 整合JPA
上一篇我们讲了spring boot 整合JdbcTemplate来进行数据的持久化, 这篇我们来说下怎么通过spring boot 整合JPA来实现数据的持久化. 一.代码实现 修改pom,引入依赖 ...
- Spring Kafka和Spring Boot整合实现消息发送与消费简单案例
本文主要分享下Spring Boot和Spring Kafka如何配置整合,实现发送和接收来自Spring Kafka的消息. 先前我已经分享了Kafka的基本介绍与集群环境搭建方法.关于Kafka的 ...
- Spring Boot整合Mybatis并完成CRUD操作
MyBatis 是一款优秀的持久层框架,被各大互联网公司使用,本文使用Spring Boot整合Mybatis,并完成CRUD操作. 为什么要使用Mybatis?我们需要掌握Mybatis吗? 说的官 ...
- spring boot整合Hadoop
最近需要用spring boot + mybatis整合hadoop,其中也有碰到一些坑,记录下来方便后面的人少走些弯路. 背景呢是因为需要在 web 中上传文件到 hdfs ,所以需要在spring ...
- Spring Boot整合Elasticsearch
Spring Boot整合Elasticsearch Elasticsearch是一个全文搜索引擎,专门用于处理大型数据集.根据描述,自然而然使用它来存储和搜索应用程序日志.与Logstash和K ...
- spring boot 整合 百度ueditor富文本
百度的富文本没有提供Java版本的,只给提供了jsp版本,但是呢spring boot 如果是使用内置tomcat启动的话整合jsp是非常困难得,今天小编给大家带来spring boot整合百度富文本 ...
- spring boot 整合quartz ,job不能注入的问题
在使用spring boot 整合quartz的时候,新建定时任务类,实现job接口,在使用@AutoWire或者@Resource时,运行时出现nullpointException的问题.显然是相关 ...
随机推荐
- Object.prototype.toString.call(obj).slice(8,-1)
1.Object.prototype.toString() 该方法返回描述某个对象数据类型的字符串,如自定义的对象没有被覆盖,则会返回“[object type]”,其中,type则是实际的对象类型. ...
- 数据中台技术汇(二)| DataSimba系列之数据采集平台
继上期数据中台技术汇栏目发布DataSimba——企业级一站式大数据智能服务平台,本期介绍DataSimba的数据采集平台. DataSimba采集平台属于DataSimba的数据计算及服务平台的一部 ...
- EntityNotFoundException EntityExistException
package me.zhengjie.common.exception; import org.springframework.util.StringUtils; import java.util. ...
- PyCharm4.5 中文破解版破解步骤
1.在下载之家下载PyCharm4.5中文版软件包,然后右击软件安装包选择解压到“pycharm4.5.3”. 2.在解压文件夹中找到pycharm-professional-4.5.3,右击打开. ...
- zoj2588-tarjan求桥/割边
tarjan求桥,算法流程详见核心代码: void tarjan(int k){ dfn[k]=low[k]=++cnt; //fa[k]=(edge){f,0,fid}; for(int i=hea ...
- 五、RabbitMQ Java Client基本使用详解
Java Client的5.x版本系列需要JDK 8,用于编译和运行.在Android上,仅支持Android 7.0或更高版本.4.x版本系列支持7.0之前的JDK 6和Android版本. 加入R ...
- 乐观锁(Optimistic Lock)
乐观锁(非阻塞)指不通过锁表来解决并发问题,一般情况下表数据都会加入一个version字段,对该字段进行比较更新来保证数据的一致性. 这里写了个demo,应该可以说明乐观锁的问题. public cl ...
- navisworks安装未完成,某些产品无法安装的解决方法
navisworks提示安装未完成,某些产品无法安装该怎样解决呢?,一些朋友在win7或者win10系统下安装navisworks失败提示navisworks安装未完成,某些产品无法安装,也有时候想重 ...
- apache和tomcat的关系
apache和tomcat的关系: 举个例子:apache是一辆卡车,上面可以装一些东西如html等.但是不能装水,要装水必须要有容器(桶),tomcat就是一个桶(装像JAVA这样的水),而这个桶也 ...
- JS做深度学习3——数据结构
最近在上海上班了,很久没有写博客了,闲下来继续关注和研究Tensorflow.js 关于深度学习的文章我也已经写了不少,部分早期作品可能包含了不少错误的认识,在后面的博文中会改进或重新审视. 今天聊聊 ...