Skip to content

Commit

Permalink
Introduce Spring Cloud Consul
Browse files Browse the repository at this point in the history
  • Loading branch information
making committed Dec 31, 2020
1 parent e48d25c commit b0a8783
Show file tree
Hide file tree
Showing 20 changed files with 87 additions and 12 deletions.
4 changes: 4 additions & 0 deletions cart-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.wavefront</groupId>
<artifactId>wavefront-spring-boot-starter</artifactId>
Expand Down
4 changes: 4 additions & 0 deletions cart-api/src/main/resources/application-consul.properties
Original file line number Diff line number Diff line change
@@ -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}
3 changes: 2 additions & 1 deletion cart-api/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
spring.security.oauth2.resourceserver.jwt.issuer-uri=http://127.0.0.1:5006/oauth/token
spring.cloud.consul.enabled=false
4 changes: 4 additions & 0 deletions catalog-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.wavefront</groupId>
<artifactId>wavefront-spring-boot-starter</artifactId>
Expand Down
4 changes: 4 additions & 0 deletions catalog-api/src/main/resources/application-consul.properties
Original file line number Diff line number Diff line change
@@ -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}
3 changes: 2 additions & 1 deletion catalog-api/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
spring.security.oauth2.resourceserver.jwt.issuer-uri=http://127.0.0.1:5006/oauth/token
spring.cloud.consul.enabled=false
4 changes: 4 additions & 0 deletions order-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.wavefront</groupId>
<artifactId>wavefront-spring-boot-starter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
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;

@Component
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<CustomerResponse> retrieveCustomer(String customerUri) {
return this.webClient.get()
.uri(this.props.getUserUrl(), b -> b.path("me").build())
.uri("me")
.retrieve()
.bodyToMono(CustomerResponse.class);
}
Expand Down
7 changes: 7 additions & 0 deletions order-api/src/main/resources/application-consul.properties
Original file line number Diff line number Diff line change
@@ -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
10 changes: 9 additions & 1 deletion order-api/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
resilience4j.circuitbreaker.configs.default.record-failure-predicate=lol.maki.socks.config.circuitbreaker.RecordFailurePredicate
logging.level.lol.maki.socks.config.LoggingExchangeFilterFunction=DEBUG
4 changes: 4 additions & 0 deletions payment-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.wavefront</groupId>
<artifactId>wavefront-spring-boot-starter</artifactId>
Expand Down
4 changes: 4 additions & 0 deletions payment-api/src/main/resources/application-consul.properties
Original file line number Diff line number Diff line change
@@ -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}
3 changes: 2 additions & 1 deletion payment-api/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
spring.security.oauth2.resourceserver.jwt.issuer-uri=http://127.0.0.1:5006/oauth/token
spring.cloud.consul.enabled=false
4 changes: 4 additions & 0 deletions shipping-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.wavefront</groupId>
<artifactId>wavefront-spring-boot-starter</artifactId>
Expand Down
4 changes: 4 additions & 0 deletions shipping-api/src/main/resources/application-consul.properties
Original file line number Diff line number Diff line change
@@ -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}
3 changes: 2 additions & 1 deletion shipping-api/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
spring.security.oauth2.resourceserver.jwt.issuer-uri=http://127.0.0.1:5006/oauth/token
spring.cloud.consul.enabled=false
4 changes: 4 additions & 0 deletions shop-ui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.wavefront</groupId>
<artifactId>wavefront-spring-boot-starter</artifactId>
Expand Down
10 changes: 10 additions & 0 deletions shop-ui/src/main/resources/application-consul.properties
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions shop-ui/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b0a8783

Please sign in to comment.