Skip to content

Commit

Permalink
Fixed actuator endpoints'
Browse files Browse the repository at this point in the history
  • Loading branch information
marcingrzejszczak committed Oct 27, 2022
1 parent 6db0d48 commit 20c32e6
Show file tree
Hide file tree
Showing 27 changed files with 173 additions and 104 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.spring.cloud.samples.brewery.acceptance;

import java.util.Random;

import io.spring.cloud.samples.brewery.acceptance.common.AbstractBreweryAcceptance;
import io.spring.cloud.samples.brewery.acceptance.common.SpanUtil;
import io.spring.cloud.samples.brewery.acceptance.common.tech.TestConditions;
Expand All @@ -22,15 +20,15 @@ public void before() {
@Test
public void should_successfully_pass_Trace_Id_via_rest_template() {
// setup:
warm_up_the_environment(() -> check_brewery(CommunicationType.REST_TEMPLATE));
// warm_up_the_environment(() -> check_brewery(CommunicationType.REST_TEMPLATE));
// given:
check_brewery(CommunicationType.REST_TEMPLATE);
}

@Test
public void should_successfully_brew_the_beer_via_rest_template_and_service_discovery() {
// given:
String referenceProcessId = SpanUtil.idToHex(new Random().nextLong());
String referenceProcessId = SpanUtil.generateReferenceProcessId();
RequestEntity requestEntity = an_order_for_all_ingredients_with_process_id(referenceProcessId, CommunicationType.REST_TEMPLATE);
// when:
presenting_service_has_been_called(requestEntity);
Expand All @@ -41,7 +39,7 @@ public void should_successfully_brew_the_beer_via_rest_template_and_service_disc
@Test
public void should_successfully_brew_the_beer_via_feign_and_service_discovery() {
// given:
String referenceProcessId = SpanUtil.idToHex(new Random().nextLong());
String referenceProcessId = SpanUtil.generateReferenceProcessId();
RequestEntity requestEntity = an_order_for_all_ingredients_with_process_id(referenceProcessId, CommunicationType.FEIGN);
// when:
presenting_service_has_been_called(requestEntity);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.spring.cloud.samples.brewery.acceptance;

import io.spring.cloud.samples.brewery.acceptance.common.WhatToTest;
import io.spring.cloud.samples.brewery.acceptance.common.tech.TestConditions;
import org.junit.jupiter.api.Test;

Expand All @@ -15,8 +16,8 @@ public void will_fail_if_WHAT_TO_TEST_system_property_has_not_been_passed_or_is_

static class WhatToTestSystemPropertyNotPassedOrIsWrongException extends RuntimeException {
WhatToTestSystemPropertyNotPassedOrIsWrongException() {
super("System property 'WHAT_TO_TEST' is equal [${System.getProperty('WHAT_TO_TEST')}]. Valid entries" +
" are ${WhatToTest.values()}");
super("System property 'WHAT_TO_TEST' is equal [" + System.getProperty("WHAT_TO_TEST") + "] Valid entries" +
" are " + WhatToTest.values());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ abstract class AbstractBreweryAcceptance {
base16(parent-id) = 00f067aa0ba902b7
base16(trace-flags) = 01 // sampled
*/
String referenceProcessId = "00-0000000000000000" + SpanUtil.idToHex(new Random().nextLong()) + "-" + SpanUtil.idToHex(new Random().nextLong()) + "-01"
String referenceProcessId = SpanUtil.generateReferenceProcessId();
RequestEntity requestEntity = an_order_for_all_ingredients_with_process_id(referenceProcessId, communicationType);
// when:
presenting_service_has_been_called(requestEntity);
Expand All @@ -105,6 +105,8 @@ abstract class AbstractBreweryAcceptance {
presenting_service_has_been_called(requestEntity);
// then:
beer_has_been_brewed_for_process_id(referenceProcessId);
// and:
entry_for_trace_id_is_present_in_Zipkin(referenceProcessId.split("-")[1])
}

void entry_for_trace_id_is_present_in_Zipkin(String traceId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package io.spring.cloud.samples.brewery.acceptance.common;

import java.util.Random;

/**
* @author Marcin Grzejszczak
* @since
*/
public class SpanUtil {

public static String generateReferenceProcessId() {
return "00-0000000000000000" + SpanUtil.idToHex(new Random().nextLong()) + "-" + SpanUtil.idToHex(new Random().nextLong()) + "-01";
}

/**
* Represents given long id as 16-character lower-hex string
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class TestConditions {

public static final Closure<Boolean> SERVICE_DISCOVERY = {
return whatToTestSystemPropMatchesAny(
[WhatToTest.CONSUL, WhatToTest.EUREKA, WhatToTest.ZOOKEEPER, WhatToTest.SCS, WhatToTest.WAVEFRONT]
[WhatToTest.CONSUL, WhatToTest.EUREKA, WhatToTest.ZOOKEEPER, WhatToTest.WAVEFRONT]
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,18 @@
package io.spring.cloud.samples.brewery;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import io.micrometer.context.ContextSnapshot;
import io.spring.cloud.samples.brewery.common.TestConfiguration;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
@EnableAsync
@EnableFeignClients
@Import(TestConfiguration.class)
public class BrewingApplication {

public static void main(String[] args) {
new SpringApplication(BrewingApplication.class).run(args);
}


/**
* NAME OF THE BEAN IS IMPORTANT!
*
* We need to wrap this for Async related things to propagate the context.
*
* @see EnableAsync
*/
@Bean
Executor taskExecutor() {
return ContextSnapshot.captureAll().wrapExecutorService(Executors.newCachedThreadPool());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import io.spring.cloud.samples.brewery.common.events.EventGateway;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -18,12 +17,6 @@
@EnableConfigurationProperties(IngredientsProperties.class)
class AggregationConfiguration {

@Bean
@LoadBalanced
RestTemplate aggregationLoadBalancedRestTemplate(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder.build();
}

@Bean
MaturingServiceUpdater maturingServiceUpdater(IngredientsProperties ingredientsProperties,
IngredientWarehouse ingredientWarehouse,
Expand All @@ -34,8 +27,7 @@ MaturingServiceUpdater maturingServiceUpdater(IngredientsProperties ingredientsP
}

@Bean
IngredientsCollector ingredientsCollector(RestTemplateBuilder restTemplateBuilder,
IngredientsProxy ingredientsProxy, BaggageManager baggageManager) {
return new IngredientsCollector(aggregationLoadBalancedRestTemplate(restTemplateBuilder), ingredientsProxy, baggageManager);
IngredientsCollector ingredientsCollector(IngredientsProxy ingredientsProxy, BaggageManager baggageManager, @LoadBalanced RestTemplate restTemplate) {
return new IngredientsCollector(restTemplate, ingredientsProxy, baggageManager);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import io.spring.cloud.samples.brewery.common.TestConfiguration;
import io.spring.cloud.samples.brewery.common.events.EventGateway;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.circuitbreaker.resilience4j.Resilience4JCircuitBreakerFactory;
import org.springframework.cloud.circuitbreaker.resilience4j.Resilience4JConfigurationProperties;
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;
Expand All @@ -31,21 +30,15 @@ class BottlingConfiguration {
@Bean
BottlerService bottlingService(BottlingWorker bottlingWorker,
PresentingClient presentingClient,
ObservationRegistry observationRegistry, CircuitBreakerFactory circuitBreakerFactory, RestTemplateBuilder restTemplateBuilder, BaggageManager baggageManager) {
return new BottlerService(bottlingWorker, presentingClient, bottlingLoadBalancedRestTemplate(restTemplateBuilder), observationRegistry, circuitBreakerFactory, baggageManager);
}

@Bean
@LoadBalanced
RestTemplate bottlingLoadBalancedRestTemplate(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder.build();
ObservationRegistry observationRegistry, CircuitBreakerFactory circuitBreakerFactory, @LoadBalanced RestTemplate restTemplate, BaggageManager baggageManager) {
return new BottlerService(bottlingWorker, presentingClient, restTemplate, observationRegistry, circuitBreakerFactory, baggageManager);
}

@Bean
BottlingWorker bottlingWorker(ObservationRegistry observationRegistry,
PresentingClient presentingClient,
RestTemplateBuilder restTemplateBuilder, EventGateway eventGateway, BaggageManager baggageManager) {
return new BottlingWorker(observationRegistry, presentingClient, bottlingLoadBalancedRestTemplate(restTemplateBuilder), eventGateway, baggageManager);
@LoadBalanced RestTemplate restTemplate, EventGateway eventGateway, BaggageManager baggageManager) {
return new BottlingWorker(observationRegistry, presentingClient, restTemplate, eventGateway, baggageManager);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.spring.cloud.samples.brewery.common;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import io.micrometer.context.ContextSnapshot;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.client.RestTemplate;

@Configuration(proxyBeanMethods = false)
class CommonConfiguration {

@Bean
@LoadBalanced
RestTemplate loadBalancedRestTemplate(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder.build();
}

@Configuration
@EnableAsync
static class AsyncConfig implements AsyncConfigurer {
@Override
public Executor getAsyncExecutor() {
return ContextSnapshot.captureAll().wrapExecutorService(Executors.newCachedThreadPool());
}
}


/**
* NAME OF THE BEAN IS IMPORTANT!
*
* We need to wrap this for Async related things to propagate the context.
*
* @see EnableAsync
*/
@Bean(name = "taskExecutor")
Executor taskExecutor() {
return ContextSnapshot.captureAll().wrapExecutorService(Executors.newCachedThreadPool());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import io.spring.cloud.samples.brewery.common.events.EventGateway;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
Expand All @@ -20,18 +19,12 @@
@EnableConfigurationProperties(BrewProperties.class)
class BrewConfiguration {

@Bean
@LoadBalanced
RestTemplate brewLoadBalancedRestTemplate(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder.build();
}

@Bean
BottlingServiceUpdater bottlingServiceUpdater(ObservationRegistry observationRegistry, PresentingServiceClient presentingServiceClient,
BottlingService bottlingService, RestTemplateBuilder restTemplateBuilder,
BottlingService bottlingService, @LoadBalanced RestTemplate restTemplate,
EventGateway eventGateway, CircuitBreakerFactory circuitBreakerFactory, BaggageManager baggageManager, BrewProperties properties) {
return new BottlingServiceUpdater(properties, observationRegistry, presentingServiceClient,
bottlingService, brewLoadBalancedRestTemplate(restTemplateBuilder), eventGateway, circuitBreakerFactory, baggageManager);
bottlingService, restTemplate, eventGateway, circuitBreakerFactory, baggageManager);
}

}
1 change: 0 additions & 1 deletion brewing/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ spring.cloud.stream:
endpoints.health.sensitive: false
endpoints.default.web.enabled: true
management.security.enabled: false
management.endpoints.web.base-path: /

spring:
main:
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
buildscript {
repositories {
mavenLocal()
// mavenLocal()
mavenCentral()
maven {
url "https://repo.spring.io/snapshot"
Expand Down Expand Up @@ -83,7 +83,7 @@ configure(subprojects) {
}

repositories {
mavenLocal()
// mavenLocal()
mavenCentral()
maven {
url "https://repo.spring.io/snapshot"
Expand Down
1 change: 0 additions & 1 deletion config-server/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ spring:
endpoints.health.sensitive: false
endpoints.default.web.enabled: true
management.security.enabled: false
management.endpoints.web.base-path: /
management:
endpoints:
web:
Expand Down
11 changes: 11 additions & 0 deletions docker-compose-CONSUL.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ if [[ "${READY_FOR_TESTS}" == "no" ]] ; then
exit 1
fi

READY_FOR_TESTS="no"
PORT_TO_CHECK=3100
echo "Run the rest of infra"
docker-compose -f $dockerComposeFile up -d zipkin loki prometheus grafana
netcat_local_port $PORT_TO_CHECK && READY_FOR_TESTS="yes"

if [[ "${READY_FOR_TESTS}" == "no" ]] ; then
echo "Loki failed to start..."
exit 1
fi

READY_FOR_TESTS="no"
PORT_TO_CHECK=8888
echo "Waiting for the Config Server app to boot for [$(( WAIT_TIME * RETRIES ))] seconds"
Expand Down
20 changes: 13 additions & 7 deletions docker-compose-CONSUL.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@ services:
- "5672:5672"
- "15672:15672"

tempo:
image: grafana/tempo
# tempo:
# image: grafana/tempo
# extra_hosts: ['host.docker.internal:host-gateway']
# command: [ "-config.file=/etc/tempo.yaml" ]
# volumes:
# - ./docker/tempo/tempo-local.yaml:/etc/tempo.yaml
# - ./tempo-data:/tmp/tempo
# ports:
# - "14268" # jaeger ingest
# - "9411:9411" # zipkin

zipkin:
image: openzipkin/zipkin
extra_hosts: ['host.docker.internal:host-gateway']
command: [ "-config.file=/etc/tempo.yaml" ]
volumes:
- ./docker/tempo/tempo-local.yaml:/etc/tempo.yaml
- ./tempo-data:/tmp/tempo
ports:
- "14268" # jaeger ingest
- "9411:9411" # zipkin

loki:
Expand Down
11 changes: 11 additions & 0 deletions docker-compose-EUREKA.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ if [[ "${READY_FOR_TESTS}" == "no" ]] ; then
exit 1
fi

READY_FOR_TESTS="no"
PORT_TO_CHECK=3100
echo "Run the rest of infra"
docker-compose -f $dockerComposeFile up -d zipkin loki prometheus grafana
netcat_local_port $PORT_TO_CHECK && READY_FOR_TESTS="yes"

if [[ "${READY_FOR_TESTS}" == "no" ]] ; then
echo "Loki failed to start..."
exit 1
fi

READY_FOR_TESTS="no"
PORT_TO_CHECK=8888
echo "Waiting for the Config Server app to boot for [$(( WAIT_TIME * RETRIES ))] seconds"
Expand Down
Loading

0 comments on commit 20c32e6

Please sign in to comment.