I implementing a microservices using kafka cloud-stream with avro when I put Sleuth dependency the OUTPUT of the Function, the producer couldn't serialize avro.
Dependency in gradle.build:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
buildscript {
dependencies {
classpath("com.commercehub.gradle.plugin:gradle-avro-plugin:0.16.0")
}
}
plugins {
id("org.springframework.boot") version "2.4.2"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
kotlin("jvm") version "1.4.21"
kotlin("plugin.spring") version "1.4.21"
}
apply(plugin = "com.commercehub.gradle.plugin.avro")
group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_15
repositories {
mavenCentral()
maven { url = uri("https://packages.confluent.io/maven/") }
}
extra["springCloudVersion"] = "2020.0.1"
dependencies {
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.cloud:spring-cloud-stream")
implementation("org.springframework.cloud:spring-cloud-starter-stream-kafka")
implementation("org.apache.kafka:kafka-streams")
//if you take off sleuth dependency of the project the AVRO is converted the json
implementation("org.springframework.cloud:spring-cloud-starter-sleuth")
implementation("io.confluent:kafka-avro-serializer:5.5.0")
implementation("org.apache.avro:avro:1.10.0")
implementation("com.github.javafaker:javafaker:1.0.2")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}
dependencyManagement {
imports {
mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}")
}
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "15"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
follows the little AVRO schema that I used:
{
"namespace": "com.example.demo.events",
"type": "record",
"name": "Person",
"fields": [
{
"name": "name",
"type": "string",
"avro.java.string": "String"
}
]
}
The configuration in application.yml:
server.port: 9091
spring:
application.name: demo-processor
cloud:
stream:
function:
definition: transform
bindings:
transform-in-0:
destination: topic-a
contentType: application/*+avro
group: 'process-uppercase'
transform-out-0:
destination: topic-b
contentType: application/*+avro
producer:
useNativeEncoding: true
kafka:
binder:
brokers: localhost:9092
bindings:
transform-in-0:
consumer:
configuration:
schema.registry.url: http://localhost:8081
value.deserializer: io.confluent.kafka.serializers.KafkaAvroDeserializer
specific.avro.reader: true
transform-out-0:
producer:
configuration:
schema.registry.url: http://localhost:8081
value.serializer: io.confluent.kafka.serializers.KafkaAvroSerializer
This the code:
package com.example.demo
import com.example.demo.events.Person
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.annotation.Bean
import org.springframework.messaging.Message
import org.springframework.messaging.support.MessageBuilder
import java.util.function.Function
#SpringBootApplication
class DemoProcessorApplication {
//this function work very well without Sleuth dependency
#Bean
fun transform() = Function<Message<Person>, Message<Person>> {
var person = it.payload
var name = person.getName()
person.setName(name.toUpperCase())
println("Transform ${name} to ${name.toUpperCase()}")
MessageBuilder.withPayload(person).build()
}
}
fun main(args: Array<String>) {
runApplication<DemoProcessorApplication>(*args)
}
I thank you for your help!
Related
I added spring-cloud-starter-netflix-eureka-client gradle depedency in
my project and shrik the depedency. But when go use
#EnableEurekaClient in my Main class it show me suggestion create
#EnableEurekaClient annotation. Don't show any import file of eureka
client.
Unresolved reference: EnableEurekaClient
productserviceApplication.kt
package com.main.productservice
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
#SpringBootApplication
#EnableEurekaClient
class ProductServiceApplication
fun main(args: Array<String>) {
runApplication<ProductServiceApplication>(*args)
}
gradle.kt
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "2.5.2"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
kotlin("jvm") version "1.5.20"
kotlin("plugin.spring") version "1.5.20"
}
group = "com.main"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.
kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.springframework.cloud:spring-cloud-starter-netflix-eureka-client")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "11"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
I am getting error at #EnableEurekaClient
#EnableEurekaClient is deprecated no need to annotate at springboot main applicationit is fine if we add the spring-cloud-starter-netflix-eureka-client dependency in pom and if we have the application name in yml or properties file it will be registered to Eureka Server
Make sure to import the Spring Cloud BOM in your Gradle build definition:
extra["springCloudVersion"] = "2020.0.3"
dependencyManagement {
imports {
mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}")
}
}
The EnableEurekaClient annotation is in package org.springframework.cloud.netflix.eureka. You should add the following import to your main application file:
import org.springframework.cloud.netflix.eureka.EnableEurekaClient
I want to setup communication between all the services that I created.
When I add cloud config server gradle dependency in my project and run
the project I got this type of issue
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method modifyRequestBodyGatewayFilterFactory in org.springframework.cloud.gateway.config.GatewayAutoConfiguration required a bean of type 'org.springframework.http.codec.ServerCodecConfigurer' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.http.codec.ServerCodecConfigurer' in your configuration.
Process finished with exit code 1
I have defined everything properly according to my knowledge, But I am not
able to find the issue from the last 2 days .I try myself
these are the files I created in this project
build.gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "2.4.5"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
kotlin("jvm") version "1.4.32"
kotlin("plugin.spring") version "1.4.32"
}
group = "com.main"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11
repositories {
mavenCentral()
}
extra["springCloudVersion"] = "2020.0.2"
dependencies {
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.springframework.cloud:spring-cloud-config-server")
implementation("org.springframework.cloud:spring-cloud-starter-gateway")
implementation("org.springframework.cloud:spring-cloud-starter-netflix-eureka-client")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}
dependencyManagement {
imports {
mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}")
}
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "11"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
ApiGatewayApplication.kt
package com.main.apigateway
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.cloud.netflix.eureka.EnableEurekaClient
#SpringBootApplication
#EnableEurekaClient
class ApigatewayApplication
fun main(args: Array<String>) {
runApplication<ApigatewayApplication>(*args)
}
application.yml
server:
port: 8999
spring:
application:
name: api-gateway
cloud:
gateway:
routes:
- id: DEPARTMENT-SERVICE
uri: lb://DEPARTMENT-SERVICE
predicates:
- Path=/departments/**
- id: USER-REGISTRATION
uri: lb://USER-REGISTRATION
predicates:
- Path=/users/**
bootsrap.yml
spring:
cloud:
config:
enabled: true
uri: http://localhost:9296
I am unable to properly inject an #Value application property in my Kotlin Spring Boot application. The property as defined in my application.yml file, and subsequently referenced in an additional-spring-configuration-metadata.json file (under resources -> META-INF), is not properly being added to the bean expression context. Using IntelliJ version 2020.2.1, when I hover over the property, I see a Cannot resolve configuration property error. Attempting to run the application (with the configuration property value construction-injected into a class) leads to a Unsatisfied dependency expressed through constructor parameter error.
build.gradle.kts
plugins {
id("org.springframework.boot") version "2.3.3.RELEASE"
id("io.spring.dependency-management") version "1.0.10.RELEASE"
kotlin("jvm") version "1.3.72"
kotlin("plugin.spring") version "1.3.72"
}
group = "com.myProject"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11
repositories {
mavenCentral()
}
extra["springCloudVersion"] = "Hoxton.SR8"
dependencies {
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
implementation("org.springframework.boot:spring-boot-configuration-processor:2.3.3.RELEASE")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
}
testImplementation("io.projectreactor:reactor-test")
}
buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath("com.google.cloud.tools:appengine-gradle-plugin:2.2.0")
}
}
apply(plugin = "com.google.cloud.tools.appengine")
configure<com.google.cloud.tools.gradle.appengine.appyaml.AppEngineAppYamlExtension> {
deploy {
projectId = "my-cloud-project"
version = "GCLOUD_CONFIG"
}
}
dependencyManagement {
imports {
mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}")
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "11"
}
}
Error Message
Spring error modal
additional-spring-configuration-metadata.json
{
"properties": [
{
"name": "otherApi.baseUrl",
"type": "java.lang.String",
"description": "Description for otherApi.baseUrl."
}
]
}
I've added annotation processing dependencies, invalidated caches and restarted, and played around with Kotlin specific annotation processors (kapt). I've also followed the instructions here: https://www.jetbrains.com/help/idea/annotation-processors-support.html
What am I missing? Any and all help would be appreciated. Thanks!
you need to declare the below as as annoatationProcessor
implementation("org.springframework.boot:spring-boot-configuration-processor:2.3.3.RELEASE")
to
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor:2.3.3.RELEASE")
If your application.yml (or application.properties) looks like this:
spring:
datasource:
username: postgres
password: postgres
url: jdbc:postgresql://localhost:5433/company
driver-class-name: org.postgresql.Driver
then try to rewrite each property to full-name format for every property:
spring.datasource.username: postgres
spring.datasource.password: postgres
spring.datasource.url: jdbc:postgresql://localhost:5433/company
spring.datasource.driver-class-name: org.postgresql.Driver
I am running a spring cloud config server.I follow the user guide,and successfully start it up,it can load the configuration from github config-repo/licensingservice/licensingservice.yml
.I can use the /decrypt and /encript endpoints ,but when I run http://localhost:8888/licensingservice/default
it always decrypts the sensitive message
spring.datasource.password: "{cipher}4788dfe1ccbe6485934aec2ffeddb06163ea3d616df5fd75be96aadd4df1da91" into
"spring.datasource.password": "p0stgr#s"
I have put spring.cloud.config.server.encrypt.enabled=false in bootstrap.yml, and can see it by the
localhost:8888/actuator/env
the configuration on the github:
example.property: "I AM IN THE DEFAULT"
spring.jpa.database: "POSTGRESQL"
spring.datasource.platform: "postgres"
spring.jpa.show-sql: "true"
spring.database.driverClassName: "org.postgresql.Driver"
spring.datasource.url: "jdbc:postgresql://database:5432/eagle_eye_local"
spring.datasource.username: "postgres"
spring.datasource.password: "{cipher}4788dfe1ccbe6485934aec2ffeddb06163ea3d616df5fd75be96aadd4df1da91"
spring.datasource.testWhileIdle: "true"
spring.datasource.validationQuery: "SELECT 1"
spring.jpa.properties.hibernate.dialect: "org.hibernate.dialect.PostgreSQLDialect"
redis.server: "redis"
redis.port: "6379"
signing.key: "345345fsdfsf5345"
configuration get from postman:
{
"name": "licensingservice",
"profiles": [
"default"
],
"label": null,
"version": "56d63a8c0c3dcb0c5c93db1f00cf71856371db8b",
"state": null,
"propertySources": [
{
"name": "https://github.com/carnellj/config-repo//licensingservice/licensingservice.yml",
"source": {
"example.property": "I AM IN THE DEFAULT",
"spring.jpa.database": "POSTGRESQL",
"spring.datasource.platform": "postgres",
"spring.jpa.show-sql": "true",
"spring.database.driverClassName": "org.postgresql.Driver",
"spring.datasource.url": "jdbc:postgresql://database:5432/eagle_eye_local",
"spring.datasource.username": "postgres",
"spring.datasource.testWhileIdle": "true",
"spring.datasource.validationQuery": "SELECT 1",
"spring.jpa.properties.hibernate.dialect": "org.hibernate.dialect.PostgreSQLDialect",
"redis.server": "redis",
"redis.port": "6379",
"signing.key": "345345fsdfsf5345",
"spring.datasource.password": "p0stgr#s"
}
}
]
}
my build script:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "2.3.1.RELEASE"
id("io.spring.dependency-management") version "1.0.9.RELEASE"
kotlin("jvm") version "1.3.72"
kotlin("plugin.spring") version "1.3.72"
}
group = "com.matches"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8
repositories {
mavenCentral()
}
extra["springCloudVersion"] = "Hoxton.SR6"
dependencies {
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.springframework.cloud:spring-cloud-config-server")
implementation("org.springframework.cloud:spring-cloud-starter-config")
implementation("org.springframework.cloud:spring-cloud-starter-eureka")
implementation("org.springframework.boot:spring-boot-starter-actuator")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
}
}
dependencyManagement {
imports {
mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}")
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
}
}
ConfigserverApplication:
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.cloud.config.server.EnableConfigServer
#SpringBootApplication
#EnableConfigServer
class ConfigserverApplication
fun main(args: Array<String>) {
runApplication<ConfigserverApplication>(*args)
}
I can not find out why it alway decrypts the password?
spring-cloud-config-server always decrypt the encrypted message? NO
ConfigClient can also decrypt if we set encrypt to false like this in ConfigServer.
spring:
cloud:
config:
server:
encrypt:
enabled: false
git:
uri: linkToYourURI
And in the ConfigClient use the key for decryption like this.
Remember to put this in bootstrap.yml as for application.yml it will be too late, see this for further clarification.
encrypt:
key: PutYourKeyHere
I'm new at spring boot with kotlin.
I have error when application start up.
I had been seeking various solutions in the other answers in Stackoverflow.
But no answers gave me no solutions.
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
My application.properties configuration is following.
spring.datasource.url=jdbc:mysql://localhost/db_name
spring.datasource.username=db_user
spring.datasource.password=db_pass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
And build.gradle.kts is following.
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "2.2.0.RELEASE"
id("io.spring.dependency-management") version "1.0.8.RELEASE"
war
kotlin("jvm") version "1.3.50"
kotlin("plugin.spring") version "1.3.50"
kotlin("plugin.jpa") version "1.3.50"
}
group = "jp.co.blowfish.springboot"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8
val developmentOnly by configurations.creating
configurations {
runtimeClasspath {
extendsFrom(developmentOnly)
}
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
developmentOnly("org.springframework.boot:spring-boot-devtools")
runtimeOnly("mysql:mysql-connector-java")
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
}
}
I have no idea what is wrong.
It is missing the port of Mysql by default is 3306
spring.datasource.url=jdbc:mysql://localhost:3306/db_name`
or application.properties is in resources folder.