Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions currency-conversion-service/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
labels:
app: currency-conversion
name: currency-conversion
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: currency-conversion
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
labels:
app: currency-conversion
spec:
containers:
- image: devbith/spring-microservice-currency-conversion-service:0.0.1-SNAPSHOT
imagePullPolicy: IfNotPresent
name: spring-microservice-currency-conversion-service
envFrom:
- configMapRef:
name: currency-conversion
# env:
# - name: CURRENCY_EXCHANGE_URL
# value: http://currency-exchange
restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
annotations:
cloud.google.com/neg: '{"ingress":true}'
labels:
app: currency-conversion
name: currency-conversion
namespace: default
spec:
ports:
- port: 8100
protocol: TCP
targetPort: 8100
selector:
app: currency-conversion
sessionAffinity: None
type: LoadBalancer
---
apiVersion: v1
data:
CURRENCY_EXCHANGE_URL: http://currency-exchange
kind: ConfigMap
metadata:
name: currency-conversion
namespace: default
16 changes: 8 additions & 8 deletions currency-conversion-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-sleuth-zipkin</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.excercise.springmicroervice.currencyconversionservice.dto.CurrencyConversion;
import com.excercise.springmicroervice.currencyconversionservice.proxy.CurrencyExchangeProxy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -16,6 +18,9 @@
public class CurrencyConversionController {


private Logger logger = LoggerFactory.getLogger(CurrencyConversionController.class);


@Autowired
CurrencyExchangeProxy currencyExchangeProxy;

Expand All @@ -40,8 +45,13 @@ public CurrencyConversion calculateConversionFeign(@PathVariable String from, @P

CurrencyConversion currencyConversion = currencyExchangeProxy.calculateConversion(from, to);

return new CurrencyConversion(currencyConversion.getId(), from, to, quantity,
logger.info("Calculating conversion: from="+from+", to="+to+", quantity="+quantity);

CurrencyConversion calculatedCurrencyConversion = new CurrencyConversion(currencyConversion.getId(), from, to, quantity,
currencyConversion.getConversionMultiple(), quantity.multiply(currencyConversion.getConversionMultiple()),
currencyConversion.getEnvironment() + " feign");

logger.info("Total conversion: "+calculatedCurrencyConversion.getTotalCalculatedAmount().toString());
return calculatedCurrencyConversion;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,18 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(name = "currency-exchange")
/**
* Kubernetes automatically creates few environment variables for the deployed service in pod
* If a service is deployed with app name 'currency-exchange' then
* It will create env variables with service name as prefix
* Example: CURRENCY_EXCHANGE_SERVICE_HOST which get IP as the value
*
* We could use the env variable created by k8s for service deployed and use it in feign-client url
* //@FeignClient(name = "currency-exchange", url = "${CURRENCY_EXCHANGE_SERVICE_HOST:http://localhost}:8000")
*/


@FeignClient(name = "currency-exchange", url = "${CURRENCY_EXCHANGE_URL:http://localhost}:8000")
public interface CurrencyExchangeProxy {

@GetMapping("/currency-exchange/from/{from}/to/{to}")
Expand Down
23 changes: 16 additions & 7 deletions currency-conversion-service/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,24 @@ spring:
import: optional:configserver:http://localhost:8888
sleuth:
sampler:
baseUrl: http://localhost:9411
probability: 1.0
server:
port: 8100

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka
instance:
prefer-ip-address: true
#eureka:
# client:
# serviceUrl:
# defaultZone: http://localhost:8761/eureka
# instance:
# prefer-ip-address: true

management:
endpoint:
health:
probes:
enabled: true
health:
livenessstate:
enabled: true
readinessstate:
enabled: true
56 changes: 56 additions & 0 deletions currency-exchange-service/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
labels:
app: currency-exchange
name: currency-exchange
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: currency-exchange
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
labels:
app: currency-exchange
spec:
containers:
- image: devbith/spring-microservice-currency-exchange-service:0.0.2-SNAPSHOT
imagePullPolicy: IfNotPresent
name: spring-microservice-currency-exchange-service
readinessProbe:
httpGet:
port: 8000
path: /actuator/health/readiness
livenessProbe:
httpGet:
port: 8000
path: /actuator/health/liveness
restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
annotations:
cloud.google.com/neg: '{"ingress":true}'
labels:
app: currency-exchange
name: currency-exchange
namespace: default
spec:
ports:
- port: 8000
protocol: TCP
targetPort: 8000
selector:
app: currency-exchange
sessionAffinity: None
type: LoadBalancer
18 changes: 9 additions & 9 deletions currency-exchange-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>com.excercise.spring-microervice</groupId>
<artifactId>currency-exchange-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>0.0.2-SNAPSHOT</version>
<name>currency-exchange-service</name>
<description>currency-exchange-service</description>
<properties>
Expand Down Expand Up @@ -38,10 +38,10 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
Expand All @@ -50,10 +50,10 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-sleuth-zipkin</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.amqp</groupId>-->
<!-- <artifactId>spring-rabbit</artifactId>-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
//@RestController
public class CircuitBreakerController {

private Logger logger = LoggerFactory.getLogger(CircuitBreakerController.class);

@GetMapping("/sample-api")
//@Retry(name = "sampleA", fallbackMethod = "hardCodedResponse")
@CircuitBreaker(name = "sampleA", fallbackMethod = "hardCodedResponse")
@RateLimiter(name = "sampleA")
@Bulkhead(name = "sampleA")
//@GetMapping("/sample-api")
////@Retry(name = "sampleA", fallbackMethod = "hardCodedResponse")
//@CircuitBreaker(name = "sampleA", fallbackMethod = "hardCodedResponse")
//@RateLimiter(name = "sampleA")
//@Bulkhead(name = "sampleA")
public String sampleApi() {
logger.info("Sample API call received");
ResponseEntity<String> forEntity = new RestTemplate().getForEntity("http://localhost:8080/some-duppy-url", String.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ public class CurrencyExchangeController {
@GetMapping("/currency-exchange/from/{from}/to/{to}")
public CurrencyExchange retrieveExchange(@PathVariable String from, @PathVariable String to) {
logger.info("retrieve value from {} to {}", from, to);

final String port = environment.getProperty("local.server.port");
final String host = environment.getProperty("HOSTNAME");
final String version = "v0.0.2";

Optional<CurrencyExchange> currencyExchangeOptional = currencyExchangeService.findByFromAndTo(from, to);
if (currencyExchangeOptional.isPresent()) {
CurrencyExchange currencyExchange = currencyExchangeOptional.get();
currencyExchange.setEnvironment(port);
currencyExchange.setEnvironment(port +":"+host+" "+version);
return currencyExchange;
}
throw new RuntimeException("Currency not found");
Expand Down
67 changes: 39 additions & 28 deletions currency-exchange-service/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,46 @@ spring:
h2:
console:
enabled: true
sleuth:
sampler:
baseUrl: http://localhost:9411
probability: 1.0
# sleuth:
# sampler:
# baseUrl: http://localhost:9411
# probability: 1.0

server:
port: 8000
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka
instance:
prefer-ip-address: true

resilience4j.retry:
instances:
sampleA:
maxAttempts: 5
waitDuration: 1s
enableExponentialBackoff: true

resilience4j.ratelimiter:
instances:
sampleA:
limitForPeriod: 2
limitRefreshPeriod: 10s

resilience4j.bulkhead:
instances:
sampleA:
maxConcurrentCalls: 10
#eureka:
# client:
# serviceUrl:
# defaultZone: http://localhost:8761/eureka
# instance:
# prefer-ip-address: true
#
#resilience4j.retry:
# instances:
# sampleA:
# maxAttempts: 5
# waitDuration: 1s
# enableExponentialBackoff: true
#
#resilience4j.ratelimiter:
# instances:
# sampleA:
# limitForPeriod: 2
# limitRefreshPeriod: 10s
#
#resilience4j.bulkhead:
# instances:
# sampleA:
# maxConcurrentCalls: 10
#

management:
endpoint:
health:
probes:
enabled: true
health:
livenessstate:
enabled: true
readinessstate:
enabled: true
Loading