使用 Kotlin + Spring Boot 进行后端开发

小舟.jpg

Kotlin

Kotlin 是一个基于 JVM 的编程语言,它的简洁、便利早已不言而喻。Kotlin 能够胜任 Java 做的所有事。目前,我们公司 C 端 的 Android 产品全部采用 Kotlin 编写。公司的后端项目也可能会使用 Kotlin,所以我给他们做一些 demo 进行演示。

示例一:结合 Redis 进行数据存储和查询

1.1 配置 gradle

在build.gradle中添加插件和依赖的库。

plugins {
    id 'java'
    id 'org.jetbrains.kotlin.jvm' version '1.3.0'
}

ext {
    libraries = [

            rxjava                    : "2.2.2",

            logback                   : "1.2.3",

            spring_boot               : "2.1.0.RELEASE",

            commons_pool2             : "2.6.0",

            fastjson                  : "1.2.51"
    ]
}

group 'com.kotlin.tutorial'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

def libs = rootProject.ext.libraries // 库

repositories {
    mavenCentral()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    compile "org.jetbrains.kotlin:kotlin-reflect:1.3.0"
    testCompile group: 'junit', name: 'junit', version: '4.12'

    implementation "io.reactivex.rxjava2:rxjava:${libs.rxjava}"

    implementation "ch.qos.logback:logback-classic:${libs.logback}"
    implementation "ch.qos.logback:logback-core:${libs.logback}"
    implementation "ch.qos.logback:logback-access:${libs.logback}"

    implementation "org.springframework.boot:spring-boot-starter-web:${libs.spring_boot}"
    implementation "org.springframework.boot:spring-boot-starter-data-redis:${libs.spring_boot}"
    implementation "org.apache.commons:commons-pool2:${libs.commons_pool2}"
    implementation "com.alibaba:fastjson:${libs.fastjson}"
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

1.2 创建 SpringKotlinApplication:

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication


/**
 * Created by tony on 2018/11/13.
 */
@SpringBootApplication
open class SpringKotlinApplication

fun main(args: Array<String>) {
    SpringApplication.run(SpringKotlinApplication::class.java, *args)
}

需要注意open的使用,如果不加open会报如下的错误:

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: @Configuration class 'SpringKotlinApplication' may not be final. Remove the final modifier to continue.

因为 Kotlin 的类默认是final的,所以这里需要使用open关键字。

1.3 配置 redis

在 application.yml 中添加 redis 的配置

spring:
  redis:
    #数据库索引
    database: 0
    host: 127.0.0.1
    port: 6379
    password:
    lettuce:
      pool:
        #最大连接数
        max-active: 8
        #最大阻塞等待时间(负数表示没限制)
        max-wait: -1
        #最大空闲
        max-idle: 8
        #最小空闲
        min-idle: 0
    #连接超时时间
    timeout: 10000

接下来定义 redis 的序列化器,本文采用fastjson,当然使用gson、jackson等都可以,看个人喜好。

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.serializer.SerializerFeature
import org.springframework.data.redis.serializer.RedisSerializer
import org.springframework.data.redis.serializer.SerializationException
import java.nio.charset.Charset

/**
 * Created by tony on 2018/11/13.
 */

class FastJsonRedisSerializer<T>(private val clazz: Class<T>) : RedisSerializer<T> {

    @Throws(SerializationException::class)
    override fun serialize(t: T?) = if (null == t) {
            ByteArray(0)
        } else JSON.toJSONString(t, SerializerFeature.WriteClassName).toByteArray(DEFAULT_CHARSET)

    @Throws(SerializationException::class)
    override fun deserialize(bytes: ByteArray?): T? {

        if (null == bytes || bytes.size <= 0) {
            return null
        }
        val str = String(bytes, DEFAULT_CHARSET)
        return JSON.parseObject(str, clazz) as T
    }

    companion object {
        private val DEFAULT_CHARSET = Charset.forName("UTF-8")
    }
}

创建 RedisConfig

import org.springframework.data.redis.core.RedisTemplate
import org.springframework.data.redis.connection.RedisConnectionFactory
import org.springframework.context.annotation.Bean
import org.springframework.data.redis.cache.RedisCacheManager
import org.springframework.cache.CacheManager
import org.springframework.cache.annotation.CachingConfigurerSupport
import org.springframework.cache.annotation.EnableCaching
import org.springframework.context.annotation.Configuration
import org.springframework.data.redis.serializer.StringRedisSerializer
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.data.redis.core.RedisOperations
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
import org.springframework.boot.autoconfigure.data.redis.RedisProperties


/**
 * Created by tony on 2018/11/13.
 */

@EnableCaching
@Configuration
@ConditionalOnClass(RedisOperations::class)
@EnableConfigurationProperties(RedisProperties::class)
open class RedisConfig : CachingConfigurerSupport() {

    @Bean(name = arrayOf("redisTemplate"))
    @ConditionalOnMissingBean(name = arrayOf("redisTemplate"))
    open fun redisTemplate(redisConnectionFactory: RedisConnectionFactory): RedisTemplate<Any, Any> {

        val template = RedisTemplate<Any, Any>()

        val fastJsonRedisSerializer = FastJsonRedisSerializer(Any::class.java)

        template.valueSerializer = fastJsonRedisSerializer
        template.hashValueSerializer = fastJsonRedisSerializer

        template.keySerializer = StringRedisSerializer()
        template.hashKeySerializer = StringRedisSerializer()

        template.connectionFactory = redisConnectionFactory
        return template
    }

    //缓存管理器
    @Bean
    open fun cacheManager(redisConnectionFactory: RedisConnectionFactory): CacheManager {
        val builder = RedisCacheManager
                .RedisCacheManagerBuilder
                .fromConnectionFactory(redisConnectionFactory)
        return builder.build()
    }

}

这里也都需要使用open,理由同上。

1.4 创建 Service

创建一个 User 对象,使用 datat class 类型。

data class User(var userName:String,var password:String):Serializable

创建操作 User 的Service接口

import com.kotlin.tutorial.user.User

/**
 * Created by tony on 2018/11/13.
 */
interface IUserService {

    fun getUser(username: String): User

    fun createUser(username: String,password: String)
}

创建 Service 的实现类:

import com.kotlin.tutorial.user.User
import com.kotlin.tutorial.user.service.IUserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.redis.core.RedisTemplate
import org.springframework.stereotype.Service


/**
 * Created by tony on 2018/11/13.
 */
@Service
class UserServiceImpl : IUserService {

    @Autowired
    lateinit var redisTemplate: RedisTemplate<Any, Any>

    override fun getUser(username: String): User {

        var user = redisTemplate.opsForValue().get("user_${username}")

        if (user == null) {

            user = User("default","000000")
         }

        return user as User
    }

    override fun createUser(username: String, password: String) {

        redisTemplate.opsForValue().set("user_${username}", User(username, password))
    }

}

1.5 创建 Controller

创建一个 UserController,包含 createUser、getUser 两个接口。

import com.kotlin.tutorial.user.User
import com.kotlin.tutorial.user.service.IUserService
import com.kotlin.tutorial.web.dto.HttpResponse

import org.springframework.beans.factory.annotation.Autowired
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


/**
 * Created by tony on 2018/11/13.
 */
@RestController
@RequestMapping("/user")
class UserController {

    @Autowired
    lateinit var userService: IUserService

    @GetMapping("/getUser")
    fun getUser(@RequestParam("name") userName: String): HttpResponse<User> {

        return HttpResponse(userService.getUser(userName))
    }

    @GetMapping("/createUser")
    fun createUser(@RequestParam("name") userName: String,@RequestParam("password") password: String): HttpResponse<String> {

        userService.createUser(userName,password)

        return HttpResponse("create ${userName} success")
    }
}

创建完 Controller 之后,可以进行测试了。

创建用户tony:

创建用户tony.png

查询用户tony:

查询用户tony.jpeg

创建用户monica:

创建用户monica.jpeg

查询用户monica:

查询用户monica.jpeg

示例二:结合 RxJava 模拟顺序、并发地执行任务

2.1 创建 MockTask

首先定义一个任务接口,所有的任务都需要实现该接口:

/**
 * Created by tony on 2018/11/13.
 */
interface ITask {

    fun execute()
}

再创建一个模拟的任务,其中delayInSeconds用来模拟任务所花费的时间,单位是秒。

import java.util.concurrent.TimeUnit
import com.kotlin.tutorial.task.ITask

/**
 * Created by tony on 2018/11/13.
 */
class MockTask(private val delayInSeconds: Int) : ITask {

    /**
     * Stores information if task was started.
     */
    var started: Boolean = false

    /**
     * Stores information if task was successfully finished.
     */
    var finishedSuccessfully: Boolean = false

    /**
     * Stores information if the task was interrupted.
     * It can happen if the thread that is running this task was killed.
     */
    var interrupted: Boolean = false

    /**
     * Stores the thread identifier in which the task was executed.
     */
    var threadId: Long = 0

    override fun execute() {
        try {
            this.threadId = Thread.currentThread().id
            this.started = true
            TimeUnit.SECONDS.sleep(delayInSeconds.toLong())
            this.finishedSuccessfully = true
        } catch (e: InterruptedException) {
            this.interrupted = true
        }

    }
}

2.2 创建 ConcurrentTasksExecutor

顺序执行的话比较简单,一个任务接着一个任务地完成即可,是单线程的操作。

对于并发而言,在这里借助 RxJava 的 merge 操作符来将多个任务进行合并。还用到了 RxJava 的任务调度器 Scheduler,createScheduler()是按照所需的线程数来创建Scheduler的。

import com.kotlin.tutorial.task.ITask
import io.reactivex.Completable
import io.reactivex.schedulers.Schedulers
import org.slf4j.LoggerFactory
import org.springframework.util.CollectionUtils
import java.util.*
import java.util.concurrent.Executors
import java.util.stream.Collectors


/**
 * Created by tony on 2018/11/13.
 */
class ConcurrentTasksExecutor(private val numberOfConcurrentThreads: Int, private val tasks: Collection<ITask>?) : ITask {

    val log = LoggerFactory.getLogger(this.javaClass)

    constructor(numberOfConcurrentThreads: Int, vararg tasks: ITask) : this(numberOfConcurrentThreads, if (tasks == null) null else Arrays.asList<ITask>(*tasks)) {}

    init {

        if (numberOfConcurrentThreads < 0) {
            throw RuntimeException("Amount of threads must be higher than zero.")
        }
    }

    /**
     * Converts collection of tasks (except null tasks) to collection of completable actions.
     * Each action will be executed in thread according to the scheduler created with [.createScheduler] method.
     *
     * @return list of completable actions
     */
    private val asConcurrentTasks: List<Completable>
        get() {

            if (tasks!=null) {

                val scheduler = createScheduler()

                return tasks.stream()
                        .filter { task -> task != null }
                        .map { task ->
                            Completable
                                    .fromAction {
                                        task.execute()
                                    }
                                    .subscribeOn(scheduler)
                        }
                        .collect(Collectors.toList())
            } else {

                return ArrayList<Completable>()
            }
        }

    /**
     * Checks whether tasks collection is empty.
     *
     * @return true if tasks collection is null or empty, false otherwise
     */
    private val isTasksCollectionEmpty: Boolean
        get() = CollectionUtils.isEmpty(tasks)


    /**
     * Executes all tasks concurrent way only if collection of tasks is not empty.
     * Method completes when all of the tasks complete (or one of them fails).
     * If one of the tasks failed the the exception will be rethrown so that it can be handled by mechanism that calls this method.
     */
    override fun execute() {

        if (isTasksCollectionEmpty) {
            log.warn("There are no tasks to be executed.")
            return
        }

        log.debug("Executing #{} tasks concurrent way.", tasks?.size)
        Completable.merge(asConcurrentTasks).blockingAwait()
    }

    /**
     * Creates a scheduler that will be used for executing tasks concurrent way.
     * Scheduler will use number of threads defined in [.numberOfConcurrentThreads]
     *
     * @return scheduler
     */
    private fun createScheduler() = Schedulers.from(Executors.newFixedThreadPool(numberOfConcurrentThreads))
}

2.3 创建 Controller

创建一个 TasksController,包含 sequential、concurrent 两个接口,会分别把sequential 和 concurrent 执行任务的时间展示出来。

import com.kotlin.tutorial.task.impl.ConcurrentTasksExecutor
import com.kotlin.tutorial.task.impl.MockTask
import com.kotlin.tutorial.web.dto.TaskResponse
import com.kotlin.tutorial.web.dto.ErrorResponse
import com.kotlin.tutorial.web.dto.HttpResponse
import org.springframework.http.HttpStatus
import org.springframework.util.StopWatch
import org.springframework.web.bind.annotation.*
import java.util.stream.Collectors
import java.util.stream.IntStream

/**
 * Created by tony on 2018/11/13.
 */
@RestController
@RequestMapping("/tasks")
class TasksController {

    @GetMapping("/sequential")
    fun sequential(@RequestParam("task") taskDelaysInSeconds: IntArray): HttpResponse<TaskResponse> {

        val watch = StopWatch()
        watch.start()

        IntStream.of(*taskDelaysInSeconds)
                .mapToObj{
                    MockTask(it)
                }
                .forEach{
                    it.execute()
                }

        watch.stop()
        return HttpResponse(TaskResponse(watch.totalTimeSeconds))
    }

    @GetMapping("/concurrent")
    fun concurrent(@RequestParam("task") taskDelaysInSeconds: IntArray, @RequestParam("threads",required = false,defaultValue = "1") numberOfConcurrentThreads: Int): HttpResponse<TaskResponse> {

        val watch = StopWatch()
        watch.start()

        val delayedTasks = IntStream.of(*taskDelaysInSeconds)
                .mapToObj{
                    MockTask(it)
                }
                .collect(Collectors.toList())

        ConcurrentTasksExecutor(numberOfConcurrentThreads, delayedTasks).execute()

        watch.stop()
        return HttpResponse(TaskResponse(watch.totalTimeSeconds))
    }

    @ExceptionHandler(IllegalArgumentException::class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    fun handleException(e: IllegalArgumentException) = ErrorResponse(e.message)
}

顺序地执行多个任务:http://localhost:8080/tasks/sequential?task=1&task=2&task=3&task=4

顺序执行多个任务.jpeg

每个任务所花费的时间分别是1秒、2秒、3秒和4秒。最后,一共花费了10.009秒。

两个线程并发地执行多个任务:http://localhost:8080/tasks/concurrent?task=1&task=2&task=3&task=4&threads=2

两个线程并发执行多任务.jpeg

三个线程并发地执行多个任务:http://localhost:8080/tasks/concurrent?task=1&task=2&task=3&task=4&threads=3

三个线程并发执行多任务.jpeg

总结

本文使用了 Kotlin 的特性跟 Spring Boot 整合进行后端开发。Kotlin 的很多语法糖使得开发变得更加便利,当然 Kotlin 也是 Java 的必要补充。

本文 demo 的 github 地址:https://github.com/fengzhizi715/kotlin-spring-demo

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容