grpc相关使用(三):grpc client端创建

client端创建

创建项目

创建一个基础的spring boot项目
在这里插入图片描述

修改pom文件

第三方依赖版本

 <properties>
        <java.version>1.8</java.version>
        <os-maven-plugin.version>1.6.1</os-maven-plugin.version>
        <protobuf-maven-plugin.version>0.6.1</protobuf-maven-plugin.version>
        <grpc-client-spring-boot-starter>2.4.0.RELEASE</grpc-client-spring-boot-starter>
    </properties>

导入第三方依赖

	    <dependencies>
        <dependency>
            <groupId>net.devh</groupId>
            <artifactId>grpc-client-spring-boot-starter</artifactId>
            <version>${grpc-client-spring-boot-starter}</version>
        </dependency>
        <!--  方便测试 搞一个web服务-->
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-web</artifactId>
          </dependency>
          <dependency>
              <groupId>org.projectlombok</groupId>
              <artifactId>lombok</artifactId>
          </dependency>
      </dependencies>

更改bulid

<build>
		<!--grpc proto文件编译插件-->
		<extensions>
			<extension>
				<groupId>kr.motd.maven</groupId>
				<artifactId>os-maven-plugin</artifactId>
				<version>${os-maven-plugin.version}</version>
			</extension>
		</extensions>

		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<excludes>
						<exclude>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
						</exclude>
					</excludes>
				</configuration>
			</plugin>
			<!--grpc proto文件编译插件-->
			<plugin>
				<groupId>org.xolstice.maven.plugins</groupId>
				<artifactId>protobuf-maven-plugin</artifactId>
				<version>${protobuf-maven-plugin.version}</version>
				<configuration>
					<protocArtifact>com.google.protobuf:protoc:3.5.1-1:exe:${os.detected.classifier}</protocArtifact>
					<pluginId>grpc-java</pluginId>
					<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.16.1:exe:${os.detected.classifier}</pluginArtifact>
				</configuration>
				<executions>
					<execution>
						<goals>
							<goal>compile</goal>
							<goal>compile-custom</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

		</plugins>
	</build>

修改配置文件

grpc:
  client:
    GLOBAL:
      negotiation-type: plaintext
      # 修改客户端端默认入参最大大小,默认值为4M ,这里修改为20M   20*1024*1024
      max-inbound-message-size: 20971520
      # 客户端指定连接服务端地址
      address: 'static://127.0.0.1:8989'

创建proto

创建proto文件夹

在main文件夹下创建proto文件。

创建.proto文件

syntax = "proto3";

package com.example.grpc;

option java_multiple_files = true;
option java_package = "com.example.grpc.lib";
option java_outer_classname = "HelloWorldProto";

// The greeting service definition.
service MyService {
    // Sends a greeting
    rpc SayHello (HelloRequest) returns (HelloReply) {
    }
}

// The request message containing the user's name.
message HelloRequest {
    string name = 1;
}

// The response message containing the greetings
message HelloReply {
    string message = 1;
}

在这里插入图片描述

编译

在这里插入图片描述

如果出现编译异常问题。有可能是yaml文件编码问题,更改成utf-8编码就可以了。
在这里插入图片描述

使用

/**
* grpc 客户端的名称
*/
@GrpcClient(value = “test-client”)
private MyServiceGrpc.MyServiceBlockingStub simpleStub;
其中Mysercie是你.proto中的service 。


/**
 * @author dzp
 * @date 2022/5/29
 */
@Service
public class MyGrpcClientService {
    /**
     * grpc 客户端的名称
     */
    @GrpcClient(value = "test-client")
    private MyServiceGrpc.MyServiceBlockingStub simpleStub;

    public String sendMessage(final String name) {
        try {

            final HelloReply response = simpleStub.sayHello(HelloRequest.newBuilder().setName(name).build());
            return response.getMessage();
        } catch (final StatusRuntimeException e) {
            return "FAILED with " + e.getStatus().getCode().name();
        }
    }

}

验证

编写测试接口

在grpc-client中编写一个简单接口。
创建一个controller文件夹,编写一个TestController.java

@RestController
public class TestController {

    @Resource
    private MyGrpcClientService testService;

    @GetMapping("/test")
    public  Object test(String name){
        return testService.sendMessage(name);
    }
}

分别启动server端和client端,在使用postman进行调用测试。
调用 成功
在这里插入图片描述
在这里插入图片描述

到了这一步,grpc简单使用已经完成。最后我们只需要让甲方提供给 我们proto文件夹 进行编译下, 然后编写我们的业务代码就可以了。

交流群

java交流群: 868794080

参考资料

grpc框架参考资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一枚开发小咸鱼

原创不宜,请作者喝杯咖啡吧。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值