Redis Installation and use

Source: Internet
Author: User
Tags redis xmlns
installation under CentOS

Reference
-Download Compressed package wget http://download.redis.io/releases/redis-3.2.6.tar.gz Installation

CD redis-3.2.6 make

&& make install
Start
Src/redis-server
redis-server/etc/redis.conf #启动时指定配置文件

CP Redis-benchmark REDIS-CLI redis-server/usr/bin/
This will not be implemented with the addition of the./, and can be anywhere to perform test Redis

[Root@localhost redis-3.2.6]# redis-cli 
127.0.0.1:6379> set name Hi
OK
127.0.0.1:6379> get Name
"HI"
Turn off Redis Services
REDIS-CLI shutdown use in spring maven Dependency
<dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId> spring-data-redis</artifactid>
      <version>1.0.0.RELEASE</version>
    </dependency>
XML Configuration

On the internet to see a circle, are configured with annotations, usually these configuration code like to use XML style, configure

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs I= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans.xsd "> <bean id=" poolconfig "class=" Redis.clients.jedis.JedisPoolConfig "/> <bean id=" jedisconnectionfactory "class=" Org.springframework.data.redis.connection.jedis.JedisConnectionFactory "> <property name=" HostName "Val Ue= "127.0.0.1"/> <property name= "port" value= "6379"/> <property name= "password" value= "xxx"/ > <property name= "poolconfig" ref= "Poolconfig"/> </bean> <bean id= "Stringredisseriali Zer "class=" Org.springframework.data.redis.serializer.StringRedisSerializer "/> <bean id=" Redistemplate " class= "Org.springframework.data.redis.core.RedisTemplate" > <property name= "ConnectionFactory" ref= "jedisconnectionfactory"/> <property name= "Keyserializer" ref= "stringRedisSerial Izer "/> </bean> </beans>

How to work with Redis

Package Com.xxx.ws.redis;
Import Com.xxx.ws.entity.Hi;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.data.redis.core.RedisTemplate;

Import org.springframework.stereotype.Component;
Import Java.util.HashMap;
Import Java.util.Map;

Import Java.util.concurrent.TimeUnit;
 /** * Created on 2017/1/25.

    */@Component public class Redishandler {@Autowired private redistemplate<string,object> redistemplate;
    Public Object GetValue (final String key) {return Redistemplate.opsforvalue (). get (key); 
        The public void SetValue (final String key,final string value) {Redistemplate.opsforvalue (). Set (Key,value);
    Redistemplate.expire (key,1, timeunit.seconds);
        public void SetHi (final Hi user) {final String key = String.Format ("hi:%s", User.getid ());

        Final map< string, object > Properties = New hashmap< String, Object > (); Properties.put ("id", User.getId ());
        Properties.put ("Name", User.getname ());
        Properties.put ("msg", user.getmsg ());

        Properties.put ("num", User.getnum ());
    Redistemplate.opsforhash (). Putall (Key, properties);

        Public Hi Gethi (final int id) {Final String key = String.Format ("hi:%s", id);
        Final String name = (string) redistemplate.opsforhash (). Get (Key, "name");
        Final Integer num = (integer) redistemplate.opsforhash (). Get (Key, "num");


        Final String msg = (string) redistemplate.opsforhash (). Get (Key, "MSG");
    return new Hi (ID, name, num,msg); }

}

You can then use the protected mode

about redis.conf

Redis.clients.jedis.exceptions.JedisDataException:DENIED Redis is running in protected mode because protected mode is ENA Bled, no bind address was Specified,no Authentication password are requested to clients.
 The This mode connections is only accepted from the loopback interface. If you want-to-connect from external computers to Redis-adopt one of the following solutions:1) Just disable PR otected mode sending the command ' CONFIG SET protected-mode No ' from the loopback interface by connecting to Redis from th E same host The server is running, however make sure Redis are not publicly accessible from the Internet if your do so.
 Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the PROTECTE 
 d mode option to ' No ', and then restarting the server. 
 3) If You started the server manually just for testing, restart it with the '--protected-mode no ' option. 4) Setup a bind AddreSS or an authentication password. Note:you only need to does one of the above things in order for the server to start accepting connections from the outside .

If this is the case, the Redis default has a protected mode yes. Other machines in this mode are not connected, only the bind IP can be connected. Set it to no, then comment out the bind machine and set the Requiredpass, And then you can connect the

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.