零、版本说明:
springboot: 2.7.18
使用log4j2,不使用springboot自带的logback
scala版本:2.11
jackson版本:2.16.0
一、依赖:
groovy
buildscript {
dependencies {
// using spring-boot-maven-plugin as package tool
classpath("org.springframework.boot:spring-boot-maven-plugin:2.7.18")
}
}
plugins {
id 'idea'
id 'scala'
id 'org.springframework.boot' version '2.7.18'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}
// 指定java版本
sourceCompatibility = 1.8
targetCompatibility = 1.8
// 尽量使用2.16.0版本的jackson对scala支持更好。
ext {
jackson_version = '2.16.0'
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web") {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}
implementation("org.springframework.boot:spring-boot-starter-log4j2")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude group: 'org.junit.jupiter'
}
testImplementation 'io.projectreactor:reactor-test:3.4.29'
implementation("com.fasterxml.jackson.core:jackson-core:${jackson_version}")
implementation("com.fasterxml.jackson.core:jackson-annotations:${jackson_version}")
implementation("com.fasterxml.jackson.core:jackson-databind:${jackson_version}")
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-xml:${jackson_version}")
implementation("com.fasterxml.jackson.module:jackson-module-scala_2.11:${jackson_version}")
testImplementation "com.fasterxml.jackson.core:jackson-core:${jackson_version}"
testImplementation("com.fasterxml.jackson.core:jackson-annotations:${jackson_version}")
testImplementation("com.fasterxml.jackson.core:jackson-databind:${jackson_version}")
testImplementation("com.fasterxml.jackson.dataformat:jackson-dataformat-xml:${jackson_version}")
testImplementation("com.fasterxml.jackson.module:jackson-module-scala_2.11:${jackson_version}")
}
configurations {
all*.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}
主启动类
此处也可以继承scala的App
但需要注意要重新App
中的main方法。
scala
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.{ SpringBootApplication}
@SpringBootApplication
class AppServe
object AppServer {
private val log = org.slf4j.LoggerFactory.getLogger(classOf[AppServer])
def main(args: Array[String]): Unit = {
log.info(s"${getClass.getName} get args: ${args.toList.toString()}")
SpringApplication.run(classOf[AppServer], args: _*)
}
}
springboot序列化使用的是jackson,而自带的jackson版本较低,且不支持scala。
scala
import com.fasterxml.jackson.databind.Module
import com.fasterxml.jackson.module.scala.{ClassTagExtensions, DefaultScalaModule}
import org.springframework.context.annotation.{Bean, Configuration}
import lombok.extern.slf4j.Slf4j
/**
* https://blog.csdn.net/beibaozhou1656/article/details/100966023
*/
@Configuration
class JacksonConfiguration {
@Bean
def defaultScalaModule(): Module = {
DefaultScalaModule::ClassTagExtensions
}
}
controller类
注意:@Resource()
(@Autowire
)使用方法。
scala
import org.springframework.web.bind.annotation.{PostMapping, PutMapping, RequestBody, RequestMapping, RestController}
import lombok.extern.slf4j.Slf4j
@Slf4j
@RestController
@RequestMapping(Array("/v1"))
class BatchController @Resource()(batchService: BatchSaveService) {
@PostMapping(Array("/batch/save"))
def batchSave(@RequestBody tbls: java.util.List[Element]): Unit = {
batchService.batch(tbls.asScala)
}
}
配置文件值获取
application.properties,application.yml,application.yaml文件
scala
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component
import javax.annotation.PostConstruct
@Slf4j
@Component
class KerberosEnvConfiguration() {
// 注意:要去除value=s"${}"的`s`。
@Value(value = "${udf.kafka.consumer.krb5-conf}") var krb5Conf: String = _
@PostConstruct
def init(): Unit = {
// 使用在post construct之后可以使用krb5Conf变量
System.setProperty( ... )
}
def getConf():String={
krb5Conf
}
}
scala
import lombok.extern.slf4j.Slf4j
import org.apache.kafka.clients.consumer.ConsumerRecord
import org.springframework.kafka.annotation.KafkaListener
import org.springframework.stereotype.{Component}
import javax.annotation.Resource
/**
* kerberos整合kafka、springboot
* https://blog.csdn.net/weixin_40496191/article/details/124056953
* SpringBoot集成Kafka详解
* https://blog.csdn.net/qq_20865839/article/details/13394898
*/
@Slf4j
@Component
class KafkaTableService @Resource()(service: CommonServiceImpl) extends Logging {
val mapper = JsonUtils.getMapper
// 可以使用#{}在注解中使用被@Component的的方法。
@KafkaListener(topics = Array("#{kerberosEnvConfiguration.getConf()}"))
def onMessage(record: ConsumerRecord[String, String]) = {
// do nothing
}
}
注意:
1、springboot打包必须使用spring-boot-maven-plugin
,不能使用shadowJar
如下jar目录结构:
2、idea中debug
3、无需设置main-class,springboot的插件设置。
4、生产环境启动nohub java -jar xxx.jar 2>&1 &
参考文章:
<>