Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux綜合 >> 學習 Linux

CentOS 7 在 VMware 中搭建 Redis Cluster 六節點叢集

環境:CentOS 7(VMware)+ Redis 3.2.5。Redis 3.2.5 的叢集工具需要較新的 Ruby 與 gem,本次使用 Ruby 2.3.3;低版本 Ruby 在建立叢集時會出錯。

一、前置工具

1. 安裝開發套件

yum install openssl* openssl-devel zlib-devel gcc gcc-c++ make autoconf \
readline-devel curl-devel expat-devel gettext-devel

2. 安裝 Ruby

wget https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.3.tar.gz
tar zxvf ruby-2.3.3.tar.gz
cd ruby-2.3.3
./configure
make && make install
ruby -v

3. 安裝 redis gem

wget https://rubygems.global.ssl.fastly.net/gems/redis-3.2.1.gem
gem install redis-3.2.1.gem

二、建構叢集

1. 安裝 Redis

wget https://download.redis.io/releases/redis-3.2.5.tar.gz
tar -zxf redis-3.2.5.tar.gz
cd redis-3.2.5
make && make install

2. 建立六份設定檔

叢集最少需要 6 個節點(3 master + 3 slave):

mkdir conf
cd conf
vi redis-6379.conf
cp redis-6379.conf redis-6380.conf
cp redis-6379.conf redis-6381.conf
cp redis-6379.conf redis-6382.conf
cp redis-6379.conf redis-6383.conf
cp redis-6379.conf redis-6384.conf

設定檔內容(每份只改 port 與相關檔名):

port 6379
daemonize yes
dir /data/redis/redis-6379
logfile /data/redis/redis-6379/redis-6379.log
pidfile /var/run/redis-6379.pid
dbfilename dump-6379.rdb
appendonly yes
appendfilename "appendonly-6379.aof"
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 5000
protected-mode no

protected-mode 的說明可參考:https://arui.me/index.php/archives/151/。關閉保護模式僅適合實驗環境,正式部署應改用密碼或 bind 限制來源。

3. 啟動六個節點

./src/redis-server conf/redis-6379.conf
./src/redis-server conf/redis-6380.conf
./src/redis-server conf/redis-6381.conf
./src/redis-server conf/redis-6382.conf
./src/redis-server conf/redis-6383.conf
./src/redis-server conf/redis-6384.conf

4. 建立叢集

--replicas 1 代表每個 master 配一個 slave。這裡刻意使用實機 IP 而非 127.0.0.1,因為用 loopback 建立的叢集,JedisCluster 客戶端會連不上:

./src/redis-trib.rb create --replicas 1 172.21.20.25:6379 172.21.20.25:6380 \
  172.21.20.25:6381 172.21.20.25:6382 172.21.20.25:6383 172.21.20.25:6384

CentOS 7 在 VMware 中搭建 Redis Cluster 六節點叢集

CentOS 7 在 VMware 中搭建 Redis Cluster 六節點叢集

5. 檢查叢集狀態

redis-cli -h 172.21.20.25 -p 6379

三、Java 客戶端接入

使用 JedisCluster 搭配 Spring Boot,Maven 依賴:

<dependency>
  <groupId>redis.clients</groupId>
  <artifactId>jedis</artifactId>
  <version>2.9.0</version>
</dependency>

Bean 設定:

@Configuration
@EnableConfigurationProperties(RedisSettings.class)
public class JedisClusterConfig {

    @Autowired
    private RedisSettings redisSettings;

    @Bean
    public JedisCluster jedisCluster() {
        Set<HostAndPort> set = new HashSet<HostAndPort>();
        List<String> hostPorts = redisSettings.getNodes();
        if (null != hostPorts && hostPorts.size() > 0) {
            String[] ipPortPair;
            for (String hostPort : hostPorts) {
                ipPortPair = hostPort.split(":");
                set.add(new HostAndPort(ipPortPair[0], Integer.parseInt(ipPortPair[1])));
            }
        }
        return new JedisCluster(set, 300000);
    }

    @Bean
    public GenericObjectPoolConfig genericObjectPoolConfig() {
        GenericObjectPoolConfig config = new GenericObjectPoolConfig();
        config.setMaxTotal(500);
        config.setMaxIdle(5);
        config.setMaxWaitMillis(1000 * 100);
        config.setTestOnBorrow(true);
        return config;
    }
}

設定屬性類別與 redis.properties:

@ConfigurationProperties(locations = "classpath:redis.properties", prefix = "redis.cluster")
public class RedisSettings {
    private List<String> nodes;
    public List<String> getNodes() { return nodes; }
    public void setNodes(List<String> nodes) { this.nodes = nodes; }
}

redis.cluster.nodes[0] = 172.21.20.25:6379
redis.cluster.nodes[1] = 172.21.20.25:6380
redis.cluster.nodes[2] = 172.21.20.25:6381
redis.cluster.nodes[3] = 172.21.20.25:6382
redis.cluster.nodes[4] = 172.21.20.25:6383
redis.cluster.nodes[5] = 172.21.20.25:6384

四、防火牆

CentOS 7 預設使用 firewalld(7 以下才是 iptables):

systemctl stop firewalld.service
systemctl disable firewalld.service

若沿用 iptables,重啟後永久生效的寫法:

chkconfig iptables on    # 開啟
chkconfig iptables off   # 關閉

立即生效但重啟失效:

service iptables start   # 開啟
service iptables stop    # 關閉
service iptables restart # 重啟
service iptables save    # 保存設定

叢集實際運作需要 6379-6384 與 16379-16384 兩段埠都通,若堅持開著防火牆,請放行這兩段而非直接關閉。

參考:https://www.07net01.com/2016/10/1691935.html

https://xxxxxx/Linuxjc/1184784.html TechArticle

Copyright © Linux教程網 All Rights Reserved