Config Client | Config Client | Config Client
\1 |2 /3
+-------------+
|Config Server|
+-------------+
-
Fetching values from application properties to a class variables.
@ConfigurationProperties("{name}")
-
Centralized Configuration of Microservice server
Dependency : Spring-Cloud-Config-Server
-
Adding git to config server application.
- higher priority
-
Server Configurations :
spring.cloud.config.server.git.uri=file:///{Current directory Path}
port : server.port={port} -
Clients Configurations :
Dependency : spring-cloud-starter-config
props : spring.config.import=optional:configserver:http://localhost:8888 -
Multiple Profiles in app.props (activate a profile):
spring.profiles.active={name}
spring.cloud.config.profile={name}
-
Currency Exchange Service (Microservice Example) :
-
link : http://localhost:8000/currency-exchange/from/USD/to/INR
-
Fetch environment values from a class
import org.springframework.core.env.Environment
-
Run multiple Instance of java app on different ports
add vm options in run config : [-Dserver.port={port}]
-
-
Currency Conversion Service (connect with exchange service) :
-
link : http://localhost:8100/currency-conversion/from/USD/to/INR/quantity/10
-
Get REst Data from different microservice via URI :
RestTemplate rt = new RestTemplate().{methods};
-
dependency : spring-cloud-starter-openfeign
Main class : @EnableFeignClients
Proxy Interface : @FeignClient(name={spring.app.name}, url={url})
-
-
Eureka Naming server (Registry for microservices name):
Dependency : spring-cloud-starter-netflix-eureka-client
Main class : @EnableEurekaServer- Disable self discovery of server
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false - link : http://localhost:8761
- Disable self discovery of server
-
Eureka Clients (service discovery) :
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
-
Load Balancing on client side (feign)
- remove url in @FeignClient(url=?) for auto load balancing.
-
Api Gateway Discovery Locator (authentication, authorization, logging, rate limiting)
Dependency : spring-cloud-starter-gateway
props : spring.cloud.gateway.discovery.locator.enabled=true -
Links (Initial) :
-
Fixing uppercase letters in links :
props : spring.cloud.gateway.discovery.locator.lower-case-service-id=true
-
Routes with Spring Cloud Gateway
- Define a @Configuration Class
- Define a @Bean of return type
RouteLocator
with parameters(RouteLocatorBuilder builder)
- Define routes :
return builder.routes()
.route(p -> p
.path("/get")
//GatewayFilterSpec filters
.filters(f -> f
.addRequestHeader("{MyHeader}", "{MyURI}")
.addRequestParameter("{MyParam}", "{MyValue}"))
.uri("http://httpbin.org/get"))
//predicate
.route(p -> p
.path("/{path}/**")
.uri("lb://{path}"))
//for rewriting paths
.route(p -> p
.path("/currency-conversion-new/**")
.filters(f -> f.rewritePath("currency-conversion-new/(?<segment>.*)", "/currency-conversion-feign/${segment}"))
.uri("lb://currency-conversion"))
.build();
- links :
-
Spring Cloud Gateway Logging Filter
class : implements gateway.filter.GlobalFilter implement method : Mono filter(ServerWebExchange exchange, GatewayFilterChain chain)
-
Circuit Breaker from Resilience4j
Dependency : resilience4j-spring-boot2
Dependency : spring-boot-starter-aop@Retry(name = "{name}")
on api method- fallback :
@Retry(fallbackMethod={implemented Method name with accepting Exception param})
- fallback :
- some properties :
resilience4j.retry.instances.{api-name}.max-attempts=5
resilience4j.retry.instances.{api-name}.wait-duration=2s
resilience4j.retry.instances.{api-name}.enable-exponential-backoff=true
-
Breaking Circuit for failing microservices (closed -> open <-> half_open -> closed):
@CircuitBreaker(name = "default", fallbackMethod = "fallbackRestTemplate")
props : resilience4j.circuitbreaker.instances.{name}.{property}={value} -
Rate Limiting
@RateLimiter(name = {name})
props : resilience4j.ratelimiter.instances.{name}.{property}={value} -
Allowing Concurrent requeusts
@BulkHead(name={name})
-
Docker commands
Tailing logs : logs {id} -f
Search Image : search {imageName}
Stopping container : pause / unpause, stop, kill
AutoRestart Container : run --restart=always
Events : events
Running process : top
container details : stats
cpu quota : run --cpu-quota={1-100000}
memory : run -m {memSize}{m/g}
docker daemon : system df -
Distributed Tracing
Tracing / Debugging
- Run zipkin on port 9411
-
OpenTelemetry
-
Dependencies :
Micrometer : micrometer-observation
OpenTelemetry : micrometer-tracing-bridge-otel
Zipkin : opentelemetry-exporter-zipkin -
Properties :
management.tracing.sampling.probability={,1.0}
logging.pattern.level=%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}] -
zipkin link : http://localhost:9411
-
Enable Tracing in feign api calls :
Dependency : feign-micrometer
-
Integrate micrometer with RestTemplate :
@Configuration(proxyBeanMethods = false) class RestTemplateConfiguration { //create RestTemplate using RestTemplateBuilder @Bean RestTemplate restTemplate(RestTemplateBuilder builder) { return builder.build(); } }
-
- Creating container image from pom.xml
<configuration> <image> <name>{imageName}</name> </image> <pullPolicy>IF_NOT_PRESENT</pullPolicy> //fixes {:} issue <docker> <host>//./pipe/dockerDesktopLinuxEngine</host> </docker> </configuration>
- run image: docker run -p 8000:8000 devraman/ms-currency-exchange-service:0.0.1-SNAPSHOT
## Day 10