-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
08668f3
commit 2c9dfb1
Showing
9 changed files
with
192 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 27 additions & 21 deletions
48
examples/quick-start/src/main/java/grpcstarter/example/QuickStartApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,45 @@ | ||
package grpcstarter.example; | ||
|
||
import io.grpc.stub.StreamObserver; | ||
import io.grpc.testing.protobuf.SimpleRequest; | ||
import io.grpc.testing.protobuf.SimpleResponse; | ||
import io.grpc.testing.protobuf.SimpleServiceGrpc; | ||
import order.v1.GetOrdersByUserIdRequest; | ||
import order.v1.OrderServiceGrpc; | ||
import org.springframework.boot.ApplicationRunner; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.boot.builder.SpringApplicationBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import user.v1.GetUserRequest; | ||
import user.v1.UserServiceGrpc; | ||
|
||
@SpringBootApplication | ||
public class QuickStartApp extends SimpleServiceGrpc.SimpleServiceImplBase { | ||
public class QuickStartApp { | ||
|
||
public static void main(String[] args) { | ||
new SpringApplicationBuilder(QuickStartApp.class) | ||
.properties("grpc.client.base-packages=io.grpc") | ||
.properties("grpc.client.authority=127.0.0.1:9090") | ||
.run(args); | ||
} | ||
|
||
@Override | ||
public void unaryRpc(SimpleRequest request, StreamObserver<SimpleResponse> r) { | ||
var response = SimpleResponse.newBuilder() | ||
.setResponseMessage("Hello " + request.getRequestMessage()) | ||
.build(); | ||
r.onNext(response); | ||
r.onCompleted(); | ||
SpringApplication.run(QuickStartApp.class, args); | ||
} | ||
|
||
@Bean | ||
ApplicationRunner runner(SimpleServiceGrpc.SimpleServiceBlockingStub stub) { | ||
ApplicationRunner runner( | ||
SimpleServiceGrpc.SimpleServiceBlockingStub simpleStub, | ||
UserServiceGrpc.UserServiceBlockingStub userStub, | ||
OrderServiceGrpc.OrderServiceBlockingStub orderStub) { | ||
return args -> { | ||
var response = stub.unaryRpc( | ||
SimpleRequest.newBuilder().setRequestMessage("World!").build()); | ||
System.out.println(response.getResponseMessage()); | ||
var responseMessage = simpleStub | ||
.unaryRpc(SimpleRequest.newBuilder() | ||
.setRequestMessage("World!") | ||
.build()) | ||
.getResponseMessage(); | ||
System.out.println("Response message: " + responseMessage); | ||
|
||
var user = userStub.getUser(GetUserRequest.newBuilder().setId("1").build()) | ||
.getUser(); | ||
System.out.println("User: " + user); | ||
|
||
var orders = orderStub | ||
.getOrdersByUserId( | ||
GetOrdersByUserIdRequest.newBuilder().setUserId("1").build()) | ||
.getOrdersList(); | ||
System.out.println("Orders: " + orders); | ||
}; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
examples/quick-start/src/main/java/grpcstarter/example/services/OrderServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package grpcstarter.example.services; | ||
|
||
import io.grpc.stub.StreamObserver; | ||
import java.util.List; | ||
import order.v1.GetOrdersByUserIdRequest; | ||
import order.v1.GetOrdersByUserIdResponse; | ||
import order.v1.Order; | ||
import order.v1.OrderServiceGrpc; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class OrderServiceImpl extends OrderServiceGrpc.OrderServiceImplBase { | ||
@Override | ||
public void getOrdersByUserId( | ||
GetOrdersByUserIdRequest request, StreamObserver<GetOrdersByUserIdResponse> responseObserver) { | ||
var orders = List.of( | ||
Order.newBuilder() | ||
.setId("1") | ||
.setUserId(request.getUserId()) | ||
.setPrice(100) | ||
.build(), | ||
Order.newBuilder() | ||
.setId("2") | ||
.setUserId(request.getUserId()) | ||
.setPrice(200) | ||
.build(), | ||
Order.newBuilder() | ||
.setId("3") | ||
.setUserId(request.getUserId()) | ||
.setPrice(300) | ||
.build()); | ||
var response = | ||
GetOrdersByUserIdResponse.newBuilder().addAllOrders(orders).build(); | ||
responseObserver.onNext(response); | ||
responseObserver.onCompleted(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
examples/quick-start/src/main/java/grpcstarter/example/services/SimpleServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package grpcstarter.example.services; | ||
|
||
import io.grpc.stub.StreamObserver; | ||
import io.grpc.testing.protobuf.SimpleRequest; | ||
import io.grpc.testing.protobuf.SimpleResponse; | ||
import io.grpc.testing.protobuf.SimpleServiceGrpc; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class SimpleServiceImpl extends SimpleServiceGrpc.SimpleServiceImplBase { | ||
@Override | ||
public void unaryRpc(SimpleRequest request, StreamObserver<SimpleResponse> responseObserver) { | ||
var response = SimpleResponse.newBuilder() | ||
.setResponseMessage("Hello " + request.getRequestMessage()) | ||
.build(); | ||
responseObserver.onNext(response); | ||
responseObserver.onCompleted(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
examples/quick-start/src/main/java/grpcstarter/example/services/UserServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package grpcstarter.example.services; | ||
|
||
import io.grpc.stub.StreamObserver; | ||
import org.springframework.stereotype.Component; | ||
import user.v1.GetUserRequest; | ||
import user.v1.GetUserResponse; | ||
import user.v1.User; | ||
import user.v1.UserServiceGrpc; | ||
|
||
@Component | ||
public class UserServiceImpl extends UserServiceGrpc.UserServiceImplBase { | ||
@Override | ||
public void getUser(GetUserRequest request, StreamObserver<GetUserResponse> responseObserver) { | ||
var user = User.newBuilder().setId(request.getId()).setName("Freeman").build(); | ||
var response = GetUserResponse.newBuilder().setUser(user).build(); | ||
responseObserver.onNext(response); | ||
responseObserver.onCompleted(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
syntax = "proto3"; | ||
|
||
package order.v1; | ||
|
||
option java_multiple_files = true; | ||
|
||
message Order { | ||
string id = 1; | ||
string user_id = 2; | ||
double price = 3; | ||
} | ||
|
||
message GetOrdersByUserIdRequest { | ||
string user_id = 1; | ||
} | ||
|
||
message GetOrdersByUserIdResponse { | ||
repeated Order orders = 1; | ||
} | ||
|
||
service OrderService { | ||
rpc GetOrdersByUserId(GetOrdersByUserIdRequest) returns (GetOrdersByUserIdResponse) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
syntax = "proto3"; | ||
|
||
package user.v1; | ||
|
||
option java_multiple_files = true; | ||
|
||
message User { | ||
string id = 1; | ||
string name = 2; | ||
} | ||
|
||
message GetUserRequest { | ||
string id = 1; | ||
} | ||
|
||
message GetUserResponse { | ||
User user = 1; | ||
} | ||
|
||
service UserService { | ||
rpc GetUser(GetUserRequest) returns (GetUserResponse) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,12 @@ | ||
grpc: | ||
server: | ||
port: 9090 | ||
reflection: | ||
enabled: true | ||
client: | ||
base-packages: [ io.grpc.testing, user.v1, order.v1 ] | ||
channels: | ||
- authority: localhost:${grpc.server.port} | ||
stubs: [ io.grpc.testing.** ] | ||
- authority: 127.0.0.1:${grpc.server.port} | ||
stubs: [ user.v1.**, order.v1.** ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters