MasterSlave, Sentinel and Single servers usage configuration renamed

pull/38/head
Nikita 11 years ago
parent 733b7bed8d
commit 7c8948a368

@ -26,11 +26,11 @@ import org.redisson.codec.RedissonCodec;
*/ */
public class Config { public class Config {
private SentinelConnectionConfig sentinelConnectionConfig; private SentinelServersConfig sentinelServersConfig;
private MasterSlaveConnectionConfig masterSlaveConnectionConfig; private MasterSlaveServersConfig masterSlaveServersConfig;
private SingleConnectionConfig singleConnectionConfig; private SingleServerConfig singleServerConfig;
/** /**
* Threads amount shared between all redis node clients * Threads amount shared between all redis node clients
@ -53,14 +53,14 @@ public class Config {
setThreads(oldConf.getThreads()); setThreads(oldConf.getThreads());
setCodec(oldConf.getCodec()); setCodec(oldConf.getCodec());
if (oldConf.getSingleConnectionConfig() != null) { if (oldConf.getSingleServerConfig() != null) {
setSingleConnectionConfig(new SingleConnectionConfig(oldConf.getSingleConnectionConfig())); setSingleServerConfig(new SingleServerConfig(oldConf.getSingleServerConfig()));
} }
if (oldConf.getMasterSlaveConnectionConfig() != null) { if (oldConf.getMasterSlaveServersConfig() != null) {
setMasterSlaveConnectionConfig(new MasterSlaveConnectionConfig(oldConf.getMasterSlaveConnectionConfig())); setMasterSlaveServersConfig(new MasterSlaveServersConfig(oldConf.getMasterSlaveServersConfig()));
} }
if (oldConf.getSentinelConnectionConfig() != null ) { if (oldConf.getSentinelServersConfig() != null ) {
setSentinelConnectionConfig(new SentinelConnectionConfig(oldConf.getSentinelConnectionConfig())); setSentinelServersConfig(new SentinelServersConfig(oldConf.getSentinelServersConfig()));
} }
} }
@ -78,52 +78,54 @@ public class Config {
return codec; return codec;
} }
public SingleConnectionConfig useSingleConnection() { public SingleServerConfig useSingleServer() {
if (masterSlaveConnectionConfig != null) { checkMasterSlaveServersConfig();
throw new IllegalStateException("master/slave connection already used!"); checkSentinelServersConfig();
}
if (singleConnectionConfig == null) { if (singleServerConfig == null) {
singleConnectionConfig = new SingleConnectionConfig(); singleServerConfig = new SingleServerConfig();
} }
return singleConnectionConfig; return singleServerConfig;
} }
SingleConnectionConfig getSingleConnectionConfig() {
return singleConnectionConfig; SingleServerConfig getSingleServerConfig() {
return singleServerConfig;
} }
void setSingleConnectionConfig(SingleConnectionConfig singleConnectionConfig) { void setSingleServerConfig(SingleServerConfig singleConnectionConfig) {
this.singleConnectionConfig = singleConnectionConfig; this.singleServerConfig = singleConnectionConfig;
} }
public SentinelConnectionConfig useSentinelConnection() { public SentinelServersConfig useSentinelConnection() {
if (singleConnectionConfig != null) { checkSingleServerConfig();
throw new IllegalStateException("single connection already used!"); checkMasterSlaveServersConfig();
}
if (sentinelConnectionConfig == null) { if (sentinelServersConfig == null) {
sentinelConnectionConfig = new SentinelConnectionConfig(); sentinelServersConfig = new SentinelServersConfig();
} }
return sentinelConnectionConfig; return sentinelServersConfig;
} }
SentinelConnectionConfig getSentinelConnectionConfig() {
return sentinelConnectionConfig; SentinelServersConfig getSentinelServersConfig() {
return sentinelServersConfig;
} }
void setSentinelConnectionConfig(SentinelConnectionConfig sentinelConnectionConfig) { void setSentinelServersConfig(SentinelServersConfig sentinelConnectionConfig) {
this.sentinelConnectionConfig = sentinelConnectionConfig; this.sentinelServersConfig = sentinelConnectionConfig;
} }
public MasterSlaveConnectionConfig useMasterSlaveConnection() { public MasterSlaveServersConfig useMasterSlaveConnection() {
if (singleConnectionConfig != null) { checkSingleServerConfig();
throw new IllegalStateException("single connection already used!"); checkSentinelServersConfig();
}
if (masterSlaveConnectionConfig == null) { if (masterSlaveServersConfig == null) {
masterSlaveConnectionConfig = new MasterSlaveConnectionConfig(); masterSlaveServersConfig = new MasterSlaveServersConfig();
} }
return masterSlaveConnectionConfig; return masterSlaveServersConfig;
} }
MasterSlaveConnectionConfig getMasterSlaveConnectionConfig() { MasterSlaveServersConfig getMasterSlaveServersConfig() {
return masterSlaveConnectionConfig; return masterSlaveServersConfig;
} }
void setMasterSlaveConnectionConfig(MasterSlaveConnectionConfig masterSlaveConnectionConfig) { void setMasterSlaveServersConfig(MasterSlaveServersConfig masterSlaveConnectionConfig) {
this.masterSlaveConnectionConfig = masterSlaveConnectionConfig; this.masterSlaveServersConfig = masterSlaveConnectionConfig;
} }
public int getThreads() { public int getThreads() {
@ -135,4 +137,22 @@ public class Config {
return this; return this;
} }
private void checkSentinelServersConfig() {
if (sentinelServersConfig != null) {
throw new IllegalStateException("sentinel servers config already used!");
}
}
private void checkMasterSlaveServersConfig() {
if (masterSlaveServersConfig != null) {
throw new IllegalStateException("master/slave servers already used!");
}
}
private void checkSingleServerConfig() {
if (singleServerConfig != null) {
throw new IllegalStateException("single server config already used!");
}
}
} }

@ -23,7 +23,7 @@ import java.util.List;
import org.redisson.connection.LoadBalancer; import org.redisson.connection.LoadBalancer;
import org.redisson.connection.RoundRobinLoadBalancer; import org.redisson.connection.RoundRobinLoadBalancer;
public class MasterSlaveConnectionConfig extends BaseConfig<MasterSlaveConnectionConfig> { public class MasterSlaveServersConfig extends BaseConfig<MasterSlaveServersConfig> {
/** /**
* Сonnection load balancer for multiple Redis slave servers * Сonnection load balancer for multiple Redis slave servers
@ -55,10 +55,10 @@ public class MasterSlaveConnectionConfig extends BaseConfig<MasterSlaveConnectio
*/ */
private int masterConnectionPoolSize = 100; private int masterConnectionPoolSize = 100;
public MasterSlaveConnectionConfig() { public MasterSlaveServersConfig() {
} }
MasterSlaveConnectionConfig(MasterSlaveConnectionConfig config) { MasterSlaveServersConfig(MasterSlaveServersConfig config) {
super(config); super(config);
setLoadBalancer(config.getLoadBalancer()); setLoadBalancer(config.getLoadBalancer());
setMasterAddress(config.getMasterAddress()); setMasterAddress(config.getMasterAddress());
@ -73,7 +73,7 @@ public class MasterSlaveConnectionConfig extends BaseConfig<MasterSlaveConnectio
* *
* @param masterAddress * @param masterAddress
*/ */
public MasterSlaveConnectionConfig setMasterAddress(String masterAddress) { public MasterSlaveServersConfig setMasterAddress(String masterAddress) {
try { try {
this.masterAddress = new URI("//" + masterAddress); this.masterAddress = new URI("//" + masterAddress);
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
@ -94,7 +94,7 @@ public class MasterSlaveConnectionConfig extends BaseConfig<MasterSlaveConnectio
* @param addresses * @param addresses
* @return * @return
*/ */
public MasterSlaveConnectionConfig addSlaveAddress(String ... sAddresses) { public MasterSlaveServersConfig addSlaveAddress(String ... sAddresses) {
for (String address : sAddresses) { for (String address : sAddresses) {
try { try {
slaveAddresses.add(new URI("//" + address)); slaveAddresses.add(new URI("//" + address));
@ -119,7 +119,7 @@ public class MasterSlaveConnectionConfig extends BaseConfig<MasterSlaveConnectio
* @param slaveConnectionPoolSize * @param slaveConnectionPoolSize
* @return * @return
*/ */
public MasterSlaveConnectionConfig setSlaveConnectionPoolSize(int slaveConnectionPoolSize) { public MasterSlaveServersConfig setSlaveConnectionPoolSize(int slaveConnectionPoolSize) {
this.slaveConnectionPoolSize = slaveConnectionPoolSize; this.slaveConnectionPoolSize = slaveConnectionPoolSize;
return this; return this;
} }
@ -134,7 +134,7 @@ public class MasterSlaveConnectionConfig extends BaseConfig<MasterSlaveConnectio
* @param masterConnectionPoolSize * @param masterConnectionPoolSize
* @return * @return
*/ */
public MasterSlaveConnectionConfig setMasterConnectionPoolSize(int masterConnectionPoolSize) { public MasterSlaveServersConfig setMasterConnectionPoolSize(int masterConnectionPoolSize) {
this.masterConnectionPoolSize = masterConnectionPoolSize; this.masterConnectionPoolSize = masterConnectionPoolSize;
return this; return this;
} }
@ -152,7 +152,7 @@ public class MasterSlaveConnectionConfig extends BaseConfig<MasterSlaveConnectio
* @see org.redisson.connection.RoundRobinLoadBalancer * @see org.redisson.connection.RoundRobinLoadBalancer
* @see org.redisson.connection.BaseLoadBalancer * @see org.redisson.connection.BaseLoadBalancer
*/ */
public MasterSlaveConnectionConfig setLoadBalancer(LoadBalancer loadBalancer) { public MasterSlaveServersConfig setLoadBalancer(LoadBalancer loadBalancer) {
this.loadBalancer = loadBalancer; this.loadBalancer = loadBalancer;
return this; return this;
} }
@ -167,7 +167,7 @@ public class MasterSlaveConnectionConfig extends BaseConfig<MasterSlaveConnectio
* @param slaveSubscriptionConnectionPoolSize * @param slaveSubscriptionConnectionPoolSize
* @return * @return
*/ */
public MasterSlaveConnectionConfig setSlaveSubscriptionConnectionPoolSize(int slaveSubscriptionConnectionPoolSize) { public MasterSlaveServersConfig setSlaveSubscriptionConnectionPoolSize(int slaveSubscriptionConnectionPoolSize) {
this.slaveSubscriptionConnectionPoolSize = slaveSubscriptionConnectionPoolSize; this.slaveSubscriptionConnectionPoolSize = slaveSubscriptionConnectionPoolSize;
return this; return this;
} }

@ -71,12 +71,12 @@ public class Redisson {
Redisson(Config config) { Redisson(Config config) {
this.config = config; this.config = config;
Config configCopy = new Config(config); Config configCopy = new Config(config);
if (configCopy.getMasterSlaveConnectionConfig() != null) { if (configCopy.getMasterSlaveServersConfig() != null) {
connectionManager = new MasterSlaveConnectionManager(configCopy.getMasterSlaveConnectionConfig(), configCopy); connectionManager = new MasterSlaveConnectionManager(configCopy.getMasterSlaveServersConfig(), configCopy);
} else if (configCopy.getSingleConnectionConfig() != null) { } else if (configCopy.getSingleServerConfig() != null) {
connectionManager = new SingleConnectionManager(configCopy.getSingleConnectionConfig(), configCopy); connectionManager = new SingleConnectionManager(configCopy.getSingleServerConfig(), configCopy);
} else { } else {
connectionManager = new SentinelConnectionManager(configCopy.getSentinelConnectionConfig(), configCopy); connectionManager = new SentinelConnectionManager(configCopy.getSentinelServersConfig(), configCopy);
} }
} }
@ -87,7 +87,7 @@ public class Redisson {
*/ */
public static Redisson create() { public static Redisson create() {
Config config = new Config(); Config config = new Config();
config.useSingleConnection().setAddress("127.0.0.1:6379"); config.useSingleServer().setAddress("127.0.0.1:6379");
// config.useMasterSlaveConnection().setMasterAddress("127.0.0.1:6379").addSlaveAddress("127.0.0.1:6389").addSlaveAddress("127.0.0.1:6399"); // config.useMasterSlaveConnection().setMasterAddress("127.0.0.1:6379").addSlaveAddress("127.0.0.1:6389").addSlaveAddress("127.0.0.1:6399");
// config.useSentinelConnection().setMasterName("mymaster").addSentinelAddress("127.0.0.1:26389", "127.0.0.1:26379"); // config.useSentinelConnection().setMasterName("mymaster").addSentinelAddress("127.0.0.1:26389", "127.0.0.1:26379");
return create(config); return create(config);

@ -20,21 +20,21 @@ import java.net.URISyntaxException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class SentinelConnectionConfig { public class SentinelServersConfig {
private List<URI> sentinelAddresses = new ArrayList<URI>(); private List<URI> sentinelAddresses = new ArrayList<URI>();
private String masterName; private String masterName;
public SentinelConnectionConfig() { public SentinelServersConfig() {
} }
SentinelConnectionConfig(SentinelConnectionConfig config) { SentinelServersConfig(SentinelServersConfig config) {
setSentinelAddresses(config.getSentinelAddresses()); setSentinelAddresses(config.getSentinelAddresses());
setMasterName(config.getMasterName()); setMasterName(config.getMasterName());
} }
public SentinelConnectionConfig setMasterName(String masterName) { public SentinelServersConfig setMasterName(String masterName) {
this.masterName = masterName; this.masterName = masterName;
return this; return this;
} }
@ -42,7 +42,7 @@ public class SentinelConnectionConfig {
return masterName; return masterName;
} }
public SentinelConnectionConfig addSentinelAddress(String ... addresses) { public SentinelServersConfig addSentinelAddress(String ... addresses) {
for (String address : addresses) { for (String address : addresses) {
try { try {
sentinelAddresses.add(new URI("//" + address)); sentinelAddresses.add(new URI("//" + address));

@ -18,7 +18,7 @@ package org.redisson;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
public class SingleConnectionConfig extends BaseConfig<SingleConnectionConfig> { public class SingleServerConfig extends BaseConfig<SingleServerConfig> {
/** /**
* Redis server address * Redis server address
@ -37,10 +37,10 @@ public class SingleConnectionConfig extends BaseConfig<SingleConnectionConfig> {
*/ */
private int connectionPoolSize = 100; private int connectionPoolSize = 100;
SingleConnectionConfig() { SingleServerConfig() {
} }
SingleConnectionConfig(SingleConnectionConfig config) { SingleServerConfig(SingleServerConfig config) {
super(config); super(config);
setAddress(config.getAddress()); setAddress(config.getAddress());
setConnectionPoolSize(config.getConnectionPoolSize()); setConnectionPoolSize(config.getConnectionPoolSize());
@ -53,7 +53,7 @@ public class SingleConnectionConfig extends BaseConfig<SingleConnectionConfig> {
* *
* @param connectionPoolSize * @param connectionPoolSize
*/ */
public SingleConnectionConfig setConnectionPoolSize(int connectionPoolSize) { public SingleServerConfig setConnectionPoolSize(int connectionPoolSize) {
this.connectionPoolSize = connectionPoolSize; this.connectionPoolSize = connectionPoolSize;
return this; return this;
} }
@ -68,7 +68,7 @@ public class SingleConnectionConfig extends BaseConfig<SingleConnectionConfig> {
* @param connectionPoolSize * @param connectionPoolSize
* @return * @return
*/ */
public SingleConnectionConfig setSubscriptionConnectionPoolSize(int subscriptionConnectionPoolSize) { public SingleServerConfig setSubscriptionConnectionPoolSize(int subscriptionConnectionPoolSize) {
this.subscriptionConnectionPoolSize = subscriptionConnectionPoolSize; this.subscriptionConnectionPoolSize = subscriptionConnectionPoolSize;
return this; return this;
} }
@ -81,7 +81,7 @@ public class SingleConnectionConfig extends BaseConfig<SingleConnectionConfig> {
* *
* @param address * @param address
*/ */
public SingleConnectionConfig setAddress(String address) { public SingleServerConfig setAddress(String address) {
try { try {
this.address = new URI("//" + address); this.address = new URI("//" + address);
} catch (URISyntaxException e) { } catch (URISyntaxException e) {

@ -34,7 +34,7 @@ import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Semaphore; import java.util.concurrent.Semaphore;
import org.redisson.Config; import org.redisson.Config;
import org.redisson.MasterSlaveConnectionConfig; import org.redisson.MasterSlaveServersConfig;
import org.redisson.codec.RedisCodecWrapper; import org.redisson.codec.RedisCodecWrapper;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -69,22 +69,22 @@ public class MasterSlaveConnectionManager implements ConnectionManager {
private Semaphore masterConnectionsSemaphore; private Semaphore masterConnectionsSemaphore;
protected MasterSlaveConnectionConfig config; protected MasterSlaveServersConfig config;
MasterSlaveConnectionManager() { MasterSlaveConnectionManager() {
} }
public MasterSlaveConnectionManager(MasterSlaveConnectionConfig cfg, Config config) { public MasterSlaveConnectionManager(MasterSlaveServersConfig cfg, Config config) {
init(cfg, config); init(cfg, config);
} }
protected void init(MasterSlaveConnectionConfig config, Config cfg) { protected void init(MasterSlaveServersConfig config, Config cfg) {
init(cfg); init(cfg);
init(config); init(config);
} }
protected void init(MasterSlaveConnectionConfig config) { protected void init(MasterSlaveServersConfig config) {
this.config = config; this.config = config;
balancer = config.getLoadBalancer(); balancer = config.getLoadBalancer();
balancer.init(codec, config.getPassword()); balancer.init(codec, config.getPassword());

@ -26,8 +26,8 @@ import java.util.Map;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import org.redisson.Config; import org.redisson.Config;
import org.redisson.MasterSlaveConnectionConfig; import org.redisson.MasterSlaveServersConfig;
import org.redisson.SentinelConnectionConfig; import org.redisson.SentinelServersConfig;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -42,14 +42,14 @@ public class SentinelConnectionManager extends MasterSlaveConnectionManager {
private final List<RedisClient> sentinels = new ArrayList<RedisClient>(); private final List<RedisClient> sentinels = new ArrayList<RedisClient>();
public SentinelConnectionManager(final SentinelConnectionConfig cfg, Config config) { public SentinelConnectionManager(final SentinelServersConfig cfg, Config config) {
init(cfg, config); init(cfg, config);
} }
private void init(final SentinelConnectionConfig cfg, final Config config) { private void init(final SentinelServersConfig cfg, final Config config) {
init(config); init(config);
final MasterSlaveConnectionConfig c = new MasterSlaveConnectionConfig(); final MasterSlaveServersConfig c = new MasterSlaveServersConfig();
for (URI addr : cfg.getSentinelAddresses()) { for (URI addr : cfg.getSentinelAddresses()) {
RedisClient client = new RedisClient(group, addr.getHost(), addr.getPort()); RedisClient client = new RedisClient(group, addr.getHost(), addr.getPort());
RedisAsyncConnection<String, String> connection = client.connectAsync(); RedisAsyncConnection<String, String> connection = client.connectAsync();

@ -20,8 +20,8 @@ import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Semaphore; import java.util.concurrent.Semaphore;
import org.redisson.Config; import org.redisson.Config;
import org.redisson.MasterSlaveConnectionConfig; import org.redisson.MasterSlaveServersConfig;
import org.redisson.SingleConnectionConfig; import org.redisson.SingleServerConfig;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -35,8 +35,8 @@ public class SingleConnectionManager extends MasterSlaveConnectionManager {
private final Semaphore subscribeConnectionsSemaphore; private final Semaphore subscribeConnectionsSemaphore;
private final Queue<RedisPubSubConnection> subscribeConnections = new ConcurrentLinkedQueue<RedisPubSubConnection>(); private final Queue<RedisPubSubConnection> subscribeConnections = new ConcurrentLinkedQueue<RedisPubSubConnection>();
public SingleConnectionManager(SingleConnectionConfig cfg, Config config) { public SingleConnectionManager(SingleServerConfig cfg, Config config) {
MasterSlaveConnectionConfig newconfig = new MasterSlaveConnectionConfig(); MasterSlaveServersConfig newconfig = new MasterSlaveServersConfig();
String addr = cfg.getAddress().getHost() + ":" + cfg.getAddress().getPort(); String addr = cfg.getAddress().getHost() + ":" + cfg.getAddress().getPort();
newconfig.setPassword(cfg.getPassword()); newconfig.setPassword(cfg.getPassword());
newconfig.setMasterAddress(addr); newconfig.setMasterAddress(addr);

Loading…
Cancel
Save