sudo apt-get install redis
Do allow port 6379, which is being used by Redis.
sudo ufw allow 6379
You can connect to redis using redis-cli. You can test your operations on the redis server using the cli
Connect to remote redis client
redis-cli -h 192.168.1.20 -p 6379
Get and SET operation using CLI.
127.0.0.1:6379> SET x "Hello World"
OK
127.0.0.1:6379> GET X
(nil)
127.0.0.1:6379> GET x
"Hello World"
Now we will try to perform the same operation using the java client.
There are two popular clients in java for connecting to Redis.
We are going to use redisson for our example.
grab 'org.redisson:redisson:3.2.0'
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.2.0</version>
</dependency>
In the below code we are trying to connnect to redis server installed on the same machine.
import org.redisson.Redisson;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
Config config = new Config();
// use single Redis server
config.useSingleServer().setAddress("redis://127.0.0.1:6379");
redisson = Redisson.create(config);
// perform operations
bucket = redisson.getBucket("simpleObject");
bucket.set("This is object value");
RMap<String, String> map = redisson.getMap("simpleMap");
map.put("mapKey", "This is map value");
String objectValue = bucket.get();
System.out.println("stored object value: " + objectValue);
String mapValue = map.get("mapKey");
System.out.println("stored map value: " + mapValue);
redisson.shutdown();