如何让 spring 中的 prometheus webflux r2dbc 协同工作?当我尝试 运行 来自服务的实际 api 时,它给了我错误
how to get prometheus webflux r2dbc in spring work together? It gives me error when I try to run actual apis from service
我在 spring-boot2 中使用 prometheus 生成指标,我正在使用 webflux 和 r2dbc 库进行反应式编程,但是我不确定它们是否可以协同工作。请帮助我理解我做错了什么。
它给出以下错误
java.lang.NoSuchMethodError: org.springframework.transaction.reactive.TransactionSynchronizationManager.currentTransaction()Lreactor/core/publisher/Mono;
at org.springframework.data.r2dbc.connectionfactory.ConnectionFactoryUtils.doGetConnection(ConnectionFactoryUtils.java:88) ~[spring-data-r2dbc-1.0.0.M2.jar:1.0.0.M2] at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:605) ~[spring-data-commons-2.2.3.RELEASE.jar:2.2.3.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93) ~[spring-aop-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.data.repository.core.support.MethodInvocationValidator.invoke(MethodInvocationValidator.java:99) ~[spring-data-commons-2.2.3.RELEASE.jar:2.2.3.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at
我的 build.gradle 看起来像这样:
plugins {
id("org.springframework.boot") version "2.2.2.RELEASE",
id ("io.spring.dependency-management") version "1.0.8.RELEASE"
}
dependencies {
implementation ("org.springframework.boot.experimental:spring-boot-starter-r2dbc")
implementation ("org.springframework.boot.experimental:spring-boot-starter-data-r2dbc")
implementation ("org.springframework.boot:spring-boot-starter")
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("io.micrometer:micrometer-registry-prometheus")
implementation ("org.springframework.boot:spring-boot-starter-webflux")
testImplementation ("io.projectreactor:reactor-test")
}
控制器函数如下所示:
@Timed(histogram = true )
@GetMapping("/abc")
fun process(@PathVariable ab: String): Mono<xyz> {
return service.getXYZ(xyz)
}
当使用 spring 初始化程序生成新项目时,它将为您提供以下依赖项:
dependencies {
implementation 'org.springframework.boot.experimental:spring-boot-actuator-autoconfigure-r2dbc'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot.experimental:spring-boot-starter-data-r2dbc'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation 'org.springframework.boot.experimental:spring-boot-test-autoconfigure-r2dbc'
testImplementation 'io.projectreactor:reactor-test'
}
您似乎缺少 spring-boot-actuator-autoconfigure-r2dbc
,您可以删除
implementation ("org.springframework.boot.experimental:spring-boot-starter-r2dbc")
因为 spring-boot-starter-data-r2dbc
将引入其传递依赖项。
由于 R2DBC 仍处于实验阶段,其自动配置尚未合并到 spring 引导中,必须单独包含。我已经向开发人员提出了一个问题,并要求现在这部分应该在官方文档中提及。
我在 spring-boot2 中使用 prometheus 生成指标,我正在使用 webflux 和 r2dbc 库进行反应式编程,但是我不确定它们是否可以协同工作。请帮助我理解我做错了什么。
它给出以下错误
java.lang.NoSuchMethodError: org.springframework.transaction.reactive.TransactionSynchronizationManager.currentTransaction()Lreactor/core/publisher/Mono;
at org.springframework.data.r2dbc.connectionfactory.ConnectionFactoryUtils.doGetConnection(ConnectionFactoryUtils.java:88) ~[spring-data-r2dbc-1.0.0.M2.jar:1.0.0.M2] at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:605) ~[spring-data-commons-2.2.3.RELEASE.jar:2.2.3.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93) ~[spring-aop-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.data.repository.core.support.MethodInvocationValidator.invoke(MethodInvocationValidator.java:99) ~[spring-data-commons-2.2.3.RELEASE.jar:2.2.3.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at
我的 build.gradle 看起来像这样:
plugins {
id("org.springframework.boot") version "2.2.2.RELEASE",
id ("io.spring.dependency-management") version "1.0.8.RELEASE"
}
dependencies {
implementation ("org.springframework.boot.experimental:spring-boot-starter-r2dbc")
implementation ("org.springframework.boot.experimental:spring-boot-starter-data-r2dbc")
implementation ("org.springframework.boot:spring-boot-starter")
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("io.micrometer:micrometer-registry-prometheus")
implementation ("org.springframework.boot:spring-boot-starter-webflux")
testImplementation ("io.projectreactor:reactor-test")
}
控制器函数如下所示:
@Timed(histogram = true )
@GetMapping("/abc")
fun process(@PathVariable ab: String): Mono<xyz> {
return service.getXYZ(xyz)
}
当使用 spring 初始化程序生成新项目时,它将为您提供以下依赖项:
dependencies {
implementation 'org.springframework.boot.experimental:spring-boot-actuator-autoconfigure-r2dbc'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot.experimental:spring-boot-starter-data-r2dbc'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation 'org.springframework.boot.experimental:spring-boot-test-autoconfigure-r2dbc'
testImplementation 'io.projectreactor:reactor-test'
}
您似乎缺少 spring-boot-actuator-autoconfigure-r2dbc
,您可以删除
implementation ("org.springframework.boot.experimental:spring-boot-starter-r2dbc")
因为 spring-boot-starter-data-r2dbc
将引入其传递依赖项。
由于 R2DBC 仍处于实验阶段,其自动配置尚未合并到 spring 引导中,必须单独包含。我已经向开发人员提出了一个问题,并要求现在这部分应该在官方文档中提及。