macOS创建Redis集群脚本与SpringBoot测试

不管现实多么惨不忍睹,都要持之以恒地相信,这只是黎明前短暂的黑暗而已。不要惶恐眼前的难关迈不过去,不要担心此刻的付出没有回报,别再花时间等待天降好运。真诚做人,努力做事!你想要的,岁月都会给你。macOS创建Redis集群脚本与SpringBoot测试,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

环境准备

Redis:6.2.6
SpringBoot:v2.2.6.RELEASE
Java:8
Maven:3.6.3

安装Redis

macOS安装:brew install redis

搭建集群

安装完Redis直接用我编写的脚本,一键启停。

集群启动脚本

脚本说明:自动创建9个Redis节点配置文件。
新建脚本文件run.sh,将下面脚本写入文件。

echo "==============================当前工作目录==============================="
workdir="$(cd $(dirname $0); pwd)/redis-cluster"
echo "$workdir"
# 层级文件创建
mkdir -p "$workdir"
cd "$workdir"
echo "=============================创建节点配置文件=============================="
for i in {1..9}; do
  conf_file="$workdir/700${i}.conf"
  conf_content="port 700${i}\r\ndaemonize no\r\ncluster-enabled yes\r\ncluster-config-file cluster-nodes-700${i}.conf\r\ncluster-node-timeout 1000\r\ncluster-replica-validity-factor 5\r\ncluster-migration-barrier 1\r\ncluster-require-full-coverage yes\r\ncluster-replica-no-failover no\r\nappendonly yes"
  echo "创建文件并写入配置数据:$conf_file"
  # 创建配置文件
  touch "${conf_file}"
  # 避免因为权限而找不到文件
  chmod u+x "${conf_file}"
  # 向配置文件写入数据
  echo "${conf_content}" > "${conf_file}"
done
echo "==============================节点启动信息==============================="
conf_list=""
for i in {1..9}; do
  redis-server "$workdir"/700"${i}".conf &
  conf_list="$conf_list""[redis-cluster/700${i}.conf]"
done
echo "redis-server run with cluster node:{$conf_list}"
sleep 3s
echo "==============================集群节点信息==============================="
redis-cli -c -h 127.0.0.1 -p 7001 cluster nodes
echo "===============================集群信息=================================="
redis-cli -c -h 127.0.0.1 -p 7001 cluster info
echo "==============================集群槽位信息================================="
redis-cli --cluster create 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006 127.0.0.1:7007 127.0.0.1:7008 127.0.0.1:7009 --cluster-replicas 1
redis-cli -c -h 127.0.0.1 -p 7001 cluster slots
echo "==============================集群进程信息================================="
ps aux|grep redis
echo "===============================集群启动完成==============================="
echo "have fun!!!"
集群停止脚本

新建脚本文件stop.sh,将下面脚背写入文件。

echo "==============================当前工作目录==============================="
workdir=$(cd $(dirname $0); pwd)
echo "$workdir"
echo "==============================关闭集群节点==============================="
for i in {1..9}; do
  redis-cli -p 700"$i" shutdown
  echo "关闭节点{700$i} OK!"
done
echo "===============================集群关闭完成==============================="
echo "see you later..."

SpringBootTest测试Redis集群

配置文件

pom.xml

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>

application.yml

spring:
  redis:
    cluster:
      urls:
        - 127.0.0.1:7001
        - 127.0.0.1:7002
        - 127.0.0.1:7003
        - 127.0.0.1:7004
        - 127.0.0.1:7005
        - 127.0.0.1:7006
        - 127.0.0.1:7007
        - 127.0.0.1:7008
        - 127.0.0.1:7009
      poolConfig:
        max-total: 9
        max-idle: 9
        max-wait-millis: -1
        min-idle: 0

RedisConfig.java

@Getter
@Setter
@Configuration
@ConfigurationProperties(prefix = "spring.redis.cluster")
public class RedisConfig {
    private List<String> urls;
    private JedisPoolConfig poolConfig;

    @Bean
    RedisClusterConfiguration redisClusterConfiguration() {
        RedisClusterConfiguration configuration = new RedisClusterConfiguration();
        List<RedisNode> nodes = new ArrayList<>();
        for (String url : urls) {
            String[] strings = url.split(":");
            nodes.add(new RedisNode(strings[0], Integer.parseInt(strings[1])));
        }
//        configuration.setPassword(RedisPassword.of("你的密码,没有留白"));
        configuration.setClusterNodes(nodes);
        return configuration;
    }

    @Bean
    <K, V> RedisTemplate<K, V> redisTemplate() {
        RedisTemplate<K, V> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(jedisConnectionFactory());
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
        return redisTemplate;
    }

    @Bean
    StringRedisTemplate stringRedisTemplate() {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(jedisConnectionFactory());
        stringRedisTemplate.setKeySerializer(new StringRedisSerializer());
        stringRedisTemplate.setKeySerializer(new StringRedisSerializer());
        return stringRedisTemplate;
    }

    /**
     * Jedis 连接工厂
     */
    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        return new JedisConnectionFactory(redisClusterConfiguration(), poolConfig);
    }
}
编写单元测试
@EnableConfigurationProperties
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {RedisAutoConfiguration.class})
public class AppTest {

    @Autowired
    private RedisTemplate<String, Map<String, String>> redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Test
    public void redisTemplate() {
        ValueOperations<String, Map<String, String>> ops = redisTemplate.opsForValue();
        Map<String, String> map = new HashMap<>();
        map.put("name", "《鸡你太美》");
        map.put("author", "蔡徐坤");
        ops.set("cxk", map);
        System.out.println(ops.get("cxk"));
    }
}

运行测试

1 启动Redis集群

可以通过bash启动。
在这里插入图片描述
也可以通过idea图形化启动。
在这里插入图片描述
启动效果:
在这里插入图片描述
关闭节点:见上述关闭脚本。

2 执行单元测试

在这里插入图片描述
测试结果:
在这里插入图片描述

源码

参见:https://github.com/lmmarisej/SpringBootVue/releases/tag/1.0-SNAPSHOT-redis-cluster

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/180296.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!