Skip to content

Commit

Permalink
Merge branch 'master' into bump-grpc
Browse files Browse the repository at this point in the history
  • Loading branch information
luankevinferreira committed Sep 19, 2021
2 parents 2b0b19d + e334c1b commit 6653745
Show file tree
Hide file tree
Showing 8 changed files with 628 additions and 27 deletions.
45 changes: 45 additions & 0 deletions docs/en/client/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This section describes how you can configure your grpc-spring-boot-starter clien
- [Configuration via Properties](#configuration-via-properties)
- [Choosing the Target](#choosing-the-target)
- [Configuration via Beans](#configuration-via-beans)
- [GrpcClientBean](#grpcclientbean)
- [GrpcChannelConfigurer](#grpcchannelconfigurer)
- [ClientInterceptor](#clientinterceptor)
- [StubFactory](#stubfactory)
Expand Down Expand Up @@ -115,6 +116,50 @@ First of all most of the beans can be replaced by custom ones, that you can conf
If you don't wish to go that far, you can use classes such as `GrpcChannelConfigurer` and `StubTransformer` to configure
the channels, stubs and other components without losing the features provided by this library.

### GrpcClientBean

This annotation is used to create injectable beans from your otherwise non-injectable `@GrpcClient` instances.
The annotation can be repeatedly added to any of your `@Configuration` classes.

> **Note:** We recommend using either `@GrpcClientBean`s or fields annotated with `@GrpcClient` throughout your
> application, as mixing the two might cause confusion for future developers.
````java
@Configuration
@GrpcClientBean(
clazz = TestServiceBlockingStub.class,
beanName = "blockingStub",
client = @GrpcClient("test")
)
@GrpcClientBean(
clazz = FactoryMethodAccessibleStub.class,
beanName = "accessibleStub",
client = @GrpcClient("test"))
public class YourCustomConfiguration {

@Bean
FooService fooServiceBean(@Autowired TestServiceGrpc.TestServiceBlockingStub blockingStub) {
return new FooService(blockingStub);
}

}

@Service
@AllArgsConsturtor
public class BarService {

private FactoryMethodAccessibleStub accessibleStub;

public String receiveGreeting(String name) {
HelloRequest request = HelloRequest.newBuilder()
.setName(name)
.build();
return accessibleStub.sayHello(request).getMessage();
}

}
````

### GrpcChannelConfigurer

The grpc client configurer allows you to add your custom configuration to grpc's `ManagedChannelBuilder`s.
Expand Down
46 changes: 44 additions & 2 deletions docs/en/client/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,14 @@ If you don't wish to use any advanced features, then the first element is probab
The annotation that marks fields and setters for auto injection of clients.
Supports `Channel`s, and all kinds of `Stub`s.
Do not use `@GrpcClient` in conjunction with `@Autowired` or `@Inject`.
Currently it isn't supported for constructor and `@Bean` method parameters. \
Currently, it isn't supported for constructor and `@Bean` method parameters. For this case look below
to the `@GrpcClientBean`. \
**Note:** Services provided by the same application can only be accessed/called in/after the
`ApplicationStartedEvent`. Stubs connecting to services outside of the application can be used earlier; starting with
`@PostConstruct` / `InitializingBean#afterPropertiesSet()`.
- [`@GrpcClientBean`](https://javadoc.io/page/net.devh/grpc-client-spring-boot-autoconfigure/latest/net/devh/boot/grpc/client/inject/GrpcClientBean.html):
The annotation helps to register `@GrpcClient` beans in the Spring context to be used with `@Autowired` and
`@Qualifier`. The annotation can be repeatedly added to any of your `@Configuration` classes.
- [`Channel`](https://javadoc.io/page/io.grpc/grpc-all/latest/io/grpc/Channel.html):
The Channel is a connection pool for a single address. The target servers might serve multiple grpc-services though.
The address will be resolved using a `NameResolver` and might point to a fixed or dynamic number of servers.
Expand Down Expand Up @@ -167,13 +171,51 @@ public class FoobarService {
public String receiveGreeting(String name) {
HelloRequest request = HelloRequest.newBuilder()
.setName(name)
.build()
.build();
return myServiceStub.sayHello(request).getMessage();
}

}
````

Also you can feel free to inject stub with `@GrpcClientBean` with `@Configuration` for more wide usage in
another services.

> **Note:** We recommend using either `@GrpcClientBean`s or fields annotated with `@GrpcClient` throughout your
> application, as mixing the two can cause confusion for future developers.
````java
@Configuration
@GrpcClientBean(
clazz = TestServiceGrpc.TestServiceBlockingStub.class,
beanName = "blockingStub",
client = @GrpcClient("test")
)
public class YourCustomConfiguration {

@Bean
FoobarService foobarService(@Autowired TestServiceGrpc.TestServiceBlockingStub blockingStub) {
return new FoobarService(blockingStub);
}

}

@Service
@AllArgsConsturtor
public class FoobarService {

private TestServiceBlockingStub blockingStub;

public String receiveGreeting(String name) {
HelloRequest request = HelloRequest.newBuilder()
.setName(name)
.build();
return blockingStub.sayHello(request).getMessage();
}

}
````

## Additional Topics <!-- omit in toc -->

- *Getting Started*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import io.grpc.CallCredentials;
import io.grpc.CallOptions;
Expand All @@ -39,7 +40,7 @@

/**
* An annotation for fields of type {@link Channel} or subclasses of {@link AbstractStub}/gRPC client services. Also
* works for annotated methods that only take a single parameter of the same types. Annotated fields/methods will be
* works for annotated methods that only take a single parameter of these types. Annotated fields/methods will be
* automatically populated/invoked by Spring.
*
* <p>
Expand All @@ -59,8 +60,7 @@
* interceptors and applied using {@link ClientInterceptors#interceptForward(Channel, ClientInterceptor...)}.
* </p>
*
* @author Michael (yidongnan@gmail.com)
* @since 2016/12/7
* @see GrpcClientBean Add as bean to the {@link ApplicationContext}.
*/
@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2016-2021 Michael Zhang <yidongnan@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package net.devh.boot.grpc.client.inject;

import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;

/**
* Annotation that can be added to {@link Configuration} classes to add a {@link GrpcClient} bean to the
* {@link ApplicationContext}.
*/
@Target(ElementType.TYPE)
@Repeatable(GrpcClientBeans.class)
@Retention(RetentionPolicy.RUNTIME)
public @interface GrpcClientBean {

/**
* The type of the bean to create.
*
* @return The type of the bean.
*/
Class<?> clazz();

/**
* The name of the bean to create. If empty, a name will be generated automatically based on the bean class and the
* client name.
*
* @return The optional name of the bean.
*/
String beanName() default "";

/**
* The client definition used to create the channel and grab all properties.
*
* @return The client definition to use.
*/
GrpcClient client();

}

0 comments on commit 6653745

Please sign in to comment.