文章目录

  • redis-jedis连接java
  • 开放外界ip
  • 保护模式
  • 非保护模式
  • 单机redis连接及测试
  • 是否能ping通
  • 设置key/vuel对
  • 通过key获得vuel
  • 通过pool获得连接
  • 自增操作
  • 运行截图
  • 项目打包
  • 集群redis连接(未)
  • spring单机redis连接
  • maven坐标
  • applicationContext.xml
  • 测试
  • spring集群redis连接(未)
  • spring-dao层封装
  • 接口
  • 实现类
  • 打印截图
  • 项目打包


redis-jedis连接java

java使用mysqlxxx.jar来连接/操作mysql,类似java可以使用jedisxxx.jar来连接/操作redis。

开放外界ip

在配置文件中,配置哪些ip地址可以连接redis。

保护模式

开启保护模式

protected-mode yes

开放指定ip

bind 192.168.1.100

只有 192.168.1.100 ip才能连接redis。

非保护模式

关闭保护模式

protected-mode no

注释 bind 参数

#bind 127.0.0.1

所有ip都能连接redis。

单机redis连接及测试

maven坐标

<dependencies>
  	<dependency>
  		<groupId>redis.clients</groupId>
  		<artifactId>jedis</artifactId>
  		<version>2.9.0</version>
  	</dependency>
  	<dependency>
  		<groupId>junit</groupId>
  		<artifactId>junit</artifactId>
  		<version>4.12</version>
  	</dependency>
  </dependencies>

是否能ping通

/**
	 * 测试:是否能ping通redis
	 */
	@Test
	public void test() {
		Jedis jedis = new Jedis("192.168.197.130", 6378);
		String ping = jedis.ping();
		System.out.println(ping);
	}

设置key/vuel对

/**
	 * 测试:添加kay/vual对
	 */
	@Test
	public void test2() {
		Jedis jedis = new Jedis("192.168.197.130", 6378);
		String set = jedis.set("name", "cjw");
		System.out.println(set);
	}

通过key获得vuel

/**
	 * 测试:通过key获得vule
	 */
	@Test
	public void test3() {
		Jedis jedis = new Jedis("192.168.197.130", 6378);
		String set = jedis.set("name", "cjw");
		System.out.println(set);
	}

通过pool获得连接

/**
	 * 测试:连接池获取连接
	 */
	@Test
	public void test4() {
		JedisPool jPool = new JedisPool("192.168.197.130", 6378);
		Jedis resource = jPool.getResource();
		System.out.println(resource.ping());
	}

自增操作

/**
	 * 测试:自增操作
	 */
	@Test
	public void test5() {
		JedisPool jPool = new JedisPool("192.168.197.130", 6378);
		Jedis resource = jPool.getResource();
		String age = resource.get("age");
		System.out.println("age:"+age);
		resource.incr("age");
		age = resource.get("age");
		System.out.println("incr age...");
		System.out.println("age:"+age);
	}

运行截图

redis ssl redis ssl连接 java_System

项目打包

项目 打包 提取码:su1x 环境:eclipse,maven,jdk1.8

集群redis连接(未)

spring单机redis连接

maven坐标

<dependencies>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-context</artifactId>
  		<version>5.1.7.RELEASE</version>
  	</dependency>
  	<dependency>
  		<groupId>junit</groupId>
  		<artifactId>junit</artifactId>
  		<version>4.12</version>
  	</dependency>
  	<dependency>
  		<groupId>redis.clients</groupId>
  		<artifactId>jedis</artifactId>
  		<version>2.9.0</version>
  	</dependency>
  </dependencies>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

	<!-- 连接池配置 -->
	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<!-- 最大连接数 -->
		<property name="maxTotal" value="30" />
		<!-- 最大空闲连接数 -->
		<property name="maxIdle" value="10" />
		<!-- 每次释放连接的最大数目 -->
		<property name="numTestsPerEvictionRun" value="1024" />
		<!-- 释放连接的扫描间隔(毫秒) -->
		<property name="timeBetweenEvictionRunsMillis" value="30000" />
		<!-- 连接最小空闲时间 -->
		<property name="minEvictableIdleTimeMillis" value="1800000" />
		<!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
		<property name="softMinEvictableIdleTimeMillis" value="10000" />
		<!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
		<property name="maxWaitMillis" value="1500" />
		<!-- 在获取连接的时候检查有效性, 默认false -->
		<property name="testOnBorrow" value="false" />
		<!-- 在空闲时检查有效性, 默认false -->
		<property name="testWhileIdle" value="false" />
		<!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
		<property name="blockWhenExhausted" value="false" />
	</bean>
	<!-- jedis客户端单机版 -->
	<bean id="redisClient" class="redis.clients.jedis.JedisPool">
		<constructor-arg name="host" value="192.168.197.130"></constructor-arg>
		<constructor-arg name="port" value="6378"></constructor-arg>
		<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
	</bean>


</beans>

测试

/**
	 * 测试:sping整合redis连接测试
	 */
	@Test
	public void test() {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		JedisPool jPool = ac.getBean(JedisPool.class);
		Jedis jedis = jPool.getResource();
		System.out.println(jedis.ping());
	}

