下面列出了org.quartz.spi.SchedulerSignaler#redis.clients.jedis.Protocol 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Bean(name = "jedis.pool")
@Autowired
public JedisSentinelPool jedisPool(@Qualifier("jedis.pool.config") JedisPoolConfig config,
@Value("${spring.redis.sentinel.master}") String clusterName,
@Value("${spring.redis.sentinel.nodes}") String sentinelNodes,
@Value("${spring.redis.timeout}") int timeout,
@Value("${spring.redis.password}") String password) {
logger.info("缓存服务器的主服务名称:" + clusterName + ", 主从服务ip&port:" + sentinelNodes);
Assert.isTrue(StringUtils.isNotEmpty(clusterName), "主服务名称配置为空");
Assert.isTrue(StringUtils.isNotEmpty(sentinelNodes), "主从服务地址配置为空");
Set<String> sentinels = Sets.newHashSet(StringUtils.split(sentinelNodes, ","));
JedisSentinelPool sentinelJedisPool = new JedisSentinelPool(clusterName, sentinels, config, Protocol.DEFAULT_TIMEOUT, password);
return sentinelJedisPool;
}
@Override
public GelfSender create(GelfSenderConfiguration configuration) throws IOException {
String graylogHost = configuration.getHost();
URI hostUri = URI.create(graylogHost);
int port = hostUri.getPort();
if (port <= 0) {
port = configuration.getPort();
}
if (port <= 0) {
port = Protocol.DEFAULT_PORT;
}
if (hostUri.getFragment() == null || hostUri.getFragment().trim().equals("")) {
throw new IllegalArgumentException("Redis URI must specify fragment");
}
if (hostUri.getHost() == null) {
throw new IllegalArgumentException("Redis URI must specify host");
}
Pool<Jedis> pool = RedisSenderPoolProvider.getJedisPool(hostUri, port);
return new GelfREDISSender(pool, hostUri.getFragment(), configuration.getErrorReporter());
}
static RedisConnectionInfo parseConnectionUri(String connection) {
URI redisConnection = URI.create(connection);
String host = redisConnection.getHost();
int port = redisConnection.getPort() == -1 ? Protocol.DEFAULT_PORT : redisConnection.getPort();
int database = JedisURIHelper.getDBIndex(redisConnection);
String password = JedisURIHelper.getPassword(redisConnection);
boolean ssl = connection.startsWith(REDIS_SSL_SCHEME);
return RedisConnectionInfo.builder()
.host(host)
.port(port)
.database(database)
.password(password)
.ssl(ssl)
.build();
}
public void init(String address, String password, boolean ssl) {
String[] addressSplit = address.split(":");
String host = addressSplit[0];
int port = addressSplit.length > 1 ? Integer.parseInt(addressSplit[1]) : Protocol.DEFAULT_PORT;
this.jedisPool = new JedisPool(new JedisPoolConfig(), host, port, Protocol.DEFAULT_TIMEOUT, password, ssl);
this.plugin.getBootstrap().getScheduler().executeAsync(() -> {
this.sub = new Subscription(this);
try (Jedis jedis = this.jedisPool.getResource()) {
jedis.subscribe(this.sub, CHANNEL);
} catch (Exception e) {
e.printStackTrace();
}
});
}
private static long measureInputMulti() throws Exception {
long duration = 0;
InputStream is = new ByteArrayInputStream(
"*4\r\n$3\r\nfoo\r\n$13\r\nbarbarbarfooz\r\n$5\r\nHello\r\n$5\r\nWorld\r\n".getBytes());
RedisInputStream in = new RedisInputStream(is);
for (int n = 0; n <= TOTAL_OPERATIONS; n++) {
long start = System.nanoTime();
Protocol.read(in);
duration += (System.nanoTime() - start);
in.reset();
}
return duration;
}
@Test
public void testMasterSlaveShardingConsistency() {
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(3);
shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT));
shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 1));
shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 2));
Sharded<Jedis, JedisShardInfo> sharded = new Sharded<Jedis, JedisShardInfo>(shards,
Hashing.MURMUR_HASH);
List<JedisShardInfo> otherShards = new ArrayList<JedisShardInfo>(3);
otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT));
otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 1));
otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 2));
Sharded<Jedis, JedisShardInfo> sharded2 = new Sharded<Jedis, JedisShardInfo>(otherShards,
Hashing.MURMUR_HASH);
for (int i = 0; i < 1000; i++) {
JedisShardInfo jedisShardInfo = sharded.getShardInfo(Integer.toString(i));
JedisShardInfo jedisShardInfo2 = sharded2.getShardInfo(Integer.toString(i));
assertEquals(shards.indexOf(jedisShardInfo), otherShards.indexOf(jedisShardInfo2));
}
}
@Test
public void testMasterSlaveShardingConsistencyWithShardNaming() {
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(3);
shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT, "HOST1:1234"));
shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 1, "HOST2:1234"));
shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 2, "HOST3:1234"));
Sharded<Jedis, JedisShardInfo> sharded = new Sharded<Jedis, JedisShardInfo>(shards,
Hashing.MURMUR_HASH);
List<JedisShardInfo> otherShards = new ArrayList<JedisShardInfo>(3);
otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT, "HOST2:1234"));
otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 1, "HOST3:1234"));
otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 2, "HOST1:1234"));
Sharded<Jedis, JedisShardInfo> sharded2 = new Sharded<Jedis, JedisShardInfo>(otherShards,
Hashing.MURMUR_HASH);
for (int i = 0; i < 1000; i++) {
JedisShardInfo jedisShardInfo = sharded.getShardInfo(Integer.toString(i));
JedisShardInfo jedisShardInfo2 = sharded2.getShardInfo(Integer.toString(i));
assertEquals(jedisShardInfo.getName(), jedisShardInfo2.getName());
}
}
@Test
public void buildACommand() throws IOException {
PipedInputStream pis = new PipedInputStream();
BufferedInputStream bis = new BufferedInputStream(pis);
PipedOutputStream pos = new PipedOutputStream(pis);
RedisOutputStream ros = new RedisOutputStream(pos);
Protocol.sendCommand(ros, Protocol.Command.GET, "SOMEKEY".getBytes(Protocol.CHARSET));
ros.flush();
pos.close();
String expectedCommand = "*2\r\n$3\r\nGET\r\n$7\r\nSOMEKEY\r\n";
int b;
StringBuilder sb = new StringBuilder();
while ((b = bis.read()) != -1) {
sb.append((char) b);
}
assertEquals(expectedCommand, sb.toString());
}
/**
* Create a Jedis poll with pippo settings. URL format:
* 'redis://[:[email protected]]host[:port][/db-number][?option=value]'
*
* @param settings pippo settings
* @return Jedis pool
*/
public static JedisPool create(final PippoSettings settings) {
String host = settings.getString(HOST, Protocol.DEFAULT_HOST).trim();
int minIdle = settings.getInteger(MIN_IDLE, GenericObjectPoolConfig.DEFAULT_MIN_IDLE);
int maxIdle = settings.getInteger(MAX_IDLE, GenericObjectPoolConfig.DEFAULT_MAX_IDLE);
int maxTotal = settings.getInteger(MAX_TOTAL, GenericObjectPoolConfig.DEFAULT_MAX_TOTAL);
JedisPoolConfig config = new JedisPoolConfig();
config.setMinIdle(minIdle);
config.setMaxIdle(maxIdle);
config.setMaxTotal(maxTotal);
try {
return new JedisPool(config, new URI(host));
} catch (URISyntaxException e) {
throw new PippoRuntimeException("Malformed redis URI", e);
}
}
private boolean slaveOf(final long appId, final String masterHost, final int masterPort, final String slaveHost,
final int slavePort) {
final Jedis slave = redisCenter.getJedis(appId, slaveHost, slavePort, Protocol.DEFAULT_TIMEOUT * 3, Protocol.DEFAULT_TIMEOUT * 3);
try {
boolean isSlave = new IdempotentConfirmer() {
@Override
public boolean execute() {
String result = slave.slaveof(masterHost, masterPort);
return result != null && result.equalsIgnoreCase("OK");
}
}.run();
if (!isSlave) {
logger.error(String.format("modifyAppConfig:ip=%s,port=%s failed", slaveHost, slavePort));
return false;
}
redisCenter.configRewrite(appId, slaveHost, slavePort);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return false;
} finally {
if (slave != null)
slave.close();
}
return true;
}
private void readModelByJson() {
String strResult = null;
try {
ObjectMapper objMapper = new ObjectMapper();
objMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true)
.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
File file = new File(filePath);
if (file.exists()) {
JsonNode rootNode = objMapper.readTree(file);
strResult = rootNode.toString().replace(Integer.toString(Protocol.DEFAULT_PORT),
Integer.toString(PORT));
}
} catch (Exception ignored) {
}
model = strResult;
}
@Override
public void init(final NamedList args) {
final GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxTotal(getInt(args, MAX_CONNECTIONS_FIELD, DEFAULT_MAX_CONNECTIONS));
final String host = getString(args, HOST_FIELD, HostAndPort.LOCALHOST_STR);
final int timeout = getInt(args, TIMEOUT_FIELD, Protocol.DEFAULT_TIMEOUT);
final String password = getString(args, PASSWORD_FIELD, null);
final int database = getInt(args, DATABASE_FIELD, Protocol.DEFAULT_DATABASE);
final int retries = getInt(args, RETRIES_FIELD, DEFAULT_RETRIES);
final String[] hostAndPort = host.split(":");
final JedisPool jedisConnectionPool = createPool(poolConfig, hostAndPort[0],
hostAndPort.length == 2 ? Integer.parseInt(hostAndPort[1]) : Protocol.DEFAULT_PORT, timeout, password,
database);
connectionHandler = createCommandHandler(jedisConnectionPool, retries);
log.info("Initialized RedisQParserPlugin with host: " + host);
}
@Before
public void setUpRedis() throws IOException, SchedulerConfigException {
port = getPort();
logger.debug("Attempting to start embedded Redis server on port " + port);
redisServer = RedisServer.builder()
.port(port)
.build();
redisServer.start();
final short database = 1;
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setTestOnBorrow(true);
jedisPool = new JedisPool(jedisPoolConfig, host, port, Protocol.DEFAULT_TIMEOUT, null, database);
jobStore = new RedisJobStore();
jobStore.setHost(host);
jobStore.setLockTimeout(2000);
jobStore.setPort(port);
jobStore.setInstanceId("testJobStore1");
jobStore.setDatabase(database);
mockScheduleSignaler = mock(SchedulerSignaler.class);
jobStore.initialize(null, mockScheduleSignaler);
schema = new RedisJobStoreSchema();
jedis = jedisPool.getResource();
jedis.flushDB();
}
public Pool<Jedis> build(
String name, JedisDriverProperties properties, GenericObjectPoolConfig objectPoolConfig) {
if (properties.connection == null || "".equals(properties.connection)) {
throw new MissingRequiredConfiguration("Jedis client must have a connection defined");
}
URI redisConnection = URI.create(properties.connection);
String host = redisConnection.getHost();
int port = redisConnection.getPort() == -1 ? Protocol.DEFAULT_PORT : redisConnection.getPort();
int database = parseDatabase(redisConnection.getPath());
String password = parsePassword(redisConnection.getUserInfo());
GenericObjectPoolConfig poolConfig =
Optional.ofNullable(properties.poolConfig).orElse(objectPoolConfig);
boolean isSSL = redisConnection.getScheme().equals("rediss");
return new InstrumentedJedisPool(
registry,
// Pool name should always be "null", as setting this is incompat with some SaaS Redis
// offerings
new JedisPool(
poolConfig, host, port, properties.timeoutMs, password, database, null, isSSL),
name);
}
@Override
protected Jedis create(RedisProperties redisProperties) {
Set<String> sentinelset = new HashSet<>(Arrays.asList(redisProperties.getSentinel().getSentinels().split(",")));
this.pool
= new JedisSentinelPool(redisProperties.getSentinel().getMaster(), sentinelset, redisProperties.getConfig(),
redisProperties.getTimeout() == null ? Protocol.DEFAULT_TIMEOUT : redisProperties.getTimeout(),
redisProperties.getPassword(), redisProperties.getDatabase());
return this.pool.getResource();
}
@Override
protected Jedis create(RedisProperties redisProperties) {
Codis codis = redisProperties.getCodis();
this.pool = RoundRobinJedisPool.create().curatorClient(codis.getZkAddrs(), codis.getZkSessionTimeoutMs())
.zkProxyDir("/jodis/" + codis.getCodisProxyName()).poolConfig(redisProperties.getConfig())
.database(redisProperties.getDatabase()).password(redisProperties.getPassword())
.timeoutMs(Protocol.DEFAULT_TIMEOUT).build();
return this.pool.getResource();
}
@Override
protected Jedis create(RedisProperties redisProperties) {
this.pool = new JedisPool(
redisProperties.getConfig(),
redisProperties.getHost(),
redisProperties.getPort(),
redisProperties.getTimeout() == null ? Protocol.DEFAULT_TIMEOUT : redisProperties.getTimeout(),
redisProperties.getPassword(),
redisProperties.getDatabase());
return this.pool.getResource();
}
@Bean(name = "jedis.pool")
@Autowired
public JedisPool jedisPool(@Qualifier("jedis.pool.config") JedisPoolConfig config,
@Value("${spring.redis.host}") String host,
@Value("${spring.redis.port}") int port,
@Value("${spring.redis.timeout}") int timeout,
@Value("${spring.redis.password}") String password) {
logger.info("缓存服务器的地址:" + host + ":" + port);
return new JedisPool(config, host, port, Protocol.DEFAULT_TIMEOUT, password);
}
private byte[] formatNum(double num, boolean exclude) {
if (num == Double.POSITIVE_INFINITY) {
return Keywords.POSITIVE_INFINITY.getRaw();
}
if (num == Double.NEGATIVE_INFINITY) {
return Keywords.NEGATIVE_INFINITY.getRaw();
}
return exclude ? SafeEncoder.encode("(" + num) : Protocol.toByteArray(num);
}
@Override
public void serializeRedisArgs(List<byte[]> args) {
args.add(Keywords.GEOFILTER.getRaw());
args.add(SafeEncoder.encode(property));
args.add(Protocol.toByteArray(lon));
args.add(Protocol.toByteArray(lat));
args.add(Protocol.toByteArray(radius));
args.add(SafeEncoder.encode(unit));
}
private DecoratedJedisPool(JedisPoolConfig jedisPoolConfig, String host, int port) {
super(jedisPoolConfig, host, port,
Config.getInstance().getJedisConnectTimeoutMs(),
Config.getInstance().getJedisSocketTimeoutMs(),
Config.getInstance().getRedisPassword(),
Protocol.DEFAULT_DATABASE, null);
this.host = host;
this.port = port;
}
public TracingJedisSentinelPool(TracingConfiguration tracingConfiguration, String masterName,
Set<String> sentinels,
final GenericObjectPoolConfig poolConfig) {
super(masterName, sentinels, poolConfig, Protocol.DEFAULT_TIMEOUT, null,
Protocol.DEFAULT_DATABASE);
this.tracingConfiguration = tracingConfiguration;
}
@Test
public void shouldConfigurePoolWithCustomHostAndPort() {
final NamedList<String> list = new NamedList<>();
list.add("host", "127.0.0.1:1000");
parserPlugin.init(list);
verify(parserPlugin).createPool(poolConfigArgument.capture(), eq("127.0.0.1"),
eq(1000), eq(Protocol.DEFAULT_TIMEOUT), passwordArgument.capture(),
eq(Protocol.DEFAULT_DATABASE));
verify(parserPlugin).createCommandHandler(poolArgument.capture(), eq(1));
assertNull(passwordArgument.getValue());
assertEquals(5, poolConfigArgument.getValue().getMaxTotal());
}
@Before
public void before() {
mockTracer.reset();
redisServer = RedisServer.builder().build();
redisServer.start();
redisSentinel = RedisSentinel.builder().port(Protocol.DEFAULT_PORT + 1).build();
redisSentinel.start();
}
public TracingJedisSentinelPool(TracingConfiguration tracingConfiguration, String masterName,
Set<String> sentinels,
final GenericObjectPoolConfig poolConfig) {
super(masterName, sentinels, poolConfig, Protocol.DEFAULT_TIMEOUT, null,
Protocol.DEFAULT_DATABASE);
this.tracingConfiguration = tracingConfiguration;
}
@Test
public void shouldConfigurePoolWithCustomPassword() {
final NamedList<String> list = new NamedList<>();
list.add("password", "s3cr3t");
parserPlugin.init(list);
verify(parserPlugin).createPool(poolConfigArgument.capture(), eq(HostAndPort.LOCALHOST_STR),
eq(Protocol.DEFAULT_PORT), eq(Protocol.DEFAULT_TIMEOUT), eq("s3cr3t"),
eq(Protocol.DEFAULT_DATABASE));
verify(parserPlugin).createCommandHandler(poolArgument.capture(), eq(1));
assertEquals(5, poolConfigArgument.getValue().getMaxTotal());
}
@Test
public void shouldConfigurePoolWithCustomTimeout() {
final NamedList<String> list = new NamedList<>();
list.add("timeout", "100");
parserPlugin.init(list);
verify(parserPlugin).createPool(poolConfigArgument.capture(), eq(HostAndPort.LOCALHOST_STR),
eq(Protocol.DEFAULT_PORT), eq(100), passwordArgument.capture(),
eq(Protocol.DEFAULT_DATABASE));
verify(parserPlugin).createCommandHandler(poolArgument.capture(), eq(1));
assertNull(passwordArgument.getValue());
assertEquals(5, poolConfigArgument.getValue().getMaxTotal());
}
@Test
public void shouldConfigurePoolWithCustomRetries() {
final NamedList<String> list = new NamedList<>();
list.add("retries", "100");
parserPlugin.init(list);
verify(parserPlugin).createPool(poolConfigArgument.capture(), eq(HostAndPort.LOCALHOST_STR),
eq(Protocol.DEFAULT_PORT), eq(Protocol.DEFAULT_TIMEOUT), passwordArgument.capture(),
eq(Protocol.DEFAULT_DATABASE));
verify(parserPlugin).createCommandHandler(poolArgument.capture(), eq(100));
assertNull(passwordArgument.getValue());
assertEquals(5, poolConfigArgument.getValue().getMaxTotal());
}
@Test
public void shouldConfigurePoolWithDefaultParametersIfNullIsGiven() {
parserPlugin.init(null);
verify(parserPlugin).createPool(poolConfigArgument.capture(), eq(HostAndPort.LOCALHOST_STR),
eq(Protocol.DEFAULT_PORT), eq(Protocol.DEFAULT_TIMEOUT), passwordArgument.capture(),
eq(Protocol.DEFAULT_DATABASE));
assertNull(passwordArgument.getValue());
assertEquals(5, poolConfigArgument.getValue().getMaxTotal());
}
public static byte[] encode(final String str) {
try {
if (str == null) {
throw new JedisDataException("value sent to redis cannot be null");
}
return str.getBytes(Protocol.CHARSET);
} catch (UnsupportedEncodingException e) {
throw new JedisException(e);
}
}