Spring Boot and Kotlin use the Redis database configuration method

  • 2021-01-06 00:34:54
  • OfStack

In addition to providing excellent automation support for common relational databases, Boot provides automated configuration support for a number of NoSQL databases, including Redis, MongoDB, Elasticsearch, Solr, and Cassandra.

Using Redis

Redis is an open source, networked, memory-persisted journaling database written in ANSI, C, Key-Value, ANSI and C.

Redis website Redis Chinese community

Introduction of depend on

Spring Boot provides the data access framework Spring Data Redis is based on Jedis. Dependencies can be configured by introducing ES32en-ES33en-ES34en-ES35en-ES36en.


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

Note: spring 1.4 was renamed spring-boot-starter-data-redis 1.4 before spring-boot-starter-redis

To use ES54en, you need to add a plug-in


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

The complete build.gradle file


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 integration SpringBoot The default no-argument constructor, which sets all classes by default open Class plug-in 
  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://stackoverflow.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"
}

Parameter configuration

Redis server configuration is added in application.yml according to the convention, the details are as follows:


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

spring.redis.database configuration is usually 0, Redis configuration can set the number of databases, the default is 16, can be understood as the database schema

Use the configuration above for the test


spring:
 redis:
 database: 2 # Redis Database index (default is 0 ) 
 host: 192.168.1.29
 port: 6379 # Redis Server connection port 
 password: 123456 # Redis Server connection password (empty by default) 
 pool:
  max-active: 8 #  Maximum number of connection pool connections (use negative values to indicate no limit) 
  max-wait: -1 #  Connection pool maximum blocking wait time (use negative values to indicate no limit) 
  max-idle: 8 #  Maximum free connection in the connection pool 
  min-idle: 0 #  The minimum free connection in the connection pool 
 timeout: 0 #  Connection timeout in milliseconds 

Create the User entity class


import java.io.Serializable
data class User(val username: String, val age: Int?) : Serializable

Test access

Write test cases to illustrate how to access ES88en.


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"`() {
  //  Save string 
  stringRedisTemplate.opsForValue().set("url", "http://quanke.name")
  log.info(" Quan Ke's blog address : ${stringRedisTemplate.opsForValue().get("url")}")
 }
 @Test
 fun `redis object test"`() {
  //  Save the object 
  val user = User(" superman ", 20)
  redisTemplate.opsForValue().set(user.username, user)
  log.info(" Superman's age: ${redisTemplate.opsForValue().get(" superman ").age}")
 }
}

conclusion


Related articles: