In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you how to use Java to operate redis, the content is very detailed, interested friends can refer to, hope to be helpful to you.
We've done enough about Redis. Let's take a look at how to use Java to manipulate redis.
Looking at the official website of redis, we find that we have a variety of solutions to operate redis with Java, as shown below:
There are a variety of solutions here, we use Jedis, other frameworks are more or less the same, I have the right to throw a brick to attract jade, partners can also study other solutions, welcome to contribute.
Configuration
To be able to successfully connect to the redis server, the client needs to check the following three configurations:
1. The remote Linux firewall has been turned off. Take my CentOS7 here as an example, turn off the firewall command
Systemctl stop firewalld.service
At the same time, one more knife can be made up.
Systemctl disable firewalld.service
Indicates that the firewall is not allowed to boot.
two。 Turn off redis protection mode, and in the redis.conf file, change protected to no, as follows:
Protected-mode no
3. Comment out the ip address binding of redis or comment out the bind:127.0.0.1 in redis.conf, as follows:
# bind:127.0.0.1
After confirming these three steps, you can connect to redis remotely.
Java side configuration
After the above configuration is completed, we can create a normal JavaSE project to test it. After the Java project is successfully created, add Jedis dependencies as follows:
Redis.clients jedis 2.9.0
Then we can test whether the connection is successful with the following simple program:
Public static void main (String [] args) {Jedis jedis = new Jedis ("192.168.248.128", 6379); String ping = jedis.ping (); System.out.println (ping);}
After running, the following result indicates that the connection is successful:
After the connection is successful, the rest is relatively simple. The method name in the Jedis class is basically the same as the command in redis. When you see the method name partner, you know what it is, so I'm not going to repeat it here.
Frequent creation and destruction of connections can affect performance. We can use connection pooling to partially solve this problem:
Public static void main (String [] args) {GenericObjectPoolConfig config = new GenericObjectPoolConfig (); config.setMaxTotal (100); config.setMaxIdle (20); JedisPool jedisPool = new JedisPool (config, "192.168.248.128", 6379); Jedis jedis = jedisPool.getResource (); System.out.println (jedis.ping ());}
In this way, connections will not be created and destroyed frequently, connection pooling can be configured as a singleton mode in JavaSE environment, and if Spring containers are used, connection pooling can be handed over to Spring container management.
The above connections are all Redis connected to a single node. If it is a Redis cluster, how to connect? It is very simple, as follows:
Set clusterNodes = new HashSet (); clusterNodes.add (new HostAndPort ("192.168.248.128", 7001); clusterNodes.add (new HostAndPort ("192.168.248.128", 7002)); clusterNodes.add (new HostAndPort ("192.168.248.128", 7003)); clusterNodes.add (new HostAndPort ("192.168.248.128", 7004)); clusterNodes.add ("192.168.248.128", 7005)) ClusterNodes.add (new HostAndPort ("192.168.248.128", 7006)); JedisCluster jc = new JedisCluster (clusterNodes); jc.set ("address", "Shenzhen"); String address = jc.get ("address"); System.out.println (address)
The methods in JedisCluster are basically the same as the Redis command, so I'm not going to repeat them.
On how to use Java to operate redis to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.