Skip to content

Commit

Permalink
refactor quick-start
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielLiu1123 committed Dec 22, 2024
1 parent 08668f3 commit 2c9dfb1
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 30 deletions.
2 changes: 2 additions & 0 deletions examples/quick-start/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ dependencies {

testImplementation(project(":grpc-starters:grpc-starter-test"))
}

apply from: "${rootDir}/gradle/protobuf.gradle"
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);
};
}
}
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();
}
}
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();
}
}
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();
}
}
23 changes: 23 additions & 0 deletions examples/quick-start/src/main/proto/order/v1/order.proto
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) {}
}
22 changes: 22 additions & 0 deletions examples/quick-start/src/main/proto/user/v1/user.proto
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) {}
}
8 changes: 8 additions & 0 deletions examples/quick-start/src/main/resources/application.yaml
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.** ]
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,57 @@

import static org.assertj.core.api.Assertions.assertThat;

import grpcstarter.extensions.test.InProcessName;
import grpcstarter.extensions.test.StubUtil;
import io.grpc.testing.protobuf.SimpleRequest;
import io.grpc.testing.protobuf.SimpleServiceGrpc;
import java.util.List;
import order.v1.GetOrdersByUserIdRequest;
import order.v1.Order;
import order.v1.OrderServiceGrpc;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import user.v1.GetUserRequest;
import user.v1.User;
import user.v1.UserServiceGrpc;

@SpringBootTest(
classes = QuickStartApp.class,
properties = {
"grpc.server.in-process.name=QuickStartAppTest",
"grpc.client.in-process.name=QuickStartAppTest",
"grpc.client.base-packages=io.grpc",
})
class QuickStartAppTest {

@InProcessName
String name;
@Autowired
SimpleServiceGrpc.SimpleServiceBlockingStub simpleStub;

@Autowired
UserServiceGrpc.UserServiceBlockingStub userStub;

@Autowired
OrderServiceGrpc.OrderServiceBlockingStub orderStub;

@Test
void testQuickStart() {
var stub = StubUtil.createStub(name, SimpleServiceGrpc.SimpleServiceBlockingStub.class);
var response = stub.unaryRpc(
SimpleRequest.newBuilder().setRequestMessage("World!").build());
var message = simpleStub
.unaryRpc(SimpleRequest.newBuilder().setRequestMessage("World!").build())
.getResponseMessage();
var expectedMessage = "Hello World!";
assertThat(message).isEqualTo(expectedMessage);

var user =
userStub.getUser(GetUserRequest.newBuilder().setId("1").build()).getUser();
var expectedUser = User.newBuilder().setId("1").setName("Freeman").build();
assertThat(user).isEqualTo(expectedUser);

assertThat(response.getResponseMessage()).isEqualTo("Hello World!");
var orders = orderStub
.getOrdersByUserId(
GetOrdersByUserIdRequest.newBuilder().setUserId("1").build())
.getOrdersList();
var expectedOrders = List.of(
Order.newBuilder().setId("1").setUserId("1").setPrice(100).build(),
Order.newBuilder().setId("2").setUserId("1").setPrice(200).build(),
Order.newBuilder().setId("3").setUserId("1").setPrice(300).build());
assertThat(orders).containsExactlyElementsOf(expectedOrders);
}
}

0 comments on commit 2c9dfb1

Please sign in to comment.