Skip to content

Commit

Permalink
add a demo server of trpc (#17)
Browse files Browse the repository at this point in the history
Co-authored-by: rick <[email protected]>
  • Loading branch information
LinuxSuRen and LinuxSuRen authored Jan 4, 2024
1 parent decfaaf commit 622512f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
24 changes: 24 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>17</java.version>
<trpc-maven-plugin.version>1.0.0</trpc-maven-plugin.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand All @@ -38,6 +41,12 @@
<version>2.2.0</version>
</dependency>

<dependency>
<groupId>com.tencent.trpc</groupId>
<artifactId>trpc-mini</artifactId>
<version>1.1.0</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand All @@ -51,6 +60,21 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.tencent.trpc</groupId>
<artifactId>trpc-maven-plugin</artifactId>
<version>${trpc-maven-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>gen-code</goal>
</goals>
</execution>
</executions>
<configuration>
<attachGeneratedFiles>true</attachGeneratedFiles>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
26 changes: 26 additions & 0 deletions src/main/java/io/github/devopsws/demo/service/TRPCService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.github.devopsws.demo.service;

import com.google.protobuf.TextFormat;
import com.tencent.trpc.core.logger.Logger;
import com.tencent.trpc.core.logger.LoggerFactory;
import com.tencent.trpc.core.rpc.RpcContext;
import io.github.devopsws.trpc.demo.proto.GreeterServiceAPI;
import io.github.devopsws.trpc.demo.proto.HelloRequestProtocol;
import org.springframework.stereotype.Service;

@Service
public class TRPCService implements GreeterServiceAPI {

private static final Logger logger = LoggerFactory.getLogger(TRPCService.class);

@Override
public HelloRequestProtocol.HelloResponse sayHello(RpcContext context, HelloRequestProtocol.HelloRequest request) {
logger.error(">>>[server]receive msg:{}", TextFormat.shortDebugString(request));
String message = request.getMessage();
HelloRequestProtocol.HelloResponse.Builder rspBuilder = HelloRequestProtocol.HelloResponse.newBuilder();
rspBuilder.setMessage("Hello " + message);
logger.error(">>>[server]return msg:{}", TextFormat.shortDebugString(rspBuilder));
return rspBuilder.build();
}

}
22 changes: 22 additions & 0 deletions src/main/proto/demo.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
syntax = "proto3";

package trpc.TestApp.TestServer;

option java_package = "io.github.devopsws.trpc.demo.proto";
option java_outer_classname = "HelloRequestProtocol";

message HelloRequest {
string message = 1;
}

message HelloResponse {
string message = 1;
}

service GreeterService {
rpc sayHello (HelloRequest) returns (HelloResponse) {}
}

service GreeterService2 {
rpc sayHi (HelloRequest) returns (HelloResponse) {}
}

0 comments on commit 622512f

Please sign in to comment.