英文:
@GrpcClient is null spring boot 3
问题
I am making grpc client using net.devh.grpc-client-spring-boot-starter
Client:
@Service
public class SystemService {
@GrpcClient("handyman-client")
StatusServiceGrpc.StatusServiceBlockingStub handymanClient;
public VersionResponse getHandymanVersion() {
return handymanClient.getVersion(Empty.newBuilder().build());
}
}
application.yaml:
grpc:
client:
handyman-client:
address: static://localhost:8080
negotiationType: plaintext
In runtime handymanClient
is null
. Tried spring boot versions from 3.0.0
to 3.0.2
. Lowering the spring boot version to 2.7.0
solves the problem - the client is being created now.
Is there a workaround to make it work on spring boot 3?
build.gradle:
plugins {
id 'java'
id 'org.springframework.boot' version '3.0.0'
id 'io.spring.dependency-management' version '1.1.0'
id "com.google.protobuf" version "0.9.2"
}
group = 'hello.grpc'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'io.micrometer:micrometer-registry-prometheus'
implementation 'javax.annotation:javax.annotation-api:1.3.2'
implementation group: 'net.devh', name: 'grpc-client-spring-boot-starter', version: '2.13.0.RELEASE'
implementation group: 'io.grpc', name: 'grpc-stub', version: '1.53.0'
implementation group: 'io.grpc', name: 'grpc-protobuf', version: '1.53.0'
}
sourceSets {
main {
java {
srcDirs 'build/generated/source/proto/main/grpc'
srcDirs 'build/generated/source/proto/main/java'
}
}
}
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:4.0.0-rc-2'
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.53.0'
}
}
generateProtoTasks {
all()*.plugins {
grpc {}
}
}
}
tasks.named('test') {
useJUnitPlatform()
}
springBoot {
buildInfo()
}
Hope this helps!
英文:
I am making grpc client using net.devh.grpc-client-spring-boot-starter
Client:
@Service
public class SystemService {
@GrpcClient("handyman-client")
StatusServiceGrpc.StatusServiceBlockingStub handymanClient;
public VersionResponse getHandymanVersion() {
return handymanClient.getVersion(Empty.newBuilder().build());
}
}
application.yaml:
grpc:
client:
handyman-client:
address: static://localhost:8080
negotiationType: plaintext
In runtime handymanClient
is null
. Tried spring boot versions from 3.0.0
to 3.0.2
. Lowering the spring boot version to 2.7.0
solves the problem - the client is being created now.
Is there a workaround to make it work on spring boot 3?
build.gradle:
<!-- begin snippet: js hide: true console: true babel: false -->
<!-- language: lang-js -->
plugins {
id 'java'
id 'org.springframework.boot' version '3.0.0'
id 'io.spring.dependency-management' version '1.1.0'
id "com.google.protobuf" version "0.9.2"
}
group = 'hello.grpc'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'io.micrometer:micrometer-registry-prometheus'
implementation 'javax.annotation:javax.annotation-api:1.3.2'
implementation group: 'net.devh', name: 'grpc-client-spring-boot-starter', version: '2.13.0.RELEASE'
implementation group: 'io.grpc', name: 'grpc-stub', version: '1.53.0'
implementation group: 'io.grpc', name: 'grpc-protobuf', version: '1.53.0'
}
sourceSets {
main {
java {
srcDirs 'build/generated/source/proto/main/grpc'
srcDirs 'build/generated/source/proto/main/java'
}
}
}
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:4.0.0-rc-2'
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.53.0'
}
}
generateProtoTasks {
all()*.plugins {
grpc {}
}
}
}
tasks.named('test') {
useJUnitPlatform()
}
springBoot {
buildInfo()
}
<!-- end snippet -->
答案1
得分: 5
根据这个问题,你必须在你的项目中使用以下配置:
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.context.annotation.Configuration;
@Configuration
@ImportAutoConfiguration({
net.devh.boot.grpc.client.autoconfigure.GrpcClientAutoConfiguration.class,
net.devh.boot.grpc.client.autoconfigure.GrpcClientMetricAutoConfiguration.class,
net.devh.boot.grpc.client.autoconfigure.GrpcClientHealthAutoConfiguration.class,
net.devh.boot.grpc.client.autoconfigure.GrpcClientSecurityAutoConfiguration.class,
net.devh.boot.grpc.client.autoconfigure.GrpcClientTraceAutoConfiguration.class,
net.devh.boot.grpc.client.autoconfigure.GrpcDiscoveryClientAutoConfiguration.class,
net.devh.boot.grpc.common.autoconfigure.GrpcCommonCodecAutoConfiguration.class,
net.devh.boot.grpc.common.autoconfigure.GrpcCommonTraceAutoConfiguration.class,
net.devh.boot.grpc.server.autoconfigure.GrpcAdviceAutoConfiguration.class,
net.devh.boot.grpc.server.autoconfigure.GrpcHealthServiceAutoConfiguration.class,
net.devh.boot.grpc.server.autoconfigure.GrpcMetadataConsulConfiguration.class,
net.devh.boot.grpc.server.autoconfigure.GrpcMetadataEurekaConfiguration.class,
net.devh.boot.grpc.server.autoconfigure.GrpcMetadataNacosConfiguration.class,
net.devh.boot.grpc.server.autoconfigure.GrpcMetadataZookeeperConfiguration.class,
net.devh.boot.grpc.server.autoconfigure.GrpcReflectionServiceAutoConfiguration.class,
net.devh.boot.grpc.server.autoconfigure.GrpcServerAutoConfiguration.class,
net.devh.boot.grpc.server.autoconfigure.GrpcServerFactoryAutoConfiguration.class,
net.devh.boot.grpc.server.autoconfigure.GrpcServerMetricAutoConfiguration.class,
net.devh.boot.grpc.server.autoconfigure.GrpcServerSecurityAutoConfiguration.class,
net.devh.boot.grpc.server.autoconfigure.GrpcServerTraceAutoConfiguration.class
})
public class GrpcConfig {
}
英文:
Based on this issue you must use the following configuration in your project :
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.context.annotation.Configuration;
@Configuration
@ImportAutoConfiguration({
net.devh.boot.grpc.client.autoconfigure.GrpcClientAutoConfiguration.class,
net.devh.boot.grpc.client.autoconfigure.GrpcClientMetricAutoConfiguration.class,
net.devh.boot.grpc.client.autoconfigure.GrpcClientHealthAutoConfiguration.class,
net.devh.boot.grpc.client.autoconfigure.GrpcClientSecurityAutoConfiguration.class,
net.devh.boot.grpc.client.autoconfigure.GrpcClientTraceAutoConfiguration.class,
net.devh.boot.grpc.client.autoconfigure.GrpcDiscoveryClientAutoConfiguration.class,
net.devh.boot.grpc.common.autoconfigure.GrpcCommonCodecAutoConfiguration.class,
net.devh.boot.grpc.common.autoconfigure.GrpcCommonTraceAutoConfiguration.class,
net.devh.boot.grpc.server.autoconfigure.GrpcAdviceAutoConfiguration.class,
net.devh.boot.grpc.server.autoconfigure.GrpcHealthServiceAutoConfiguration.class,
net.devh.boot.grpc.server.autoconfigure.GrpcMetadataConsulConfiguration.class,
net.devh.boot.grpc.server.autoconfigure.GrpcMetadataEurekaConfiguration.class,
net.devh.boot.grpc.server.autoconfigure.GrpcMetadataNacosConfiguration.class,
net.devh.boot.grpc.server.autoconfigure.GrpcMetadataZookeeperConfiguration.class,
net.devh.boot.grpc.server.autoconfigure.GrpcReflectionServiceAutoConfiguration.class,
net.devh.boot.grpc.server.autoconfigure.GrpcServerAutoConfiguration.class,
net.devh.boot.grpc.server.autoconfigure.GrpcServerFactoryAutoConfiguration.class,
net.devh.boot.grpc.server.autoconfigure.GrpcServerMetricAutoConfiguration.class,
net.devh.boot.grpc.server.autoconfigure.GrpcServerSecurityAutoConfiguration.class,
net.devh.boot.grpc.server.autoconfigure.GrpcServerTraceAutoConfiguration.class
})
public class GrpcConfig {
}
答案2
得分: 0
自从Spring Boot 3中更改了自动配置,META-INF/spring.factories 已更新为 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports。这个更新阻止了grpc-client 的自动装配。在最新的主分支上,已添加了Spring Boot 3的自动装配。您可以下载源代码并进行编译和安装。
英文:
Since autoconfiguration changed in springboot 3, META-INF/spring.factories was updated to META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports.
The update prevented grpc-client from autowiring.
On the latest master branch, the automatic assembly of springboot3 has been added. You can download the source code and compile and install it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论