|
1 | 1 | package com.baeldung.spring.cloud.bootstrap.gateway; |
2 | 2 |
|
| 3 | +import com.netflix.appinfo.InstanceInfo; |
| 4 | +import com.netflix.discovery.EurekaClient; |
3 | 5 | import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.beans.factory.annotation.Value; |
4 | 7 | import org.springframework.boot.SpringApplication; |
5 | 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; |
6 | 9 | import org.springframework.cloud.client.loadbalancer.LoadBalanced; |
7 | 10 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient; |
8 | 11 | import org.springframework.cloud.netflix.ribbon.RibbonClientSpecification; |
9 | 12 | import org.springframework.cloud.netflix.ribbon.SpringClientFactory; |
10 | 13 | import org.springframework.cloud.netflix.zuul.EnableZuulProxy; |
| 14 | +import org.springframework.cloud.sleuth.metric.SpanMetricReporter; |
| 15 | +import org.springframework.cloud.sleuth.zipkin.HttpZipkinSpanReporter; |
| 16 | +import org.springframework.cloud.sleuth.zipkin.ZipkinProperties; |
| 17 | +import org.springframework.cloud.sleuth.zipkin.ZipkinSpanReporter; |
11 | 18 | import org.springframework.context.annotation.Bean; |
12 | 19 | import org.springframework.web.client.RestTemplate; |
| 20 | +import zipkin.Span; |
13 | 21 |
|
14 | 22 | import java.util.ArrayList; |
15 | 23 | import java.util.List; |
|
18 | 26 | @EnableZuulProxy |
19 | 27 | @EnableEurekaClient |
20 | 28 | public class GatewayApplication { |
21 | | - public static void main(String[] args) { |
22 | | - SpringApplication.run(GatewayApplication.class, args); |
23 | | - } |
24 | | - |
25 | | - @Autowired(required = false) |
26 | | - private List<RibbonClientSpecification> configurations = new ArrayList<>(); |
27 | | - |
28 | | - @Bean |
29 | | - @LoadBalanced |
30 | | - RestTemplate restTemplate() { |
31 | | - return new RestTemplate(); |
32 | | - } |
33 | | - |
34 | | - @Bean |
35 | | - public SpringClientFactory springClientFactory() { |
36 | | - SpringClientFactory factory = new SpringClientFactory(); |
37 | | - factory.setConfigurations(this.configurations); |
38 | | - return factory; |
39 | | - } |
| 29 | + public static void main(String[] args) { |
| 30 | + SpringApplication.run(GatewayApplication.class, args); |
| 31 | + } |
| 32 | + |
| 33 | + @Autowired(required = false) |
| 34 | + private List<RibbonClientSpecification> configurations = new ArrayList<>(); |
| 35 | + @Autowired |
| 36 | + private EurekaClient eurekaClient; |
| 37 | + @Autowired |
| 38 | + private SpanMetricReporter spanMetricReporter; |
| 39 | + @Autowired |
| 40 | + private ZipkinProperties zipkinProperties; |
| 41 | + @Value("${spring.sleuth.web.skipPattern}") |
| 42 | + private String skipPattern; |
| 43 | + |
| 44 | + @Bean |
| 45 | + @LoadBalanced |
| 46 | + RestTemplate restTemplate() { |
| 47 | + return new RestTemplate(); |
| 48 | + } |
| 49 | + |
| 50 | + @Bean |
| 51 | + public SpringClientFactory springClientFactory() { |
| 52 | + SpringClientFactory factory = new SpringClientFactory(); |
| 53 | + factory.setConfigurations(this.configurations); |
| 54 | + return factory; |
| 55 | + } |
| 56 | + |
| 57 | + @Bean |
| 58 | + public ZipkinSpanReporter makeZipkinSpanReporter() { |
| 59 | + return new ZipkinSpanReporter() { |
| 60 | + private HttpZipkinSpanReporter delegate; |
| 61 | + private String baseUrl; |
| 62 | + |
| 63 | + @Override |
| 64 | + public void report(Span span) { |
| 65 | + InstanceInfo instance = eurekaClient.getNextServerFromEureka("zipkin", false); |
| 66 | + if (!(baseUrl != null && instance.getHomePageUrl().equals(baseUrl))) { |
| 67 | + baseUrl = instance.getHomePageUrl(); |
| 68 | + delegate = new HttpZipkinSpanReporter(baseUrl, zipkinProperties.getFlushInterval(), zipkinProperties.getCompression().isEnabled(), spanMetricReporter); |
| 69 | + if (!span.name.matches(skipPattern)) delegate.report(span); |
| 70 | + } |
| 71 | + if (!span.name.matches(skipPattern)) delegate.report(span); |
| 72 | + } |
| 73 | + }; |
| 74 | + } |
40 | 75 | } |
0 commit comments