docker的安装与容器使用(redis为例)

一、MAC系统安装 docker:

参见教程:https://www.runoob.com/docker/macos-docker-install.html

二、docker中安装容器,以安装 redis 为例:

参见教程:https://www.runoob.com/docker/docker-install-redis.html

三、启动docker中的redis:

      

四、在 Java程序中使用redis

首先在pom.xml中引入redis的依赖:

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

然后在程序中测试(确保docker 中的 redis 已经启动):

import redis.clients.jedis.Jedis;

public class stateStorageToRedis {
    public static void main(String[] args) throws Exception{
        //连接本地的 Redis 服务
        Jedis jedis = new Jedis("localhost");
        // 如果 Redis 服务设置了密码,需要下面这行,没有就不需要
        // jedis.auth("123456");
        System.out.println("连接成功");
        System.out.println("服务正在运行: "+jedis.ping());
        //设置 redis 字符串数据
        jedis.set("hello", "luochao");
        // 获取存储的数据并输出
        System.out.println("redis 存储的字符串为: "+ jedis.get("hello"));
    }
}

输出如下:

    

原文地址:https://www.cnblogs.com/luo-c/p/15503609.html