第三节:grpc1.18.0 在JAVA中调用c++发布的服务

查看

第一节入门:https://blog.csdn.net/shan165310175/article/details/86618932

第二节C++编译helloworld工程:https://blog.csdn.net/shan165310175/article/details/86619128

0. 建立maven工程(使用Idea ide)

1.maven添加依赖和插件:

	<dependencies>
		<dependency>
			<groupId>io.grpc</groupId>
			<artifactId>grpc-netty-shaded</artifactId>
			<version>1.18.0</version>
		</dependency>
		<dependency>
			<groupId>io.grpc</groupId>
			<artifactId>grpc-protobuf</artifactId>
			<version>1.18.0</version>
		</dependency>
		<dependency>
			<groupId>io.grpc</groupId>
			<artifactId>grpc-stub</artifactId>
			<version>1.18.0</version>
		</dependency>

	</dependencies>

	<build>
		<extensions>
			<extension>
				<groupId>kr.motd.maven</groupId>
				<artifactId>os-maven-plugin</artifactId>
				<version>1.5.0.Final</version>
			</extension>
		</extensions>
		<plugins>
			<plugin>
				<groupId>org.xolstice.maven.plugins</groupId>
				<artifactId>protobuf-maven-plugin</artifactId>
				<version>0.5.1</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.17.1:exe:${os.detected.classifier}</pluginArtifact>
				</configuration>
				<executions>
					<execution>
						<goals>
							<goal>compile</goal>
							<goal>compile-custom</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

如果出现插件爆红,下载不下来的,那么将代码贴到dependency中:

<dependency>
   <groupId>org.xolstice.maven.plugins</groupId>
   <artifactId>protobuf-maven-plugin</artifactId>
   <version>0.5.1</version>
</dependency>

下载完之后再删除。

此时插件那边显示:

2. 建立main/proto目录,将helloworld.proto拷贝到目录中

3.双击上面的编译命令编译proto文件,生成如下文件:

4.新建HelloworldClient类,内容如下:

package com.daily;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.StatusRuntimeException;
import io.grpc.examples.helloworld.GreeterGrpc;
import io.grpc.examples.helloworld.HelloReply;
import io.grpc.examples.helloworld.HelloRequest;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

public class HelloWorldClient {
	private static final Logger logger = Logger.getLogger(HelloWorldClient.class.getName());

	private final ManagedChannel channel;
	private final GreeterGrpc.GreeterBlockingStub blockingStub;
//	private final GreeterGrpc.GreeterFutureStub blockingStub;


	/** Construct client connecting to HelloWorld server at {@code host:port}. */
	public HelloWorldClient(String host, int port) {
		this(ManagedChannelBuilder.forAddress(host, port)
				// Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
				// needing certificates.
				.usePlaintext()
				.build());
	}

	/** Construct client for accessing HelloWorld server using the existing channel. */
	HelloWorldClient(ManagedChannel channel) {
		this.channel = channel;
		blockingStub = GreeterGrpc.newBlockingStub(channel);
//		blockingStub = GreeterGrpc.newFutureStub(channel);
	}

	public void shutdown() throws InterruptedException {
		channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
	}

	private ExecutorService threadPool = Executors.newFixedThreadPool(30);

	/** Say hello to server. */
	public void greet(String name) {
		//logger.info("Will try to greet " + name + " ...");
		HelloRequest request = HelloRequest.newBuilder().setName(name).build();
		HelloReply response;
		try {


			HelloReply helloReply = blockingStub.sayHello(request);
			System.out.println(helloReply.getMessage());


		} catch (StatusRuntimeException e) {
			logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
			return;
		}
		//System.out.println("Greeting: " + response.getMessage());
	}

	/**
	 * Greet server. If provided, the first element of {@code args} is the name to use in the
	 * greeting.
	 */
	public static void main(String[] args) throws Exception {

		HelloWorldClient client = new HelloWorldClient("localhost", 50051);
		try {
			for (int i = 0;i < 5;++i) {
				client.greet("小明"+i);
			}
			System.out.println("end>>>>>>>>>>>>>>>>>>>>>>>");
		} finally {
			client.shutdown();
		}

		Thread.sleep(5000);
	}
}

main函数中就是主要调用,该调用是同步的。

注释部分有异步的。blockingStub = GreeterGrpc.newFutureStub(channel);

开启c++服务:

运行java后输出:

 此时java调用C++服务成功。

猜你喜欢

转载自blog.csdn.net/shan165310175/article/details/86619242
今日推荐