欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring Boot 与 Kotlin 使用Redis数据库的配置方法

程序员文章站 2023-12-13 11:44:22
spring boot中除了对常用的关系型数据库提供了优秀的自动化支持之外,对于很多nosql数据库一样提供了自动化配置的支持,包括:redis, mongodb, ela...

spring boot中除了对常用的关系型数据库提供了优秀的自动化支持之外,对于很多nosql数据库一样提供了自动化配置的支持,包括:redis, mongodb, elasticsearch, solr和cassandra。

使用redis

redis是一个开源的使用 ansi c 语言编写、支持网络、可基于内存亦可持久化的日志型、 key-value 数据库。

  • redis官网
  • redis中文社区

引入依赖

spring boot提供的数据访问框架spring data redis基于jedis。可以通过引入 spring-boot-starter-data-redis 来配置依赖关系。

compile "org.springframework.boot:spring-boot-starter-data-redis:$spring_boot_version"

注意:spring boot 1.4 以后改名叫 spring-boot-starter-data-redis 1.4 之前使用 spring-boot-starter-redis

用kotlin,需要增加一个插件

apply plugin: "kotlin-jpa" //https://*.com/questions/32038177/kotlin-with-jpa-default-constructor-hell

完整的 build.gradle 文件

group 'name.quanke.kotlin'
version '1.0-snapshot'
buildscript {
 ext.kotlin_version = '1.2.10'
 ext.spring_boot_version = '1.5.4.release'
 ext.springfox_swagger2_version = '2.7.0'
 ext.mysql_version = '5.1.21'
 repositories {
  mavencentral()
 }
 dependencies {
  classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
  classpath("org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version")
//  kotlin整合springboot的默认无参构造函数,默认把所有的类设置open类插件
  classpath("org.jetbrains.kotlin:kotlin-noarg:$kotlin_version")
  classpath("org.jetbrains.kotlin:kotlin-allopen:$kotlin_version")
 }
}
apply plugin: 'kotlin'
apply plugin: "kotlin-spring" // see https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-plugin
apply plugin: 'org.springframework.boot'
apply plugin: "kotlin-jpa" //https://*.com/questions/32038177/kotlin-with-jpa-default-constructor-hell
jar {
 basename = 'chapter11-6-3-service'
 version = '0.1.0'
}
repositories {
 mavencentral()
}
dependencies {
 compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
 compile("org.jetbrains.kotlin:kotlin-reflect:${kotlin_version}")
 compile "org.springframework.boot:spring-boot-starter-web:$spring_boot_version"
 compile "org.springframework.boot:spring-boot-starter-data-redis:$spring_boot_version"
 testcompile "org.springframework.boot:spring-boot-starter-test:$spring_boot_version"
 testcompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
}
compilekotlin {
 kotlinoptions.jvmtarget = "1.8"
}
compiletestkotlin {
 kotlinoptions.jvmtarget = "1.8"
}

参数配置

按照惯例在 application.yml 中加入redis服务端的相关配置,具体说明如下:

spring:
 redis:
 database: 2
 host: 192.168.1.29
 port: 6379

其中spring.redis.database的配置通常使用0即可,redis在配置的时候可以设置数据库数量,默认为16,可以理解为数据库的schema

测试使用上面的配置就可以了

spring:
 redis:
 database: 2 # redis数据库索引(默认为0)
 host: 192.168.1.29
 port: 6379 # redis服务器连接端口
 password: 123456 # redis服务器连接密码(默认为空)
 pool:
  max-active: 8 # 连接池最大连接数(使用负值表示没有限制)
  max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
  max-idle: 8 # 连接池中的最大空闲连接
  min-idle: 0 # 连接池中的最小空闲连接
 timeout: 0 # 连接超时时间(毫秒)

创建user实体类

import java.io.serializable
data class user(val username: string, val age: int?) : serializable

测试访问

通过编写测试用例,举例说明如何访问redis。

import name.quanke.kotlin.chaper11_6_3.entity.user
import org.apache.commons.logging.logfactory
import org.junit.test
import org.junit.runner.runwith
import org.springframework.boot.test.context.springboottest
import org.springframework.data.redis.core.redistemplate
import org.springframework.data.redis.core.stringredistemplate
import org.springframework.test.context.junit4.springrunner
import javax.annotation.resource
/**
 * created by http://quanke.name on 2018/1/9.
 */
@runwith(springrunner::class)
@springboottest
class applicationtests {
 val log = logfactory.getlog(applicationtests::class.java)!!
 @resource
 lateinit var stringredistemplate: stringredistemplate
 @resource
 lateinit var redistemplate: redistemplate<string, user>
 @test
 fun `redis string test"`() {
  // 保存字符串
  stringredistemplate.opsforvalue().set("url", "http://quanke.name")
  log.info("全科的博客地址: ${stringredistemplate.opsforvalue().get("url")}")
 }
 @test
 fun `redis object test"`() {
  // 保存对象
  val user = user("超人", 20)
  redistemplate.opsforvalue().set(user.username, user)
  log.info("超人的年龄:${redistemplate.opsforvalue().get("超人").age}")
 }
}

总结

以上所述是小编给大家介绍的spring boot 与 kotlin 使用redis数据库的配置方法,希望对大家有所帮助

上一篇:

下一篇: