@GrpcClient 是空的 spring boot 3

huangapple go评论88阅读模式
英文:

@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: &#39;net.devh&#39;, name: &#39;grpc-client-spring-boot-starter&#39;, version: &#39;2.13.0.RELEASE&#39;
implementation group: &#39;io.grpc&#39;, name: &#39;grpc-stub&#39;, version: &#39;1.53.0&#39;
implementation group: &#39;io.grpc&#39;, name: &#39;grpc-protobuf&#39;, version: &#39;1.53.0&#39;

}

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.

source code

huangapple
  • 本文由 发表于 2023年2月19日 15:01:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498501.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定