In this article , I will demo how to connect to multiple redis servers using spring boot. Just as following picture shows, if you don’t know how to setup two redis servers in your environment, you can refer to this post.
Environment
Spring boot 2+
Jdk 8+
Redis
Prepare the test data in redis
I deployed two redis docker container bw-redis1 and bw-redis2, now I use command line to set some values in them:
The Project Layout
The layout of this demo project is as follows, all the example code can be found here:
The Pom
We use maven to define the dependencies, I excluded the default lettuce sdk, because it’s not stable. I’d rather use the jedis sdk instead.
The code
1. Config file
Here in the src/main/resources/application.properties, I defined two redis server configurations,
Here we define two redis connection, one is localhost:6379, the other is localhost:26379. You should change them to your real configurations.
2. Property config classes
To read the above properties file into spring boot, we must define configuration classes:
At first, we define a common class as the base class of redis configuration classes
Then, we define Redis1Property and Redis2Property class to denote the different redis configurations
And this:
Pay attention to the ConfigurationProperties annotation, we use it to load different properties from the application.properties file.
Thirdly, we should define how to get the RedisTemplate and RedisConnectionFactory from above properties
For redis server1, we define Redis1Configuration:
To avoid the following error:
We need to change Redis1Configuration to this:
Because springboot2 needs a default bean named redisTemplate, so we reuse the redis1RedisTemplate bean, add a name redisTemplate to it.
For redis server2, we define Redis2Configuration:
Now we have setup the properties and the configurations , we have beans of RedisTemplate , we can test it as follows:
Run it, we get this result:
It works!
Summary
In this post, I demonstrated how to connect to multiple redis server instances with spring boot 2, the key point is that you should setup specific configuration for each redis server connection. Thanks for your reading. All code of the project is uploaded to github.com, you can download at this link.
Final Words + More Resources
My intention with this article was to help others who might be considering using multiple redis in one spring boot application. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: [email protected].
Here are also the most important links from this article along with some further resources that will help you in this scope:
This post will show you how to deploy and setup multiple redis servers in one machine. Just as the following picture shows, I will install two redis servers on my server.
Environment
Linux or MacOS
Solution
Step 1: Install docker
You can follow this guide to install docker on your server.
Step 2: Start redis server1
Now let’s start redis server 1
Let’s check the command’s options:
Option 1: use -p to map docker port to host port
So for redis1 server, I am listening port 6379 on my host and it is mapped to docker port 6379.
Option 2: use --restart to make it always running
Using the --restart flag on Docker run you can specify a restart policy for how a container should or should not be restarted on exit.
always
Always restart the container regardless of the exit status. When you specify always, the Docker daemon will try to restart the container indefinitely. The container will also always start on daemon startup, regardless of the current state of the container.
Option 3: use --detach or -d to run in background
When starting a Docker container, you must first decide if you want to run the container in the background in a “detached” mode or in the default foreground mode:
To start a container in detached mode, you use -d=true or just -d option. By design, containers started in detached mode exit when the root process used to run the container exits,
Now let’s check the running redis:
We can see that the redis1 server in docker is running, it’s listenning on port 6379.
Step 3: start redis server 2
Now let’s deploy redis server 2 named my-redis2, I will change the listenning port to 26379, but the docker port is still 6379.
You can see that we only changed the hostport to 26379 and the name of the docker instance to my-redis2.
Let’s verify it:
It works!
Final Words + More Resources
My intention with this article was to help others who might be considering deploying their own redis. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: [email protected].
Here are also the most important links from this article along with some further resources that will help you in this scope: