慕课网~redis入门~笔记~(三)Jedis的入门使用

https://www.imooc.com/learn/839

Jedis入门

  • 客户端语言
  • Jedis
  • 使用Jedis连接Redis(使用java代码方式)

    • 在连接之前需要做以下操作

      • 开放端口号:linux服务器使用了防火墙,需要针对redis的默认端口号6379进行开放操作才能连接使用,命令如下

        firewall-cmd --zone=public --add-port=6379/tcp --permanent --permanent永久生效,没有此参数重启后失效)
        firewall-cmd --reload
        
      • 修改配置文件绑定的ip地址:由于默认指定的配置文件设置了bind属性,需要更改这个值,其实任意ip都可以进行连接,具体操作为将 bind 127.0.0.1 更改为 bind 0.0.0.0

    • 编写代码进行测试连接

    • 创建Maven项目,引入如下依赖

      • junit:用于测试
      • jedis:连接redis
      • commons-pool2:jedis的依赖

            <dependencies>
                <!-- https://mvnrepository.com/artifact/junit/junit -->
                <dependency>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                    <version>4.12</version>
                    <!--<scope>test</scope>-->
                </dependency>
        
                <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
                <dependency>
                    <groupId>redis.clients</groupId>
                    <artifactId>jedis</artifactId>
                    <version>2.9.0</version>
                </dependency>
        
                <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
                <dependency>
                    <groupId>org.apache.commons</groupId>
                    <artifactId>commons-pool2</artifactId>
                    <version>2.6.0</version>
                </dependency>
            </dependencies>
      • 测试代码如下

        • 单实例的测试

              @Test
              public void demo1(){
                  Jedis jedis=new Jedis("192.168.73.129",6379);
                  jedis.set("name","tony");
                  String value=jedis.get("name");
                  System.out.println(value);
                  jedis.close();
              }
        • 连接池方式连接

          @Test
              public void demo2() {
                  //获得连接池的配置对象
                  JedisPoolConfig config = new JedisPoolConfig();
                  //设置最大连接数
                  config.setMaxTotal(30);
                  //设置最大空闲连接数
                  config.setMaxIdle(10);
                  //获取连接池对象
                  JedisPool jedisPool = new JedisPool(config, "192.168.73.129", 6379);
                  //获取核心对象
                  Jedis jedis = null;
                  try {
                      jedis = jedisPool.getResource();
                      jedis.set("name", "hello");
                      String value = jedis.get("name");
                      System.out.println(value);
                  } catch (Exception e) {
                      e.printStackTrace();
                  } finally {
                      if (jedis != null) {
                          jedis.close();
                      }
                      if (jedisPool != null) {
                          jedisPool.close();
                      }
                  }
              }

猜你喜欢

转载自blog.csdn.net/yanlovehan/article/details/81558627