Skip to content

Commit 0e9d5e3

Browse files
committed
Add microservice transactions sample with Shared-cluster pattern with ScalarDB JDBC
1 parent 759ed86 commit 0e9d5e3

File tree

70 files changed

+14759
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+14759
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
subprojects {
2+
group = "sample"
3+
project.version = '1.0'
4+
5+
ext {
6+
scalarDbVersion = '3.13.0'
7+
grpcVersion = '1.65.0'
8+
protobufVersion = '3.24.4'
9+
picoCliVersion = '4.7.5'
10+
log4jVersion = '2.22.1'
11+
annotationVersion = '1.3.2'
12+
}
13+
14+
repositories {
15+
mavenCentral()
16+
}
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
plugins {
2+
id 'java'
3+
id 'application'
4+
}
5+
6+
dependencies {
7+
implementation project(':rpc')
8+
implementation "info.picocli:picocli:${picoCliVersion}"
9+
implementation "com.google.protobuf:protobuf-java-util:${protobufVersion}"
10+
}
11+
12+
application {
13+
mainClassName = 'sample.client.Client'
14+
}
15+
16+
archivesBaseName = "sample-order-service"
17+
18+
sourceCompatibility = 1.8
19+
targetCompatibility = 1.8
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package sample.client;
2+
3+
import picocli.CommandLine;
4+
import picocli.CommandLine.Command;
5+
import picocli.CommandLine.Option;
6+
import sample.client.command.GetCustomerInfoCommand;
7+
import sample.client.command.GetOrderCommand;
8+
import sample.client.command.GetOrdersCommand;
9+
import sample.client.command.PlaceOrderCommand;
10+
import sample.client.command.RepaymentCommand;
11+
12+
@Command(
13+
name = "bin/client",
14+
description = "Sample application for Microservice Transaction",
15+
subcommands = {
16+
PlaceOrderCommand.class,
17+
GetOrderCommand.class,
18+
GetOrdersCommand.class,
19+
GetCustomerInfoCommand.class,
20+
RepaymentCommand.class
21+
})
22+
public class Client implements Runnable {
23+
24+
@Option(
25+
names = {"-h", "--help"},
26+
usageHelp = true,
27+
description = "Displays this help message and quits.",
28+
defaultValue = "true")
29+
private Boolean showHelp;
30+
31+
@Override
32+
public void run() {
33+
if (showHelp) {
34+
CommandLine.usage(this, System.out);
35+
}
36+
}
37+
38+
public static void main(String[] args) {
39+
new CommandLine(new Client()).execute(args);
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package sample.client.command;
2+
3+
import io.grpc.ManagedChannel;
4+
import java.util.concurrent.Callable;
5+
import picocli.CommandLine.Command;
6+
import picocli.CommandLine.Parameters;
7+
import sample.rpc.CustomerServiceGrpc;
8+
import sample.rpc.GetCustomerInfoRequest;
9+
import sample.rpc.GetCustomerInfoResponse;
10+
11+
@Command(name = "GetCustomerInfo", description = "Get customer information")
12+
public class GetCustomerInfoCommand implements Callable<Integer> {
13+
14+
@Parameters(index = "0", paramLabel = "CUSTOMER_ID", description = "customer ID")
15+
private int customerId;
16+
17+
@Override
18+
public Integer call() {
19+
ManagedChannel channel = Utils.getCustomerServiceChannel();
20+
try {
21+
CustomerServiceGrpc.CustomerServiceBlockingStub stub =
22+
CustomerServiceGrpc.newBlockingStub(channel);
23+
GetCustomerInfoResponse response =
24+
stub.getCustomerInfo(
25+
GetCustomerInfoRequest.newBuilder().setCustomerId(customerId).build());
26+
Utils.printJsonString(response);
27+
return 0;
28+
} catch (Exception e) {
29+
e.printStackTrace();
30+
return 1;
31+
} finally {
32+
Utils.shutdownChannel(channel);
33+
}
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package sample.client.command;
2+
3+
import io.grpc.ManagedChannel;
4+
import java.util.concurrent.Callable;
5+
import picocli.CommandLine.Command;
6+
import picocli.CommandLine.Parameters;
7+
import sample.rpc.GetOrderRequest;
8+
import sample.rpc.GetOrderResponse;
9+
import sample.rpc.OrderServiceGrpc;
10+
11+
@Command(name = "GetOrder", description = "Get order information by order ID")
12+
public class GetOrderCommand implements Callable<Integer> {
13+
14+
@Parameters(index = "0", paramLabel = "ORDER_ID", description = "order ID")
15+
private String orderId;
16+
17+
@Override
18+
public Integer call() {
19+
ManagedChannel channel = Utils.getOrderServiceChannel();
20+
try {
21+
OrderServiceGrpc.OrderServiceBlockingStub stub = OrderServiceGrpc.newBlockingStub(channel);
22+
GetOrderResponse response =
23+
stub.getOrder(GetOrderRequest.newBuilder().setOrderId(orderId).build());
24+
Utils.printJsonString(response);
25+
return 0;
26+
} catch (Exception e) {
27+
e.printStackTrace();
28+
return 1;
29+
} finally {
30+
Utils.shutdownChannel(channel);
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package sample.client.command;
2+
3+
import io.grpc.ManagedChannel;
4+
import java.util.concurrent.Callable;
5+
import picocli.CommandLine.Command;
6+
import picocli.CommandLine.Parameters;
7+
import sample.rpc.GetOrdersRequest;
8+
import sample.rpc.GetOrdersResponse;
9+
import sample.rpc.OrderServiceGrpc;
10+
11+
@Command(name = "GetOrders", description = "Get order information by customer ID")
12+
public class GetOrdersCommand implements Callable<Integer> {
13+
14+
@Parameters(index = "0", paramLabel = "CUSTOMER_ID", description = "customer ID")
15+
private int customerId;
16+
17+
@Override
18+
public Integer call() {
19+
ManagedChannel channel = Utils.getOrderServiceChannel();
20+
try {
21+
OrderServiceGrpc.OrderServiceBlockingStub stub = OrderServiceGrpc.newBlockingStub(channel);
22+
GetOrdersResponse response =
23+
stub.getOrders(GetOrdersRequest.newBuilder().setCustomerId(customerId).build());
24+
Utils.printJsonString(response);
25+
return 0;
26+
} catch (Exception e) {
27+
e.printStackTrace();
28+
return 1;
29+
} finally {
30+
Utils.shutdownChannel(channel);
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package sample.client.command;
2+
3+
import io.grpc.ManagedChannel;
4+
import java.util.concurrent.Callable;
5+
import picocli.CommandLine.Command;
6+
import picocli.CommandLine.Parameters;
7+
import sample.rpc.ItemOrder;
8+
import sample.rpc.OrderServiceGrpc;
9+
import sample.rpc.PlaceOrderRequest;
10+
import sample.rpc.PlaceOrderResponse;
11+
12+
@Command(name = "PlaceOrder", description = "Place an order")
13+
public class PlaceOrderCommand implements Callable<Integer> {
14+
15+
@Parameters(index = "0", paramLabel = "CUSTOMER_ID", description = "customer ID")
16+
private int customerId;
17+
18+
@Parameters(
19+
index = "1",
20+
paramLabel = "ORDERS",
21+
description = "orders. The format is \"<Item ID>:<Count>,<Item ID>:<Count>,...\"")
22+
private String orders;
23+
24+
@Override
25+
public Integer call() {
26+
ManagedChannel channel = Utils.getOrderServiceChannel();
27+
try {
28+
OrderServiceGrpc.OrderServiceBlockingStub stub = OrderServiceGrpc.newBlockingStub(channel);
29+
30+
PlaceOrderRequest.Builder builder = PlaceOrderRequest.newBuilder().setCustomerId(customerId);
31+
for (String order : orders.split(",", -1)) {
32+
String[] s = order.split(":", -1);
33+
int itemId = Integer.parseInt(s[0]);
34+
int count = Integer.parseInt(s[1]);
35+
builder.addItemOrder(ItemOrder.newBuilder().setItemId(itemId).setCount(count).build());
36+
}
37+
38+
PlaceOrderResponse response = stub.placeOrder(builder.build());
39+
40+
Utils.printJsonString(response);
41+
return 0;
42+
} catch (Exception e) {
43+
e.printStackTrace();
44+
return 1;
45+
} finally {
46+
Utils.shutdownChannel(channel);
47+
}
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package sample.client.command;
2+
3+
import io.grpc.ManagedChannel;
4+
import java.util.concurrent.Callable;
5+
import picocli.CommandLine.Command;
6+
import picocli.CommandLine.Parameters;
7+
import sample.rpc.CustomerServiceGrpc;
8+
import sample.rpc.RepaymentRequest;
9+
import sample.rpc.RepaymentResponse;
10+
11+
@Command(name = "Repayment", description = "Repayment")
12+
public class RepaymentCommand implements Callable<Integer> {
13+
14+
@Parameters(index = "0", paramLabel = "CUSTOMER_ID", description = "customer ID")
15+
private int customerId;
16+
17+
@Parameters(index = "1", paramLabel = "AMOUNT", description = "amount of the money for repayment")
18+
private int amount;
19+
20+
@Override
21+
public Integer call() {
22+
ManagedChannel channel = Utils.getCustomerServiceChannel();
23+
try {
24+
CustomerServiceGrpc.CustomerServiceBlockingStub stub =
25+
CustomerServiceGrpc.newBlockingStub(channel);
26+
RepaymentResponse response =
27+
stub.repayment(
28+
RepaymentRequest.newBuilder().setCustomerId(customerId).setAmount(amount).build());
29+
Utils.printJsonString(response);
30+
return 0;
31+
} catch (Exception e) {
32+
e.printStackTrace();
33+
return 1;
34+
} finally {
35+
Utils.shutdownChannel(channel);
36+
}
37+
}
38+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package sample.client.command;
2+
3+
import com.google.protobuf.InvalidProtocolBufferException;
4+
import com.google.protobuf.MessageOrBuilder;
5+
import com.google.protobuf.util.JsonFormat;
6+
import io.grpc.ManagedChannel;
7+
import io.grpc.netty.NettyChannelBuilder;
8+
import java.util.concurrent.TimeUnit;
9+
10+
public final class Utils {
11+
12+
private Utils() {}
13+
14+
public static ManagedChannel getCustomerServiceChannel() {
15+
return NettyChannelBuilder.forAddress("localhost", 10010).usePlaintext().build();
16+
}
17+
18+
public static ManagedChannel getOrderServiceChannel() {
19+
return NettyChannelBuilder.forAddress("localhost", 10020).usePlaintext().build();
20+
}
21+
22+
public static void shutdownChannel(ManagedChannel channel) {
23+
try {
24+
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
25+
} catch (InterruptedException e) {
26+
System.err.println("failed to shutdown the channel");
27+
}
28+
}
29+
30+
public static void printJsonString(MessageOrBuilder messageOrBuilder)
31+
throws InvalidProtocolBufferException {
32+
System.out.println(JsonFormat.printer().print(messageOrBuilder));
33+
}
34+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM eclipse-temurin:8-jre-jammy
2+
3+
WORKDIR /
4+
5+
ADD customer-service.tar .
6+
7+
WORKDIR /customer-service
8+
9+
COPY customer-service.properties database.properties
10+
11+
ENTRYPOINT ["./bin/customer-service", "--config", "database.properties"]
12+
13+
EXPOSE 10010

0 commit comments

Comments
 (0)