diff --git a/webflux6-micrometer/pom.xml b/webflux6-micrometer/pom.xml index 7312723..043cc95 100644 --- a/webflux6-micrometer/pom.xml +++ b/webflux6-micrometer/pom.xml @@ -19,8 +19,8 @@ 21 17 - 3.3.0-M3 - 1.3.0-RC1 + 3.3.0-RC1 + 1.3.0 diff --git a/webflux6-micrometer/src/main/java/brave/example/Backend.java b/webflux6-micrometer/src/main/java/brave/example/Backend.java index 790b0ad..c6ae232 100644 --- a/webflux6-micrometer/src/main/java/brave/example/Backend.java +++ b/webflux6-micrometer/src/main/java/brave/example/Backend.java @@ -1,9 +1,13 @@ package brave.example; import java.time.LocalDate; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; import java.util.Optional; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.context.annotation.Import; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestHeader; @@ -27,9 +31,8 @@ public Mono printDate(@RequestHeader("user_name") Optional usern } public static void main(String[] args) { - SpringApplication.run(Backend.class, - "--spring.application.name=backend", - "--server.port=9000" - ); + new SpringApplicationBuilder(Backend.class) + .properties(ZipkinDiscoveryConfiguration.discoveryProperties()) + .run("--spring.application.name=backend", "--server.port=9000"); } } diff --git a/webflux6-micrometer/src/main/java/brave/example/Frontend.java b/webflux6-micrometer/src/main/java/brave/example/Frontend.java index 3e125df..883362c 100644 --- a/webflux6-micrometer/src/main/java/brave/example/Frontend.java +++ b/webflux6-micrometer/src/main/java/brave/example/Frontend.java @@ -2,8 +2,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.context.annotation.Import; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @@ -29,9 +29,10 @@ public class Frontend { } public static void main(String[] args) { - SpringApplication.run(Frontend.class, - "--spring.application.name=frontend", - "--server.port=8081" - ); + new SpringApplicationBuilder(Frontend.class) + .properties(ZipkinDiscoveryConfiguration.discoveryProperties()) + .run("--spring.application.name=frontend", + "--server.port=8081" + ); } } diff --git a/webflux6-micrometer/src/main/java/brave/example/ZipkinDiscoveryConfiguration.java b/webflux6-micrometer/src/main/java/brave/example/ZipkinDiscoveryConfiguration.java index 70ac52a..e26846f 100644 --- a/webflux6-micrometer/src/main/java/brave/example/ZipkinDiscoveryConfiguration.java +++ b/webflux6-micrometer/src/main/java/brave/example/ZipkinDiscoveryConfiguration.java @@ -1,8 +1,9 @@ package brave.example; import java.net.URI; -import org.springframework.boot.actuate.autoconfigure.tracing.zipkin.ZipkinAutoConfiguration; -import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import java.util.LinkedHashMap; +import java.util.Map; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.context.annotation.Bean; @@ -10,9 +11,26 @@ import zipkin2.reporter.HttpEndpointSupplier; import zipkin2.reporter.HttpEndpointSuppliers; -// TODO: make this conditional on configuration, to prevent crashes looking up Eureka. @Configuration(proxyBeanMethods = false) +@ConditionalOnProperty("EUREKA_SERVICE_URL") public class ZipkinDiscoveryConfiguration { + + /** + * It is very difficult to stop loadbalancer or discovery from initializing when starters are on + * the classpath. This is a workaround to disable it when EUREKA_SERVICE_URL is not set. + */ + static Map discoveryProperties() { + Map properties = new LinkedHashMap<>(); + String eurekaServiceUrl = System.getenv("EUREKA_SERVICE_URL"); + boolean eurekaEnabled = eurekaServiceUrl != null && !eurekaServiceUrl.isEmpty(); + if (eurekaEnabled) { + properties.put("eureka.client.serviceUrl.defaultZone", eurekaServiceUrl); + } + properties.put("spring.cloud.loadbalancer.enabled", eurekaEnabled); + properties.put("spring.cloud.discovery.enabled", eurekaEnabled); + return properties; + } + @Bean HttpEndpointSupplier.Factory loadbalancerEndpoints(LoadBalancerClient loadBalancerClient) { LoadBalancerHttpEndpointSupplier.Factory httpEndpointSupplierFactory = new LoadBalancerHttpEndpointSupplier.Factory(loadBalancerClient); diff --git a/webflux6-micrometer/src/main/resources/application.yaml b/webflux6-micrometer/src/main/resources/application.yaml index c34d8da..e3f0509 100644 --- a/webflux6-micrometer/src/main/resources/application.yaml +++ b/webflux6-micrometer/src/main/resources/application.yaml @@ -34,10 +34,6 @@ management: tracing: # Note: There is no property to bind ${brave.localServiceName:${spring.application.name}} endpoint: ${zipkin.baseUrl:http://127.0.0.1:9411}/api/v2/spans -eureka: - client: - serviceUrl: - defaultZone: http://localhost:8761/eureka/ spring: cloud: compatibilityVerifier: