Linux环境下docker中安装Redis--整合springboot

Posted You295

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux环境下docker中安装Redis--整合springboot相关的知识,希望对你有一定的参考价值。

Linux环境下docker中安装Redis--整合springboot

一:安装流程

1.搜索镜像源

docker search redis


2.下载最新版redis

docker pull docker.io/redis

3.下载成功后可以查看其信息

docker images


4.运行redis容器

docker run -itd --name redis -p 6379:6379 docker.io/redis


5.执行redis的容器

docker exec -it redis /bin/bash

6.启动redis客户端(client)

redis-cli


7.测试redis是否连接成功

二:Redis类型常用命令

1.String类型

1)SET key value :添加

2)EXPIRE 秒 :设置存活时间


3)DEL key :删除指定的key

4)GET key :获得value值

5)GETRANGE key start end :截取value值

6)GETSET key value :替换之前的字符串

7)STRLEN key :获得value值得长度

8)APPEND key value :设置值与value进行拼接返回其长度

2.Hash类型

1)HSET key field value :添加

2)HGET key field :获得value值

3)HMSET key field1 value field2 value… :集合
4)HMGET key field1 field2 :取出对应的value

3.list类型

1)LPUSH key value1 value2… :添加值,返回加入的数量

2)LRANGE key start stop :根据位置取出value


4.set类型
1)SADD key value… :添加,不能添加重复的

三:springBoot整合redis

1.引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2.配置resources

  redis:
    host: 192.168.0.158  //ip地址
    port: 6379  //端口号
    # redis的数据库从0~15
    database: 0

3.配置redis(官方给出)

@Configuration
public class MyConfig 
    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    @Bean
    public RedisTemplate<String,Object> redisTemplate()
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer
                = new Jackson2JsonRedisSerializer(Object.class);
        StringRedisSerializer stringRedisSerializer
                = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);

        redisTemplate.setEnableDefaultSerializer(true);
        redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);

        return  redisTemplate;
    

4.redis的存取
存:

  @GetMapping("/redis")
    public HttpResp redis()
        Book book = new Book();
        book.setTitle("水果湖");
        book.setAuthor("dyit");
        book.setPrice(123.00);
        redisTemplate.opsForValue().set("book",book);
        return new HttpResp();
    

取:

@GetMapping("/redis2")
    public HttpResp redis2()
        HttpResp resp = new HttpResp();
        resp.setResults(redisTemplate.opsForValue().get("book"));
        return resp;
    

以上是关于Linux环境下docker中安装Redis--整合springboot的主要内容,如果未能解决你的问题,请参考以下文章

Linux环境下docker中安装Redis--整合springboot

Linux中安装Docker,在Docker中安装MySQL和Redis并在Windows下连接访问

在 CentOS 7.9 中安装最新的 Redis

wsl中安装docker

如何在ubunto中安装docker

如何在suse linux中安装redis集群