From 3ee9b71e6915707543067b71478d43e62aa8aa45 Mon Sep 17 00:00:00 2001 From: Bishal Thapa Date: Sun, 16 Oct 2022 11:44:59 +0100 Subject: [PATCH 1/2] Changes For K8s Google Cloud Platform Deployment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit • Changes made supporting GKE deployment • Added K8s deployment yaml --- currency-conversion-service/deployment.yaml | 62 +++++++++++++++++ currency-conversion-service/pom.xml | 16 ++--- .../CurrencyConversionController.java | 12 +++- .../proxy/CurrencyExchangeProxy.java | 13 +++- .../src/main/resources/application.yml | 23 +++++-- currency-exchange-service/deployment.yaml | 48 +++++++++++++ currency-exchange-service/pom.xml | 16 ++--- .../controller/CircuitBreakerController.java | 12 ++-- .../CurrencyExchangeController.java | 6 +- .../src/main/resources/application.yml | 67 +++++++++++-------- docker-compose.yaml | 27 ++++++-- 11 files changed, 236 insertions(+), 66 deletions(-) create mode 100644 currency-conversion-service/deployment.yaml create mode 100644 currency-exchange-service/deployment.yaml diff --git a/currency-conversion-service/deployment.yaml b/currency-conversion-service/deployment.yaml new file mode 100644 index 0000000..41b05c6 --- /dev/null +++ b/currency-conversion-service/deployment.yaml @@ -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 diff --git a/currency-conversion-service/pom.xml b/currency-conversion-service/pom.xml index 7b273e9..a5ecd42 100644 --- a/currency-conversion-service/pom.xml +++ b/currency-conversion-service/pom.xml @@ -34,18 +34,18 @@ org.springframework.cloud spring-cloud-starter-openfeign - - org.springframework.cloud - spring-cloud-starter-netflix-eureka-client - + + + + org.springframework.cloud spring-cloud-starter-sleuth - - org.springframework.cloud - spring-cloud-sleuth-zipkin - + + + + org.springframework.boot spring-boot-devtools diff --git a/currency-conversion-service/src/main/java/com/excercise/springmicroervice/currencyconversionservice/controller/CurrencyConversionController.java b/currency-conversion-service/src/main/java/com/excercise/springmicroervice/currencyconversionservice/controller/CurrencyConversionController.java index ecf98a3..3830681 100644 --- a/currency-conversion-service/src/main/java/com/excercise/springmicroervice/currencyconversionservice/controller/CurrencyConversionController.java +++ b/currency-conversion-service/src/main/java/com/excercise/springmicroervice/currencyconversionservice/controller/CurrencyConversionController.java @@ -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; @@ -16,6 +18,9 @@ public class CurrencyConversionController { + private Logger logger = LoggerFactory.getLogger(CurrencyConversionController.class); + + @Autowired CurrencyExchangeProxy currencyExchangeProxy; @@ -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 currencyConversion; } } diff --git a/currency-conversion-service/src/main/java/com/excercise/springmicroervice/currencyconversionservice/proxy/CurrencyExchangeProxy.java b/currency-conversion-service/src/main/java/com/excercise/springmicroervice/currencyconversionservice/proxy/CurrencyExchangeProxy.java index da57208..bce6a3e 100644 --- a/currency-conversion-service/src/main/java/com/excercise/springmicroervice/currencyconversionservice/proxy/CurrencyExchangeProxy.java +++ b/currency-conversion-service/src/main/java/com/excercise/springmicroervice/currencyconversionservice/proxy/CurrencyExchangeProxy.java @@ -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}") diff --git a/currency-conversion-service/src/main/resources/application.yml b/currency-conversion-service/src/main/resources/application.yml index 1159460..b5d0ea8 100644 --- a/currency-conversion-service/src/main/resources/application.yml +++ b/currency-conversion-service/src/main/resources/application.yml @@ -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 \ No newline at end of file diff --git a/currency-exchange-service/deployment.yaml b/currency-exchange-service/deployment.yaml new file mode 100644 index 0000000..c42f6cf --- /dev/null +++ b/currency-exchange-service/deployment.yaml @@ -0,0 +1,48 @@ +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.1-SNAPSHOT + imagePullPolicy: IfNotPresent + name: spring-microservice-currency-exchange-service + 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 diff --git a/currency-exchange-service/pom.xml b/currency-exchange-service/pom.xml index a767c4b..e02c10b 100644 --- a/currency-exchange-service/pom.xml +++ b/currency-exchange-service/pom.xml @@ -38,10 +38,10 @@ org.springframework.cloud spring-cloud-starter-config - - org.springframework.cloud - spring-cloud-starter-netflix-eureka-client - + + + + org.springframework.boot spring-boot-starter-data-jpa @@ -50,10 +50,10 @@ org.springframework.cloud spring-cloud-starter-sleuth - - org.springframework.cloud - spring-cloud-sleuth-zipkin - + + + + diff --git a/currency-exchange-service/src/main/java/com/excercise/springmicroervice/currencyexchangeservice/controller/CircuitBreakerController.java b/currency-exchange-service/src/main/java/com/excercise/springmicroervice/currencyexchangeservice/controller/CircuitBreakerController.java index 77c25e4..0cad6a7 100644 --- a/currency-exchange-service/src/main/java/com/excercise/springmicroervice/currencyexchangeservice/controller/CircuitBreakerController.java +++ b/currency-exchange-service/src/main/java/com/excercise/springmicroervice/currencyexchangeservice/controller/CircuitBreakerController.java @@ -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 forEntity = new RestTemplate().getForEntity("http://localhost:8080/some-duppy-url", String.class); diff --git a/currency-exchange-service/src/main/java/com/excercise/springmicroervice/currencyexchangeservice/controller/CurrencyExchangeController.java b/currency-exchange-service/src/main/java/com/excercise/springmicroervice/currencyexchangeservice/controller/CurrencyExchangeController.java index f963f9e..4eee1da 100644 --- a/currency-exchange-service/src/main/java/com/excercise/springmicroervice/currencyexchangeservice/controller/CurrencyExchangeController.java +++ b/currency-exchange-service/src/main/java/com/excercise/springmicroervice/currencyexchangeservice/controller/CurrencyExchangeController.java @@ -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 = "v2"; + Optional 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"); diff --git a/currency-exchange-service/src/main/resources/application.yml b/currency-exchange-service/src/main/resources/application.yml index 1f26ca8..6b86129 100644 --- a/currency-exchange-service/src/main/resources/application.yml +++ b/currency-exchange-service/src/main/resources/application.yml @@ -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 \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index 564f345..6a4d638 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,5 +1,4 @@ -version: '3.7' - +version: '3.8' services: currency-exchange: image: devbith/spring-microservice-currency-exchange-service:0.0.1-SNAPSHOT @@ -14,7 +13,6 @@ services: EUREKA.CLIENT.SERVICEURL.DEFAULTZONE: http://naming-server:8761/eureka SPRING.SLEUTH.SAMPLER.BASEURL: http://host.docker.internal:9411/ - currency-conversion: image: devbith/spring-microservice-currency-conversion-service:0.0.1-SNAPSHOT mem_limit: 700m @@ -49,11 +47,28 @@ services: networks: - currency-network - zipkin-server: - image: openzipkin/zipkin:2.23 - mem_limit: 300m + rabbitmq: + image: rabbitmq:3-management + container_name: rabbitmq + ports: + - "5672:5672" + - "15672:15672" + environment: + - "TZ=@timezone@" + networks: + - currency-network + + zipkin: + image: openzipkin/zipkin:latest + container_name: zipkin + depends_on: + - rabbitmq + restart: always ports: - "9411:9411" + environment: + - "TZ=@timezone@" + - "RABBIT_URI=amqp://guest:guest@rabbitmq:5672" networks: - currency-network From 1c7346b6e62aa7cac5a9a461cb6b93ae7ba1ac74 Mon Sep 17 00:00:00 2001 From: Bishal Thapa Date: Sun, 16 Oct 2022 16:12:35 +0100 Subject: [PATCH 2/2] Update: Uses spring actuator probe, fix bug --- .../controller/CurrencyConversionController.java | 2 +- currency-exchange-service/deployment.yaml | 10 +++++++++- currency-exchange-service/pom.xml | 2 +- .../controller/CurrencyExchangeController.java | 2 +- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/currency-conversion-service/src/main/java/com/excercise/springmicroervice/currencyconversionservice/controller/CurrencyConversionController.java b/currency-conversion-service/src/main/java/com/excercise/springmicroervice/currencyconversionservice/controller/CurrencyConversionController.java index 3830681..d4f0cfe 100644 --- a/currency-conversion-service/src/main/java/com/excercise/springmicroervice/currencyconversionservice/controller/CurrencyConversionController.java +++ b/currency-conversion-service/src/main/java/com/excercise/springmicroervice/currencyconversionservice/controller/CurrencyConversionController.java @@ -52,6 +52,6 @@ public CurrencyConversion calculateConversionFeign(@PathVariable String from, @P currencyConversion.getEnvironment() + " feign"); logger.info("Total conversion: "+calculatedCurrencyConversion.getTotalCalculatedAmount().toString()); - return currencyConversion; + return calculatedCurrencyConversion; } } diff --git a/currency-exchange-service/deployment.yaml b/currency-exchange-service/deployment.yaml index c42f6cf..3ce5b3d 100644 --- a/currency-exchange-service/deployment.yaml +++ b/currency-exchange-service/deployment.yaml @@ -23,9 +23,17 @@ spec: app: currency-exchange spec: containers: - - image: devbith/spring-microservice-currency-exchange-service:0.0.1-SNAPSHOT + - 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 diff --git a/currency-exchange-service/pom.xml b/currency-exchange-service/pom.xml index e02c10b..739621b 100644 --- a/currency-exchange-service/pom.xml +++ b/currency-exchange-service/pom.xml @@ -10,7 +10,7 @@ com.excercise.spring-microervice currency-exchange-service - 0.0.1-SNAPSHOT + 0.0.2-SNAPSHOT currency-exchange-service currency-exchange-service diff --git a/currency-exchange-service/src/main/java/com/excercise/springmicroervice/currencyexchangeservice/controller/CurrencyExchangeController.java b/currency-exchange-service/src/main/java/com/excercise/springmicroervice/currencyexchangeservice/controller/CurrencyExchangeController.java index 4eee1da..1bdcccd 100644 --- a/currency-exchange-service/src/main/java/com/excercise/springmicroervice/currencyexchangeservice/controller/CurrencyExchangeController.java +++ b/currency-exchange-service/src/main/java/com/excercise/springmicroervice/currencyexchangeservice/controller/CurrencyExchangeController.java @@ -29,7 +29,7 @@ public CurrencyExchange retrieveExchange(@PathVariable String from, @PathVariabl final String port = environment.getProperty("local.server.port"); final String host = environment.getProperty("HOSTNAME"); - final String version = "v2"; + final String version = "v0.0.2"; Optional currencyExchangeOptional = currencyExchangeService.findByFromAndTo(from, to); if (currencyExchangeOptional.isPresent()) {