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;
@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());
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
|