gpt4 book ai didi

使用@GrpcClient实现客户端

转载 作者:我是一只小鸟 更新时间:2023-05-22 06:31:15 28 4
gpt4 key购买 nike

转载请注明出处:

  @GrpcClient 注解的作用是将 gRPC 客户端注入到 Spring 容器中,方便在应用程序中使用 gRPC 客户端调用 gRPC 服务提供的函数。使用 @GrpcClient 注解,我们可以像使用其他 Spring Bean 一样来使用 gRPC 客户端,无需手动创建连接通道和 stub 类对象,Spring Boot 会自动为我们进行管理和维护.

  使用 @GrpcClient 注解的场景通常是在 Spring Boot 中使用 gRPC 客户端时,因为 Spring Boot 已经为我们提供了自动配置功能,可以简化 gRPC 客户端的配置和使用。在这种场景下,我们只需要在应用程序中添加 @GrpcClient 注解,然后在配置文件中添加 gRPC 客户端的配置信息,就可以方便地使用 gRPC 客户端了.

  另外,@GrpcClient 注解还可以指定 gRPC 服务的名称,以区分不同的 gRPC 服务。这对于在同一个应用程序中使用多个 gRPC 服务时非常有用,可以避免不同的 gRPC 服务之间发生冲突。例如:

                          @GrpcClient("greeter"
                          
                            )

                          
                          
                            private
                          
                          
                             GreeterGrpc.GreeterBlockingStub greeterStub;
@GrpcClient(
                          
                          "calculator"
                          
                            )

                          
                          
                            private
                          
                           CalculatorGrpc.CalculatorBlockingStub calculatorStub;
                        

  在上述示例中,我们分别为 greeter 和 calculator 两个 gRPC 服务创建了不同的 stub 类对象,并使用了不同的 @GrpcClient 注解来区分它们.

以下是一个完整的使用@GrpcClient注解的使用代码:

1.配置maven相关依赖

gRPC客户端使用一下命令添加 Maven 依赖项:

                          <dependency>
    <groupId>net.devh</groupId>
    <artifactId>grpc-client-spring-boot-starter</artifactId>
    <version>2.12.0.RELEASE</version>
</dependency>
<dependency>
  <groupId>io.grpc</groupId>
  <artifactId>grpc-netty-shaded</artifactId>
  <version>1.37.0</version>
</dependency>
<dependency>
  <groupId>io.grpc</groupId>
  <artifactId>grpc-protobuf</artifactId>
  <version>1.37.0</version>
</dependency>
<dependency>
  <groupId>io.grpc</groupId>
  <artifactId>grpc-stub</artifactId>
  <version>1.37.0</version>
</dependency>
                        

  并添加一下插件:

                          <build>
    <plugins>
        <plugin>
            <groupId>org.xolstice.maven.plugins</groupId>
            <artifactId>protobuf-maven-plugin</artifactId>
            <version>0.6.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <protocArtifact>com.google.protobuf:protoc:3.12.0:exe:${os.detected.classifier}</protocArtifact>
                <pluginId>grpc-java</pluginId>
                <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.30.2:exe:${os.detected.classifier}</pluginArtifact>
                <outputDirectory>${project.build.directory}/generated-sources/protobuf/java</outputDirectory>
                <clearOutputDirectory>
                          
                            false
                          
                          </clearOutputDirectory>
                <includeCompileClasspath>
                          
                            true
                          
                          </includeCompileClasspath>
            </configuration>
        </plugin>
    </plugins>
</build>
                        

  。

2.添加proto文件:

                          syntax = "proto3"
                          
                            ;

                          
                          
                            package
                          
                          
                             com.example.grpcdemo.service;
option java_multiple_files 
                          
                          = 
                          
                            true
                          
                          
                            ;
option java_package 
                          
                          = "com.example.grpcdemo.service"
                          
                            ;
option java_outer_classname 
                          
                          = "UserServiceProto"
                          
                            ;
