springboot进阶(6):自定义memcached-spring-boot-starter
文章目录前言准备工作如何自定义memcached-spring-boot-starter一、定义memcached-spring-boot-autoconfigure模块二、定义memcached-spring-boot-starter模块三、测试引用前言memcached 是非常简单的缓存组件,是简化版的redis,常用于缓存数据。准备工作安装memcached:Memcached 教程推荐使用
·
文章目录
前言
memcached 是非常简单的缓存组件,是简化版的redis,常用于缓存数据。
准备工作
安装memcached:Memcached 教程
推荐使用docker安装:
docker run --name memcached -p 11211:11211 -d daocloud.io/library/memcached:1.5.1-alpine
如何自定义memcached-spring-boot-starter
一、定义memcached-spring-boot-autoconfigure模块
-
创建springboot模块(无需任何组件)
-
引入依赖
<!--memcached-->
<dependency>
<groupId>spy</groupId>
<artifactId>memcached</artifactId>
<version>2.4rc1</version>
<scope>compile</scope>
</dependency>
- 编写代码
package com.lcz.memcached.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "memcached")
public class MemcachedProperties {
/**
* 多主机配置
*/
private String[] memcacheds;
public void setMemcacheds(String[] memcacheds) {
this.memcacheds = memcacheds;
}
public String[] getMemcacheds() {
return memcacheds;
}
}
package com.lcz.memcached.config;
import net.spy.memcached.MemcachedClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
@Configuration //设置为配置类
@EnableConfigurationProperties(MemcachedProperties.class) //获取到配置信息
@ConditionalOnClass(MemcachedClient.class)
public class MemcachedAutoConfiguration {
@Bean
@ConditionalOnMissingBean(value=MemcachedClient.class,name="memcachedClient")
@ConditionalOnProperty(name="memcached.enabled",havingValue = "true",matchIfMissing=true)
public MemcachedClient memcachedClient(MemcachedProperties memcachedProperties) {
try {
String[] memcacheds = memcachedProperties.getMemcacheds();
if (null != memcacheds && memcacheds.length > 0) {
List<InetSocketAddress> inetSocketAddresses = new ArrayList<>();
for (String str : memcacheds) {
Assert.notNull(str, "memcacheds config is error.");
String[] array = str.split(":");
Assert.state(array.length == 2, "memcacheds config is error.");
String ip = array[0];
String port = array[1];
inetSocketAddresses.add(new InetSocketAddress(ip, Integer.parseInt(port)));
}
MemcachedClient memcachedClient = new MemcachedClient(inetSocketAddresses);
return memcachedClient;
}else{
return new MemcachedClient(new InetSocketAddress("localhost", 11211));
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
- 配置扫描META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.lcz.memcached.config.MemcachedAutoConfiguration
4. 删除多余的文件和代码(如图)
二、定义memcached-spring-boot-starter模块
-
- 创建springboot模块(无需任何组件)
-
引入依赖,刚刚定义的模块
<dependency>
<groupId>com.lcz</groupId>
<artifactId>memcached-spring-boot-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
- 删除多余的文件和代码
三、测试引用
- 在其它springboot工程里引用刚刚定义的memcached-spring-boot-starter
<dependency>
<groupId>com.lcz</groupId>
<artifactId>memcached-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
- 配置application.yml
#memcached 缓存
memcached:
memcacheds:
- 106.13.2.249:11211
- 106.13.2.249:11212
# 是否开启缓存,默认true
# enabled: true
- 注入memcachedClient试一下
@Autowired
private MemcachedClient memcachedClient;
4. 运行
测试成功,结束。
更多推荐
已为社区贡献6条内容
所有评论(0)