Skip to content

Integrate with Redis

Wuyi Chen edited this page May 10, 2019 · 12 revisions

Overview

For integrating microservices with Redis, you need to do several things:

Install Redis

Redis is an in-memory data structure project implementing a distributed, in-memory key-value database with optional durability.

Set dependencies

build.gradle

dependencies {
    compile group: 'org.springframework.data', name: 'spring-data-redis', version: '1.8.4.RELEASE'
    compile group: 'redis.clients',            name: 'jedis',             version: '2.9.0'
    compile group: 'org.apache.commons',       name: 'commons-pool2',     version: '2.4.3'
}

Set configuration

You need to add the configuration parameters for connecting Redis. Currently, the configuration of services/server has been centralized into the config server. So you need to add 2 more parameters into the configuration YAML file of the target service which needs to integrate with Redis.

licensingservice.yml

# Omit other parameters

redis.server: "127.0.0.1"
redis.port: "6379"

Also, you need to change the corresponding config class for loading those 2 new parameters into the service.

ServiceConfig.java

@Component
public class ServiceConfig{
    // Omit other fields and methods

    @Value("${redis.server}")
    private String redisServer="";

    @Value("${redis.port}")
    private String redisPort="";
	
    public String getRedisServer(){
        return redisServer;
    }

    public Integer getRedisPort(){
        return new Integer(redisPort).intValue();
    }
}
Clone this wiki locally