spring集群redis连接(未)

spring-dao层封装

接口

package com.cjw.dao;

public interface IJedisClient {
	
	/**
	 * 功能:根据key返回vule(字符串类型)
	 * @param key
	 * @return
	 */
	String get(String key);

	/**
	 * 功能:设置key/vual对(字符串类型)
	 * @param key
	 * @param value
	 * @return
	 */
	String set(String key, String value);

	/**
	 * 功能:根据key返回vule(hash)
	 * @param hkey
	 * @param key
	 * @return
	 */
	String hget(String hkey, String key);

	/**
	 * 功能:设置成员属性(hash)
	 * @param hkey
	 * @param key
	 * @param value
	 * @return
	 */
	long hset(String hkey, String key, String value);

	/**
	 * 功能:自增1
	 * @param key
	 * @return
	 */
	long incr(String key);

	/**
	 * 功能:设置key过期时间,单位为秒
	 * @param key
	 * @param second
	 * @return
	 */
	long expire(String key, int second);

	/**
	 * 功能:获取过期时间
	 * @param key
	 * @return
	 */
	long ttl(String key);

	/**
	 * 功能:删除key
	 * @param key
	 * @return
	 */
	long del(String...key);

	/**
	 * 功能:删除成员属性(hash)
	 * @param hkey
	 * @param fields
	 * @return
	 */
	long hdel(String hkey, String...fields);

}

实现类

package com.cjw.dao.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.cjw.dao.IJedisClient;

import redis.clients.jedis.Jedis;

@Repository
public class IJedisClientImpl implements IJedisClient{

	@Autowired
	private Jedis jedis;
	
	
	@Override
	public String get(String key) {
		return jedis.get(key);
	}

	@Override
	public String set(String key, String value) {
		return jedis.set(key, value);
	}

	@Override
	public String hget(String hkey, String key) {
		return jedis.hget(hkey, key);
	}

	@Override
	public long hset(String hkey, String key, String value) {
		return jedis.hset(hkey, key, value);
	}

	@Override
	public long incr(String key) {
		return jedis.incr(key);
	}

	@Override
	public long expire(String key, int second) {
		return jedis.expire(key, second);
	}

	@Override
	public long ttl(String key) {
		return jedis.ttl(key);
	}

	@Override
	public long del(String... key) {
		return jedis.del(key);
	}

	@Override
	public long hdel(String hkey, String... fields) {
		return jedis.hdel(hkey, fields);
	}

}

测试类

package com.cjw.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cjw.dao.impl.IJedisClientImpl;

public class Test01 {

	/**
	 * 测试:根据key返回vule(字符串类型)
	 */
	@Test
	public void test() {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		IJedisClientImpl clientImpl = ac.getBean(IJedisClientImpl.class);
		String set = clientImpl.get("name");
		System.out.println(set);
	}
	
	/**
	 * 测试:设置key/vual对(字符串类型)
	 */
	@Test
	public void test2() {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		IJedisClientImpl clientImpl = ac.getBean(IJedisClientImpl.class);
		String set = clientImpl.set("name", "cjw");
		System.out.println(set);
	}
	
	/**
	 * 测试:根据key返回vule(hash)
	 */
	@Test
	public void test3() {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		IJedisClientImpl clientImpl = ac.getBean(IJedisClientImpl.class);
		String set = clientImpl.hget("user", "name");
		System.out.println(set);
	}
	
	
	/**
	 * 测试:设置成员属性(hash)
	 */
	@Test
	public void test4() {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		IJedisClientImpl clientImpl = ac.getBean(IJedisClientImpl.class);
		long hset = clientImpl.hset("user", "sex", "men");
		System.out.println(hset);
	}
	
	
	/**
	 * 测试:自增1
	 */
	@Test
	public void test5() {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		IJedisClientImpl clientImpl = ac.getBean(IJedisClientImpl.class);
		long incr = clientImpl.incr("age");
		System.out.println(incr);
	}
	
	
	/**
	 * 测试:设置key过期时间,单位为秒
	 */
	@Test
	public void test6() {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		IJedisClientImpl clientImpl = ac.getBean(IJedisClientImpl.class);
		long expire = clientImpl.expire("age", 20);
		System.out.println(expire);
	}
	
	
	/**
	 * 测试:获取过期时间
	 */
	@Test
	public void test7() {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		IJedisClientImpl clientImpl = ac.getBean(IJedisClientImpl.class);
		long ttl = clientImpl.ttl("age");
		System.out.println(ttl);
	}
	
	/**
	 * 测试:删除成员属性(hash)
	 */
	@Test
	public void test8() {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		IJedisClientImpl clientImpl = ac.getBean(IJedisClientImpl.class);
		long hdel = clientImpl.hdel("user", "sex");
		System.out.println(hdel);
	}

}

打印截图

redis ssl redis ssl连接 java_redis ssl_02

项目打包

项目打包 提取码:87oa 环境:eclipse,maven,jdk1.8