美文网首页
阿里云-centos7-单机安装部署memcached--以及在

阿里云-centos7-单机安装部署memcached--以及在

作者: georgekaren | 来源:发表于2021-01-13 10:06 被阅读0次

环境信息

-购买操作系统选择centos7(7的任何一个版本都可以),如果选错了可以在阿里云管理面板的-更多--云盘和镜像--更换操作系统。


image.png

在阿里云购买ecs-购买后机器网卡环境:
公网IP-8.134.80.143、内网IP-172.30.40.95


image.png

设置阿里云端口映射:

开放1个端口
11211:memcached调用端口
配置入口-->安全组-->配置规则
点击手动添加,添加11211端口

开始安装

memcached 安装比较简单,直接yum安装完成。

yum -y install memcached


image.png

启动memcached

(停止直接kill掉进程)

memcached -d -u root -l 0.0.0.0 -p 11211 -m 128


image.png

SpringBoot接入调用

引用

pom文件引入

<!--memcached-->
      <dependency>
            <groupId>com.googlecode.xmemcached</groupId>
            <artifactId>xmemcached</artifactId>
            <version>2.4.5</version>
        </dependency>

配置文件application.yaml 的memcached参数

内容(8.134.80.143是阿里云公网IP):

memcache:
  server: 8.134.80.143:11211
  poolSize: 20
  sanitizeKeys: false
  opTimeout: 3000
image.png

创建memcached操作和测试类

属性类-MemcachedProperties:


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "memcache")
public class MemcachedProperties {

    private String server;

    private int poolSize;

    private boolean sanitizeKeys;

    private int opTimeout;

    public String getServer() {
        return server;
    }

    public void setServer(String server) {
        this.server = server;
    }

    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;
    }

    public int getOpTimeout() {
        return opTimeout;
    }

    public void setOpTimeout(int opTimeout) {
        this.opTimeout = opTimeout;
    }
}

初始化注入实现类-MemcacheConfig:


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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class MemcacheConfig {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MemcachedProperties memcachedProperties;

    //构建builder
    @Bean
    public MemcachedClientBuilder getXMBuilder(){
        MemcachedClientBuilder memcachedClientBuilder = null;
        try {
            String server =memcachedProperties.getServer();
            memcachedClientBuilder= new XMemcachedClientBuilder(server);
            memcachedClientBuilder.setFailureMode(false);
            memcachedClientBuilder.setSanitizeKeys(memcachedProperties.isSanitizeKeys());
            memcachedClientBuilder.setConnectionPoolSize(memcachedProperties.getPoolSize());
            memcachedClientBuilder.setCommandFactory(new BinaryCommandFactory());
            memcachedClientBuilder.setOpTimeout(memcachedProperties.getOpTimeout());
            memcachedClientBuilder.setSessionLocator(new KetamaMemcachedSessionLocator());
            return  memcachedClientBuilder;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Bean
    public MemcachedClient getXMClient(MemcachedClientBuilder builder){
        MemcachedClient client = null;
        try {
            client = builder.build();
            logger.info("MemcachedClient 创建成功 ");
            return client;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


}

memcached的controller测试类-MemcacheController:

import lombok.extern.slf4j.Slf4j;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.exception.MemcachedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeoutException;

@Slf4j
@RestController
public class MemcacheController {

    @Autowired
    private MemcachedClient memCachedClient;

    private int cacheTime = 3600*4 ;
    @RequestMapping("cacheSend")
    public String send(String key,String info){
        log.info(info);
        try {
            memCachedClient.set(key, cacheTime, info);
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (MemcachedException e) {
            e.printStackTrace();
        }
        return "cache成功";
    }

    @RequestMapping("cacheGet")
    public String get(String key){
        String info = "";
        try {
             info = memCachedClient.get(key);
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (MemcachedException e) {
            e.printStackTrace();
        }
        return info;
    }
}
image.png

测试

启动项目,注入成功后会提示:MemcachedClient 创建成功


image.png

测试发送存储数据
浏览器访问:http://localhost:8080/cacheSend?key=current_id&info=2021011310101
测试存储current_id:2021011310101到memcached。

image.png

测试读取数据
浏览器访问:http://localhost:8080/cacheGet?key=current_id
测试读取key=current_id的数据。

image.png

测试成功,部署调用完成。

相关文章

网友评论

      本文标题:阿里云-centos7-单机安装部署memcached--以及在

      本文链接:https://www.haomeiwen.com/subject/bvfuaktx.html