|
1 | 1 | = Spring boot starter for http://www.grpc.io/[gRPC framework.] https://bintray.com/lognet/maven/grpc-spring-boot-starter/_latestVersion[ image:https://api.bintray.com/packages/lognet/maven/grpc-spring-boot-starter/images/download.svg[Download]] image:https://travis-ci.org/LogNet/grpc-spring-boot-starter.svg?branch=master[Build Status,link=https://travis-ci.org/LogNet/grpc-spring-boot-starter] https://bintray.com/lognet/maven/grpc-spring-boot-starter?source=watch[ image:https://www.bintray.com/docs/images/bintray_badge_color.png[]] |
2 | | -
|
| 2 | +:toc: left |
| 3 | +:doctype: book |
| 4 | +:toclevels: 4 |
| 5 | +:source-highlighter: prettify |
| 6 | +:numbered: |
| 7 | +:icons: font |
3 | 8 |
|
4 | 9 |
|
5 | 10 | == Features |
@@ -145,6 +150,140 @@ public class MyGRpcServerBuilderConfigurer extends GRpcServerBuilderConfigurer() |
145 | 150 | } |
146 | 151 | ---- |
147 | 152 |
|
| 153 | +== Eureka Integration |
| 154 | + |
| 155 | +When building production-ready services, the advise is to have separate project for your service(s) gRPC API that holds only proto-generated classes both for server and client side usage. + |
| 156 | +You will then add this project as `compile` dependency to your `gRPC client` and `gRPC server` projects. |
| 157 | + |
| 158 | +To integrate `Eureka` simply follow the great https://spring.io/guides/gs/service-registration-and-discovery/[guide] from Spring. |
| 159 | + |
| 160 | +Below are the essential parts of configurations for both server and client projects. |
| 161 | + |
| 162 | +=== gRPC Server Project |
| 163 | + |
| 164 | +* Add eureka starter as dependency of your server project together with generated class from `proto` files: |
| 165 | + |
| 166 | +[source,gradle] |
| 167 | +.build.gradle |
| 168 | +---- |
| 169 | + dependencies { |
| 170 | + compile('org.springframework.cloud:spring-cloud-starter-eureka') |
| 171 | + compile project(":yourProject-api") |
| 172 | + } |
| 173 | +---- |
| 174 | + |
| 175 | +* Configure gRPC server to register itself with Eureka. |
| 176 | + |
| 177 | +[source,yaml] |
| 178 | +.bootstrap.yaml |
| 179 | +---- |
| 180 | +spring: |
| 181 | + application: |
| 182 | + name: my-service-name <1> |
| 183 | +---- |
| 184 | +<1> Eureka's `ServiceId` by default is the spring application name, provide it before the service register itself with Eureka. |
| 185 | + |
| 186 | +[source,yaml] |
| 187 | +.application.yaml |
| 188 | +---- |
| 189 | +grpc: |
| 190 | + port: 6565 <1> |
| 191 | +eureka: |
| 192 | + instance: |
| 193 | + nonSecurePort: ${grpc.port} <2> |
| 194 | + client: |
| 195 | + serviceUrl: |
| 196 | + defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/ <3> |
| 197 | +---- |
| 198 | +<1> Specify the port number the gRPC is listening on. |
| 199 | +<2> Register the eureka service port to be the same as `grpc.port` so client will know where to send the requests to. |
| 200 | +<3> Specify the registry URL, so the service will register itself using this URL. |
| 201 | + |
| 202 | +* Expose the gRPC service as part of Spring Boot Application. |
| 203 | + |
| 204 | +[source,java] |
| 205 | +.EurekaGrpcServiceApp.java |
| 206 | +---- |
| 207 | +@SpringBootApplication |
| 208 | +@EnableEurekaClient |
| 209 | +public class EurekaGrpcServiceApp { |
| 210 | +
|
| 211 | + @GRpcService |
| 212 | + public static class GreeterService extends GreeterGrpc.GreeterImplBase { |
| 213 | + @Override |
| 214 | + public void sayHello(GreeterOuterClass.HelloRequest request, StreamObserver<GreeterOuterClass.HelloReply> responseObserver) { |
| 215 | + ... |
| 216 | + } |
| 217 | + } |
| 218 | +
|
| 219 | + public static void main(String[] args) { |
| 220 | + SpringApplication.run(DemoApp.class,args); |
| 221 | + } |
| 222 | +
|
| 223 | +} |
| 224 | +---- |
| 225 | + |
| 226 | + |
| 227 | +=== gRPC Client Project |
| 228 | + |
| 229 | +* Add eureka starter as dependency of your client project together with generated class from `proto` files: |
| 230 | + |
| 231 | +[source,gradle] |
| 232 | +.build.gradle |
| 233 | +---- |
| 234 | + dependencies { |
| 235 | + compile('org.springframework.cloud:spring-cloud-starter-eureka') |
| 236 | + compile project(":yourProject-api") |
| 237 | + } |
| 238 | +---- |
| 239 | + |
| 240 | +* Configure client to find the eureka service registry: |
| 241 | + |
| 242 | +[source,yaml] |
| 243 | +.application.yaml |
| 244 | +---- |
| 245 | +eureka: |
| 246 | + client: |
| 247 | + register-with-eureka: false <1> |
| 248 | + service-url: |
| 249 | + defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/ <2> |
| 250 | +---- |
| 251 | +<1> `false` if this project is not meant to act as a `service` to another client. |
| 252 | +<2> Specify the registry URL, so this client will know where to lookup the required service. |
| 253 | + |
| 254 | +* Use `EurekaClient` to get the coordinates of grpc service instance from Eureka: |
| 255 | + |
| 256 | + [source,java] |
| 257 | + .GreeterServiceConsumerApplication.java |
| 258 | + ---- |
| 259 | + @EnableEurekaClient |
| 260 | + @SpringBootApplication |
| 261 | + public class GreeterServiceConsumerApplication { |
| 262 | + public static void main(String[] args) { |
| 263 | + SpringApplication.run(GreeterServiceConsumerApplication.class, args); |
| 264 | + } |
| 265 | + } |
| 266 | + ---- |
| 267 | + |
| 268 | + [source,java] |
| 269 | + .GreeterServiceConsumer.java |
| 270 | + ---- |
| 271 | + @EnableEurekaClient |
| 272 | + @Component |
| 273 | + public class GreeterServiceConsumer { |
| 274 | + @Autowired |
| 275 | + private EurekaClient client; |
| 276 | + public DeferredResult<String> foo(String name) { |
| 277 | + final InstanceInfo instanceInfo = client.getNextServerFromEureka("my-service-name", false); |
| 278 | + final ManagedChannel channel = ManagedChannelBuilder.forAddress(instanceInfo.getIPAddr(), instanceInfo.getPort()) |
| 279 | + .usePlaintext(true) |
| 280 | + .build(); |
| 281 | + final GreeterServiceGrpc.GreeterServiceFutureStub stub = GreeterServiceGrpc.newFutureStub(channel); |
| 282 | + stub.greet(name); |
| 283 | + |
| 284 | + } |
| 285 | + } |
| 286 | + ---- |
148 | 287 |
|
149 | 288 |
|
150 | 289 | == License |
|
0 commit comments