文档章节

Xmemcached集群与SpringBoot整合

o
 osc_pn11u1x9
发布于 2018/08/06 09:38
字数 572
阅读 12
收藏 0

精选30+云产品,助力企业轻松上云!>>>

创建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的服务端上。

o
粉丝 0
博文 500
码字总数 0
作品 0
私信 提问
加载中
请先登录后再评论。

暂无文章

jQuery获取select onChange的值 - jQuery get value of select onChange

问题: I was under the impression that I could get the value of a select input by doing this $(this).val(); 我的印象是我可以通过执行$(this).val();来获取选择输入的值$(this).val()......

javail
27分钟前
13
0
道翰天琼解密宇宙信息大脑三者最核心奥秘,破解认知智能基础理论(群聊形式)

三体论是探索研究宇宙,信息和人类大脑三者关系的理论体系。是认知智能的奠基理论体系之一。宇宙和信息,信息和人类大脑,人类大脑和宇宙,三者之间存在着某种未被完全揭示的奥秘。三体论的核...

jackli2020
29分钟前
15
0
OSChina 周日乱弹 —— 这些照片能留存下来要感谢蛇不吃相机

Osc乱弹歌单(2020)请戳(这里) 【今日歌曲】 @薛定谔的兄弟 :分享洛神有语创建的歌单「我喜欢的音乐」: 《28》- ゴンチチ 手机党少年们想听歌,请使劲儿戳(这里) @FalconChen :真得学...

小小编辑
36分钟前
66
0
如何在视频中的对象后面添加图像

作者|PRATEEK JOSHI 编译|VK 来源|Analytics Vidhya 概述 在运动物体后面添加图像是一个典型的计算机视觉项目 了解如何使用传统的计算机视觉技术在视频中添加logo 介绍 我的一位同事向我提出...

人工智能遇见磐创
40分钟前
14
0
UKUI Desktop Environment

install $ sudo add-apt-repository ppa:ubuntukylin-members/ukui3.0$ sudo apt upgrade or $ sudo apt-get install curl$ curl -sL 'https://keyserver.ubuntu.com/pks/lookup?&op=get&......

qwfys
45分钟前
14
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部