├── .gitignore ├── LICENSE ├── README.md ├── grpc-client-autoconfigurer ├── pom.xml └── src │ └── main │ ├── java │ └── org │ │ └── springframework │ │ └── boot │ │ └── autoconfigure │ │ └── grpc │ │ └── client │ │ ├── AddressChannelFactory.java │ │ ├── DiscoveryClientChannelFactory.java │ │ ├── DiscoveryClientNameResolver.java │ │ ├── DiscoveryClientResolverFactory.java │ │ ├── GrpcChannelFactory.java │ │ ├── GrpcChannelProperties.java │ │ ├── GrpcChannelsProperties.java │ │ ├── GrpcClientAutoConfiguration.java │ │ └── package-info.java │ └── resources │ └── META-INF │ └── spring.factories ├── grpc-client-starter ├── pom.xml └── src │ └── main │ └── resources │ └── META-INF │ └── spring.provides ├── grpc-parent ├── pom.xml └── src │ └── checkstyle │ ├── checkstyle-header.txt │ ├── checkstyle-suppressions.xml │ └── checkstyle.xml ├── grpc-server-autoconfigurer ├── pom.xml └── src │ └── main │ ├── java │ └── org │ │ └── springframework │ │ └── boot │ │ └── autoconfigure │ │ └── grpc │ │ └── server │ │ ├── AnnotationGrpcServiceDiscoverer.java │ │ ├── DefaultGrpcServerFactory.java │ │ ├── GrpcServerAutoConfiguration.java │ │ ├── GrpcServerFactory.java │ │ ├── GrpcServerLifecycle.java │ │ ├── GrpcServerProperties.java │ │ ├── GrpcService.java │ │ ├── GrpcServiceDefinition.java │ │ ├── GrpcServiceDiscoverer.java │ │ └── package-info.java │ └── resources │ └── META-INF │ └── spring.factories ├── grpc-server-parent └── pom.xml ├── grpc-server-starter ├── pom.xml └── src │ └── main │ └── resources │ └── META-INF │ └── spring.provides └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | ### Eclipse template 2 | 3 | .metadata 4 | bin/ 5 | tmp/ 6 | *.tmp 7 | *.bak 8 | *.swp 9 | *~.nib 10 | local.properties 11 | .settings/ 12 | .loadpath 13 | .recommenders 14 | 15 | # Eclipse Core 16 | .project 17 | 18 | # External tool builders 19 | .externalToolBuilders/ 20 | 21 | # Locally stored "Eclipse launch configurations" 22 | *.launch 23 | 24 | # PyDev specific (Python IDE for Eclipse) 25 | *.pydevproject 26 | 27 | # CDT-specific (C/C++ Development Tooling) 28 | .cproject 29 | 30 | # JDT-specific (Eclipse Java Development Tools) 31 | .classpath 32 | 33 | # Java annotation processor (APT) 34 | .factorypath 35 | 36 | # PDT-specific (PHP Development Tools) 37 | .buildpath 38 | 39 | # sbteclipse plugin 40 | .target 41 | 42 | # Tern plugin 43 | .tern-project 44 | 45 | # TeXlipse plugin 46 | .texlipse 47 | 48 | # STS (Spring Tool Suite) 49 | .springBeans 50 | 51 | # Code Recommenders 52 | .recommenders/ 53 | ### NetBeans template 54 | *.iml 55 | .idea 56 | nbproject/private/ 57 | build/ 58 | nbbuild/ 59 | dist/ 60 | nbdist/ 61 | nbactions.xml 62 | .nb-gradle/ 63 | ### JetBrains template 64 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm 65 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 66 | 67 | # User-specific stuff: 68 | .idea/workspace.xml 69 | .idea/tasks.xml 70 | .idea/dictionaries 71 | .idea/vcs.xml 72 | .idea/jsLibraryMappings.xml 73 | 74 | # Sensitive or high-churn files: 75 | .idea/dataSources.ids 76 | .idea/dataSources.xml 77 | .idea/dataSources.local.xml 78 | .idea/sqlDataSources.xml 79 | .idea/dynamic.xml 80 | .idea/uiDesigner.xml 81 | 82 | # Gradle: 83 | .idea/gradle.xml 84 | .idea/libraries 85 | 86 | # Mongo Explorer plugin: 87 | .idea/mongoSettings.xml 88 | 89 | ## File-based project format: 90 | *.iws 91 | 92 | ## Plugin-specific files: 93 | 94 | # IntelliJ 95 | /out/ 96 | 97 | # mpeltonen/sbt-idea plugin 98 | .idea_modules/ 99 | 100 | # JIRA plugin 101 | atlassian-ide-plugin.xml 102 | 103 | # Crashlytics plugin (for Android Studio and IntelliJ) 104 | com_crashlytics_export_strings.xml 105 | crashlytics.properties 106 | crashlytics-build.properties 107 | fabric.properties 108 | ### Java template 109 | *.class 110 | 111 | # Mobile Tools for Java (J2ME) 112 | .mtj.tmp/ 113 | 114 | # Package Files # 115 | *.jar 116 | *.war 117 | *.ear 118 | 119 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 120 | hs_err_pid* 121 | 122 | # Created by .ignore support plugin (hsz.mobi) 123 | 124 | target 125 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Example gRPC Spring Boot Starter 2 | ================================ 3 | This is not an official Google product. 4 | -------------------------------------------------------------------------------- /grpc-client-autoconfigurer/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 20 | 4.0.0 21 | 22 | 23 | com.github.saturnism.spring-boot-starter-grpc 24 | grpc-parent 25 | master-SNAPSHOT 26 | ../grpc-parent/pom.xml 27 | 28 | grpc-client-autoconfigurer 29 | Spring Boot gRPC Client Autoconfigurer 30 | Spring Boot gRPC Client Autoconfigurer 31 | http://projects.spring.io/spring-boot/ 32 | 33 | gRPC 34 | http://www.grpc.io 35 | 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-autoconfigure 40 | 41 | 42 | org.springframework.cloud 43 | spring-cloud-commons 44 | true 45 | 46 | 47 | io.grpc 48 | grpc-netty-shaded 49 | true 50 | 51 | 52 | io.grpc 53 | grpc-protobuf 54 | true 55 | 56 | 57 | io.grpc 58 | grpc-stub 59 | true 60 | 61 | 62 | org.springframework.boot 63 | spring-boot-configuration-processor 64 | true 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /grpc-client-autoconfigurer/src/main/java/org/springframework/boot/autoconfigure/grpc/client/AddressChannelFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package org.springframework.boot.autoconfigure.grpc.client; 19 | 20 | import io.grpc.Channel; 21 | import io.grpc.ManagedChannelBuilder; 22 | 23 | /** 24 | * Created by rayt on 5/17/16. 25 | */ 26 | public class AddressChannelFactory implements GrpcChannelFactory { 27 | private final GrpcChannelsProperties channels; 28 | public AddressChannelFactory(GrpcChannelsProperties channels) { 29 | this.channels = channels; 30 | } 31 | 32 | @Override 33 | public Channel createChannel(String name) { 34 | GrpcChannelProperties channel = channels.getChannels().get(name); 35 | return ManagedChannelBuilder. 36 | forAddress(channel.getHost(), channel.getPort()). 37 | usePlaintext(channel.isPlaintext()).build(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /grpc-client-autoconfigurer/src/main/java/org/springframework/boot/autoconfigure/grpc/client/DiscoveryClientChannelFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package org.springframework.boot.autoconfigure.grpc.client; 19 | 20 | import io.grpc.Channel; 21 | import io.grpc.ManagedChannel; 22 | import io.grpc.ManagedChannelBuilder; 23 | import io.grpc.util.RoundRobinLoadBalancerFactory; 24 | import org.springframework.cloud.client.discovery.DiscoveryClient; 25 | 26 | /** 27 | * Created by rayt on 5/17/16. 28 | */ 29 | public class DiscoveryClientChannelFactory implements GrpcChannelFactory { 30 | private final GrpcChannelsProperties channels; 31 | private final DiscoveryClient client; 32 | 33 | public DiscoveryClientChannelFactory(GrpcChannelsProperties channels, DiscoveryClient client) { 34 | this.channels = channels; 35 | this.client = client; 36 | } 37 | 38 | @Override 39 | public Channel createChannel(String name) { 40 | RoundRobinLoadBalancerFactory instance = RoundRobinLoadBalancerFactory.getInstance(); 41 | ManagedChannelBuilder builder = ManagedChannelBuilder.forTarget("spring://" + name) 42 | .nameResolverFactory(new DiscoveryClientResolverFactory(client)) 43 | .loadBalancerFactory(instance); 44 | 45 | if (channels.getChannels().containsKey(name)) { 46 | if (channels.getChannels().get(name).isPlaintext()) { 47 | builder.usePlaintext(); 48 | } 49 | } else { 50 | builder.usePlaintext(); 51 | } 52 | 53 | return builder.build(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /grpc-client-autoconfigurer/src/main/java/org/springframework/boot/autoconfigure/grpc/client/DiscoveryClientNameResolver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package org.springframework.boot.autoconfigure.grpc.client; 19 | 20 | import io.grpc.Attributes; 21 | import io.grpc.EquivalentAddressGroup; 22 | import io.grpc.NameResolver; 23 | import org.springframework.cloud.client.ServiceInstance; 24 | import org.springframework.cloud.client.discovery.DiscoveryClient; 25 | 26 | import java.net.InetSocketAddress; 27 | import java.util.ArrayList; 28 | import java.util.List; 29 | 30 | /** 31 | * Created by rayt on 5/17/16. 32 | */ 33 | public class DiscoveryClientNameResolver extends NameResolver { 34 | private final String name; 35 | private final DiscoveryClient client; 36 | private final Attributes attributes; 37 | private Listener listener; 38 | 39 | public DiscoveryClientNameResolver(String name, DiscoveryClient client, Attributes attributes) { 40 | this.name = name; 41 | this.client = client; 42 | this.attributes = attributes; 43 | } 44 | @Override 45 | public String getServiceAuthority() { 46 | return name; 47 | } 48 | 49 | @Override 50 | public void start(Listener listener) { 51 | this.listener = listener; 52 | refresh(); 53 | } 54 | 55 | @Override 56 | public void refresh() { 57 | List servers = new ArrayList<>(); 58 | for (ServiceInstance serviceInstance : client.getInstances(name)) { 59 | servers.add(new EquivalentAddressGroup(new InetSocketAddress(serviceInstance.getHost(), serviceInstance.getPort()))); 60 | } 61 | this.listener.onAddresses(servers, Attributes.EMPTY); 62 | } 63 | 64 | @Override 65 | public void shutdown() { 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /grpc-client-autoconfigurer/src/main/java/org/springframework/boot/autoconfigure/grpc/client/DiscoveryClientResolverFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package org.springframework.boot.autoconfigure.grpc.client; 19 | 20 | import io.grpc.Attributes; 21 | import io.grpc.NameResolver; 22 | import org.springframework.cloud.client.discovery.DiscoveryClient; 23 | 24 | import javax.annotation.Nullable; 25 | import java.net.URI; 26 | 27 | /** 28 | * Created by rayt on 5/17/16. 29 | */ 30 | public class DiscoveryClientResolverFactory extends NameResolver.Factory { 31 | private final DiscoveryClient client; 32 | 33 | public DiscoveryClientResolverFactory(DiscoveryClient client) { 34 | this.client = client; 35 | } 36 | 37 | @Nullable 38 | @Override 39 | public NameResolver newNameResolver(URI targetUri, Attributes params) { 40 | return new DiscoveryClientNameResolver(targetUri.getHost(), client, params); 41 | } 42 | 43 | @Override 44 | public String getDefaultScheme() { 45 | return "spring"; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /grpc-client-autoconfigurer/src/main/java/org/springframework/boot/autoconfigure/grpc/client/GrpcChannelFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package org.springframework.boot.autoconfigure.grpc.client; 19 | 20 | import io.grpc.Channel; 21 | 22 | /** 23 | * Created by rayt on 5/17/16. 24 | */ 25 | public interface GrpcChannelFactory { 26 | public Channel createChannel(String name); 27 | } 28 | -------------------------------------------------------------------------------- /grpc-client-autoconfigurer/src/main/java/org/springframework/boot/autoconfigure/grpc/client/GrpcChannelProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package org.springframework.boot.autoconfigure.grpc.client; 19 | 20 | /** 21 | * Created by rayt on 5/17/16. 22 | */ 23 | public class GrpcChannelProperties { 24 | private boolean discover = false; 25 | private String host = "localhost"; 26 | private int port = 9090; 27 | private boolean plaintext = true; 28 | 29 | public boolean isDiscover() { 30 | return discover; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return "GrpcChannelProperties{" + 36 | "discover=" + discover + 37 | ", host='" + host + '\'' + 38 | ", port=" + port + 39 | ", plaintext=" + plaintext + 40 | '}'; 41 | } 42 | 43 | public void setDiscover(boolean discover) { 44 | this.discover = discover; 45 | } 46 | 47 | public String getHost() { 48 | return host; 49 | } 50 | 51 | public void setHost(String host) { 52 | this.host = host; 53 | } 54 | 55 | public int getPort() { 56 | return port; 57 | } 58 | 59 | public void setPort(int port) { 60 | this.port = port; 61 | } 62 | 63 | public boolean isPlaintext() { 64 | return plaintext; 65 | } 66 | 67 | public void setPlaintext(boolean plaintext) { 68 | this.plaintext = plaintext; 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /grpc-client-autoconfigurer/src/main/java/org/springframework/boot/autoconfigure/grpc/client/GrpcChannelsProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package org.springframework.boot.autoconfigure.grpc.client; 19 | 20 | import org.springframework.boot.context.properties.ConfigurationProperties; 21 | import org.springframework.boot.context.properties.NestedConfigurationProperty; 22 | 23 | import java.util.Map; 24 | 25 | /** 26 | * Created by rayt on 5/17/16. 27 | */ 28 | @ConfigurationProperties("grpc.client") 29 | public class GrpcChannelsProperties { 30 | @NestedConfigurationProperty 31 | private Map channels; 32 | 33 | public Map getChannels() { 34 | return channels; 35 | } 36 | 37 | public void setChannels(Map channels) { 38 | this.channels = channels; 39 | } 40 | 41 | @Override 42 | public String toString() { 43 | return "GrpcChannelsProperties{" + 44 | "channels=" + channels + 45 | '}'; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /grpc-client-autoconfigurer/src/main/java/org/springframework/boot/autoconfigure/grpc/client/GrpcClientAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.springframework.boot.autoconfigure.grpc.client; 18 | 19 | import io.grpc.Channel; 20 | 21 | import io.grpc.LoadBalancer; 22 | import io.grpc.util.RoundRobinLoadBalancerFactory; 23 | import org.springframework.boot.autoconfigure.AutoConfigureAfter; 24 | import org.springframework.boot.autoconfigure.condition.*; 25 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 26 | import org.springframework.cloud.client.discovery.DiscoveryClient; 27 | import org.springframework.context.annotation.Bean; 28 | import org.springframework.context.annotation.Configuration; 29 | 30 | import java.util.HashMap; 31 | import java.util.Map; 32 | 33 | /** 34 | * Autoconfiguration for gRPC clients. 35 | * @author Ray Tsang 36 | */ 37 | @Configuration 38 | @EnableConfigurationProperties 39 | @ConditionalOnClass({ Channel.class }) 40 | @AutoConfigureAfter( name = {"org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration"}) 41 | public class GrpcClientAutoConfiguration { 42 | 43 | @ConditionalOnMissingBean 44 | @Bean 45 | public GrpcChannelsProperties defaultGrpcChannelsProperties() { 46 | Map defaultChannels = new HashMap(); 47 | GrpcChannelsProperties properties = new GrpcChannelsProperties(); 48 | 49 | GrpcChannelProperties defaultChannel = new GrpcChannelProperties(); 50 | defaultChannels.put("default", defaultChannel); 51 | 52 | properties.setChannels(defaultChannels); 53 | 54 | return properties; 55 | } 56 | 57 | @ConditionalOnMissingBean 58 | @ConditionalOnProperty(name="spring.cloud.discovery.enabled", havingValue = "false") 59 | @Bean 60 | public GrpcChannelFactory defaultGrpcChannelFactory(GrpcChannelsProperties channels) { 61 | return new AddressChannelFactory(channels); 62 | } 63 | 64 | @ConditionalOnMissingBean 65 | @Bean 66 | public LoadBalancer.Factory defaultGrpcLoadBalancerFactory() { 67 | return RoundRobinLoadBalancerFactory.getInstance(); 68 | } 69 | 70 | @ConditionalOnMissingBean 71 | @ConditionalOnBean(DiscoveryClient.class) 72 | @Bean 73 | public DiscoveryClientChannelFactory discoveryClientChannelFactory(GrpcChannelsProperties channels, DiscoveryClient client) { 74 | return new DiscoveryClientChannelFactory(channels, client); 75 | } 76 | 77 | /* 78 | @ConditionalOnMissingBean 79 | @Bean 80 | public AnnotationGrpcServiceDiscoverer defaultGrpcServiceFinder() { 81 | return new AnnotationGrpcServiceDiscoverer(); 82 | } 83 | 84 | @ConditionalOnMissingBean 85 | @Bean 86 | public NettyGrpcServerFactory defaultGrpcServiceFactory( 87 | GrpcServerProperties properties, GrpcStubDiscoverer discoverer) { 88 | NettyGrpcServerFactory factory = new NettyGrpcServerFactory(properties); 89 | for (GrpcServiceDefinition service : discoverer.findGrpcServices()) { 90 | factory.addService(service); 91 | } 92 | 93 | return factory; 94 | } 95 | 96 | @ConditionalOnMissingBean 97 | @Bean 98 | public GrpcServerLifecycle grpcServerLifecycle( 99 | GrpcServerFactory factory) { 100 | return new GrpcServerLifecycle(factory); 101 | } 102 | */ 103 | } 104 | -------------------------------------------------------------------------------- /grpc-client-autoconfigurer/src/main/java/org/springframework/boot/autoconfigure/grpc/client/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /** 18 | * Auto-configuration for gRPC. 19 | * @author Ray Tsang 20 | */ 21 | package org.springframework.boot.autoconfigure.grpc.client; 22 | -------------------------------------------------------------------------------- /grpc-client-autoconfigurer/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.springframework.boot.autoconfigure.grpc.client.GrpcClientAutoConfiguration 2 | -------------------------------------------------------------------------------- /grpc-client-starter/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 19 | 4.0.0 20 | com.github.saturnism.spring-boot-starter-grpc 21 | grpc-client-starter 22 | master-SNAPSHOT 23 | Spring Boot gRPC Client Starter 24 | Spring Boot gRPC Client Starter 25 | 26 | com.github.saturnism.spring-boot-starter-grpc 27 | grpc-parent 28 | master-SNAPSHOT 29 | ../grpc-parent/pom.xml 30 | 31 | 32 | 33 | com.github.saturnism.spring-boot-starter-grpc 34 | grpc-client-autoconfigurer 35 | 36 | 37 | io.grpc 38 | grpc-netty 39 | 40 | 41 | io.grpc 42 | grpc-protobuf 43 | 44 | 45 | io.grpc 46 | grpc-stub 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /grpc-client-starter/src/main/resources/META-INF/spring.provides: -------------------------------------------------------------------------------- 1 | provides: grpc-client -------------------------------------------------------------------------------- /grpc-parent/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 19 | 4.0.0 20 | com.github.saturnism.spring-boot-starter-grpc 21 | grpc-parent 22 | master-SNAPSHOT 23 | pom 24 | Spring Boot gRPC Starter Parent 25 | Spring Boot gRPC Starter Parent 26 | 27 | 1.8 28 | 1.8 29 | 2.1.4.RELEASE 30 | Greenwich.SR1 31 | 1.20.0 32 | 0.5.1 33 | 3.7.1 34 | 1.5.0.Final 35 | 36 | 37 | 38 | 39 | io.grpc 40 | grpc-bom 41 | ${grpc.version} 42 | pom 43 | import 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-starter 48 | ${spring.boot.version} 49 | pom 50 | import 51 | 52 | 53 | org.springframework.cloud 54 | spring-cloud-dependencies 55 | ${spring.cloud.version} 56 | pom 57 | import 58 | 59 | 60 | com.github.saturnism.spring-boot-starter-grpc 61 | grpc-server-autoconfigurer 62 | master-SNAPSHOT 63 | 64 | 65 | com.github.saturnism.spring-boot-starter-grpc 66 | grpc-server-starter 67 | master-SNAPSHOT 68 | 69 | 70 | com.github.saturnism.spring-boot-starter-grpc 71 | grpc-client-autoconfigurer 72 | master-SNAPSHOT 73 | 74 | 75 | com.github.saturnism.spring-boot-starter-grpc 76 | grpc-client-starter 77 | master-SNAPSHOT 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | kr.motd.maven 86 | os-maven-plugin 87 | ${os.plugin.version} 88 | 89 | 90 | org.apache.maven.plugins 91 | maven-source-plugin 92 | 93 | 94 | attach-sources 95 | 96 | jar 97 | 98 | 99 | 100 | 101 | 102 | org.xolstice.maven.plugins 103 | protobuf-maven-plugin 104 | ${protobuf.plugin.version} 105 | 106 | com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier} 107 | grpc-java 108 | io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier} 109 | 110 | 111 | 112 | 113 | compile 114 | compile-custom 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | kr.motd.maven 124 | os-maven-plugin 125 | 126 | 127 | 128 | 129 | -------------------------------------------------------------------------------- /grpc-parent/src/checkstyle/checkstyle-header.txt: -------------------------------------------------------------------------------- 1 | ^\Q/*\E$ 2 | ^\Q * Copyright \E20\d\d\-20\d\d\Q the original author or authors.\E$ 3 | ^\Q *\E$ 4 | ^\Q * Licensed under the Apache License, Version 2.0 (the "License");\E$ 5 | ^\Q * you may not use this file except in compliance with the License.\E$ 6 | ^\Q * You may obtain a copy of the License at\E$ 7 | ^\Q *\E$ 8 | ^\Q * http://www.apache.org/licenses/LICENSE-2.0\E$ 9 | ^\Q *\E$ 10 | ^\Q * Unless required by applicable law or agreed to in writing, software\E$ 11 | ^\Q * distributed under the License is distributed on an "AS IS" BASIS,\E$ 12 | ^\Q * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\E$ 13 | ^\Q * See the License for the specific language governing permissions and\E$ 14 | ^\Q * limitations under the License.\E$ 15 | ^\Q */\E$ 16 | ^$ 17 | ^.*$ 18 | -------------------------------------------------------------------------------- /grpc-parent/src/checkstyle/checkstyle-suppressions.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 19 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /grpc-parent/src/checkstyle/checkstyle.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 147 | 148 | 149 | 150 | 151 | 152 | 154 | 155 | 156 | 157 | 158 | 159 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | -------------------------------------------------------------------------------- /grpc-server-autoconfigurer/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 20 | 4.0.0 21 | 22 | 23 | com.github.saturnism.spring-boot-starter-grpc 24 | grpc-parent 25 | master-SNAPSHOT 26 | ../grpc-parent/pom.xml 27 | 28 | grpc-server-autoconfigurer 29 | Spring Boot gRPC Server Autoconfigurer 30 | Spring Boot gRPC Server Autoconfigurer 31 | http://projects.spring.io/spring-boot/ 32 | 33 | gRPC 34 | http://www.grpc.io 35 | 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-autoconfigure 40 | 41 | 42 | io.grpc 43 | grpc-netty-shaded 44 | true 45 | 46 | 47 | io.grpc 48 | grpc-protobuf 49 | true 50 | 51 | 52 | io.grpc 53 | grpc-stub 54 | true 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /grpc-server-autoconfigurer/src/main/java/org/springframework/boot/autoconfigure/grpc/server/AnnotationGrpcServiceDiscoverer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package org.springframework.boot.autoconfigure.grpc.server; 19 | 20 | import io.grpc.BindableService; 21 | import io.grpc.ServerServiceDefinition; 22 | import org.apache.commons.logging.Log; 23 | import org.apache.commons.logging.LogFactory; 24 | import org.springframework.beans.BeansException; 25 | import org.springframework.context.ApplicationContext; 26 | import org.springframework.context.ApplicationContextAware; 27 | import org.springframework.core.annotation.AnnotationUtils; 28 | 29 | 30 | import java.lang.reflect.InvocationTargetException; 31 | import java.lang.reflect.Method; 32 | import java.lang.reflect.Modifier; 33 | import java.util.ArrayList; 34 | import java.util.Arrays; 35 | import java.util.Collection; 36 | import java.util.Collections; 37 | import java.util.List; 38 | 39 | /** 40 | * Discovers gRPC service implementations by the {@link GrpcService} annotation. 41 | * @author Ray Tsang 42 | */ 43 | public class AnnotationGrpcServiceDiscoverer 44 | implements ApplicationContextAware, GrpcServiceDiscoverer { 45 | private static final Log logger = LogFactory 46 | .getLog(AnnotationGrpcServiceDiscoverer.class); 47 | 48 | private ApplicationContext applicationContext; 49 | 50 | @Override 51 | public void setApplicationContext(ApplicationContext applicationContext) 52 | throws BeansException { 53 | this.applicationContext = applicationContext; 54 | } 55 | 56 | public Collection findGrpcServiceBeanNames() { 57 | String[] beanNames = this.applicationContext 58 | .getBeanNamesForAnnotation(GrpcService.class); 59 | return Collections.unmodifiableList(Arrays.asList(beanNames)); 60 | } 61 | 62 | @Override 63 | public Collection findGrpcServices() { 64 | Collection beanNames = findGrpcServiceBeanNames(); 65 | List definitions = new ArrayList( 66 | beanNames.size()); 67 | for (String beanName : beanNames) { 68 | Object bean = this.applicationContext.getBean(beanName); 69 | Class beanClazz = bean.getClass(); 70 | if (!BindableService.class.isAssignableFrom(beanClazz)) { 71 | throw new IllegalStateException(beanClazz.getName() + " does not seem to extend a generated base implementation nor implements BindableService"); 72 | } 73 | 74 | definitions.add(new GrpcServiceDefinition(beanName, (BindableService) bean)); 75 | } 76 | return definitions; 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /grpc-server-autoconfigurer/src/main/java/org/springframework/boot/autoconfigure/grpc/server/DefaultGrpcServerFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.springframework.boot.autoconfigure.grpc.server; 18 | 19 | import io.grpc.Server; 20 | import io.grpc.ServerBuilder; 21 | import io.grpc.ServiceDescriptor; 22 | import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder; 23 | import org.apache.commons.logging.Log; 24 | import org.apache.commons.logging.LogFactory; 25 | 26 | import java.util.Collection; 27 | 28 | /** 29 | * Creates a Netty gRPC server using {@link NettyServerBuilder}. 30 | * @author Ray Tsang 31 | */ 32 | public class DefaultGrpcServerFactory implements GrpcServerFactory { 33 | private static final Log logger = LogFactory.getLog(DefaultGrpcServerFactory.class); 34 | 35 | private final GrpcServerProperties properties; 36 | private final GrpcServiceDiscoverer discoverer; 37 | 38 | public DefaultGrpcServerFactory(GrpcServerProperties properties, GrpcServiceDiscoverer discoverer) { 39 | this.properties = properties; 40 | this.discoverer = discoverer; 41 | } 42 | 43 | @Override 44 | public Server createServer() { 45 | ServerBuilder builder = ServerBuilder.forPort(getPort()); 46 | Collection definitions = discoverer.findGrpcServices(); 47 | for (GrpcServiceDefinition definition : definitions) { 48 | ServiceDescriptor descriptor = definition.getService().bindService().getServiceDescriptor(); 49 | logger.info("Registered gRPC service: " + descriptor.getName() 50 | + ", bean: " + definition.getBeanName() + ", class: " 51 | + definition.getService().getClass().getName()); 52 | builder.addService(definition.getService()); 53 | } 54 | 55 | return builder.build(); 56 | } 57 | 58 | @Override 59 | public int getPort() { 60 | return this.properties.getPort(); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /grpc-server-autoconfigurer/src/main/java/org/springframework/boot/autoconfigure/grpc/server/GrpcServerAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.springframework.boot.autoconfigure.grpc.server; 18 | 19 | import io.grpc.Server; 20 | 21 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 22 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 23 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 24 | import org.springframework.context.annotation.Bean; 25 | import org.springframework.context.annotation.Configuration; 26 | 27 | /** 28 | * Autoconfiguration for gRPC server. 29 | * @author Ray Tsang 30 | */ 31 | @Configuration 32 | @EnableConfigurationProperties 33 | @ConditionalOnClass({ Server.class, GrpcServerFactory.class }) 34 | public class GrpcServerAutoConfiguration { 35 | 36 | @ConditionalOnMissingBean 37 | @Bean 38 | public GrpcServerProperties defaultGrpcServerProperties() { 39 | return new GrpcServerProperties(); 40 | } 41 | 42 | @ConditionalOnMissingBean 43 | @Bean 44 | public AnnotationGrpcServiceDiscoverer defaultGrpcServiceFinder() { 45 | return new AnnotationGrpcServiceDiscoverer(); 46 | } 47 | 48 | @ConditionalOnMissingBean 49 | @Bean 50 | public DefaultGrpcServerFactory defaultGrpcServerFactory(GrpcServerProperties properties, GrpcServiceDiscoverer discover) { 51 | return new DefaultGrpcServerFactory(properties, discover); 52 | } 53 | 54 | @ConditionalOnMissingBean 55 | @Bean 56 | public GrpcServerLifecycle grpcServerLifecycle(GrpcServerFactory factory) { 57 | return new GrpcServerLifecycle(factory); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /grpc-server-autoconfigurer/src/main/java/org/springframework/boot/autoconfigure/grpc/server/GrpcServerFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.springframework.boot.autoconfigure.grpc.server; 18 | 19 | import io.grpc.Server; 20 | 21 | /** 22 | * Creates a gRPC server with all the gRPC services added to the registry. The server 23 | * returned is ready to be started by the caller. 24 | * @author Ray Tsang 25 | */ 26 | public interface GrpcServerFactory { 27 | Server createServer(); 28 | 29 | int getPort(); 30 | } 31 | -------------------------------------------------------------------------------- /grpc-server-autoconfigurer/src/main/java/org/springframework/boot/autoconfigure/grpc/server/GrpcServerLifecycle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.springframework.boot.autoconfigure.grpc.server; 18 | 19 | import io.grpc.BindableService; 20 | import io.grpc.Server; 21 | import io.grpc.ServiceDescriptor; 22 | import org.apache.commons.logging.Log; 23 | import org.apache.commons.logging.LogFactory; 24 | import org.springframework.context.SmartLifecycle; 25 | 26 | import java.io.IOException; 27 | import java.util.concurrent.atomic.AtomicInteger; 28 | 29 | /** 30 | * Manages the lifecycle of a gRPC server. It uses the {@link GrpcServerFactory} 31 | * to create a new instance of a gRPC server, and then manages it according to the 32 | * {@link SmartLifecycle}. 33 | * @author Ray Tsang 34 | */ 35 | public class GrpcServerLifecycle implements SmartLifecycle { 36 | private static final Log logger = LogFactory 37 | .getLog(GrpcServerLifecycle.class); 38 | private static AtomicInteger serverCounter = new AtomicInteger(-1); 39 | 40 | private volatile Server server; 41 | private volatile int phase = Integer.MAX_VALUE; 42 | private final GrpcServerFactory factory; 43 | 44 | public GrpcServerLifecycle(GrpcServerFactory factory) { 45 | this.factory = factory; 46 | } 47 | 48 | @Override 49 | public void start() { 50 | try { 51 | createAndStartGrpcServer(); 52 | } 53 | catch (IOException e) { 54 | throw new IllegalStateException(e); 55 | } 56 | } 57 | 58 | @Override 59 | public void stop() { 60 | stopAndReleaseGrpcServer(); 61 | } 62 | 63 | @Override 64 | public boolean isRunning() { 65 | return this.server == null ? false : !this.server.isShutdown(); 66 | } 67 | 68 | @Override 69 | public int getPhase() { 70 | return this.phase; 71 | } 72 | 73 | @Override 74 | public boolean isAutoStartup() { 75 | return true; 76 | } 77 | 78 | @Override 79 | public void stop(Runnable callback) { 80 | this.stop(); 81 | callback.run(); 82 | } 83 | 84 | protected void createAndStartGrpcServer() throws IOException { 85 | Server localServer = this.server; 86 | if (localServer == null) { 87 | this.server = factory.createServer(); 88 | this.server.start(); 89 | logger.info("gRPC Server started, listening on port: " + this.factory.getPort()); 90 | 91 | Thread awaitThread = new Thread( 92 | "container-" + (serverCounter.incrementAndGet())) { 93 | 94 | @Override 95 | public void run() { 96 | try { 97 | GrpcServerLifecycle.this.server.awaitTermination(); 98 | } 99 | catch (InterruptedException e) { 100 | Thread.currentThread().interrupt(); 101 | } 102 | } 103 | 104 | }; 105 | awaitThread.setDaemon(false); 106 | awaitThread.start(); 107 | } 108 | } 109 | 110 | protected void stopAndReleaseGrpcServer() { 111 | Server localServer = this.server; 112 | if (localServer != null) { 113 | localServer.shutdown(); 114 | this.server = null; 115 | logger.info("gRPC server stopped"); 116 | } 117 | } 118 | 119 | } 120 | -------------------------------------------------------------------------------- /grpc-server-autoconfigurer/src/main/java/org/springframework/boot/autoconfigure/grpc/server/GrpcServerProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.springframework.boot.autoconfigure.grpc.server; 18 | 19 | import org.springframework.beans.factory.annotation.Value; 20 | import org.springframework.boot.context.properties.ConfigurationProperties; 21 | 22 | /** 23 | * Properties for gRPC server. 24 | * @author Ray Tsang 25 | */ 26 | @ConfigurationProperties("grpc.server") 27 | public class GrpcServerProperties { 28 | /** 29 | * Server port to listen on. Defaults to 9443. 30 | */ 31 | private int port = 9090; 32 | 33 | public int getPort() { 34 | return this.port; 35 | } 36 | 37 | public void setPort(int port) { 38 | this.port = port; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /grpc-server-autoconfigurer/src/main/java/org/springframework/boot/autoconfigure/grpc/server/GrpcService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.springframework.boot.autoconfigure.grpc.server; 18 | 19 | import java.lang.annotation.Documented; 20 | import java.lang.annotation.ElementType; 21 | import java.lang.annotation.Retention; 22 | import java.lang.annotation.RetentionPolicy; 23 | import java.lang.annotation.Target; 24 | 25 | import org.springframework.stereotype.Service; 26 | 27 | /** 28 | * Use this to annotate a gRPC service implementation. 29 | * @author Ray Tsang 30 | */ 31 | @Target(ElementType.TYPE) 32 | @Retention(RetentionPolicy.RUNTIME) 33 | @Documented 34 | @Service 35 | public @interface GrpcService { 36 | } 37 | -------------------------------------------------------------------------------- /grpc-server-autoconfigurer/src/main/java/org/springframework/boot/autoconfigure/grpc/server/GrpcServiceDefinition.java: -------------------------------------------------------------------------------- 1 | package org.springframework.boot.autoconfigure.grpc.server; 2 | 3 | import io.grpc.BindableService; 4 | 5 | /** 6 | * Created by rayt on 6/23/17. 7 | */ 8 | public class GrpcServiceDefinition { 9 | private final String beanName; 10 | private final BindableService service; 11 | 12 | public GrpcServiceDefinition(String beanName, BindableService service) { 13 | this.beanName = beanName; 14 | this.service = service; 15 | } 16 | 17 | public String getBeanName() { 18 | return beanName; 19 | } 20 | 21 | public BindableService getService() { 22 | return service; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /grpc-server-autoconfigurer/src/main/java/org/springframework/boot/autoconfigure/grpc/server/GrpcServiceDiscoverer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.springframework.boot.autoconfigure.grpc.server; 18 | 19 | import io.grpc.BindableService; 20 | 21 | import java.util.Collection; 22 | 23 | /** 24 | * Discover gRPC service implementations in the application. 25 | * @author Ray Tsang 26 | */ 27 | public interface GrpcServiceDiscoverer { 28 | Collection findGrpcServices(); 29 | } 30 | -------------------------------------------------------------------------------- /grpc-server-autoconfigurer/src/main/java/org/springframework/boot/autoconfigure/grpc/server/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /** 18 | * Auto-configuration for gRPC. 19 | * @author Ray Tsang 20 | */ 21 | package org.springframework.boot.autoconfigure.grpc.server; 22 | -------------------------------------------------------------------------------- /grpc-server-autoconfigurer/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.springframework.boot.autoconfigure.grpc.server.GrpcServerAutoConfiguration 2 | -------------------------------------------------------------------------------- /grpc-server-parent/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 19 | 4.0.0 20 | com.github.saturnism.spring-boot-starter-grpc 21 | grpc-server-parent 22 | master-SNAPSHOT 23 | pom 24 | Spring Boot gRPC Server Parent 25 | Spring Boot gRPC Parent 26 | 27 | org.springframework.boot 28 | spring-boot-starter-parent 29 | 2.1.4.RELEASE 30 | 31 | 32 | 33 | 34 | 35 | com.github.saturnism.spring-boot-starter-grpc 36 | grpc-parent 37 | master-SNAPSHOT 38 | pom 39 | import 40 | 41 | 42 | com.github.saturnism.spring-boot-starter-grpc 43 | grpc-server-parent 44 | master-SNAPSHOT 45 | 46 | 47 | com.github.saturnism.spring-boot-starter-grpc 48 | grpc-server-autoconfigurer 49 | master-SNAPSHOT 50 | 51 | 52 | com.github.saturnism.spring-boot-starter-grpc 53 | grpc-server-starter 54 | master-SNAPSHOT 55 | 56 | 57 | com.github.saturnism.spring-boot-starter-grpc 58 | grpc-client-autoconfigurer 59 | master-SNAPSHOT 60 | 61 | 62 | com.github.saturnism.spring-boot-starter-grpc 63 | grpc-client-starter 64 | master-SNAPSHOT 65 | 66 | 67 | 68 | 69 | 70 | com.github.saturnism.spring-boot-starter-grpc 71 | grpc-server-starter 72 | 73 | 74 | 75 | 76 | 77 | 78 | org.xolstice.maven.plugins 79 | protobuf-maven-plugin 80 | 81 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /grpc-server-starter/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 19 | 4.0.0 20 | com.github.saturnism.spring-boot-starter-grpc 21 | grpc-server-starter 22 | master-SNAPSHOT 23 | Spring Boot gRPC Server Starter 24 | Spring Boot gRPC Server Starter 25 | 26 | com.github.saturnism.spring-boot-starter-grpc 27 | grpc-parent 28 | master-SNAPSHOT 29 | ../grpc-parent/pom.xml 30 | 31 | 32 | 33 | com.github.saturnism.spring-boot-starter-grpc 34 | grpc-server-autoconfigurer 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /grpc-server-starter/src/main/resources/META-INF/spring.provides: -------------------------------------------------------------------------------- 1 | provides: grpc-server -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 19 | 4.0.0 20 | com.github.saturnism.spring-boot-starter-grpc 21 | grpc-starter-build 22 | master 23 | pom 24 | Spring Boot gRPC Starter Build 25 | Spring Boot gRPC Starter Build 26 | 27 | 28 | Apache License, Version 2.0 29 | http://www.apache.org/licenses/LICENSE-2.0 30 | 31 | 32 | 33 | 34 | saturnism 35 | Ray Tsang 36 | 37 | 38 | 39 | 3.2.1 40 | 41 | 42 | 43 | default 44 | 45 | true 46 | 47 | 48 | grpc-parent 49 | grpc-server-autoconfigurer 50 | grpc-server-starter 51 | grpc-client-autoconfigurer 52 | grpc-client-starter 53 | grpc-server-parent 54 | 55 | 56 | 57 | 58 | --------------------------------------------------------------------------------