Java程序访问Redis

  • 1、关闭RedisServer端的防火墙

    1
    2
    3
    systemctl stop firewalld

    systemctl disable firewalld.service
  • 2、新建maven项目后导入Jedis包到pom.xml

    1
    2
    3
    4
    5
    <dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
    </dependency>
  • 3、写程序

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    @Test
    public void testConn(){
    //与Redis建立连接 IP+port
    Jedis redis = new Jedis("192.168.127.128", 6379);
    //在Redis中写字符串 key value
    redis.set("jedis:name:1","jd-zhangfei");
    //获得Redis中字符串的值
    System.out.println(redis.get("jedis:name:1"));
    //在Redis中写list
    redis.lpush("jedis:list:1","1","2","3","4","5");
    //获得list的长度
    System.out.println(redis.llen("jedis:list:1"));
    }

Spring访问Redis

    1. 新建Spring的maven项目


    1. 添加Spring依赖到pom.xml
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    <dependencies>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>5.2.5.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.2.5.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.5.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.2.5.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
    </dependency>
    </dependencies>
    1. 添加redis依赖到pom.xml
    1
    2
    3
    4
    5
    <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.0.3.RELEASE</version>
    </dependency>
    1. 在resource目录下添加redis.xml
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="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="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigur er">
    <property name="locations">
    <list>
    <value>classpath:redis.properties</value>
    </list>
    </property>
    </bean>
    <!-- redis config -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <property name="maxActive" value="${redis.pool.maxActive}" />
    <property name="maxIdle" value="${redis.pool.maxIdle}" />
    <property name="maxWait" value="${redis.pool.maxWait}" />
    <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
    </bean>
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactor y">
    <property name="hostName" value="${redis.server}"/>
    <property name="port" value="${redis.port}"/>
    <property name="timeout" value="${redis.timeout}" />
    <property name="poolConfig" ref="jedisPoolConfig" />
    </bean>
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
    <property name="connectionFactory" ref="jedisConnectionFactory"/>
    <property name="KeySerializer">
    <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"> </bean>
    </property>
    <property name="ValueSerializer">
    <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"> </bean>
    </property>
    </bean>
    </beans>
    1. 在resource目录下添加properties文件
    1
    2
    3
    4
    5
    6
    7
    8
    redis.pool.maxActive=100
    redis.pool.maxIdle=50
    redis.pool.maxWait=1000
    redis.pool.testOnBorrow=true

    redis.timeout=50000
    redis.server=192.168.72.128
    redis.port=6379
    1. 编写测试用例
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    import org.junit.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
    import java.io.Serializable;

    @ContextConfiguration({ "classpath:redis.xml" })
    public class RedisTest extends AbstractJUnit4SpringContextTests {
    @Autowired
    private RedisTemplate<Serializable, Serializable> rt;

    @Test
    public void testConn() {
    rt.opsForValue().set("name","zhangfei");
    System.out.println(rt.opsForValue().get("name"));
    }
    }

SpringBoot访问Redis

    1. 新建springboot项目


    1. 添加redis依赖包到pom.xml
    1
    2
    3
    4
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    1. 在resource目录下添加rapplication.yml配置文件
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    spring:
    redis:
    host: 192.168.72.128
    port: 6379
    jedis:
    pool:
    min-idle: 0
    max-idle: 8
    max-active: 80
    max-wait: 30000
    timeout: 3000
    1. 在java目录下添加配置类RedisConfig
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    package com.lagou.sbr.cache;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.StringRedisSerializer;

    @Configuration
    public class RedisConfig {
    @Autowired
    private RedisConnectionFactory factory;

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setHashKeySerializer(new StringRedisSerializer());
    redisTemplate.setHashValueSerializer(new StringRedisSerializer());
    redisTemplate.setValueSerializer(new StringRedisSerializer());
    redisTemplate.setConnectionFactory(factory);
    return redisTemplate;
    }
    }
    1. 添加RedisController
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    package com.lagou.sbr.controller;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    import java.util.concurrent.TimeUnit;

    @RestController
    @RequestMapping(value = "/redis")
    public class RedisController {
    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("/put")
    public String put(@RequestParam(required = true) String key,
    @RequestParam(required = true) String value) {
    //设置过期时间为20秒
    redisTemplate.opsForValue().set(key,value,20, TimeUnit.SECONDS);
    return "Success";
    }

    @GetMapping("/get")
    public String get(@RequestParam(required = true) String key){
    return (String) redisTemplate.opsForValue().get(key);
    }
    }
    1. 修改Application并运行
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    package com.lagou.sbr;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cache.annotation.EnableCaching;

    @SpringBootApplication
    @EnableCaching
    public class SpringbootRedisApplication {
    public static void main(String[] args) {
    SpringApplication.run(SpringbootRedisApplication.class, args);
    }
    }