message UserRequest {
    string name 
                          
                          = 1
                          
                            ;
    int32 age 
                          
                          = 2
                          
                            ;
}
message UserResponse {
    string message 
                          
                          = 1
                          
                            ;
}
service UserService {
    rpc addUser (UserRequest) returns (UserResponse);
    rpc deleteUser (UserRequest) returns (UserResponse);
    rpc updateUser (UserRequest) returns (UserResponse);
    rpc getUser (UserRequest) returns (UserResponse);
}
                          
                        

  执行 mvn clean install 命令即可将proto文件编译成Java类并生成到 target/generated-sources/protobuf/java 目录下。 编译后的Java类包括服务类、消息类和客户端类。以本例中的proto文件为例,编译后的Java类如下:

  • 服务类: UserServiceGrpc.java
  • 消息类: UserRequest.java UserResponse.java
  • 客户端类: UserServiceGrpc.UserServiceBlockingStub.java 接下来,我们可以使用 @GrpcClient 注解将 UserServiceGrpc.UserServiceBlockingStub 类型的gRPC客户端注入到Spring Bean中。

3.使用@GrpcClient实现服务端  

  示例代码如下:

                          
                            @Component

                          
                          
                            public
                          
                          
                            class
                          
                          
                             UserGrpcClient {
    @GrpcClient(
                          
                          "user-service-provider") 
                          
                            //
                          
                          
                             指定gRPC服务在配置中的服务名
                          
                          
                            private
                          
                          
                             UserServiceGrpc.UserServiceBlockingStub userService;
    
                          
                          
                            public
                          
                          
                            void
                          
                          
                             addUser(UserRequest request) {
        UserResponse response 
                          
                          =
                          
                             userService.addUser(request);
        
                          
                          
                            //
                          
                          
                             处理响应结果
                          
                          
                                }
}
                          
                        

  在这个例子中,我们使用 @GrpcClient 注解将 UserServiceGrpc.UserServiceBlockingStub 类型的gRPC客户端注入到了Spring Bean中,并在 addUser() 方法中使用该gRPC客户端访问远程gRPC服务。需要注意的是,在使用 @GrpcClient 注解时,需要指定gRPC服务的服务名.

4.配置文件中配置grpc的配置信息

  配置文件中配置 grpcClient 相关的配置 。

                          
                            grpc:
  client:
    user
                          
                          -service-
                          
                            provider:
      address: localhost            # gRPC服务地址
      port: 
                          
                          6565
                          
                            
      plaintext: 
                          
                          
                            true
                          
                          
                                            # 指定是否使用明文连接;
      enableKeepAlive: 
                          
                          
                            true
                          
                          
                                        # 是否启用KeepAlive机制
      keepAliveTime: 30s            # KeepAlive时间
      keepAliveTimeout: 5s              # 是否在没有gRPC调用时保持KeepAlive的连接
                          
                        

5.客户端调用

  在应用程序中,使用 UserGrpcClient 类访问gRPC服务,示例代码如下:

                          
                            import
                          
                          
                             com.example.grpcdemo.service.UserRequest;

                          
                          
                            import
                          
                          
                             com.example.grpcdemo.service.UserResponse;

                          
                          
                            import
                          
                          
                             org.springframework.beans.factory.annotation.Autowired;

                          
                          
                            import
                          
                          
                             org.springframework.web.bind.annotation.RequestMapping;

                          
                          
                            import
                          
                          
                             org.springframework.web.bind.annotation.RestController;
@RestController

                          
                          
                            public
                          
                          
                            class
                          
                          
                             UserController {
    @Autowired
    
                          
                          
                            private
                          
                          
                             UserGrpcClient userGrpcClient;
    
    @RequestMapping(
                          
                          "/addUser"
                          
                            )
    
                          
                          
                            public
                          
                          
                             String addUser(UserRequest request) {
        UserResponse response 
                          
                          =
                          
                             userGrpcClient.addUser(request);
        
                          
                          
                            return
                          
                          
                             response.getMessage();
    }
}
                          
                        

  在这个例子中,我们在 UserController 中注入了 UserGrpcClient ,并在 addUser() 方法中使用该客户端访问远程gRPC服务,处理响应结果并返回给前端.

  。

最后此篇关于使用@GrpcClient实现客户端的文章就讲到这里了,如果你想了解更多关于使用@GrpcClient实现客户端的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com