Spring Boot 2.0 连接Redis (gradle)

目录

前言

START

1. 新建项目

2. 配置 build.gradle

3. 保存后,右下角 import changes 一下

4. 这里的项目目录结构

5. 编写Application.java

6. 编写RedisConfig.java

7. 编写RedisUtil.java

8. 编写C.java

9. 配置文件application.properties

10. 启动Redis服务

11. 编译

12. 运行生成的jar

13. 测试

另外


 

 

前言

 

先上源码: https://github.com/YKRY35/SpringBoot_C_Redis

编译运行下先:

gradlew build
_start.bat

测试在:13. 测试

人生苦短,运行不起来就赶紧去找下一家。

踩了一天的坑,刚爬出来。

 

START

 

1. 新建项目

 

2. 配置 build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.5.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

bootJar {
    baseName = 'redis' // 生成jar名称
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8  //java 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-data-redis")
}

 

3. 保存后,右下角 import changes 一下

等待依赖包下载

 

4. 这里的项目目录结构

 

5. 编写Application.java

@ComponentScan 里添加一下自己需要扫描的包

package app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan({"controller","util"})
public class Application {
    public static void main(String[] args){
        SpringApplication.run(Application.class, args);
    }
}

 

6. 编写RedisConfig.java

目的是获得RedisTemplate。

class注释Annotation标注为@Configuration。

创建返回RedisTemplate的方法,标注为@Bean。

package util;

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;

@Configuration
public class RedisConfig {
    @Bean
    RedisTemplate template(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}

 

7. 编写RedisUtil.java

保存RedisTemplate,包装一些基本的操作。

片段:

@Component
public class RedisUtil {
        @Bean
        RedisUtil util(RedisTemplate template){
            return new RedisUtil(template);
        }

        private RedisTemplate template;
        public RedisUtil(RedisTemplate template){
            this.template = template;
        }
}

那么 只要声明@Autowired RedisUtil redisUtil,系统就自己找到带Bean注释的返回RedisUtil的方法,它的唯一参数template的获得方法在6.RedisConfig中。会调用RedisConfig中的template方法,参数RedisConnectionFactory factory由系统自己传入,然后再一路往回,创建出RedisUtil,其中template是单例的,不同的Redisutil的template在内存中是指向同一个东西。

RedisUtil构造函数把接收到的template给保存。

读写redis数据库这里只有简单的两种。

package util;

import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class RedisUtil {
        @Bean
        RedisUtil util(RedisTemplate template){
            return new RedisUtil(template);
        }

        private RedisTemplate template;
        public RedisUtil(RedisTemplate template){
            this.template = template;
        }


        //value

        public boolean setValue(Object key, Object value){
            try{
                template.opsForValue().set(key, value);
                return true;
            }catch(Exception e){
                e.printStackTrace();
                return false;
            }
        }

        public Object getValue(Object key){
            return template.opsForValue().get(key);
        }

}

 

8. 编写C.java

测试类。接收到http请求时写或者读sql数据库。

package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import util.RedisUtil;

@RestController
public class C {
    @Autowired
    RedisUtil redisUtil;

    @RequestMapping("/set")
    public void set(@RequestParam(value="key", defaultValue = "k") String key,
                    @RequestParam(value = "value", defaultValue = "v") String value){
        redisUtil.setValue(key, value);
    }

    @RequestMapping("/get")
    public Object get(@RequestParam(value="key", defaultValue = "k") String key){
        return redisUtil.getValue(key);
    }
}

 

9. 配置文件application.properties

这里Redis端口用的是25001

spring.redis.database=0  

# Redis服务器地址
spring.redis.host=127.0.0.1

# Redis服务器连接端口
spring.redis.port=25001  

# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8  
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0 

 

10. 启动Redis服务

测试一下启动了没:

恩,启动了。


11. 编译

gradlew build

编译成功:

生成了jar。

 

12. 运行生成的jar

java -jar build/libs/redis-0.1.0.jar --server.port=15001

这里web服务端口用的是15001。

 

13. 测试

先清空redis数据库。

flushall

 

浏览器打开:

http://localhost:15001/set?key=abc&value=def

浏览器内容空白,没报错。

应该调用了set方法。

瞅一眼redis数据库。

keys *

写进去了。

 

测试一下get。

浏览器打开:

http://localhost:15001/get?key=abc

显示:

啊,行了。

 

 

另外

RedisUtil根据需要,去包装。

项目地址: https://github.com/YKRY35/SpringBoot_C_Redis

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring Boot 提供了与 Redis 集群进行适配的方法。要适配 Redis 集群,我们需要使用 Spring BootRedis Starter 包以及 Redisson 或 Jedis Cluster。 使用 Redisson 可以很容易地适配 Redis 集群。我们只需在 Spring Boot 的 Maven 或 Gradle 构建文件中添加 Redisson 依赖项。然后,在应用程序的配置文件中,我们可以配置 RedissonClient Bean,以连接Redis 集群。在配置文件中,我们需要指定 Redis 节点的 IP 地址和端口以及 Redisson 的其他配置选项,例如最小空闲连接数和最大连接数。一旦配置完成,Spring Boot 将会自动创建 RedissonClient Bean,并让我们可以在应用程序中使用它。 如果使用 Jedis Cluster,我们可以通过将 Jedis Cluster 配置为 RedisTemplate连接工厂来适配 Redis 集群。在应用程序的配置文件中,我们需要配置 Jedis Cluster 的节点,包括节点的 IP 地址和端口。与 Redisson 类似,我们还可以指定 Jedis Cluster 的其他配置选项,例如最大连接数和连接超时时间。配置完成后,将创建 JedisConnectionFactory Bean,并将其用作 RedisTemplate连接工厂。这样,我们就可以在应用程序中使用 RedisTemplate 来访问 Redis 集群。 无论是使用 Redisson 还是 Jedis Cluster,Spring Boot 都提供了更简单的方式进行 Redis 集群适配。它们的集成已经与 Spring Boot 自动配置密切结合,我们只需按照相关文档进行配置,就可以轻松地在 Spring Boot 应用程序中使用 Redis 集群。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值