From b0a87833e0d14a90e2b383d3b6b25df2862ba1d8 Mon Sep 17 00:00:00 2001 From: Toshiaki Maki Date: Thu, 31 Dec 2020 17:31:45 +0900 Subject: [PATCH] Introduce Spring Cloud Consul gh-187 --- cart-api/pom.xml | 4 ++++ .../main/resources/application-consul.properties | 4 ++++ .../src/main/resources/application.properties | 3 ++- catalog-api/pom.xml | 4 ++++ .../main/resources/application-consul.properties | 4 ++++ .../src/main/resources/application.properties | 3 ++- order-api/pom.xml | 4 ++++ .../java/lol/maki/socks/config/SockConfig.java | 4 +++- .../lol/maki/socks/customer/CustomerClient.java | 15 +++++++++------ .../main/resources/application-consul.properties | 7 +++++++ .../src/main/resources/application.properties | 10 +++++++++- payment-api/pom.xml | 4 ++++ .../main/resources/application-consul.properties | 4 ++++ .../src/main/resources/application.properties | 3 ++- shipping-api/pom.xml | 4 ++++ .../main/resources/application-consul.properties | 4 ++++ .../src/main/resources/application.properties | 3 ++- shop-ui/pom.xml | 4 ++++ .../main/resources/application-consul.properties | 10 ++++++++++ shop-ui/src/main/resources/application.properties | 1 + 20 files changed, 87 insertions(+), 12 deletions(-) create mode 100644 cart-api/src/main/resources/application-consul.properties create mode 100644 catalog-api/src/main/resources/application-consul.properties create mode 100644 order-api/src/main/resources/application-consul.properties create mode 100644 payment-api/src/main/resources/application-consul.properties create mode 100644 shipping-api/src/main/resources/application-consul.properties create mode 100644 shop-ui/src/main/resources/application-consul.properties diff --git a/cart-api/pom.xml b/cart-api/pom.xml index 444b92e8..411777e9 100644 --- a/cart-api/pom.xml +++ b/cart-api/pom.xml @@ -82,6 +82,10 @@ org.springframework.cloud spring-cloud-starter-sleuth + + org.springframework.cloud + spring-cloud-starter-consul-discovery + com.wavefront wavefront-spring-boot-starter diff --git a/cart-api/src/main/resources/application-consul.properties b/cart-api/src/main/resources/application-consul.properties new file mode 100644 index 00000000..387952ea --- /dev/null +++ b/cart-api/src/main/resources/application-consul.properties @@ -0,0 +1,4 @@ +spring.cloud.consul.enabled=true +spring.cloud.consul.discovery.health-check-path=/actuator/health/readiness +spring.cloud.consul.discovery.metadata.instance_id=${vcap.application.instance_index:${random.int(1,1000)}} +spring.cloud.consul.discovery.metadata.instance_index=${vcap.application.instance_index:0} \ No newline at end of file diff --git a/cart-api/src/main/resources/application.properties b/cart-api/src/main/resources/application.properties index 39d9c993..b4e5aeac 100644 --- a/cart-api/src/main/resources/application.properties +++ b/cart-api/src/main/resources/application.properties @@ -18,4 +18,5 @@ server.compression.min-response-size=1KB spring.resources.cache.cachecontrol.max-age=1d server.shutdown=graceful spring.lifecycle.timeout-per-shutdown-phase=10s -spring.security.oauth2.resourceserver.jwt.issuer-uri=http://127.0.0.1:5006/oauth/token \ No newline at end of file +spring.security.oauth2.resourceserver.jwt.issuer-uri=http://127.0.0.1:5006/oauth/token +spring.cloud.consul.enabled=false \ No newline at end of file diff --git a/catalog-api/pom.xml b/catalog-api/pom.xml index 009e4a7e..c7d4e130 100644 --- a/catalog-api/pom.xml +++ b/catalog-api/pom.xml @@ -93,6 +93,10 @@ org.springframework.cloud spring-cloud-starter-sleuth + + org.springframework.cloud + spring-cloud-starter-consul-discovery + com.wavefront wavefront-spring-boot-starter diff --git a/catalog-api/src/main/resources/application-consul.properties b/catalog-api/src/main/resources/application-consul.properties new file mode 100644 index 00000000..387952ea --- /dev/null +++ b/catalog-api/src/main/resources/application-consul.properties @@ -0,0 +1,4 @@ +spring.cloud.consul.enabled=true +spring.cloud.consul.discovery.health-check-path=/actuator/health/readiness +spring.cloud.consul.discovery.metadata.instance_id=${vcap.application.instance_index:${random.int(1,1000)}} +spring.cloud.consul.discovery.metadata.instance_index=${vcap.application.instance_index:0} \ No newline at end of file diff --git a/catalog-api/src/main/resources/application.properties b/catalog-api/src/main/resources/application.properties index ecdf451f..b5068d09 100644 --- a/catalog-api/src/main/resources/application.properties +++ b/catalog-api/src/main/resources/application.properties @@ -18,4 +18,5 @@ server.compression.min-response-size=1KB spring.resources.cache.cachecontrol.max-age=1d server.shutdown=graceful spring.lifecycle.timeout-per-shutdown-phase=10s -spring.security.oauth2.resourceserver.jwt.issuer-uri=http://127.0.0.1:5006/oauth/token \ No newline at end of file +spring.security.oauth2.resourceserver.jwt.issuer-uri=http://127.0.0.1:5006/oauth/token +spring.cloud.consul.enabled=false \ No newline at end of file diff --git a/order-api/pom.xml b/order-api/pom.xml index 80d13f13..76762038 100644 --- a/order-api/pom.xml +++ b/order-api/pom.xml @@ -112,6 +112,10 @@ org.springframework.cloud spring-cloud-starter-sleuth + + org.springframework.cloud + spring-cloud-starter-consul-discovery + com.wavefront wavefront-spring-boot-starter diff --git a/order-api/src/main/java/lol/maki/socks/config/SockConfig.java b/order-api/src/main/java/lol/maki/socks/config/SockConfig.java index d1ba13fe..fe0b75c3 100644 --- a/order-api/src/main/java/lol/maki/socks/config/SockConfig.java +++ b/order-api/src/main/java/lol/maki/socks/config/SockConfig.java @@ -6,6 +6,7 @@ import lol.maki.socks.payment.client.PaymentApi; import lol.maki.socks.shipping.client.ShipmentApi; +import org.springframework.cloud.client.loadbalancer.reactive.LoadBalancedExchangeFilterFunction; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.oauth2.server.resource.web.reactive.function.client.ServletBearerExchangeFilterFunction; @@ -20,9 +21,10 @@ public SockConfig(SockProps props) { } @Bean - public WebClient webClient(WebClient.Builder builder) { + public WebClient webClient(WebClient.Builder builder, LoadBalancedExchangeFilterFunction loadBalancedExchangeFilterFunction) { return builder .filter(new ServletBearerExchangeFilterFunction()) + .filter(loadBalancedExchangeFilterFunction) .filter(LoggingExchangeFilterFunction.SINGLETON) .build(); } diff --git a/order-api/src/main/java/lol/maki/socks/customer/CustomerClient.java b/order-api/src/main/java/lol/maki/socks/customer/CustomerClient.java index a1ebb51f..8d7c197e 100644 --- a/order-api/src/main/java/lol/maki/socks/customer/CustomerClient.java +++ b/order-api/src/main/java/lol/maki/socks/customer/CustomerClient.java @@ -1,10 +1,12 @@ package lol.maki.socks.customer; import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker; +import lol.maki.socks.config.LoggingExchangeFilterFunction; import lol.maki.socks.config.SockProps; import lol.maki.socks.user.client.CustomerResponse; import reactor.core.publisher.Mono; +import org.springframework.security.oauth2.server.resource.web.reactive.function.client.ServletBearerExchangeFilterFunction; import org.springframework.stereotype.Component; import org.springframework.web.reactive.function.client.WebClient; @@ -12,17 +14,18 @@ public class CustomerClient { private final WebClient webClient; - private final SockProps props; - - public CustomerClient(WebClient webClient, SockProps props) { - this.webClient = webClient; - this.props = props; + public CustomerClient(WebClient.Builder builder, SockProps props) { + this.webClient = builder + .filter(new ServletBearerExchangeFilterFunction()) + .filter(LoggingExchangeFilterFunction.SINGLETON) + .baseUrl(props.getUserUrl()) + .build(); } @CircuitBreaker(name = "user") public Mono retrieveCustomer(String customerUri) { return this.webClient.get() - .uri(this.props.getUserUrl(), b -> b.path("me").build()) + .uri("me") .retrieve() .bodyToMono(CustomerResponse.class); } diff --git a/order-api/src/main/resources/application-consul.properties b/order-api/src/main/resources/application-consul.properties new file mode 100644 index 00000000..f40d04f5 --- /dev/null +++ b/order-api/src/main/resources/application-consul.properties @@ -0,0 +1,7 @@ +spring.cloud.consul.enabled=true +spring.cloud.consul.discovery.health-check-path=/actuator/health/readiness +spring.cloud.consul.discovery.metadata.instance_id=${vcap.application.instance_index:${random.int(1,1000)}} +spring.cloud.consul.discovery.metadata.instance_index=${vcap.application.instance_index:0} +sock.payment-url=http://payment +sock.shipping-url=http://shipping +sock.cart-url=http://cart \ No newline at end of file diff --git a/order-api/src/main/resources/application.properties b/order-api/src/main/resources/application.properties index 8812d5eb..b190637c 100644 --- a/order-api/src/main/resources/application.properties +++ b/order-api/src/main/resources/application.properties @@ -8,6 +8,13 @@ spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.initialization-mode=always spring.zipkin.enabled=false spring.sleuth.sampler.rate=30 +spring.cloud.loadbalancer.retry.enabled=true +spring.cloud.loadbalancer.retry.max-retries-on-next-service-instance=2 +spring.cloud.loadbalancer.retry.backoff.enabled=true +spring.cloud.loadbalancer.retry.backoff.min-backoff=500ms +spring.cloud.loadbalancer.retry.backoff.max-backoff=3s +spring.cloud.loadbalancer.retry.retryable-status-codes=500,502,503 +spring.cloud.consul.enabled=false management.endpoints.web.exposure.include=info,health,prometheus management.endpoint.health.probes.enabled=true management.info.git.mode=full @@ -30,4 +37,5 @@ resilience4j.circuitbreaker.configs.default.slow-call-rate-threshold=50 resilience4j.circuitbreaker.configs.default.permitted-number-of-calls-in-half-open-state=1 resilience4j.circuitbreaker.configs.default.wait-duration-in-open-state=30s resilience4j.circuitbreaker.configs.default.minimum-number-of-calls=5 -resilience4j.circuitbreaker.configs.default.record-failure-predicate=lol.maki.socks.config.circuitbreaker.RecordFailurePredicate \ No newline at end of file +resilience4j.circuitbreaker.configs.default.record-failure-predicate=lol.maki.socks.config.circuitbreaker.RecordFailurePredicate +logging.level.lol.maki.socks.config.LoggingExchangeFilterFunction=DEBUG \ No newline at end of file diff --git a/payment-api/pom.xml b/payment-api/pom.xml index d7005490..5347dc53 100644 --- a/payment-api/pom.xml +++ b/payment-api/pom.xml @@ -84,6 +84,10 @@ org.springframework.cloud spring-cloud-starter-sleuth + + org.springframework.cloud + spring-cloud-starter-consul-discovery + com.wavefront wavefront-spring-boot-starter diff --git a/payment-api/src/main/resources/application-consul.properties b/payment-api/src/main/resources/application-consul.properties new file mode 100644 index 00000000..387952ea --- /dev/null +++ b/payment-api/src/main/resources/application-consul.properties @@ -0,0 +1,4 @@ +spring.cloud.consul.enabled=true +spring.cloud.consul.discovery.health-check-path=/actuator/health/readiness +spring.cloud.consul.discovery.metadata.instance_id=${vcap.application.instance_index:${random.int(1,1000)}} +spring.cloud.consul.discovery.metadata.instance_index=${vcap.application.instance_index:0} \ No newline at end of file diff --git a/payment-api/src/main/resources/application.properties b/payment-api/src/main/resources/application.properties index d28bd974..5c6de573 100644 --- a/payment-api/src/main/resources/application.properties +++ b/payment-api/src/main/resources/application.properties @@ -14,4 +14,5 @@ server.compression.min-response-size=1KB spring.resources.cache.cachecontrol.max-age=1d server.shutdown=graceful spring.lifecycle.timeout-per-shutdown-phase=10s -spring.security.oauth2.resourceserver.jwt.issuer-uri=http://127.0.0.1:5006/oauth/token \ No newline at end of file +spring.security.oauth2.resourceserver.jwt.issuer-uri=http://127.0.0.1:5006/oauth/token +spring.cloud.consul.enabled=false \ No newline at end of file diff --git a/shipping-api/pom.xml b/shipping-api/pom.xml index 9608a3ef..24dd83d0 100644 --- a/shipping-api/pom.xml +++ b/shipping-api/pom.xml @@ -88,6 +88,10 @@ org.springframework.cloud spring-cloud-starter-sleuth + + org.springframework.cloud + spring-cloud-starter-consul-discovery + com.wavefront wavefront-spring-boot-starter diff --git a/shipping-api/src/main/resources/application-consul.properties b/shipping-api/src/main/resources/application-consul.properties new file mode 100644 index 00000000..387952ea --- /dev/null +++ b/shipping-api/src/main/resources/application-consul.properties @@ -0,0 +1,4 @@ +spring.cloud.consul.enabled=true +spring.cloud.consul.discovery.health-check-path=/actuator/health/readiness +spring.cloud.consul.discovery.metadata.instance_id=${vcap.application.instance_index:${random.int(1,1000)}} +spring.cloud.consul.discovery.metadata.instance_index=${vcap.application.instance_index:0} \ No newline at end of file diff --git a/shipping-api/src/main/resources/application.properties b/shipping-api/src/main/resources/application.properties index 06cabb2b..853caec2 100644 --- a/shipping-api/src/main/resources/application.properties +++ b/shipping-api/src/main/resources/application.properties @@ -18,4 +18,5 @@ server.compression.min-response-size=1KB spring.resources.cache.cachecontrol.max-age=1d server.shutdown=graceful spring.lifecycle.timeout-per-shutdown-phase=10s -spring.security.oauth2.resourceserver.jwt.issuer-uri=http://127.0.0.1:5006/oauth/token \ No newline at end of file +spring.security.oauth2.resourceserver.jwt.issuer-uri=http://127.0.0.1:5006/oauth/token +spring.cloud.consul.enabled=false \ No newline at end of file diff --git a/shop-ui/pom.xml b/shop-ui/pom.xml index d2bbff5a..36d89516 100644 --- a/shop-ui/pom.xml +++ b/shop-ui/pom.xml @@ -101,6 +101,10 @@ org.springframework.cloud spring-cloud-starter-sleuth + + org.springframework.cloud + spring-cloud-starter-consul-discovery + com.wavefront wavefront-spring-boot-starter diff --git a/shop-ui/src/main/resources/application-consul.properties b/shop-ui/src/main/resources/application-consul.properties new file mode 100644 index 00000000..e51f8f8e --- /dev/null +++ b/shop-ui/src/main/resources/application-consul.properties @@ -0,0 +1,10 @@ +spring.cloud.consul.enabled=true +spring.cloud.consul.discovery.health-check-path=/management/health/readiness +spring.cloud.consul.discovery.metadata.instance_id=${vcap.application.instance_index:${random.int(1,1000)}} +spring.cloud.consul.discovery.metadata.instance_index=${vcap.application.instance_index:0} +sock.catalog-url=http://catalog +sock.payment-url=http://payment +sock.shipping-url=http://shipping +sock.order-url=http://order +sock.cart-url=http://cart +simple.dns.enabled=false \ No newline at end of file diff --git a/shop-ui/src/main/resources/application.properties b/shop-ui/src/main/resources/application.properties index 600a60c1..1cbc509e 100644 --- a/shop-ui/src/main/resources/application.properties +++ b/shop-ui/src/main/resources/application.properties @@ -18,6 +18,7 @@ spring.cloud.loadbalancer.retry.backoff.enabled=true spring.cloud.loadbalancer.retry.backoff.min-backoff=500ms spring.cloud.loadbalancer.retry.backoff.max-backoff=3s spring.cloud.loadbalancer.retry.retryable-status-codes=500,502,503 +spring.cloud.consul.enabled=false spring.zipkin.enabled=false spring.sleuth.sampler.rate=30 management.endpoints.web.exposure.include=info,health,prometheus,gateway