環(huán)境信息
-購買操作系統(tǒng)選擇centos7(7的任何一個(gè)版本都可以)括饶,如果選錯(cuò)了可以在阿里云管理面板的-更多--云盤和鏡像--更換操作系統(tǒng)株茶。
在阿里云購買ecs-購買后機(jī)器網(wǎng)卡環(huán)境:
公網(wǎng)IP-8.134.80.143、內(nèi)網(wǎng)IP-172.30.40.95
設(shè)置阿里云端口映射:
開放1個(gè)端口
11211:memcached調(diào)用端口
配置入口-->安全組-->配置規(guī)則
點(diǎn)擊手動(dòng)添加图焰,添加11211端口
開始安裝
memcached 安裝比較簡單启盛,直接yum安裝完成。
yum -y install memcached
啟動(dòng)memcached
(停止直接kill掉進(jìn)程)
memcached -d -u root -l 0.0.0.0 -p 11211 -m 128
SpringBoot接入調(diào)用
引用
pom文件引入
<!--memcached-->
<dependency>
<groupId>com.googlecode.xmemcached</groupId>
<artifactId>xmemcached</artifactId>
<version>2.4.5</version>
</dependency>
配置文件application.yaml 的memcached參數(shù)
內(nèi)容(8.134.80.143是阿里云公網(wǎng)IP):
memcache:
server: 8.134.80.143:11211
poolSize: 20
sanitizeKeys: false
opTimeout: 3000
創(chuàng)建memcached操作和測(cè)試類
屬性類-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;
}
}
初始化注入實(shí)現(xiàn)類-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;
//構(gòu)建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 創(chuàng)建成功 ");
return client;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
}
memcached的controller測(cè)試類-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;
}
}
測(cè)試
啟動(dòng)項(xiàng)目,注入成功后會(huì)提示:MemcachedClient 創(chuàng)建成功
測(cè)試發(fā)送存儲(chǔ)數(shù)據(jù)
瀏覽器訪問:http://localhost:8080/cacheSend?key=current_id&info=2021011310101
測(cè)試存儲(chǔ)current_id:2021011310101到memcached技羔。
測(cè)試讀取數(shù)據(jù)
瀏覽器訪問:http://localhost:8080/cacheGet?key=current_id
測(cè)試讀取key=current_id的數(shù)據(jù)僵闯。
測(cè)試成功,部署調(diào)用完成藤滥。