Spring Boot and Kotlin use the Redis database configuration method, kotlinredis

Source: Internet
Author: User

Spring Boot and Kotlin use the Redis database configuration method, kotlinredis

In addition to providing excellent automation support for common relational databases, Spring Boot also provides automated configuration support for many NoSQL databases, including Redis, MongoDB, Elasticsearch, solr and Cassandra.

Use Redis

Redis is an open-source log-type and Key-Value database that is written in ansi c and supports Network, memory-based, and persistent.

  • Official Redis website
  • Redis Chinese community

Introduce dependency

Spring Data Redis, a Data access framework provided by Spring Boot, is based on Jedis. You can configure the dependency by introducing spring-boot-starter-data-redis.

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

Note: after spring boot 1.4 is changed to spring-boot-starter-data-redis 1.4, use spring-boot-starter-redis before

To use kotlin, you need to add a plug-in.

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

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 integrates SpringBoot's default no-argument constructor. By default, 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-pluginapply plugin: 'org. springframework. boot 'apply plugin: "kotlin-jpa" // https://stackoverflow.com/questions/32038177/kotlin-with-jpa-default-constructor-helljar {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

Add related configurations of the Redis server to application. yml according to the Convention. The details are as follows:

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

The configuration of spring. redis. database is usually set to 0. When configuring Redis, you can set the number of databases. The default value is 16, which can be understood as the schema of the database.

Use the above configuration for testing.

Spring: redis: database: 2 # Redis database index (0 by default) host: 192.168.1.29 port: 6379 # Redis server connection port password: 123456 # Redis server connection password (null by default) pool: max-active: 8 # maximum number of connections in the connection pool (negative value indicates no limit) max-wait:-1 # maximum blocking wait time in the connection pool (negative value indicates no limit) max-idle: 8 # maximum idle connections in the connection pool min-idle: 0 # minimum idle connections in the connection pool timeout: 0 # connection timeout (MS)

Create a User object class

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

Test Access

Write test cases to illustrate how to access Redis.

Import name. quanke. kotlin. chaper11_6_3.entity.Userimport org. apache. commons. logging. logFactoryimport org. junit. testimport org. junit. runner. runWithimport org. springframework. boot. test. context. springBootTestimport org. springframework. data. redis. core. redisTemplateimport org. springframework. data. redis. core. stringRedisTemplateimport org. springframework. test. context. junit4.SpringRunnerimport javax. annota Tion. resource/*** Created by http://quanke.name on 2018/1/9. * // @ RunWith (SpringRunner: class) @ SpringBootTestclass 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 the stringRedisTemplate String. opsForValue (). set ("url", "http://quanke.name") log.info ("General blog address: $ {stringRedisTemplate. opsForValue (). get ("url")} ")} @ Test fun 'redis object test" '() {// save object val user = User ("Superman", 20) redisTemplate. opsForValue (). set (user. username, user) log.info ("Superman's age: $ {redisTemplate. opsForValue (). get ("Superman "). age }")}}

Summary

The above section describes how to use Spring Boot and Kotlin to configure Redis databases. I hope it will help you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.