@@ -9,6 +9,7 @@ This section describes how you can configure your grpc-spring-boot-starter clien
9
9
- [ Configuration via Properties] ( #configuration-via-properties )
10
10
- [ Choosing the Target] ( #choosing-the-target )
11
11
- [ Configuration via Beans] ( #configuration-via-beans )
12
+ - [ GrpcClientBean] ( #grpcclientbean )
12
13
- [ GrpcChannelConfigurer] ( #grpcchannelconfigurer )
13
14
- [ ClientInterceptor] ( #clientinterceptor )
14
15
- [ StubFactory] ( #stubfactory )
@@ -38,9 +39,10 @@ If you prefer to read the sources instead, you can do so
38
39
[ here] ( https://github.com/yidongnan/grpc-spring-boot-starter/blob/master/grpc-client-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java#L58 ) .
39
40
40
41
The properties for the channels are all prefixed with ` grpc.client.__name__. ` and ` grpc.client.__name__.security. `
41
- respectively. The channel name is taken from the ` @GrpcClient ` annotation. If you wish to configure some options such as
42
- trusted certificates for all servers at once you can do so using ` GLOBAL ` as name. Properties that are defined for a
43
- name channel take precedence over global once.
42
+ respectively. The channel name is taken from the ` @GrpcClient("__name__") ` annotation.
43
+ If you wish to configure some options such as trusted certificates for all servers at once,
44
+ you can do so using ` GLOBAL ` as name.
45
+ Properties that are defined for a specific/named channel take precedence over ` GLOBAL ` ones.
44
46
45
47
### Choosing the Target
46
48
@@ -55,6 +57,7 @@ There are a number of supported schemes, that you can use to determine the targe
55
57
56
58
- ` static ` (Prio 4): \
57
59
A simple static list of IPs (both v4 and v6), that can be use connect to the server (Supports ` localhost ` ). \
60
+ For resolvable hostnames please use ` dns ` instead. \
58
61
Example: ` static://192.168.1.1:8080,10.0.0.1:1337 `
59
62
- [ ` dns ` ] ( https://github.com/grpc/grpc-java/blob/master/core/src/main/java/io/grpc/internal/DnsNameResolver.java#L66 )
60
63
(Prio 5): \
@@ -72,11 +75,16 @@ There are a number of supported schemes, that you can use to determine the targe
72
75
The self address or scheme is a keyword that is available, if you also use ` grpc-server-spring-boot-starter ` and
73
76
allows you to connect to the server without specifying the own address/port. This is especially useful for tests
74
77
where you might want to use random server ports to avoid conflicts. \
75
- Example: ` self ` or ` self :self`
78
+ Example: ` self:self `
76
79
- ` in-process ` : \
77
80
This is a special scheme that will bypass the normal channel factory and will use the ` InProcessChannelFactory `
78
81
instead. Use it to connect to the [ ` InProcessServer ` ] ( ../server/configuration.md#enabling-the-inprocessserver ) . \
79
82
Example: ` in-process:foobar `
83
+ - ` unix ` (Available on Unix based systems only): \
84
+ This is a special scheme that uses unix's domain socket addresses to connect to a server. \
85
+ If you are using ` grpc-netty ` you also need the ` netty-transport-native-epoll ` dependency.
86
+ ` grpc-netty-shaded ` already contains that dependency, so there is no need to add anything for it to work. \
87
+ Example: ` unix:/run/grpc-server `
80
88
- * custom* : \
81
89
You can define custom
82
90
[ ` NameResolverProvider ` s] ( https://javadoc.io/page/io.grpc/grpc-all/latest/io/grpc/NameResolverProvider.html ) those
@@ -108,6 +116,50 @@ First of all most of the beans can be replaced by custom ones, that you can conf
108
116
If you don't wish to go that far, you can use classes such as ` GrpcChannelConfigurer ` and ` StubTransformer ` to configure
109
117
the channels, stubs and other components without losing the features provided by this library.
110
118
119
+ ### GrpcClientBean
120
+
121
+ This annotation is used to create injectable beans from your otherwise non-injectable ` @GrpcClient ` instances.
122
+ The annotation can be repeatedly added to any of your ` @Configuration ` classes.
123
+
124
+ > ** Note:** We recommend using either ` @GrpcClientBean ` s or fields annotated with ` @GrpcClient ` throughout your
125
+ > application, as mixing the two might cause confusion for future developers.
126
+
127
+ ```` java
128
+ @Configuration
129
+ @GrpcClientBean (
130
+ clazz = TestServiceBlockingStub . class,
131
+ beanName = " blockingStub" ,
132
+ client = @GrpcClient (" test" )
133
+ )
134
+ @GrpcClientBean (
135
+ clazz = FactoryMethodAccessibleStub . class,
136
+ beanName = " accessibleStub" ,
137
+ client = @GrpcClient (" test" ))
138
+ public class YourCustomConfiguration {
139
+
140
+ @Bean
141
+ FooService fooServiceBean (@Autowired TestServiceGrpc .TestServiceBlockingStub blockingStub ) {
142
+ return new FooService (blockingStub);
143
+ }
144
+
145
+ }
146
+
147
+ @Service
148
+ @AllArgsConsturtor
149
+ public class BarService {
150
+
151
+ private FactoryMethodAccessibleStub accessibleStub;
152
+
153
+ public String receiveGreeting (String name ) {
154
+ HelloRequest request = HelloRequest . newBuilder()
155
+ .setName(name)
156
+ .build();
157
+ return accessibleStub. sayHello(request). getMessage();
158
+ }
159
+
160
+ }
161
+ ````
162
+
111
163
### GrpcChannelConfigurer
112
164
113
165
The grpc client configurer allows you to add your custom configuration to grpc's ` ManagedChannelBuilder ` s.
@@ -151,34 +203,48 @@ The following examples demonstrate how to use annotations to create a global cli
151
203
152
204
```` java
153
205
@Configuration
154
- public class ThirdPartyInterceptorConfig {}
206
+ public class GlobalInterceptorConfiguration {
155
207
156
- @GrpcGlobalServerInterceptor
157
- LogGrpcInterceptor logServerInterceptor () {
208
+ @GrpcGlobalClientInterceptor
209
+ LogGrpcInterceptor logClientInterceptor () {
158
210
return new LogGrpcInterceptor ();
159
211
}
160
212
161
213
}
162
214
````
163
215
164
- This variant is very handy if you wish to add third-party interceptors to the global scope.
216
+ The following example demonstrates creation via ` GlobalClientInterceptorConfigurer `
217
+
218
+ ```` java
219
+ @Configuration
220
+ public class GlobalInterceptorConfiguration {
221
+
222
+ @Bean
223
+ GlobalClientInterceptorConfigurer globalClientInterceptorConfigurer () {
224
+ interceptors - > interceptors. add(new LogGrpcInterceptor ());
225
+ }
226
+
227
+ }
228
+ ````
229
+
230
+ These variant are very handy if you wish to add third-party interceptors to the global scope.
165
231
166
232
For your own interceptor implementations you can achieve the same result by adding the annotation to the class itself:
167
233
168
234
```` java
169
- @GrpcGlobalServerInterceptor
170
- public class LogGrpcInterceptor implements ServerInterceptor {
235
+ @GrpcGlobalClientInterceptor
236
+ public class LogGrpcInterceptor implements ClientInterceptor {
171
237
172
238
private static final Logger log = LoggerFactory . getLogger(LogGrpcInterceptor . class);
173
239
174
240
@Override
175
- public <ReqT , RespT > ServerCall . Listener <ReqT > interceptCall (
176
- ServerCall <ReqT , RespT > serverCall ,
177
- Metadata metadata ,
178
- ServerCallHandler< ReqT , RespT > serverCallHandler ) {
241
+ public <ReqT , RespT > ClientCall <ReqT , RespT > interceptCall (
242
+ final MethodDescriptor <ReqT , RespT > method ,
243
+ final CallOptions callOptions ,
244
+ final Channel next ) {
179
245
180
- log. info(serverCall . getMethodDescriptor() . getFullMethodName());
181
- return serverCallHandler . startCall(serverCall, metadata );
246
+ log. info(method . getFullMethodName());
247
+ return next . newCall(method, callOptions );
182
248
}
183
249
184
250
}
@@ -267,6 +333,7 @@ you have to define it via spring context (unless you wish to use `static`).
267
333
- [ Getting Started] ( getting-started.md )
268
334
- * Configuration*
269
335
- [ Security] ( security.md )
336
+ - [ Tests with Grpc-Stubs] ( testing.md )
270
337
271
338
----------
272
339
0 commit comments