DB-redis-install

docker swarm 安装 redis

  1. 创建redis挂载目录/dockerdata/v-redis

  2. 并在该目录vim redis.conf新建配置文件,配置文件添加如下内容

    1
    requirepass <登陆密码,最好64位以上>
  3. 编辑vim stack-redis.yml,内容如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    version: '3'
    services:
    redis:
    image: redis
    restart: always
    ports:
    - 14007:6379
    command: "redis-server /data/redis.conf"
    volumes:
    - "/dockerdata/v-redis:/data"
    deploy:
    replicas: 1
    restart_policy:
    condition: on-failure
    placement:
    constraints: [node.hostname == xuanps]
  4. 运行启动docker stack deploy -c stack-redis.yml redis

  5. 测试命令

    1
    2
    3
    4
    5
    6
    #本地直接redis-cli不需要任何参数
    redis-cli -h host -p port -a password
    #进入redis,用改名了进行密码登陆
    AUTH "password"
    #查询所有key
    keys *

springboot 连接redis

  1. 添加pom.xml依赖

    1
    2
    3
    4
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
  2. 编辑application.yml添加redis连接信息

    1
    2
    3
    4
    5
    6
    spring:
    redis:
    database: 0
    host: 112.74.51.136
    port: 14007
    password: <你的密码>
  3. 编写测试类

    1
    2
    3
    4
    5
    6
    7
    8
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Test
    public void testredis(){
    stringRedisTemplate.opsForValue().set("testconnect","hello world");
    String test= stringRedisTemplate.opsForValue().get("testconnect");
    log.info(test);
    }

idea redis插件iedis