Skip to content

Commit 7a3ae34

Browse files
authored
Merge pull request #473 from yidongnan/docs/client-interceptors
Improve client interceptor documentation
2 parents 5dea4b7 + ca0ccd6 commit 7a3ae34

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

docs/en/client/configuration.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,60 @@ public GrpcChannelConfigurer keepAliveClientConfigurer() {
128128
129129
### ClientInterceptor
130130

131+
`ClientInterceptor`s can be used for various tasks, including:
132+
133+
- Authentication/Authorization
134+
- Request validation
135+
- Response filtering
136+
- Attaching additional context to the call (e.g. tracing ids)
137+
- Exception to error `Status` response mapping
138+
- Logging
139+
- ...
140+
131141
There are three ways to add a `ClientInterceptor` to your channel.
132142

133143
- Define the `ClientInterceptor` as a global interceptor using either the `@GrpcGlobalClientInterceptor` annotation,
134144
or a `GlobalClientInterceptorConfigurer`
135145
- Explicitly list them in the `@GrpcClient#interceptors` or `@GrpcClient#interceptorNames` field
136146
- Use a `StubTransformer` and call `stub.withInterceptors(ClientInterceptor... interceptors)`
137147

148+
The following examples demonstrate how to use annotations to create a global client interceptor:
149+
150+
````java
151+
@Configuration
152+
public class ThirdPartyInterceptorConfig {}
153+
154+
@GrpcGlobalServerInterceptor
155+
ServerInterceptor logServerInterceptor() {
156+
return new LogGrpcInterceptor();
157+
}
158+
159+
}
160+
````
161+
162+
This variant is very handy if you wish to add third-party interceptors to the global scope.
163+
164+
For your own interceptor implementations you can achieve the same result by adding the annotation to the class itself:
165+
166+
````java
167+
@GrpcGlobalServerInterceptor
168+
public class LogGrpcInterceptor implements ServerInterceptor {
169+
170+
private static final Logger log = LoggerFactory.getLogger(LogGrpcInterceptor.class);
171+
172+
@Override
173+
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
174+
ServerCall<ReqT, RespT> serverCall,
175+
Metadata metadata,
176+
ServerCallHandler<ReqT, RespT> serverCallHandler) {
177+
178+
log.info(serverCall.getMethodDescriptor().getFullMethodName());
179+
return serverCallHandler.startCall(serverCall, metadata);
180+
}
181+
182+
}
183+
````
184+
138185
### StubFactory
139186

140187
A `StubFactory` is used to create a `Stub` of a specific type. The registered stub factories will be checked in order

0 commit comments

Comments
 (0)