栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

环境准备

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)
echo "$workdir"
echo "=============================创建节点配置文件=============================="
for i in {1..9}; do
  conf_file="$workdir/redis-cluster/700${i}.conf"
  conf_content="port 700${i}rndaemonize norncluster-enabled yesrncluster-config-file cluster-nodes-700${i}.confrncluster-node-timeout 1000rncluster-replica-validity-factor 5rncluster-migration-barrier 1rncluster-require-full-coverage yesrncluster-replica-no-failover nornappendonly yes"
  echo "创建文件并写入配置数据:$conf_file"
  # 层级文件创建
  mkdir -p "$workdir/redis-cluster"
  # 创建配置文件
  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"/redis-cluster/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 -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

        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
            redis.clients
            jedis
        

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 urls;
    private JedisPoolConfig poolConfig;

    @Bean
    RedisClusterConfiguration redisClusterConfiguration() {
        RedisClusterConfiguration configuration = new RedisClusterConfiguration();
        List 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
     RedisTemplate redisTemplate() {
        RedisTemplate 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;
    }

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

    @Autowired
    private RedisTemplate> redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Test
    public void redisTemplate() {
        ValueOperations> ops = redisTemplate.opsForValue();
        Map 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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/322154.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号