AppConfig.java
3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.tianting.infoloop.config;
import brave.Tracing;
import brave.baggage.BaggagePropagation;
import brave.baggage.BaggagePropagationConfig;
import brave.handler.MutableSpan;
import brave.handler.SpanHandler;
import brave.propagation.B3Propagation;
import brave.propagation.CurrentTraceContext;
import brave.propagation.TraceContext;
import brave.sampler.Sampler;
import com.tianting.infoloop.service.grpc.MealOrderGrpcService;
import com.tianting.infoloop.service.grpc.OrderGrpcService;
import io.grpc.Server;
import io.grpc.ServerInterceptor;
import io.grpc.ServerInterceptors;
import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.NonNull;
import static com.tianting.infoloop.constants.ConfigConstants.APP_NAME;
import static com.tianting.infoloop.constants.ConfigConstants.BRAVE_PROPAGATION_DEBUG_FIELD;
import static com.tianting.infoloop.constants.ConfigConstants.SERVICE_PORT;
@Configuration
public class AppConfig implements ApplicationContextAware {
public static ApplicationContext APP_CONTEXT = null;
private static final Logger LOGGER = LoggerFactory.getLogger("tracing");
@Override
public void setApplicationContext(@NonNull final ApplicationContext applicationContext) {
APP_CONTEXT = applicationContext;
}
@Bean
Tracing tracing(@Value(APP_NAME) final String serviceName) {
return Tracing
.newBuilder()
.sampler(Sampler.ALWAYS_SAMPLE)
.localServiceName(serviceName)
.propagationFactory(BaggagePropagation
.newFactoryBuilder(B3Propagation.FACTORY)
.add(BaggagePropagationConfig.SingleBaggageField.remote(BRAVE_PROPAGATION_DEBUG_FIELD))
.build())
.currentTraceContext(CurrentTraceContext.Default.create())
.addSpanHandler(new SpanHandler() {
@Override
public boolean end(final TraceContext context,
final MutableSpan span,
final Cause cause) {
LOGGER.info(span.toString());
return true;
}
})
.build();
}
@Bean
public Server serviceServer(@Value(SERVICE_PORT) final int port,
final ServerInterceptor serverInterceptor,
final OrderGrpcService orderGrpcService,
final MealOrderGrpcService mealOrderGrpcService) {
return NettyServerBuilder
.forPort(port)
.addService(ServerInterceptors.intercept(orderGrpcService, serverInterceptor))
.addService(ServerInterceptors.intercept(mealOrderGrpcService, serverInterceptor))
.build();
}
}