SpringBoot集成Memcached的項(xiàng)目實(shí)踐
1、Memcached 介紹
Memcached 是一個(gè)高性能的分布式內(nèi)存對(duì)象緩存系統(tǒng),用于動(dòng)態(tài)Web應(yīng)用以減輕數(shù)據(jù)庫(kù)負(fù)載。它通過在內(nèi)存中
緩存數(shù)據(jù)和對(duì)象來減少讀取數(shù)據(jù)庫(kù)的次數(shù),從而提高動(dòng)態(tài)、數(shù)據(jù)庫(kù)驅(qū)動(dòng)網(wǎng)站的速度。Memcached基于一個(gè)存儲(chǔ)
鍵/值對(duì)的hashmap。其守護(hù)進(jìn)程(daemon )是用C寫的,但是客戶端可以用任何語言來編寫,并通過
memcached協(xié)議與守護(hù)進(jìn)程通信。
因?yàn)?Spring Boot 沒有針對(duì) Memcached 提供對(duì)應(yīng)的組建包,因此需要我們自己來集成。官方推出的 Java 客戶端
Spymemcached 是一個(gè)比較好的選擇之一。
Spymemcached 最早由 Dustin Sallings 開發(fā),Dustin 后來和別人一起創(chuàng)辦了 Couchbase (原NorthScale),職位
為首席架構(gòu)師。2014 加入 Google。Spymemcached 是一個(gè)采用 Java 開發(fā)的異步、單線程的 Memcached 客戶
端, 使用 NIO 實(shí)現(xiàn)。Spymemcached 是 Memcached 的一個(gè)流行的 Java client 庫(kù),性能表現(xiàn)出色,廣泛應(yīng)用于
Java + Memcached 項(xiàng)目中。
2、Windows下安裝Memcached
官網(wǎng)上并未提供 Memcached 的 Windows 平臺(tái)安裝包,我們可以使用以下鏈接來下載,你需要根據(jù)自己的系統(tǒng)平
臺(tái)及需要的版本號(hào)點(diǎn)擊對(duì)應(yīng)的鏈接下載即可:
32位系統(tǒng) 1.2.5版本:
http://static.runoob.com/download/memcached-1.2.5-win32-bin.zip
32位系統(tǒng) 1.2.6版本:
http://static.runoob.com/download/memcached-1.2.6-win32-bin.zip
32位系統(tǒng) 1.4.4版本:
http://static.runoob.com/download/memcached-win32-1.4.4-14.zip
64位系統(tǒng) 1.4.4版本:
http://static.runoob.com/download/memcached-win64-1.4.4-14.zip
32位系統(tǒng) 1.4.5版本:
http://static.runoob.com/download/memcached-1.4.5-x86.zip
64位系統(tǒng) 1.4.5版本:
http://static.runoob.com/download/memcached-1.4.5-amd64.zip
在 1.4.5 版本以前 memcached 可以作為一個(gè)服務(wù)安裝,而在 1.4.5 及之后的版本刪除了該功能。因此我們以下介
紹兩個(gè)不同版本 1.4.4 及 1.4.5的不同安裝方法。
2.1 memcached <1.4.5 版本安裝
1、解壓下載的安裝包到指定目錄。
2、在 1.4.5 版本以前 memcached 可以作為一個(gè)服務(wù)安裝,使用管理員權(quán)限運(yùn)行以下命令:
c:\memcached\memcached.exe -d install
3、然后我們可以使用以下命令來啟動(dòng)和關(guān)閉 memcached 服務(wù):
c:\memcached\memcached.exe -d start c:\memcached\memcached.exe -d stop
4、如果要修改 memcached 的配置項(xiàng), 可以在命令行中執(zhí)行 regedit.exe
命令打開注冊(cè)表并找到
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\memcached
來進(jìn)行修改。
如果要提供 memcached 使用的緩存配置可以修改 ImagePath
為:
"c:\memcached\memcached.exe" -d runservice -m 512
-m 512
意思是設(shè)置 memcached 最大的緩存配置為512M。
此外我們還可以通過使用 memcached.exe -h
命令查看更多的參數(shù)配置。
5、如果我們需要卸載 memcached ,可以使用以下命令:
c:\memcached\memcached.exe -d uninstall
2.2 memcached >= 1.4.5 版本安裝
1、解壓下載的安裝包到指定目錄。
2、在 memcached1.4.5 版本之后,memcached 不能作為服務(wù)來運(yùn)行,需要使用任務(wù)計(jì)劃中來開啟一個(gè)普通的
進(jìn)程,在 window 啟動(dòng)時(shí)設(shè)置 memcached自動(dòng)執(zhí)行。
我們使用管理員身份執(zhí)行以下命令將 memcached 添加來任務(wù)計(jì)劃表中:
schtasks /create /sc onstart /tn memcached /tr "'c:\memcached\memcached.exe' -m 512"
注意:-m 512
意思是設(shè)置 memcached 最大的緩存配置為512M。
注意:我們可以通過使用 c:\memcached\memcached.exe -h
命令查看更多的參數(shù)配置。
3、如果需要?jiǎng)h除 memcached 的任務(wù)計(jì)劃可以執(zhí)行以下命令:
schtasks /delete /tn memcached
3、整合Memcached
3.1 pom依賴
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> </parent> <groupId>com.example</groupId> <artifactId>spring-boot-memcache-spymemcached</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-boot-memcache-spymemcached</name> <description>spring-boot-memcache-spymemcached</description> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>net.spy</groupId> <artifactId>spymemcached</artifactId> <version>2.12.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3.2 配置文件
memcache.ip=127.0.0.1 memcache.port=11211
分別配置 memcache 的 Ip 地址和 端口。
3.3 設(shè)置配置對(duì)象
創(chuàng)建 MemcacheSource
接收配置信息
package com.example.config; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "memcache") public class MemcacheSource { private String ip; private int port; public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } }
@ConfigurationProperties(prefix = "memcache")
的意思會(huì)以 memcache.*
為開通將對(duì)應(yīng)的配置文件加載
到屬性中。
3.4 啟動(dòng)初始化 MemcachedClient
利用 CommandLineRunner
在項(xiàng)目啟動(dòng)的時(shí)候配置好 MemcachedClient
。
package com.example.config; import net.spy.memcached.MemcachedClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.io.IOException; import java.net.InetSocketAddress; @Component public class MemcachedRunner implements CommandLineRunner { protected Logger logger = LoggerFactory.getLogger(this.getClass()); @Resource private MemcacheSource memcacheSource; private MemcachedClient client = null; @Override public void run(String... args) throws Exception { try { client = new MemcachedClient(new InetSocketAddress(memcacheSource.getIp(), memcacheSource.getPort())); } catch (IOException e) { logger.error("inint MemcachedClient failed ", e); } } public MemcachedClient getClient() { return client; } }
3.5 啟動(dòng)類
package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
3.6 測(cè)試
package com.example; import com.example.config.MemcachedRunner; import net.spy.memcached.MemcachedClient; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; @RunWith(SpringRunner.class) @SpringBootTest public class RepositoryTests { @Resource private MemcachedRunner memcachedRunner; @Test public void testSetGet() { MemcachedClient memcachedClient = memcachedRunner.getClient(); memcachedClient.set("testkey", 1000, "666666"); System.out.println("*********** " + memcachedClient.get("testkey").toString()); } }
使用中先測(cè)試插入一個(gè) key 為 testkey ,1000 為過期時(shí)間單位為 毫秒,最后的 666666 為 key 對(duì)應(yīng)的值。
執(zhí)行測(cè)試用例 testSetGet ,控制臺(tái)輸出內(nèi)容:
*********** 666666
到此這篇關(guān)于SpringBoot集成Memcached的項(xiàng)目實(shí)踐的文章就介紹到這了,更多相關(guān)SpringBoot集成Memcached內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring中使用事務(wù)嵌套時(shí)需要警惕的問題分享
最近項(xiàng)目上有一個(gè)使用事務(wù)相對(duì)復(fù)雜的業(yè)務(wù)場(chǎng)景報(bào)錯(cuò)了。在絕大多數(shù)情況下,都是風(fēng)平浪靜,沒有問題。其實(shí)內(nèi)在暗流涌動(dòng),在有些異常情況下就會(huì)報(bào)錯(cuò),這種偶然性的問題很有可能就會(huì)在暴露到生產(chǎn)上造成事故,那究竟是怎么回事呢?本文就來簡(jiǎn)單講講2023-04-04解決cmd執(zhí)行javac報(bào)錯(cuò):不是內(nèi)部或外部命令,也不是可運(yùn)行的程序
剛接觸JAVA的新手可能就不知道怎么解決'JAVAC'不是內(nèi)部命令或外部命令,這篇文章主要給大家介紹了關(guān)于解決cmd執(zhí)行javac報(bào)錯(cuò):不是內(nèi)部或外部命令,也不是可運(yùn)行的程序的相關(guān)資料,需要的朋友可以參考下2023-11-11Java數(shù)據(jù)結(jié)構(gòu)之Map與Set專篇講解
這篇文章通過實(shí)例面試題目來講解Java中Map和Set之間的關(guān)系,具有很好的參考價(jià)值,Map與Set在面試中經(jīng)常會(huì)遇到。一起跟隨小編過來看看吧2022-01-01mybatis關(guān)聯(lián)關(guān)系映射的實(shí)現(xiàn)
MyBatis的關(guān)聯(lián)關(guān)系映射在復(fù)雜數(shù)據(jù)模型中至關(guān)重要,使開發(fā)人員能夠以最靈活的方式滿足不同項(xiàng)目的需求,本文就來介紹一下mybatis關(guān)聯(lián)關(guān)系映射的實(shí)現(xiàn),感興趣的可以了解一下2023-09-09SpringBoot+Spring Security+JWT實(shí)現(xiàn)RESTful Api權(quán)限控制的方法
這篇文章主要介紹了SpringBoot+Spring Security+JWT實(shí)現(xiàn)RESTful Api權(quán)限控制的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03淺析Java迭代器Iterator和Iterable的區(qū)別
Java語言中,Iterator和Iterable都是用來遍歷集合類數(shù)據(jù)結(jié)構(gòu)的接口,雖然它們有很多相似的地方,但在具體實(shí)現(xiàn)中卻有著一些不同之處,本文將詳細(xì)分析它們的區(qū)別,并提供相應(yīng)的代碼示例,需要的朋友可以參考下2023-07-07