Redis Connect Java Project

Keywords: Database Redis Jedis Maven Apache

1. Create a new Maven Project: Redis
2. Allow output folders of source folders


3. Modify the pom.xml file

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>Redis</groupId>
	<artifactId>Redis</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>Redis Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<!--4.12junit Only Unit Testing Function -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>

		<!--Introduce servlet Dependence: Solution jsp Page error reporting -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>4.0.0-b07</version>
			<scope>provided</scope>
		</dependency>

		<!--Introduce redis Client jedis -->
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>2.9.0</version>
		</dependency>

	</dependencies>
	<build>
		<finalName>Redis</finalName>
	</build>
</project>

4. Create a new unit test class: try to connect Redis installed on Linux

5. Open the virtual machine to connect Putty to Super Administrator Provinces and enter ifconfig in the root directory to view Linux's ip

Enter / usr/myRedis and enter the command in turn.
./redis-server redis.conf
./redis-cli

Current code in TestRedis.java

package com.redis.test;

import org.junit.Test;

import redis.clients.jedis.Jedis;

/**
 * 
* @ClassName: TestRedis
* @Description:Attempt to connect Redis installed on Linux
* @author Small thought
* @date 2018 8:08 p.m. on October 18, 2001
*
 */
public class TestRedis {
	
	@Test
	public void test(){
		//Create client objects for java connection redis
		// ip of host:redis installed host
		// port:redis Occupied Port Number
		Jedis jedis=new Jedis("192.168.110.128",6379);
		System.out.println(jedis.ping());
		
	}

}

Then run in the unit test and report back

This error requires to go to the redis installation directory to modify his redis.conf
Enter commands in the / usr/myRedis directory and return:
vim redis.conf


After the modification, press esc and enter: wq return
Then restart Linux to restart redis, console output

Redis connects to java successfully and can operate redis in java

In the end, it's not easy to write. If you like it or it's helpful to you, remember to praise it, pay attention to it or collect it.

Posted by crseader on Tue, 29 Jan 2019 21:45:15 -0800