Skip to content

Commit 551d124

Browse files
committed
BAEL-574 adding reporters as beans instead of config classes
1 parent c4b6937 commit 551d124

7 files changed

Lines changed: 133 additions & 116 deletions

File tree

‎spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/EurekaZipkinSpanReporter.java‎

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
package com.baeldung.spring.cloud.bootstrap.gateway;
22

3+
import com.netflix.appinfo.InstanceInfo;
4+
import com.netflix.discovery.EurekaClient;
35
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.beans.factory.annotation.Value;
47
import org.springframework.boot.SpringApplication;
58
import org.springframework.boot.autoconfigure.SpringBootApplication;
69
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
710
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
811
import org.springframework.cloud.netflix.ribbon.RibbonClientSpecification;
912
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
1013
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;
1118
import org.springframework.context.annotation.Bean;
1219
import org.springframework.web.client.RestTemplate;
20+
import zipkin.Span;
1321

1422
import java.util.ArrayList;
1523
import java.util.List;
@@ -18,23 +26,50 @@
1826
@EnableZuulProxy
1927
@EnableEurekaClient
2028
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+
}
4075
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,52 @@
11
package com.baeldung.spring.cloud.bootstrap.svcbook;
22

3+
import com.netflix.appinfo.InstanceInfo;
4+
import com.netflix.discovery.EurekaClient;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.beans.factory.annotation.Value;
37
import org.springframework.boot.SpringApplication;
48
import org.springframework.boot.autoconfigure.SpringBootApplication;
59
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
10+
import org.springframework.cloud.sleuth.metric.SpanMetricReporter;
11+
import org.springframework.cloud.sleuth.zipkin.HttpZipkinSpanReporter;
12+
import org.springframework.cloud.sleuth.zipkin.ZipkinProperties;
13+
import org.springframework.cloud.sleuth.zipkin.ZipkinSpanReporter;
14+
import org.springframework.context.annotation.Bean;
15+
import zipkin.Span;
616

717
@SpringBootApplication
818
@EnableEurekaClient
919
public class BookServiceApplication {
20+
21+
@Autowired
22+
private EurekaClient eurekaClient;
23+
@Autowired
24+
private SpanMetricReporter spanMetricReporter;
25+
@Autowired
26+
private ZipkinProperties zipkinProperties;
27+
@Value("${spring.sleuth.web.skipPattern}")
28+
private String skipPattern;
29+
1030
public static void main(String[] args) {
1131
SpringApplication.run(BookServiceApplication.class, args);
1232
}
33+
34+
@Bean
35+
public ZipkinSpanReporter makeZipkinSpanReporter() {
36+
return new ZipkinSpanReporter() {
37+
private HttpZipkinSpanReporter delegate;
38+
private String baseUrl;
39+
40+
@Override
41+
public void report(Span span) {
42+
InstanceInfo instance = eurekaClient.getNextServerFromEureka("zipkin", false);
43+
if (!(baseUrl != null && instance.getHomePageUrl().equals(baseUrl))) {
44+
baseUrl = instance.getHomePageUrl();
45+
delegate = new HttpZipkinSpanReporter(baseUrl, zipkinProperties.getFlushInterval(), zipkinProperties.getCompression().isEnabled(), spanMetricReporter);
46+
if (!span.name.matches(skipPattern)) delegate.report(span);
47+
}
48+
if (!span.name.matches(skipPattern)) delegate.report(span);
49+
}
50+
};
51+
}
1352
}

‎spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/EurekaZipkinSpanReporter.java‎

Lines changed: 0 additions & 32 deletions
This file was deleted.

‎spring-cloud/spring-cloud-bootstrap/svc-book/src/main/resources/bootstrap.properties‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ spring.cloud.config.password=configPassword
77
eureka.client.serviceUrl.defaultZone=http://discUser:discPassword@localhost:8082/eureka/
88

99
spring.sleuth.sampler.percentage=1.0
10-
spring.sleuth.web.skipPattern=(^cleanup.*)
10+
spring.sleuth.web.skipPattern=(^cleanup.*)
11+
spring.zipkin.locator.discovery.enabled=true

‎spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/EurekaZipkinSpanReporter.java‎

Lines changed: 0 additions & 32 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,51 @@
11
package com.baeldung.spring.cloud.bootstrap.svcrating;
22

3+
import com.netflix.appinfo.InstanceInfo;
4+
import com.netflix.discovery.EurekaClient;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.beans.factory.annotation.Value;
37
import org.springframework.boot.SpringApplication;
48
import org.springframework.boot.autoconfigure.SpringBootApplication;
59
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
10+
import org.springframework.cloud.sleuth.metric.SpanMetricReporter;
11+
import org.springframework.cloud.sleuth.zipkin.HttpZipkinSpanReporter;
12+
import org.springframework.cloud.sleuth.zipkin.ZipkinProperties;
13+
import org.springframework.cloud.sleuth.zipkin.ZipkinSpanReporter;
14+
import org.springframework.context.annotation.Bean;
15+
import zipkin.Span;
616

717
@SpringBootApplication
818
@EnableEurekaClient
919
public class RatingServiceApplication {
20+
@Autowired
21+
private EurekaClient eurekaClient;
22+
@Autowired
23+
private SpanMetricReporter spanMetricReporter;
24+
@Autowired
25+
private ZipkinProperties zipkinProperties;
26+
@Value("${spring.sleuth.web.skipPattern}")
27+
private String skipPattern;
28+
1029
public static void main(String[] args) {
1130
SpringApplication.run(RatingServiceApplication.class, args);
1231
}
32+
33+
@Bean
34+
public ZipkinSpanReporter makeZipkinSpanReporter() {
35+
return new ZipkinSpanReporter() {
36+
private HttpZipkinSpanReporter delegate;
37+
private String baseUrl;
38+
39+
@Override
40+
public void report(Span span) {
41+
InstanceInfo instance = eurekaClient.getNextServerFromEureka("zipkin", false);
42+
if (!(baseUrl != null && instance.getHomePageUrl().equals(baseUrl))) {
43+
baseUrl = instance.getHomePageUrl();
44+
delegate = new HttpZipkinSpanReporter(baseUrl, zipkinProperties.getFlushInterval(), zipkinProperties.getCompression().isEnabled(), spanMetricReporter);
45+
if (!span.name.matches(skipPattern)) delegate.report(span);
46+
}
47+
if (!span.name.matches(skipPattern)) delegate.report(span);
48+
}
49+
};
50+
}
1351
}

0 commit comments

Comments
 (0)