Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add protobin support from jar #37

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.github.thinkerou</groupId>
<artifactId>test-app-protos</artifactId>
<version>1.0-SNAPSHOT</version>

<name>test-app-protos</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<protobuf.maven.plugin>0.6.1</protobuf.maven.plugin>
<protobuf.version>1.41.0</protobuf.version>
</properties>

<dependencies>
<!-- https://mvnrepository.com/artifact/io.grpc/grpc-stub -->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>${protobuf.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.grpc/grpc-protobuf -->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>${protobuf.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-services</artifactId>
<version>${protobuf.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>${protobuf.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<build>
<finalName>server</finalName>
<extensions>
<!--
The below extension helps determine the OS information so that
we can download the corresponding pre-built binary for the specific OS
when generating code from proto files
-->
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.7.0</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<encoding>UTF-8</encoding>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>${protobuf.maven.plugin}</version>
<configuration>
<writeDescriptorSet>true</writeDescriptorSet>
<protocArtifact>com.google.protobuf:protoc:3.5.1:exe:${os.detected.classifier}
</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.12.0:exe:${os.detected.classifier}
</pluginArtifact>
<protoSourceRoot>src/main/resources</protoSourceRoot>
<descriptorSetFileName>${project.name}.protobin</descriptorSetFileName>
<descriptorSetOutputDirectory>${project.build.outputDirectory}</descriptorSetOutputDirectory>
<includeDependenciesInDescriptorSet>true</includeDependenciesInDescriptorSet>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Helps add the generated code back to the CLASSPATH so that it can be resolved
by your code -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/target/generated-sources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<!-- Run shade goal on package phase -->
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<finalName>server</finalName>
<transformers>
<!-- add Main-Class to manifest file -->
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.github.thinkerou.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.github.thinkerou;

import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.protobuf.services.ProtoReflectionService;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

//Sample format for interaction
//grpcurl --plaintext -d '{"ticker_symbol":"MSFT","company_name":"Microsoft Corp","description":"Microsoft company"}' \
//localhost:8080 com.github.thinkerou.StockQuoteProvider/unaryGetStockQuote

public class LocalGrpcServer {

private final Server server;

public LocalGrpcServer(int port) {
server = ServerBuilder.forPort(port)
.addService(new StockService())
.addService(ProtoReflectionService.newInstance())
.build();
}

public void start() {
try {
server.start();
server.awaitTermination();
}catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}

public void stop() {
try {
server.shutdownNow().awaitTermination(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.thinkerou;

public class Main {

public static void main(String[] args) {
int port = Integer.parseInt(System.getProperty("server.port", "8080"));
LocalGrpcServer server = new LocalGrpcServer(port);
Runnable task = server::stop;
Runtime.getRuntime().addShutdownHook(new Thread(task));
server.start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.github.thinkerou;

import com.github.thinkerou.StockQuoteProviderGrpc.StockQuoteProviderImplBase;
import io.grpc.stub.StreamObserver;
import java.util.concurrent.ThreadLocalRandom;
import java.util.logging.Logger;
import java.util.stream.IntStream;

public class StockService extends StockQuoteProviderImplBase {

private static final Logger logger = Logger.getLogger(StockService.class.getName());

@Override
public void unaryGetStockQuote(Stock request, StreamObserver<StockQuote> responseObserver) {
StockQuote stock = StockQuote.newBuilder()
.setPrice(fetchStockPriceBid(request))
.setOfferNumber(1)
.setDescription("Price for stock:" + request.getTickerSymbol())
.build();
responseObserver.onNext(stock);
responseObserver.onCompleted();
}

@Override
public void serverSideStreamingGetListStockQuotes(Stock request,
StreamObserver<StockQuote> responseObserver) {
IntStream.rangeClosed(1, 5)
.forEach(i -> {
StockQuote stockQuote = StockQuote.newBuilder()
.setPrice(fetchStockPriceBid(request))
.setOfferNumber(i)
.setDescription("Price for stock:" + request.getTickerSymbol())
.build();
responseObserver.onNext(stockQuote);
});
responseObserver.onCompleted();
}

@Override
public StreamObserver<Stock> clientSideStreamingGetStatisticsOfStocks(
StreamObserver<StockQuote> responseObserver) {
return new StreamObserver<Stock>() {
private int count;
private double price = 0.0;
final StringBuffer sb = new StringBuffer();

@Override
public void onNext(Stock stock) {
count++;
price += fetchStockPriceBid(stock);
sb.append(":")
.append(stock.getTickerSymbol());
}

@Override
public void onCompleted() {
responseObserver.onNext(
StockQuote.newBuilder()
.setPrice(price / count)
.setDescription("Statistics:" + sb)
.build()
);
responseObserver.onCompleted();
}

@Override
public void onError(Throwable t) {
logger.warning("error: " + t.getMessage());
}
};
}

@Override
public StreamObserver<Stock> bidirectionalStreamingGetListsStockQuotes(
StreamObserver<StockQuote> responseObserver) {
return new StreamObserver<Stock>() {
@Override
public void onNext(Stock request) {
IntStream.rangeClosed(1, 5)
.forEach(i -> {
StockQuote stockQuote = StockQuote.newBuilder()
.setPrice(fetchStockPriceBid(request))
.setOfferNumber(i)
.setDescription("Price for stock:" + request.getTickerSymbol())
.build();
responseObserver.onNext(stockQuote);
});
}

@Override
public void onError(Throwable t) {
logger.warning("error:" + t.getMessage());
}

@Override
public void onCompleted() {
responseObserver.onCompleted();
}
};
}

private static double fetchStockPriceBid(Stock stock) {
return stock.getTickerSymbol()
.length()
+ ThreadLocalRandom.current()
.nextDouble(-0.1d, 0.1d);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
syntax = "proto3";
package com.github.thinkerou;
option java_multiple_files = true;

import "google/protobuf/struct.proto";

message VerifyRequest {

}

message VerifyResponse {
google.protobuf.Struct someData = 1;
}

service LoginService {
rpc login(VerifyRequest) returns (VerifyResponse);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
syntax = "proto3";
package com.github.thinkerou;
option java_multiple_files = true;

message StockQuote {
double price = 1;
int32 offer_number = 2;
string description = 3;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
syntax = "proto3";
package com.github.thinkerou;
option java_multiple_files = true;

import "stockquotes.proto";

service StockQuoteProvider {

rpc unaryGetStockQuote(Stock) returns (StockQuote) {}

rpc serverSideStreamingGetListStockQuotes(Stock) returns (stream StockQuote) {}

rpc clientSideStreamingGetStatisticsOfStocks(stream Stock) returns (StockQuote) {}

rpc bidirectionalStreamingGetListsStockQuotes(stream Stock) returns (stream StockQuote) {}
}
message Stock {
string ticker_symbol = 1;
string company_name = 2;
string description = 3;
}
18 changes: 18 additions & 0 deletions karate-grpc-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,24 @@
<artifactId>karate-core</artifactId>
<version>${karate.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.classgraph/classgraph -->
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
<version>4.8.132</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.23.1</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private String getFullMethodName() {
/**
* Returns the appropriate method type based on whether the client or server expect streams.
*/
private MethodDescriptor.MethodType getMethodType() {
public MethodDescriptor.MethodType getMethodType() {
boolean clientStreaming = protoMethodDescriptor.toProto().getClientStreaming();
boolean serverStreaming = protoMethodDescriptor.toProto().getServerStreaming();

Expand Down
Loading