Home > Java > javaTutorial > body text

How to connect Java to Redis

WBOY
Release: 2023-05-19 11:25:05
forward
2311 people have browsed it

Java connects to Redis

Jedis Client is a java client recommended by the Redis official website. The library file implements encapsulation and calling of various redis APIs.

Introducing the jar package

I created a maven project, so I only need to add

<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>3.0.0</version>
		</dependency>
Copy after login

to the pom file. If it is not a maven project, you have to make sure to introduce the relevant dependencies


How to connect Java to Redis

Write test class

package cn.jiangdoc;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
 * 
 * @author jiangdoc
 *
 */
public class JedisUtil {
	public static void main(String[] args) {
	//ip地址,端口号
		Jedis jedis = cli_single("192.168.1.103", 6379);
		jedis.set("key", "first Java connect!");
		String value = jedis.get("key");
		System.out.println(value);
		
	}
	/**
	 * 单个连接
	 * 
	 * @param host
	 * @param port
	 * @return
	 */
	public static Jedis cli_single(String host, int port) {
		try {
			return new Jedis(host, port);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 连接池
	 * 
	 * @param host
	 * @param port
	 * @return
	 */
	public static Jedis cli_pool(String host, int port) {
		JedisPoolConfig config = new JedisPoolConfig();
		// 最大连接数
		config.setMaxTotal(10);
		// 最大连接空闲数
		config.setMaxIdle(2);
		JedisPool jedisPool = new JedisPool(config, host, port);
		try{
			
			return jedisPool.getResource();
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
}
Copy after login

Note: If

error occurs: Exception in thread “main” redis.clients.jedis.exceptions.JedisConnectionException:

Check whether the port is open

Solution:

  • 1. Turn off the firewall: service iptables stop

  • 2. Open port:

(1. Modify the configuration file: vi /etc/sysconfig/iptabls append: -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 6379-j ACCEPT

(2. Package save changes: service iptables save

(3. Restart the service: service iptables restart

View redis configuration file

How to connect Java to Redis

Error report: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command ‘CONFIG SET protected-mode no’ from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to ‘no’, and then restarting the server. 3) If you started the server manually just for testing, restart it with the ‘protected-mode no’ option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

The error message is very long, but it mainly means that redis has turned on protected mode, which is also a new feature added in Redis 3.2. Redis that turns on protected mode only allows local login, which is also set in the configuration file. In redis.conf

it turns out that yes means turning on the protected mode. You can fill in the password or no to turn off the protected mode. Here we choose to turn off the protected mode. After wq saves and exits, restart redis-server

How to connect Java to Redis

Just run it below

Jedis Common Method API

Some time ago I introduced to you how to deploy and operate redis in the Linux environment. Today I will introduce to you how to operate redis in our Java code. Next step by step:

1. First, import jedis-2.1.0.jar (jedis basic package)

into the java project

2. Create jedis object

How to connect Java to Redis

3. Key operations

How to connect Java to Redis

4. String operations

How to connect Java to Redis

5. Integer and floating-point number operations

How to connect Java to Redis

6. List operations

How to connect Java to Redis

7. Sets ( Set) operation

How to connect Java to Redis

8. Hash operation

How to connect Java to Redis

9. Ordered set (Zsort) operation

How to connect Java to Redis

10. Sorting operation

How to connect Java to Redis

The above is the detailed content of How to connect Java to Redis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template