springboot集成memCached

springboot通过bean的方式创建memcache:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.caiyi.financial.nirvana.conf;

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

/**
* 自定义类,读取配置文件中memcache的内容
*/
@Component
@ConfigurationProperties(prefix = "memcache")
public class SockIOPoolConfig {

private String[] servers;

private Integer[] weights;

private int initConn;

private int minConn;

private int maxConn;

private long maintSleep;

private boolean nagle;

private int socketTO;

public String[] getServers() {
return servers;
}

public void setServers(String[] servers) {
this.servers = servers;
}

public Integer[] getWeights() {
return weights;
}

public void setWeights(Integer[] weights) {
this.weights = weights;
}

public int getInitConn() {
return initConn;
}

public void setInitConn(int initConn) {
this.initConn = initConn;
}

public int getMinConn() {
return minConn;
}

public void setMinConn(int minConn) {
this.minConn = minConn;
}

public int getMaxConn() {
return maxConn;
}

public void setMaxConn(int maxConn) {
this.maxConn = maxConn;
}

public long getMaintSleep() {
return maintSleep;
}

public void setMaintSleep(long maintSleep) {
this.maintSleep = maintSleep;
}

public boolean isNagle() {
return nagle;
}

public void setNagle(boolean nagle) {
this.nagle = nagle;
}

public int getSocketTO() {
return socketTO;
}

public void setSocketTO(int socketTO) {
this.socketTO = socketTO;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.caiyi.financial.nirvana.conf;

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
* 配置连接池
*/
@Component
public class MemcachedConfig {

@Autowired
SockIOPoolConfig sockIOPoolConfig;

@Bean
public SockIOPool sockIOPool(){
//获取连接池的实例
SockIOPool pool = SockIOPool.getInstance();
//服务器列表及其权重
String[] servers = sockIOPoolConfig.getServers();
Integer[] weights = sockIOPoolConfig.getWeights();

//设置服务器信息
pool.setServers(servers);
pool.setWeights(weights);

//设置初始连接数、最小连接数、最大连接数、最大处理时间
pool.setInitConn(sockIOPoolConfig.getInitConn());
pool.setMinConn(sockIOPoolConfig.getMinConn());
pool.setMaxConn(sockIOPoolConfig.getMaxConn());

//设置连接池守护线程的睡眠时间
pool.setMaintSleep(sockIOPoolConfig.getMaintSleep());

//设置TCP参数,连接超时
pool.setNagle(sockIOPoolConfig.isNagle());
pool.setSocketConnectTO(sockIOPoolConfig.getSocketTO());

//初始化并启动连接池
pool.initialize();

return pool;
}

@Bean
@ConditionalOnBean(SockIOPool.class)
public MemCachedClient memCachedClient(){
return new MemCachedClient();
}

}

在springboot配置文件中增加如下配置:

1
2
3
4
5
6
7
8
9
memcache:
servers: 192.168.1.51:10112
weights: 5
initConn: 20
minConn: 10
maxConn: 50
maintSleep: 3000
nagle: false
socketTO: 3000

springboot集成memCached
https://www.wekri.com/springboot/springboot集成memcached/
Author
Echo
Posted on
January 18, 2018
Licensed under