尊敬的 gRPC/Spring 用戶,為了增強 grpc-ecosystem/grpc-spring 的用戶體驗,我們進行了本次調查,作為建立直接溝通管道的手段。非常感謝您的回饋。
自述文件:英語 | 中文
文檔:英文 | 中文
使用@GrpcService
實現自動配置和運行 gRPC 伺服器
使用@GrpcClient
自動建立和管理您的 grpc 通道和存根
支援其他 grpc-java 風格(例如 Reactive gRPC (RxJava)、grpc-kotlin...)
io.grpc.BindableService
)StubFactory
s支援 Spring-Security
支援Spring雲
DiscoveryClient
讀取服務的目標位址(所有類型)支援 Spring Sleuth 作為分散式追蹤解決方案
(如果存在brave-instrumentation-grpc)
支援全域和自訂 gRPC 伺服器/客戶端攔截器
自動公制支援(基於千分尺/執行器)
也適用於(非陰影)grpc-netty
最新版本是3.1.0.RELEASE
它是用 spring-boot 3.2.4
和 spring-cloud 2023.0.0
編譯的,但它也與其他多種版本相容。所有版本及其各自庫版本的概述可以在我們的文件中找到。
注意:該項目也可以在沒有 Spring-Boot 的情況下使用,但這需要一些手動 bean 配置。
若要使用 Maven 新增依賴項,請使用下列命令:
< dependency >
< groupId >net.devh</ groupId >
< artifactId >grpc-spring-boot-starter</ artifactId >
< version >3.1.0.RELEASE</ version >
</ dependency >
使用 Gradle 新增相依性:
dependencies {
implementation ' net.devh:grpc-spring-boot-starter:3.1.0.RELEASE '
}
若要使用 Maven 新增依賴項,請使用下列命令:
< dependency >
< groupId >net.devh</ groupId >
< artifactId >grpc-server-spring-boot-starter</ artifactId >
< version >3.1.0.RELEASE</ version >
</ dependency >
使用 Gradle 新增相依性:
dependencies {
implementation ' net.devh:grpc-server-spring-boot-starter:3.1.0.RELEASE '
}
使用@GrpcService
註釋您的伺服器介面實現
@ GrpcService
public class GrpcServerService extends GreeterGrpc . GreeterImplBase {
@ Override
public void sayHello ( HelloRequest req , StreamObserver < HelloReply > responseObserver ) {
HelloReply reply = HelloReply . newBuilder (). setMessage ( "Hello ==> " + req . getName ()). build ();
responseObserver . onNext ( reply );
responseObserver . onCompleted ();
}
}
預設情況下,grpc 伺服器將監聽連接埠9090
。這些和其他設定可以透過 Spring 的屬性機制進行變更。伺服器使用grpc.server.
前綴。
請參閱我們的文件以了解更多詳細資訊。
若要使用 Maven 新增依賴項,請使用下列命令:
< dependency >
< groupId >net.devh</ groupId >
< artifactId >grpc-client-spring-boot-starter</ artifactId >
< version >3.1.0.RELEASE</ version >
</ dependency >
使用 Gradle 新增相依性:
dependencies {
compile ' net.devh:grpc-client-spring-boot-starter:3.1.0.RELEASE '
}
使用@GrpcClient(serverName)
註解 grpc 用戶端存根的字段
不要與@Autowired
或@Inject
結合使用
@ GrpcClient ( "gRPC server name" )
private GreeterGrpc . GreeterBlockingStub greeterStub ;
注意:您可以對多個通道使用相同的 grpc 伺服器名稱,也可以使用不同的存根(即使使用不同的攔截器)。
然後你可以像這樣向你的伺服器發送查詢:
HelloReply response = stub . sayHello ( HelloRequest . newBuilder (). setName ( name ). build ());
可以為每個客戶端單獨配置目標位址。但在某些情況下,您可以僅依賴預設配置。您可以透過NameResolver.Factory
beans 自訂預設 url 映射。如果您不配置該 bean,則會使用預設方案和名稱(例如: dns:/<name>
)猜測預設 uri:
這些和其他設定可以透過 Spring 的屬性機制進行變更。客戶端使用grpc.client.(serverName).
前綴。
請參閱我們的文件以了解更多詳細資訊。
該庫支援grpc-netty
和grpc-netty-shaded
。後一種可能會防止與不相容的 grpc 版本發生衝突或需要不同版本的 netty 的庫之間發生衝突。
注意:如果類別路徑中存在著色的 netty,則該程式庫將始終優先於非著色的 grpc-netty。
您可以將它與 Maven 一起使用,如下所示:
< dependency >
< groupId >io.grpc</ groupId >
< artifactId >grpc-netty</ artifactId >
< version >${grpcVersion}</ version >
</ dependency >
<!-- For both -->
< dependency >
< groupId >net.devh</ groupId >
< artifactId >grpc-spring-boot-starter</ artifactId >
< version >...</ version >
< exclusions >
< exclusion >
< groupId >io.grpc</ groupId >
< artifactId >grpc-netty-shaded</ artifactId >
</ exclusion >
</ exclusions >
</ dependency >
<!-- For the server (only) -->
< dependency >
< groupId >net.devh</ groupId >
< artifactId >grpc-server-spring-boot-starter</ artifactId >
< version >...</ version >
< exclusions >
< exclusion >
< groupId >io.grpc</ groupId >
< artifactId >grpc-netty-shaded</ artifactId >
</ exclusion >
</ exclusions >
</ dependency >
<!-- For the client (only) -->
< dependency >
< groupId >net.devh</ groupId >
< artifactId >grpc-client-spring-boot-starter</ artifactId >
< version >...</ version >
< exclusions >
< exclusion >
< groupId >io.grpc</ groupId >
< artifactId >grpc-netty-shaded</ artifactId >
</ exclusion >
</ exclusions >
</ dependency >
使用 Gradle 時就像這樣:
implementation " io.grpc:grpc-netty: ${ grpcVersion } "
implementation ' net.devh:grpc-spring-boot-starter:... ' exclude group : ' io.grpc ' , module : ' grpc-netty-shaded ' // For both
implementation ' net.devh:grpc-client-spring-boot-starter:... ' exclude group : ' io.grpc ' , module : ' grpc-netty-shaded ' // For the client (only)
implementation ' net.devh:grpc-server-spring-boot-starter:... ' exclude group : ' io.grpc ' , module : ' grpc-netty-shaded ' // For the server (only)
請在此閱讀有關我們範例項目的更多資訊。
請參閱我們的文件尋求協助。
始終歡迎您的貢獻!請參閱 CONTRIBUTING.md 以了解詳細指南。