├── .gitignore
├── samples-facade
├── README.md
├── src
│ └── main
│ │ └── java
│ │ └── com
│ │ └── anoyi
│ │ └── grpc
│ │ └── facade
│ │ ├── entity
│ │ ├── PetEntity.java
│ │ └── UserEntity.java
│ │ └── service
│ │ ├── UserServiceBySofaHessian.java
│ │ ├── UserServiceByFastJSON.java
│ │ └── UserServiceByProtoStuff.java
└── pom.xml
├── spring-boot-starter-grpc
├── README.md
├── src
│ └── main
│ │ ├── resources
│ │ └── META-INF
│ │ │ ├── spring.factories
│ │ │ └── spring-configuration-metadata.json
│ │ ├── java
│ │ └── com
│ │ │ └── anoyi
│ │ │ └── grpc
│ │ │ ├── exception
│ │ │ ├── GrpcException.java
│ │ │ └── GrpcResponseException.java
│ │ │ ├── constant
│ │ │ ├── GrpcResponseStatus.java
│ │ │ └── SerializeType.java
│ │ │ ├── config
│ │ │ ├── RemoteServer.java
│ │ │ ├── GrpcProperties.java
│ │ │ └── GrpcAutoConfiguration.java
│ │ │ ├── util
│ │ │ ├── ClassNameUtils.java
│ │ │ ├── ProtobufUtils.java
│ │ │ └── SerializeUtils.java
│ │ │ ├── service
│ │ │ ├── GrpcRequest.java
│ │ │ ├── SerializeService.java
│ │ │ ├── impl
│ │ │ │ ├── ProtoStuffSerializeService.java
│ │ │ │ ├── FastJSONSerializeService.java
│ │ │ │ └── SofaHessianSerializeService.java
│ │ │ ├── GrpcResponse.java
│ │ │ └── CommonService.java
│ │ │ ├── annotation
│ │ │ ├── GrpcService.java
│ │ │ └── GrpcServiceScan.java
│ │ │ ├── ServerContext.java
│ │ │ ├── GrpcServer.java
│ │ │ ├── binding
│ │ │ └── GrpcServiceProxy.java
│ │ │ └── GrpcClient.java
│ │ └── proto
│ │ └── service.proto
└── pom.xml
├── samples-server
├── src
│ └── main
│ │ ├── resources
│ │ └── application.yml
│ │ └── java
│ │ └── com
│ │ └── anoyi
│ │ └── grpc
│ │ └── server
│ │ ├── Application.java
│ │ ├── controller
│ │ └── V0UserController.java
│ │ └── service
│ │ ├── UserServiceBySofaHessianImpl.java
│ │ ├── UserServiceByProtoStuffImpl.java
│ │ └── UserServiceByFastJSONImpl.java
├── README.md
├── Dockerfile
└── pom.xml
├── samples-client
├── src
│ └── main
│ │ ├── resources
│ │ └── application.yaml
│ │ └── java
│ │ └── com
│ │ └── anoyi
│ │ └── grpc
│ │ └── client
│ │ ├── Application.java
│ │ └── controller
│ │ ├── V2UserController.java
│ │ ├── V1UserController.java
│ │ └── V3UserController.java
├── Dockerfile
├── README.md
└── pom.xml
├── test
├── README.md
├── src
│ └── test
│ │ └── com.anoyi.grpc.test
│ │ ├── V0LocalTest.scala
│ │ ├── V1SofaHessianTest.scala
│ │ ├── V2ProtoStuffTest.scala
│ │ └── V3FastJsonTest.scala
└── pom.xml
├── README.md
├── stack.yml
└── LICENSE
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea
2 |
3 | */**/target
4 |
5 | */**/*.iml
6 |
--------------------------------------------------------------------------------
/samples-facade/README.md:
--------------------------------------------------------------------------------
1 | ### 安装到本地 Maven 仓库
2 |
3 | 1、Maven Install
4 | ```
5 | mvn clean install
6 | ```
--------------------------------------------------------------------------------
/spring-boot-starter-grpc/README.md:
--------------------------------------------------------------------------------
1 | ### 安装到本地 Maven 仓库
2 |
3 | 1、Maven Install
4 | ```
5 | mvn clean install
6 | ```
--------------------------------------------------------------------------------
/samples-server/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 8080
3 |
4 | spring:
5 | grpc:
6 | enable: true
7 | port: 6565
--------------------------------------------------------------------------------
/samples-server/README.md:
--------------------------------------------------------------------------------
1 | ### 构建镜像
2 |
3 | 1、Maven 打包
4 | ```
5 | mvn clean package
6 | ```
7 |
8 | 2、构建 Docker 镜像
9 | ```
10 | docker build -t server .
11 | ```
--------------------------------------------------------------------------------
/spring-boot-starter-grpc/src/main/resources/META-INF/spring.factories:
--------------------------------------------------------------------------------
1 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.anoyi.grpc.config.GrpcAutoConfiguration
--------------------------------------------------------------------------------
/samples-client/src/main/resources/application.yaml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 8081
3 |
4 | spring:
5 | grpc:
6 | remote-servers:
7 | - server: user
8 | host: 127.0.0.1
9 | port: 6565
10 |
--------------------------------------------------------------------------------
/test/README.md:
--------------------------------------------------------------------------------
1 | ### 使用帮助
2 |
3 | 修改测试报告的输出路径 `pom.xml`:
4 | ```
5 |
6 | /Users/admin/code/gatling
7 |
8 | ```
9 |
10 | 执行测试
11 | ```
12 | mvn gatling:test
13 | ```
--------------------------------------------------------------------------------
/spring-boot-starter-grpc/src/main/java/com/anoyi/grpc/exception/GrpcException.java:
--------------------------------------------------------------------------------
1 | package com.anoyi.grpc.exception;
2 |
3 | public class GrpcException extends RuntimeException {
4 |
5 | public GrpcException(String message){
6 | super(message);
7 | }
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/samples-client/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM registry.cn-hangzhou.aliyuncs.com/micro-java/openjdk:8-jre-alpine
2 | MAINTAINER yangyuandong@tezign.com
3 | ENV TZ="Asia/Shanghai" HOME="/root" JVM_PARAMS=" " SPRING_PARAMS=" "
4 | WORKDIR ${HOME}
5 | ADD target/*.jar ${HOME}/ROOT.jar
6 | EXPOSE 8081
7 | CMD java $JVM_PARAMS -Djava.security.egd=file:/dev/./urandom -jar ${HOME}/ROOT.jar $SPRING_PARAMS
--------------------------------------------------------------------------------
/samples-server/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM registry.cn-hangzhou.aliyuncs.com/micro-java/openjdk:8-jre-alpine
2 | MAINTAINER yangyuandong@tezign.com
3 | ENV TZ="Asia/Shanghai" HOME="/root" JVM_PARAMS=" " SPRING_PARAMS=" "
4 | WORKDIR ${HOME}
5 | ADD target/*.jar ${HOME}/ROOT.jar
6 | EXPOSE 8080
7 | CMD java $JVM_PARAMS -Djava.security.egd=file:/dev/./urandom -jar ${HOME}/ROOT.jar $SPRING_PARAMS
--------------------------------------------------------------------------------
/samples-client/README.md:
--------------------------------------------------------------------------------
1 | ### 构建镜像
2 |
3 | 1、修改 src/main/resources/application.yaml 文件为:
4 | ```
5 | server:
6 | port: 8081
7 |
8 | spring:
9 | grpc:
10 | remote-servers:
11 | - server: user
12 | host: server
13 | port: 6565
14 | ```
15 |
16 | 2、Maven 打包
17 | ```
18 | mvn clean package
19 | ```
20 |
21 | 3、构建 Docker 镜像
22 | ```
23 | docker build -t client .
24 | ```
--------------------------------------------------------------------------------
/spring-boot-starter-grpc/src/main/java/com/anoyi/grpc/constant/GrpcResponseStatus.java:
--------------------------------------------------------------------------------
1 | package com.anoyi.grpc.constant;
2 |
3 | public enum GrpcResponseStatus {
4 |
5 | SUCCESS(0), ERROR(-1);
6 |
7 | private int code;
8 |
9 | GrpcResponseStatus(int code) {
10 | this.code = code;
11 | }
12 |
13 | public int getCode() {
14 | return code;
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/samples-server/src/main/java/com/anoyi/grpc/server/Application.java:
--------------------------------------------------------------------------------
1 | package com.anoyi.grpc.server;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class Application {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(Application.class, args);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 轻量级 RPC 框架
2 |
3 | Spring Boot 快速集成 gRPC,轻松实现远程方法调用。
4 |
5 | 
6 |
7 | ### 使用文档
8 |
9 | [新人入门文档](https://github.com/ChinaSilence/spring-boot-starter-grpc/wiki)
10 |
11 | ### 版本信息
12 |
13 | 名称|版本
14 | --|--
15 | grpc-java|1.33.1
16 | spring-boot|2.4.0.RELEASE
17 | sofa-hessian|3.3.12
18 | fastjson|1.2.75
19 |
--------------------------------------------------------------------------------
/samples-facade/src/main/java/com/anoyi/grpc/facade/entity/PetEntity.java:
--------------------------------------------------------------------------------
1 | package com.anoyi.grpc.facade.entity;
2 |
3 | import lombok.Data;
4 |
5 | import java.io.Serializable;
6 |
7 | /**
8 | * 宠物
9 | */
10 | @Data
11 | public class PetEntity implements Serializable {
12 |
13 | private static final long serialVersionUID = 0L;
14 |
15 | private String type;
16 |
17 | private String name;
18 |
19 | private UserEntity owner;
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/spring-boot-starter-grpc/src/main/java/com/anoyi/grpc/config/RemoteServer.java:
--------------------------------------------------------------------------------
1 | package com.anoyi.grpc.config;
2 |
3 | import lombok.Data;
4 |
5 | /**
6 | * 远程服务
7 | */
8 | @Data
9 | public class RemoteServer {
10 |
11 | /**
12 | * 服务名
13 | */
14 | private String server;
15 |
16 | /**
17 | * 主机地址
18 | */
19 | private String host;
20 |
21 | /**
22 | * 服务端口号
23 | */
24 | private int port;
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/samples-facade/src/main/java/com/anoyi/grpc/facade/service/UserServiceBySofaHessian.java:
--------------------------------------------------------------------------------
1 | package com.anoyi.grpc.facade.service;
2 |
3 |
4 | import com.anoyi.grpc.annotation.GrpcService;
5 | import com.anoyi.grpc.facade.entity.UserEntity;
6 |
7 | import java.util.List;
8 |
9 | @GrpcService(server = "user")
10 | public interface UserServiceBySofaHessian {
11 |
12 | void insert(UserEntity userEntity);
13 |
14 | void deleteById(Long id);
15 |
16 | List findAll();
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/stack.yml:
--------------------------------------------------------------------------------
1 | version: '3.7'
2 | services:
3 | server:
4 | image: "server:latest"
5 | networks:
6 | - idp_network
7 | deploy:
8 | replicas: 2
9 | endpoint_mode: dnsrr
10 | client:
11 | image: "client:latest"
12 | networks:
13 | - idp_network
14 | ports:
15 | - target: 8081
16 | published: 8081
17 | protocol: tcp
18 | mode: host
19 | deploy:
20 | mode: global
21 |
22 | networks:
23 | idp_network:
24 | external: true
--------------------------------------------------------------------------------
/samples-client/src/main/java/com/anoyi/grpc/client/Application.java:
--------------------------------------------------------------------------------
1 | package com.anoyi.grpc.client;
2 |
3 | import com.anoyi.grpc.annotation.GrpcServiceScan;
4 | import org.springframework.boot.SpringApplication;
5 | import org.springframework.boot.autoconfigure.SpringBootApplication;
6 |
7 | @SpringBootApplication
8 | @GrpcServiceScan(packages = {"com.anoyi.grpc.facade"})
9 | public class Application {
10 |
11 | public static void main(String[] args) {
12 | SpringApplication.run(Application.class, args);
13 | }
14 |
15 | }
--------------------------------------------------------------------------------
/spring-boot-starter-grpc/src/main/proto/service.proto:
--------------------------------------------------------------------------------
1 | syntax = "proto3";
2 |
3 | option java_package = "com.anoyi.rpc";
4 | option java_outer_classname = "GrpcService";
5 | option java_multiple_files = false;
6 |
7 | // 定义通用的 Grpc 服务
8 | service CommonService {
9 | // 处理请求
10 | rpc handle ( Request ) returns ( Response ) {}
11 | }
12 |
13 | // 定义通用的 Grpc 请求体
14 | message Request {
15 | int32 serialize = 1;
16 | bytes request = 2;
17 | }
18 |
19 | // 定义通用的 Grpc 响应体
20 | message Response {
21 | bytes response = 1;
22 | }
--------------------------------------------------------------------------------
/spring-boot-starter-grpc/src/main/java/com/anoyi/grpc/util/ClassNameUtils.java:
--------------------------------------------------------------------------------
1 | package com.anoyi.grpc.util;
2 |
3 | public class ClassNameUtils {
4 |
5 | /**
6 | * 将 Class 全限定名转化为 beanName
7 | *
8 | * 比如:com.anoyi.service.UserService -> userService
9 | */
10 | public static String beanName(String className){
11 | String[] path = className.split("\\.");
12 | String beanName = path[path.length - 1];
13 | return Character.toLowerCase(beanName.charAt(0)) + beanName.substring(1);
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/spring-boot-starter-grpc/src/main/java/com/anoyi/grpc/service/GrpcRequest.java:
--------------------------------------------------------------------------------
1 | package com.anoyi.grpc.service;
2 |
3 | import lombok.Data;
4 |
5 | import java.io.Serializable;
6 |
7 | @Data
8 | public class GrpcRequest implements Serializable {
9 |
10 | private static final long serialVersionUID = 4729940126314117605L;
11 |
12 | /**
13 | * 接口
14 | */
15 | private String clazz;
16 |
17 | /**
18 | * 方法
19 | */
20 | private String method;
21 |
22 | /**
23 | * service 方法参数
24 | */
25 | private Object[] args;
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/samples-facade/src/main/java/com/anoyi/grpc/facade/service/UserServiceByFastJSON.java:
--------------------------------------------------------------------------------
1 | package com.anoyi.grpc.facade.service;
2 |
3 |
4 | import com.anoyi.grpc.annotation.GrpcService;
5 | import com.anoyi.grpc.constant.SerializeType;
6 | import com.anoyi.grpc.facade.entity.UserEntity;
7 |
8 | import java.util.List;
9 |
10 | @GrpcService(server = "user", serialization = SerializeType.FASTJSON)
11 | public interface UserServiceByFastJSON {
12 |
13 | void insert(String userEntityJson);
14 |
15 | void deleteById(String id);
16 |
17 | List findAll();
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/samples-facade/src/main/java/com/anoyi/grpc/facade/service/UserServiceByProtoStuff.java:
--------------------------------------------------------------------------------
1 | package com.anoyi.grpc.facade.service;
2 |
3 |
4 | import com.anoyi.grpc.annotation.GrpcService;
5 | import com.anoyi.grpc.constant.SerializeType;
6 | import com.anoyi.grpc.facade.entity.UserEntity;
7 |
8 | import java.util.List;
9 |
10 | @GrpcService(server = "user", serialization = SerializeType.PROTOSTUFF)
11 | public interface UserServiceByProtoStuff {
12 |
13 | void insert(UserEntity userEntity);
14 |
15 | void deleteById(Long id);
16 |
17 | List findAll();
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/spring-boot-starter-grpc/src/main/java/com/anoyi/grpc/exception/GrpcResponseException.java:
--------------------------------------------------------------------------------
1 | package com.anoyi.grpc.exception;
2 |
3 | import com.anoyi.grpc.service.GrpcResponse;
4 |
5 | public class GrpcResponseException extends RuntimeException {
6 |
7 | private GrpcResponse response;
8 |
9 | public GrpcResponseException(GrpcResponse response){
10 | super(response.getMessage());
11 | this.response = response;
12 | }
13 |
14 |
15 | public GrpcResponse getResponse() {
16 | return response;
17 | }
18 |
19 | public void setResponse(GrpcResponse response) {
20 | this.response = response;
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/spring-boot-starter-grpc/src/main/java/com/anoyi/grpc/annotation/GrpcService.java:
--------------------------------------------------------------------------------
1 | package com.anoyi.grpc.annotation;
2 |
3 | import com.anoyi.grpc.constant.SerializeType;
4 |
5 | import java.lang.annotation.Documented;
6 | import java.lang.annotation.Inherited;
7 | import java.lang.annotation.Retention;
8 |
9 | import static java.lang.annotation.RetentionPolicy.RUNTIME;
10 |
11 | @Documented
12 | @Inherited
13 | @Retention(RUNTIME)
14 | public @interface GrpcService {
15 |
16 | /**
17 | * 远程服务名
18 | */
19 | String server() default "";
20 |
21 | /**
22 | * 序列化工具实现类
23 | */
24 | SerializeType[] serialization() default {};
25 |
26 | }
--------------------------------------------------------------------------------
/spring-boot-starter-grpc/src/main/java/com/anoyi/grpc/service/SerializeService.java:
--------------------------------------------------------------------------------
1 | package com.anoyi.grpc.service;
2 |
3 | import com.anoyi.rpc.GrpcService;
4 | import com.google.protobuf.ByteString;
5 |
6 | public interface SerializeService {
7 |
8 | /**
9 | * 序列化
10 | */
11 | ByteString serialize(GrpcResponse response);
12 |
13 | /**
14 | * 序列化
15 | */
16 | ByteString serialize(GrpcRequest request);
17 |
18 | /**
19 | * 反序列化
20 | */
21 | GrpcRequest deserialize(GrpcService.Request request);
22 |
23 | /**
24 | * 反序列化
25 | */
26 | GrpcResponse deserialize(GrpcService.Response response);
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/samples-facade/src/main/java/com/anoyi/grpc/facade/entity/UserEntity.java:
--------------------------------------------------------------------------------
1 | package com.anoyi.grpc.facade.entity;
2 |
3 | import lombok.Data;
4 |
5 | import java.io.Serializable;
6 | import java.util.List;
7 | import java.util.Map;
8 |
9 | /**
10 | * 人物
11 | */
12 | @Data
13 | public class UserEntity implements Serializable {
14 |
15 | private static final long serialVersionUID = 0L;
16 |
17 | private Long id;
18 |
19 | private String name;
20 |
21 | private int age;
22 |
23 | private String gender;
24 |
25 | private Map scores;
26 |
27 | private UserEntity friend;
28 |
29 | private PetEntity pet;
30 |
31 | private List