利用Kotlin + Spring Boot实现后端开发

前言

Spring官方最近宣布,将在Spring Framework 5.0版本中正式支持Kotlin语言。这意味着Spring Boot 2.x版本将为Kotlin提供一流的支持。

这并不会令人意外,因为Pivotal团队以广泛接纳​​JVM语言(如Scala和Groovy)而闻名。

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:

创建用户monica:

查询用户monica:

示例二:结合 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

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

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

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

总结

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

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

好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对我们的支持。

(0)

相关推荐

  • Spring Boot 与 Kotlin 上传文件的示例代码

    如果我们做一个小型的web站,而且刚好选择的kotlin 和Spring Boot技术栈,那么上传文件的必不可少了,当然,如果你做一个中大型的web站,那建议你使用云存储,能省不少事情. 这篇文章就介绍怎么使用kotlin 和Spring Boot上传文件 构建工程 如果对于构建工程还不是很熟悉的可以参考<我的第一个Kotlin应用> 完整 build.gradle 文件 group 'name.quanke.kotlin' version '1.0-SNAPSHOT' buildscript

  • Spring Boot 与 Kotlin 使用JdbcTemplate连接MySQL数据库的方法

    之前介绍了一些Web层的例子,包括构建RESTful API.使用Thymeleaf模板引擎渲染Web视图,但是这些内容还不足以构建一个动态的应用.通常我们做App也好,做Web应用也好,都需要内容,而内容通常存储于各种类型的数据库,服务端在接收到访问请求之后需要访问数据库获取并处理成展现给用户使用的数据形式. 本文介绍在Spring Boot基础下配置数据源和通过 JdbcTemplate 编写数据访问的示例. 数据源配置 在我们访问数据库的时候,需要先配置一个数据源,下面分别介绍一下几种不同

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

    Spring Boot中除了对常用的关系型数据库提供了优秀的自动化支持之外,对于很多NoSQL数据库一样提供了自动化配置的支持,包括:Redis, MongoDB, Elasticsearch, Solr和Cassandra. 使用Redis Redis是一个开源的使用 ANSI C 语言编写.支持网络.可基于内存亦可持久化的日志型. Key-Value 数据库. Redis官网 Redis中文社区 引入依赖 Spring Boot提供的数据访问框架Spring Data Redis基于Jedi

  • spring boot + jpa + kotlin入门实例详解

    spring boot +jpa的文章网络上已经有不少,这里主要补充一下用kotlin来做. kotlin里面的data class来创建entity可以帮助我们减少不少的代码,比如现在这个User的Entity,这是Java版本的: @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; private String firstName; private S

  • Spring Boot与Kotlin定时任务的示例(Scheduling Tasks)

    在编写Spring Boot应用中会遇到这样的场景,比如:需要定时地发送一些短信.邮件之类的操作,也可能会定时地检查和监控一些标志.参数等. 创建定时任务 在Spring Boot中编写定时任务是非常简单的事,下面通过实例介绍如何在Spring Boot中创建定时任务,实现每过5秒输出一下当前时间. 在Spring Boot的主类中加入@EnableScheduling注解,启用定时任务的配置 import org.springframework.boot.SpringApplication i

  • 详解用Kotlin写一个基于Spring Boot的RESTful服务

    Spring太复杂了,配置这个东西简直就是浪费生命.尤其在没有什么并发压力,随便搞一个RESTful服务,让整个业务跑起来先的情况下,更是么有必要纠结在一堆的XML配置上.显然这么想的人是很多的,于是就有了Spring Boot.又由于Java 8太墨迹于是有了Kotlin. 数据源使用MySql.通过Spring Boot这个基本不怎么配置的,不怎么微的微框架的Spring Data JPA和Hibernate来访问数据. 处理依赖 这里使用Gradle来处理依赖. 首先下载官网给的初始项目:

  • Kotlin + Spring Boot 请求参数验证的代码实例

    编写 Web 应用程序的时候,经常要做的事就是要对前端传回的数据进行简单的验证,比如是否非空.字符长度是否满足要求,邮箱格式是否正确等等.在 Spring Boot 中,可以使用 Bean Validation (JSR-303) 技术通过注解的方式来进行参数验证. 准备 DTO 对象 data class UserRegisterModel( @get: NotEmpty(message = "User name is required") @get: Size(message =

  • 关于Spring Boot和Kotlin的联合开发

    一.概述 spring官方最近宣布,将在Spring Framework 5.0版本中正式支持Kotlin语言.这意味着Spring Boot 2.x版本将为Kotlin提供一流的支持. 这并不会令人意外,因为Pivotal团队以广泛接纳​​JVM语言(如Scala和Groovy)而闻名.下面我们用Spring Boot 2.x和Kotlin应用程序. 二.搭建环境 1.环境 IntelliJ和Eclipse都对Kotlin提供了支持,可以根据自己的喜好搭建Kotlin开发环境. 2.构建应用

  • Spring Boot 与 kotlin 使用Thymeleaf模板引擎渲染web视图的方法

    本篇给大家介绍Spring Boot 与 kotlin 使用Thymeleaf模板引擎渲染web视图. 静态资源访问 在我们开发Web应用的时候,需要引用大量的js.css.图片等静态资源,使用Spring Boot 与 kotlin如何去支持这些静态资源?,很简单. 默认配置 Spring Boot默认提供静态资源目录位置需置于 classpath 下,目录名需符合如下规则: /static /public /resources /META-INF/resources 举例:我们可以在src/

  • 使用Spring boot + jQuery上传文件(kotlin)功能实例详解

    文件上传也是常见的功能,趁着周末,用Spring boot来实现一遍. 前端部分 前端使用jQuery,这部分并不复杂,jQuery可以读取表单内的文件,这里可以通过formdata对象来组装键值对,formdata这种方式发送表单数据更为灵活.你可以使用它来组织任意的内容,比如使用 formData.append("test1","hello world"); 在kotlin后端就可以使用@RequestParam("test1") greet

随机推荐