{"id":15024557,"url":"https://github.com/nity-io/grpc-spring-boot-project","last_synced_at":"2025-04-12T06:32:04.594Z","repository":{"id":36448510,"uuid":"158718248","full_name":"nity-io/grpc-spring-boot-project","owner":"nity-io","description":"Spring Boot Project for gRPC包含gRPC Server Spring Boot Starter、gRPC Client Spring Boot Starter及相关示例","archived":false,"fork":false,"pushed_at":"2022-01-21T23:23:14.000Z","size":197,"stargazers_count":13,"open_issues_count":2,"forks_count":7,"subscribers_count":0,"default_branch":"master","last_synced_at":"2024-10-31T10:42:58.480Z","etag":null,"topics":["grpc","spring-boot","springboot","springbootstarter"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nity-io.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-11-22T15:26:15.000Z","updated_at":"2023-06-21T06:10:28.000Z","dependencies_parsed_at":"2022-08-08T15:00:32.157Z","dependency_job_id":null,"html_url":"https://github.com/nity-io/grpc-spring-boot-project","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nity-io%2Fgrpc-spring-boot-project","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nity-io%2Fgrpc-spring-boot-project/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nity-io%2Fgrpc-spring-boot-project/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nity-io%2Fgrpc-spring-boot-project/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nity-io","download_url":"https://codeload.github.com/nity-io/grpc-spring-boot-project/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223502361,"owners_count":17155938,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["grpc","spring-boot","springboot","springbootstarter"],"created_at":"2024-09-24T20:00:32.493Z","updated_at":"2024-11-07T11:04:59.056Z","avatar_url":"https://github.com/nity-io.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"gRPC Spring Boot Project\n========================================\n\n优雅的集成gRPC到Spring Boot项目。分[grpc-server-spring-boot-starter](grpc-server-spring-boot-starter)、[grpc-client-spring-boot-starter](grpc-client-spring-boot-starter)。\n\n支持四种模式：\n\n- inProcess 进程内模式，只使用在测试和功能演示场景\n- simple 明文模式，可使用在内网微服务\n- tls TLS模式，服务端、客户端使用证书保证通信安全，可对公网提供服务\n- custom 自定义模式，在以上模式不满足要求的情况下，可以对服务端、客户端进行自定义\n\n----------\n## 示例\n\n- [InProcess](grpc-spring-boot-samples/grpc-spring-boot-sample-inprocess)\n\n- [Server Simple](grpc-spring-boot-samples/grpc-spring-boot-sample-server-simple)\n\n- [Client Simple](grpc-spring-boot-samples/grpc-spring-boot-sample-client-simple)\n\n- [Server Tls](grpc-spring-boot-samples/grpc-spring-boot-sample-server-tls)\n\n- [Client Tls](grpc-spring-boot-samples/grpc-spring-boot-sample-client-tls)\n\n- [Server Custom](grpc-spring-boot-samples/grpc-spring-boot-sample-server-custom)\n\n- [Client Custom](grpc-spring-boot-samples/grpc-spring-boot-sample-client-custom)\n\n\n#### 生成Tls测试证书\n\n```\n$ sh grpc-spring-boot-samples/tools/create_openssl_key.sh\n```\n\n证书默认生成在/tmp/sslcert，可修改脚本自定义\n\n----------\n## 使用\n\n### Server\n\n##### Maven\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eio.nity.grpc\u003c/groupId\u003e\n    \u003cartifactId\u003egrpc-server-spring-boot-starter\u003c/artifactId\u003e\n    \u003cversion\u003e1.0.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n##### Gradle\n```gradle\ncompile 'io.nity.grpc:grpc-server-spring-boot-starter:1.0.0'\n```\n\n##### application.properties\n```properties\ngrpc.server.enableReflection=true\ngrpc.server.model=simple\ngrpc.server.host=localhost\ngrpc.server.port=50440\n```\n\n##### Java\n```java\n@GrpcService\npublic class GreeterGrpcService extends GreeterGrpc.GreeterImplBase {\n\n    @Override\n    public void sayHello(HelloRequest request, StreamObserver\u003cHelloReply\u003e responseObserver) {\n        //grpc service implement code\n    }\n\n}\n```\n\n#### 如果需要对Server进一步配置\n1.可创建GrpcServerBuilderConfigurer bean，在configure里对创建好的serverBuilder进一步配置\n```java\n@Configuration\npublic class GrpcCustomConfig {\n\n    @Bean\n    public GrpcServerBuilderConfigurer serverBuilderConfigurer() {\n        return serverBuilder -\u003e {\n            //sample code\n            //serverBuilder.maxInboundMessageSize()\n        };\n    }\n}\n```\n\n2.使用custom模式，创建ServerBuilder bean，根据需要对serverBuilder进行配置\n```java\n@Configuration\npublic class GrpcCustomConfig {\n    \n    @Bean\n    @ConditionalOnProperty(value = \"grpc.server.model\", havingValue = GrpcServerProperties.SERVER_MODEL_CUSTOM)\n    public ServerBuilder getServerBuilder() {\n        ServerBuilder\u003c?\u003e serverBuilder;\n        \n        //create and config serverBuilder\n        \n        return serverBuilder;\n    }\n\n}\n```\n\n### Client\n\n##### Maven\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eio.nity.grpc\u003c/groupId\u003e\n    \u003cartifactId\u003egrpc-client-spring-boot-starter\u003c/artifactId\u003e\n    \u003cversion\u003e1.0.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n##### Gradle\n```gradle\ncompile 'io.nity.grpc:grpc-client-spring-boot-starter:1.0.0'\n```\n\n##### application.properties\n```properties\ngrpc.client.default.model=simple\ngrpc.client.default.host=localhost\ngrpc.client.default.port=50440\n```\n\n##### Java\n```java\n@RestController\npublic class GreeterController {\n\n    @GrpcClient(\"default\")\n    private GreeterGrpc.GreeterBlockingStub greeterBlockingStub;\n\n    @RequestMapping(value = {\"/greet\"})\n    public String greet() {\n        //code...\n        response = greeterBlockingStub.sayHello(request);\n        //code...\n    }\n\n}\n```\n\n#### 如果需要对Client进一步配置\n1.可创建GrpcChannelBuilderConfigurer bean，在configure里对创建好的channelBuilder进一步配置\n```java\n@Configuration\npublic class GrpcClientConfig {\n\n    @Bean\n    public GrpcChannelBuilderConfigurer channelBuilderConfigurer() {\n        return (channelBuilder, name) -\u003e {\n            log.info(\"configure channelBuilder...\");\n            //channelBuilder.loadBalancerFactory(something);\n            //etc.\n        };\n    }\n\n}\n```\n\n2.可创建GrpcChannelConfigurer bean，在configure里对创建好的channel进一步配置\n```java\n@Configuration\npublic class GrpcClientConfig {\n\n    @Bean\n    public GrpcChannelConfigurer channelConfigurer() {\n        return (channel, name) -\u003e {\n            log.info(\"configure channel...\");\n            //ClientInterceptors.intercept(channel, interceptors);\n            //etc.\n        };\n    }\n}\n```\n\n3.可使用custom模式，创建CustomChannelFactory bean，实现ChannelBuilder的创建逻辑\n```java\n@Configuration\npublic class GrpcClientConfig {\n\n    @Bean\n    public CustomChannelFactory customChannelFactory(final GrpcClientPropertiesMap clientPropertiesMap,\n                                                     final GrpcChannelBuilderConfigurer channelBuilderConfigurer,\n                                                     final GrpcChannelConfigurer channelConfigurer) {\n        return new CustomChannelFactory(clientPropertiesMap, channelBuilderConfigurer, channelConfigurer) {\n            @Override\n            protected ManagedChannelBuilder newChannelBuilder(final String name, final GrpcClientProperties clientProperties) {\n                ManagedChannelBuilder\u003c?\u003e managedChannelBuilder;\n                //create and config ChannelBuilder\n                return managedChannelBuilder;\n            }\n        };\n    }\n\n}\n```\n\n### Server + Client\n##### Maven\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eio.nity.grpc\u003c/groupId\u003e\n    \u003cartifactId\u003egrpc-spring-boot-starter\u003c/artifactId\u003e\n    \u003cversion\u003e1.0.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n##### Gradle\n```gradle\ncompile 'io.nity.grpc:grpc-spring-boot-starter:1.0.0'\n```\n\n#### Snapshots仓库\nhttps://oss.sonatype.org/content/repositories/snapshots/\n\n具体的代码请参照各示例模块。\n\n----------\n## License\n[Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.html) Copyright (C) Apache Software Foundation\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnity-io%2Fgrpc-spring-boot-project","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnity-io%2Fgrpc-spring-boot-project","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnity-io%2Fgrpc-spring-boot-project/lists"}