Skip to content

Commit 1b4d133

Browse files
committed
[ADD] gRPC
1 parent ab58dbd commit 1b4d133

File tree

7 files changed

+182
-0
lines changed

7 files changed

+182
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
# Version
66

77
* java 1.8
8+
* kotlin 1.3.31
89
* spring boot 2.1.3.RELEASE

SpringBootGRpc/build.gradle

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
buildscript {
2+
ext {
3+
springBootVersion = '2.1.3.RELEASE'
4+
gRpcVersion = '0.8.8'
5+
}
6+
repositories {
7+
mavenCentral()
8+
}
9+
dependencies {
10+
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
11+
classpath("com.google.protobuf:protobuf-gradle-plugin:${gRpcVersion}")
12+
}
13+
}
14+
15+
16+
plugins {
17+
id "com.google.protobuf" version "0.8.8"
18+
id "java"
19+
}
20+
21+
apply plugin: 'java'
22+
apply plugin: 'eclipse'
23+
apply plugin: 'org.springframework.boot'
24+
apply plugin: 'io.spring.dependency-management'
25+
26+
group = 'com.example'
27+
version = '0.0.1-SNAPSHOT'
28+
sourceCompatibility = 1.8
29+
30+
repositories {
31+
mavenCentral()
32+
}
33+
34+
dependencies {
35+
compile('org.springframework.boot:spring-boot-starter-web')
36+
compile('io.github.lognet:grpc-spring-boot-starter:3.3.0')
37+
compile('com.google.protobuf:protobuf-java-util:3.8.0')
38+
testCompile('org.springframework.boot:spring-boot-starter-test')
39+
}
40+
41+
sourceSets {
42+
main {
43+
java {
44+
srcDirs 'build/generated/source/proto/main/java'
45+
srcDirs 'build/generated/source/proto/main/grpc'
46+
}
47+
}
48+
}
49+
protobuf {
50+
protoc {
51+
path = protocPath
52+
}
53+
plugins {
54+
grpc {
55+
artifact = 'io.grpc:protoc-gen-grpc-java:1.21.0'
56+
}
57+
}
58+
generateProtoTasks {
59+
all()*.plugins {
60+
grpc {}
61+
}
62+
}
63+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.example;
2+
3+
import io.grpc.stub.StreamObserver;
4+
import org.lognet.springboot.grpc.GRpcService;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.boot.SpringApplication;
8+
import org.springframework.boot.autoconfigure.SpringBootApplication;
9+
import org.springframework.util.StringUtils;
10+
11+
@SpringBootApplication
12+
public class SpringBootGRpcApplication {
13+
14+
public static void main(String[] args) {
15+
SpringApplication.run(SpringBootGRpcApplication.class, args);
16+
}
17+
}
18+
19+
@GRpcService
20+
class MessageSenderImpl extends MessageSenderGrpc.MessageSenderImplBase {
21+
22+
private static final Logger logger = LoggerFactory.getLogger(MessageSenderImpl.class);
23+
24+
@Override
25+
public void send(MessageSenderProto.MessageRequest request, StreamObserver<MessageSenderProto.MessageResponse> responseObserver) {
26+
27+
logger.info("send >> request={}", request);
28+
29+
try {
30+
if (StringUtils.isEmpty(request.getContents())) {
31+
throw new IllegalArgumentException("content is empty");
32+
}
33+
34+
MessageSenderProto.MessageResponse response = MessageSenderProto.MessageResponse.newBuilder()
35+
.setStatus("success")
36+
.setReason("ok")
37+
.build();
38+
39+
responseObserver.onNext(response);
40+
} catch (Exception ex) {
41+
responseObserver.onError(ex);
42+
} finally {
43+
responseObserver.onCompleted();
44+
}
45+
46+
}
47+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
syntax = "proto3";
2+
3+
option java_package = "com.example";
4+
option java_outer_classname = "MessageSenderProto";
5+
6+
service MessageSender {
7+
8+
rpc send (MessageRequest) returns (MessageResponse) {}
9+
}
10+
11+
message MessageRequest {
12+
string from = 2;
13+
string to = 3;
14+
string contents = 4;
15+
}
16+
17+
message MessageResponse {
18+
string status = 1;
19+
string reason = 2;
20+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
grpc.port=6565
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.example;
2+
3+
import io.grpc.ManagedChannel;
4+
import io.grpc.ManagedChannelBuilder;
5+
import io.grpc.StatusRuntimeException;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
import org.springframework.boot.test.context.SpringBootTest;
12+
import org.springframework.test.context.junit4.SpringRunner;
13+
14+
@RunWith(SpringRunner.class)
15+
@SpringBootTest
16+
public class SpringBootGRpcApplicationTests {
17+
18+
private static final Logger logger = LoggerFactory.getLogger(SpringBootGRpcApplicationTests.class);
19+
20+
private ManagedChannel channel;
21+
22+
@Before
23+
public void init() {
24+
channel = ManagedChannelBuilder.forAddress("localhost", 6565)
25+
.usePlaintext()
26+
.build();
27+
}
28+
29+
@Test
30+
public void test() {
31+
MessageSenderGrpc.MessageSenderBlockingStub messageSenderBlockingStub = MessageSenderGrpc.newBlockingStub(channel);
32+
33+
MessageSenderProto.MessageRequest messageRequest = MessageSenderProto.MessageRequest.newBuilder()
34+
.setFrom("wonchul")
35+
.setTo("naeun")
36+
.setContents("hello!")
37+
.build();
38+
39+
try {
40+
MessageSenderProto.MessageResponse messageResponse = messageSenderBlockingStub.send(messageRequest);
41+
logger.info("test >> response={}", messageResponse);
42+
} catch (StatusRuntimeException sr) {
43+
logger.error("status={}", sr.getStatus()); // Status{code=UNKNOWN, description=null, cause=null}
44+
logger.error("metadata={}", sr.getTrailers()); // Metadata(content-type=application/grpc)
45+
}
46+
}
47+
}

settings.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ findProject(':SpringBootException')?.name = 'spring-boot-exception'
3939
include 'SpringBootGracefulShutdown'
4040
findProject(':SpringBootGracefulShutdown')?.name = 'spring-boot-graceful-shutdown'
4141

42+
include 'SpringBootGRpc'
43+
findProject(':SpringBootGRpc')?.name = 'spring-boot-grpc'
44+
4245
include 'SpringBootHateoas'
4346
findProject(':SpringBootHateoas')?.name = 'spring-boot-hateoas'
4447

0 commit comments

Comments
 (0)