Using spring Data Redis Operations Redis (a) is comprehensive

Source: Internet
Author: User
Tags constructor redis xmlns

The Spring-data-redis Project (SDR) provides a higher level of abstraction for Redis's Key-value data store operations, similar to the spring framework for JDBC support.

Project home: http://projects.spring.io/spring-data-redis/

Project Document: http://docs.spring.io/spring-data/redis/docs/1.5.0.RELEASE/reference/html/

This article mainly introduces the actual use of spring Data Redis.

New features of 1.Spring Data Redis 1.5

Added the Redis hyperloglog command pfadd,pfcount,pfmerge

You can use Jackson to serialize Java types based on Redisserializer

Configuring a Redis Sentinel connection using Propertysource, currently only Jedis client support

2.Spring Data Redis?

Spring Data Redis makes it easier to read and write Redis databases in spring applications.

Spring Data Redis provides the integration of four Redis service Java client packages, namely Jedis, Jredis, SRP and lettuce

3. Version requirements

Spring Data redis1.2.x requires jdk1.6+,spring framwork3.2.8+

Key-value Storage Services Redis 2.6.x+

4. Build the Environment

This article assumes that the Redis service has been installed and is running successfully.

To create a MAVEN project and add dependent jars, this article mainly uses the Jedis

<dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId> spring-data-redis</artifactid>
      <version>1.5.0.RELEASE</version>
    </dependency>
<dependency>
      <groupId>redis.clients</groupId>
      <artifactid>jedis</artifactid >
      <version>2.6.2</version>
    </dependency>

5. Connect to Redis Service

In spring Data Redis connections are obtained through the redisconnection and Redisconnectionfactory classes in the Org.springframework.data.redis.connection package.

5.1 Configuring Jedisconnectionfactory

<?xml version= "1.0" encoding= "UTF-8"?> <beans
xmlns= "Http://www.springframework.org/schema/beans"
  xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  xmlns:p= "http://www.springframework.org/schema/ P "
  xsi:schemalocation=" Http://www.springframework.org/schema/beans http://www.springframework.org/schema/ Beans/spring-beans.xsd ">

  <bean id=" jedisconnectionfactory "class=" Org.springframework.data.redis.connection.jedis.JedisConnectionFactory "p:host-name=" Server "p:port=" 6379 "/>

</beans>

5.2 Configuration Jredis,srp,lettuce configuration is similar to the above configuration, only need to configure the corresponding Redisconnectionfactory interface implementation interface, it is located in the following package:

5.3 About Redisconnectionfactory attention issues

The above four connectors do not all support all of Redis's features, they are different, if the calling method is not supported in the connection API, then throws "Unsupportedoperationexception" exception, the specific need to understand the corresponding Redis Implementation of the Java client jar.

5.4 About Redis Sentinel (temporarily known as Sentinel) support

The Redis Sentinel Monitoring Master Service, when the primary service fails, is able to switch to the slave service, which will be upgraded from the service to the primary service to ensure failure recovery. Using this feature requires setting the Redissentinelconfiguration property in Jedisconnectionfactory, which is currently supported by Jedis for Redis Sentinel.

The encoding is as follows:

Public Redisconnectionfactory jedisconnectionfactory () {
  redissentinelconfiguration sentinelconfig = new Redissentinelconfiguration (). Master ("MyMaster")
  . Sentinel ("127.0.0.1", 26379). Sentinel ("127.0.0.1", 26380);
  return new Jedisconnectionfactory (sentinelconfig);
}

Configure in the Spring container:

<bean id= "Sentinelconfig" class= "org.springframework.data.redis.connection.RedisSentinelConfiguration" > &L T;constructor-arg name= "Master" value= "MyMaster"/> <constructor-arg name= "Sentinelhostandports" > <s
        Et> <value>192.168.88.153:26379</value> <value>192.168.88.153:26380</value>
  
  <value>192.168.88.153:26382</value> </set> </constructor-arg> </bean> <bean id= "Jedisconnectionfactory" class= " Org.springframework.data.redis.connection.jedis.JedisConnectionFactory "> <constructor-arg ref=" Sentinelconfig "/> </bean> 

Note: Be aware of using externally accessible IP addresses when configuring Redis sentinel.conf files because when the Redis-sentinel service and Redis-server are on the same machine, the primary service IP becomes 127.0 in the configuration file when the primary service changes. 0.1, so that the external will not be able to access the. If the application, the Redis service on the same machine does not exist such a hidden danger, the situation is more practical network environment.

Once configured, after instantiating jedisconnectionfactory, the following log is visible:

2015-4-1 17:29:30 redis.clients.jedis.JedisSentinelPool initsentinels
Info: Trying to find master from available
Sentinels ... 2015-4-1 17:29:30 redis.clients.jedis.JedisSentinelPool initsentinels
info: Redis Master running at 192.168.88.153:6384, starting Sentinel listeners
... 2015-4-1 17:29:30 redis.clients.jedis.JedisSentinelPool initpool
info: Created Jedispool to master at 192.168.88.153:6384

The Redis instance 192.168.88.153:6384 in the experimental environment is the primary service.

5.5 Following a set of code to show the specific use

@FixMethodOrder (methodsorters.name_ascending) public class Testjedis {public static applicationcontext ctx;

  public static jedisconnectionfactory Jedisconnetionfactory;

  Public Jedisconnection jedisconnection; @SuppressWarnings ("unchecked") @BeforeClass public static void Setbeforeclass () {ctx = new Classpathxmlapplicatio
    Ncontext ("Spring-redis.xml");
  Jedisconnetionfactory = (jedisconnectionfactory) ctx. Getbean ("Jedisconnectionfactory");
  } @Before public void Setbefore () {jedisconnection = Jedisconnetionfactory.getconnection ();
  } @After public void Setafter () {jedisconnection.close (); private void print (Collection<redisserver> c) {for (iterator<redisserver> iter = C.iterator (); Iter. Hasnext ();)
      {Redisserver rs = (redisserver) iter.next ();
    System.out.println (Rs.gethost () + ":" + rs.getport ()); }}//Simple test jedisconnection @Ignore @Test public void test1 () {if (!jedisconnection.Exists (new string ("zz"). GetBytes ())) {Jedisconnection.set (New string ("zz"). GetBytes (), new string ("zz"). G
    Etbytes ()); }} @Ignore @Test public void Test2 () {set<byte[]> keys = Jedisconnection.keys (New String ("*"). GetByte
    s ()); for (iterator<byte[]> iter = Keys.iterator (); Iter.hasnext ();)
    {System.out.println (New String (Iter.next ())); }}//Test Sentinel <
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.