committed by
Andrew Shvayka
4 changed files with 248 additions and 17 deletions
@ -0,0 +1,77 @@ |
|||
/** |
|||
* Copyright © 2016-2019 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.dao.cache; |
|||
|
|||
import org.apache.commons.lang3.StringUtils; |
|||
import org.springframework.beans.factory.annotation.Value; |
|||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
|||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
|||
import org.springframework.context.annotation.Configuration; |
|||
import org.springframework.data.redis.connection.RedisClusterConfiguration; |
|||
import org.springframework.data.redis.connection.RedisNode; |
|||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.Collections; |
|||
import java.util.List; |
|||
|
|||
@Configuration |
|||
@ConditionalOnMissingBean(CaffeineCacheConfiguration.class) |
|||
@ConditionalOnProperty(prefix = "redis.connection", value = "type", havingValue = "cluster") |
|||
public class TBRedisClusterConfiguration extends TBRedisCacheConfiguration { |
|||
|
|||
private static final String COMMA = ","; |
|||
private static final String COLON = ":"; |
|||
|
|||
@Value("${redis.cluster.nodes}") |
|||
private String clusterNodes; |
|||
|
|||
@Value("${redis.cluster.max-redirects}") |
|||
private Integer maxRedirects; |
|||
|
|||
@Value("${redis.cluster.useDefaultPoolConfig}") |
|||
private boolean useDefaultPoolConfig; |
|||
|
|||
@Value("${redis.password}") |
|||
private String password; |
|||
|
|||
public JedisConnectionFactory loadFactory() { |
|||
RedisClusterConfiguration clusterConfiguration = new RedisClusterConfiguration(); |
|||
clusterConfiguration.setClusterNodes(getNodes(clusterNodes)); |
|||
clusterConfiguration.setMaxRedirects(maxRedirects); |
|||
clusterConfiguration.setPassword(password); |
|||
if (useDefaultPoolConfig) { |
|||
return new JedisConnectionFactory(clusterConfiguration); |
|||
} else { |
|||
return new JedisConnectionFactory(clusterConfiguration, buildPoolConfig()); |
|||
} |
|||
} |
|||
|
|||
private List<RedisNode> getNodes(String nodes) { |
|||
List<RedisNode> result; |
|||
if (StringUtils.isBlank(nodes)) { |
|||
result = Collections.emptyList(); |
|||
} else { |
|||
result = new ArrayList<>(); |
|||
for (String hostPort : nodes.split(COMMA)) { |
|||
String host = hostPort.split(COLON)[0]; |
|||
Integer port = Integer.valueOf(hostPort.split(COLON)[1]); |
|||
result.add(new RedisNode(host, port)); |
|||
} |
|||
} |
|||
return result; |
|||
} |
|||
} |
|||
@ -0,0 +1,88 @@ |
|||
/** |
|||
* Copyright © 2016-2019 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.dao.cache; |
|||
|
|||
import org.springframework.beans.factory.annotation.Value; |
|||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
|||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
|||
import org.springframework.context.annotation.Configuration; |
|||
import org.springframework.data.redis.connection.RedisStandaloneConfiguration; |
|||
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration; |
|||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; |
|||
|
|||
import java.time.Duration; |
|||
|
|||
@Configuration |
|||
@ConditionalOnMissingBean(CaffeineCacheConfiguration.class) |
|||
@ConditionalOnProperty(prefix = "redis.connection", value = "type", havingValue = "standalone") |
|||
public class TBRedisStandaloneConfiguration extends TBRedisCacheConfiguration { |
|||
|
|||
@Value("${redis.standalone.host}") |
|||
private String host; |
|||
|
|||
@Value("${redis.standalone.port}") |
|||
private Integer port; |
|||
|
|||
@Value("${redis.standalone.clientName}") |
|||
private String clientName; |
|||
|
|||
@Value("${redis.standalone.connectTimeout}") |
|||
private Long connectTimeout; |
|||
|
|||
@Value("${redis.standalone.readTimeout}") |
|||
private Long readTimeout; |
|||
|
|||
@Value("${redis.standalone.useDefaultClientConfig}") |
|||
private boolean useDefaultClientConfig; |
|||
|
|||
@Value("${redis.standalone.usePoolConfig}") |
|||
private boolean usePoolConfig; |
|||
|
|||
@Value("${redis.db}") |
|||
private Integer db; |
|||
|
|||
@Value("${redis.password}") |
|||
private String password; |
|||
|
|||
public JedisConnectionFactory loadFactory() { |
|||
RedisStandaloneConfiguration standaloneConfiguration = new RedisStandaloneConfiguration(); |
|||
standaloneConfiguration.setHostName(host); |
|||
standaloneConfiguration.setPort(port); |
|||
standaloneConfiguration.setDatabase(db); |
|||
standaloneConfiguration.setPassword(password); |
|||
if (useDefaultClientConfig) { |
|||
return new JedisConnectionFactory(standaloneConfiguration); |
|||
} else { |
|||
return new JedisConnectionFactory(standaloneConfiguration, buildClientConfig()); |
|||
} |
|||
} |
|||
|
|||
private JedisClientConfiguration buildClientConfig() { |
|||
if (usePoolConfig) { |
|||
return JedisClientConfiguration.builder() |
|||
.clientName(clientName) |
|||
.connectTimeout(Duration.ofMillis(connectTimeout)) |
|||
.readTimeout(Duration.ofMillis(readTimeout)) |
|||
.usePooling().poolConfig(buildPoolConfig()) |
|||
.build(); |
|||
} else { |
|||
return JedisClientConfiguration.builder() |
|||
.clientName(clientName) |
|||
.connectTimeout(Duration.ofMillis(connectTimeout)) |
|||
.readTimeout(Duration.ofMillis(readTimeout)).build(); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue