Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
3bfa9ed
Add GrpcClient soft bean registration and autowiring
a-simeshin Sep 5, 2021
97b7d0d
Add GrpcClientBean annotations for single and multiple application co…
a-simeshin Sep 10, 2021
025368f
Add method to define grpc client`s bean-name
a-simeshin Sep 12, 2021
fa7ed05
Add lazy getter for configurable bean factory
a-simeshin Sep 12, 2021
fae40e7
Warn to log changed to BeanCreationException for @GrpcClientBean regi…
a-simeshin Sep 12, 2021
89004b0
Add check for Configuration or TestConfiguration presence, add test
a-simeshin Sep 12, 2021
88d1a3a
Finding @TestConfiguration presence with AnnotationUtils, removed unn…
a-simeshin Sep 12, 2021
340fe8f
Add test for cases with @GrpcClientBean and @GrpcClient usage. Fix ex…
a-simeshin Sep 12, 2021
23d9dfe
Add @GrpcClientBean to docs, removed inline imports
a-simeshin Sep 14, 2021
752bcc9
Removed unused logger, client docs changes
a-simeshin Sep 17, 2021
c231cea
Update docs/en/client/getting-started.md
a-simeshin Sep 17, 2021
6ed9f81
Update docs/en/client/getting-started.md
a-simeshin Sep 17, 2021
059a1ae
Update docs/en/client/getting-started.md
a-simeshin Sep 17, 2021
2485a20
Update docs/en/client/configuration.md
a-simeshin Sep 17, 2021
47771e5
Update docs/en/client/configuration.md
a-simeshin Sep 17, 2021
7b2d3ad
Update docs/en/client/configuration.md
a-simeshin Sep 17, 2021
4439e6a
Update docs/en/client/getting-started.md
a-simeshin Sep 17, 2021
0573c8f
Update grpc-client-spring-boot-autoconfigure/src/main/java/net/devh/b…
a-simeshin Sep 17, 2021
6b65d9d
Update grpc-client-spring-boot-autoconfigure/src/main/java/net/devh/b…
a-simeshin Sep 17, 2021
71428c6
Renamed field in a more readable form
a-simeshin Sep 17, 2021
18ea434
Polishing and NPE fix
ST-DDT Sep 18, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ([email protected])
* @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 <[email protected]>
*
* 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();

}
Loading