π¨βπ» Full list what has been used:
Spring web framework
Spring WebFlux Reactive REST Services
gRPC Java gRPC
gRPC-Spring-Boot-Starter gRPC Spring Boot Starter
Salesforce Reactive gRPC Salesforce Reactive gRPC
Spring Data R2DBC a specification to integrate SQL databases using reactive drivers
Zipkin open source, end-to-end distributed tracing
Spring Cloud Sleuth autoconfiguration for distributed tracing
Prometheus monitoring and alerting
Grafana for to compose observability dashboards with everything from Prometheus
Kubernetes automating deployment, scaling, and management of containerized applications
Docker and docker-compose
Helm The package manager for Kubernetes
Flywaydb for migrations
Source code you can find in the GitHub repository.
For this project let's implement Spring microservice using gRPC and Postgresql.
Previously have the same one using Kotlin,
this on ll very close but using 17 Java and Spring WebFlux.
gRPC is very good for low latency and high throughput communication, so it's great for microservices where efficiency is critical.
Messages are encoded with Protobuf by default. While Protobuf is efficient to send and receive, its binary format.
Spring doesn't provide us gRPC starter out of the box, and we have to use community one, the most popular is yidongnan
and LogNet, both are good and ready to use,
for this project selected the first one.
For reactive gRPC available Salesforce reactive-grpc.
In the first step, we have to add gRPC Java Codegen Plugin for Protobuf Compiler.
All UI interfaces will be available on ports:
Swagger UI: http://localhost:8000/webjars/swagger-ui/index.html
Grafana UI: http://localhost:3000
Zipkin UI: http://localhost:9411
Prometheus UI: http://localhost:9090
Docker-compose file for this project:
version: "3.9"
services:
microservices_postgresql:
image: postgres:latest
container_name: microservices_postgresql
expose:
- "5432"
ports:
- "5432:5432"
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=bank_accounts
- POSTGRES_HOST=5432
command: -p 5432
volumes:
- ./docker_data/microservices_pgdata:/var/lib/postgresql/data
networks: [ "microservices" ]
prometheus:
image: prom/prometheus:latest
container_name: prometheus
ports:
- "9090:9090"
command:
- --config.file=/etc/prometheus/prometheus.yml
volumes:
- ./monitoring/prometheus.yml:/etc/prometheus/prometheus.yml:ro
networks: [ "microservices" ]
node_exporter:
container_name: microservices_node_exporter
restart: always
image: prom/node-exporter
ports:
- '9101:9100'
networks: [ "microservices" ]
grafana:
container_name: microservices_grafana
restart: always
image: grafana/grafana
ports:
- '3000:3000'
networks: [ "microservices" ]
zipkin:
image: openzipkin/zipkin:latest
restart: always
container_name: microservices_zipkin
ports:
- "9411:9411"
networks: [ "microservices" ]
networks:
microservices:
name: microservices
gRPC messages are serialized using Protobuf, an efficient binary message format, it serializes very quickly on the server and client,
and its serialization results in small message payloads, important in limited bandwidth scenarios like mobile apps.
The interface contract for specifying the RPC definitions for each service would be defined using Protocol Buffers.
Each microservice will have a proto file defined here for this.
At the first we have to define a service in a proto file and compile it, it has at most unary methods and one server streaming:
syntax = "proto3";
package com.example.grpc.bank.service;
import "google/protobuf/wrappers.proto";
import "google/protobuf/timestamp.proto";
service BankAccountService {
rpc createBankAccount (CreateBankAccountRequest) returns (CreateBankAccountResponse);
rpc getBankAccountById (GetBankAccountByIdRequest) returns (GetBankAccountByIdResponse);
rpc depositBalance (DepositBalanceRequest) returns (DepositBalanceResponse);
rpc withdrawBalance (WithdrawBalanceRequest) returns (WithdrawBalanceResponse);
rpc getAllByBalance (GetAllByBalanceRequest) returns (stream GetAllByBalanceResponse);
rpc getAllByBalanceWithPagination(GetAllByBalanceWithPaginationRequest) returns (GetAllByBalanceWithPaginationResponse);
}
message BankAccountData {
string id = 1;
string firstName = 2;
string lastName = 3;
string email = 4;
string address = 5;
string currency = 6;
string phone = 7;
double balance = 8;
string createdAt = 9;
string updatedAt = 10;
}
message CreateBankAccountRequest {
string email = 1;
string firstName = 2;
string lastName = 3;
string address = 4;
string currency = 5;
string phone = 6;
double balance = 7;
}
message CreateBankAccountResponse {
BankAccountData bankAccount = 1;
}
message GetBankAccountByIdRequest {
string id = 1;
}
message GetBankAccountByIdResponse {
BankAccountData bankAccount = 1;
}
message DepositBalanceRequest {
string id = 1;
double balance = 2;
}
message DepositBalanceResponse {
BankAccountData bankAccount = 1;
}
message WithdrawBalanceRequest {
string id = 1;
double balance = 2;
}
message WithdrawBalanceResponse {
BankAccountData bankAccount = 1;
}
message GetAllByBalanceRequest {
double min = 1;
double max = 2;
int32 page = 3;
int32 size = 4;
}
message GetAllByBalanceResponse {
BankAccountData bankAccount = 1;
}
message GetAllByBalanceWithPaginationRequest {
double min = 1;
double max = 2;
int32 page = 3;
int32 size = 4;
}
message GetAllByBalanceWithPaginationResponse {
repeated BankAccountData bankAccount = 1;
int32 page = 2;
int32 size = 3;
int32 totalElements = 4;
int32 totalPages = 5;
bool isFirst = 6;
bool isLast = 7;
}
The actual maven dependencies for gRPC:
<dependencies>
<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-server-spring-boot-starter</artifactId>
<version>2.13.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.salesforce.servicelibs</groupId>
<artifactId>reactor-grpc-stub</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>${java.grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>${java.grpc.version}</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
<version>3.21.7</version>
</dependency>
</dependencies>
And the maven protobuf plugin:
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:${protobuf.protoc.version}:exe:${os.detected.classifier}
</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${java.grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
<protocPlugins>
<protocPlugin>
<id>reactor-grpc</id>
<groupId>com.salesforce.servicelibs</groupId>
<artifactId>reactor-grpc</artifactId>
<version>1.2.3</version>
<mainClass>com.salesforce.reactorgrpc.ReactorGrpcGenerator</mainClass>
</protocPlugin>
</protocPlugins>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
The plugin generates a class for each of your gRPC services.
For example: ReactorBankAccountServiceGrpc where BankAccountGrpcService is the name of the gRPC service in the proto file.
This class contains both the client stubs and the server ImplBase that you will need to extend.
After compilation is done, we can implement out gRPC service.
@GrpcService allows us to pass a list of interceptors specific to this service, so we can add LogGrpcInterceptor here.
For request validation let's use spring-boot-starter-validation which uses Hibernate Validator
@Slf4j
@GrpcService(interceptors = {LogGrpcInterceptor.class})
@RequiredArgsConstructor
public class BankAccountGrpcService extends ReactorBankAccountServiceGrpc.BankAccountServiceImplBase {
private final BankAccountService bankAccountService;
private final Tracer tracer;
private static final Long TIMEOUT_MILLIS = 5000L;
private final Validator validator;
@Override
@NewSpan
public Mono<CreateBankAccountResponse> createBankAccount(Mono<CreateBankAccountRequest> request) {
return request.flatMap(req -> bankAccountService.createBankAccount(validate(BankAccountMapper.of(req)))
.doOnNext(v -> spanTag("req", req.toString())))
.map(bankAccount -> CreateBankAccountResponse.newBuilder().setBankAccount(BankAccountMapper.toGrpc(bankAccount)).build())
.timeout(Duration.ofMillis(TIMEOUT_MILLIS))
.doOnError(this::spanError)
.doOnSuccess(result -> log.info("created account: {}", result.getBankAccount()));
}
@Override
@NewSpan
public Mono<GetBankAccountByIdResponse> getBankAccountById(Mono<GetBankAccountByIdRequest> request) {
return request.flatMap(req -> bankAccountService.getBankAccountById(UUID.fromString(req.getId()))
.doOnNext(v -> spanTag("id", req.getId()))
.doOnSuccess(bankAccount -> spanTag("bankAccount", bankAccount.toString()))
.map(bankAccount -> GetBankAccountByIdResponse.newBuilder().setBankAccount(BankAccountMapper.toGrpc(bankAccount)).build()))
.timeout(Duration.ofMillis(TIMEOUT_MILLIS))
.doOnError(this::spanError)
.doOnSuccess(response -> log.info("bankAccount: {}", response.getBankAccount()));
}
@Override
@NewSpan
public Mono<DepositBalanceResponse> depositBalance(Mono<DepositBalanceRequest> request) {
return request
.flatMap(req -> bankAccountService.depositAmount(UUID.fromString(req.getId()), BigDecimal.valueOf(req.getBalance()))
.doOnEach(v -> spanTag("req", req.toString()))
.map(bankAccount -> DepositBalanceResponse.newBuilder().setBankAccount(BankAccountMapper.toGrpc(bankAccount)).build()))
.timeout(Duration.ofMillis(TIMEOUT_MILLIS))
.doOnError(this::spanError)
.doOnSuccess(response -> log.info("bankAccount: {}", response.getBankAccount()));
}
@Override
@NewSpan
public Mono<WithdrawBalanceResponse> withdrawBalance(Mono<WithdrawBalanceRequest> request) {
return request.flatMap(req -> bankAccountService.withdrawAmount(UUID.fromString(req.getId()), BigDecimal.valueOf(req.getBalance()))
.doOnNext(v -> spanTag("req", req.toString()))
.map(bankAccount -> WithdrawBalanceResponse.newBuilder().setBankAccount(BankAccountMapper.toGrpc(bankAccount)).build()))
.timeout(Duration.ofMillis(TIMEOUT_MILLIS))
.doOnError(this::spanError)
.doOnSuccess(response -> log.info("bankAccount: {}", response.getBankAccount()));
}
@Override
@NewSpan
public Flux<GetAllByBalanceResponse> getAllByBalance(Mono<GetAllByBalanceRequest> request) {
return request
.flatMapMany(req -> bankAccountService.findBankAccountByBalanceBetween(BankAccountMapper.findByBalanceRequestDtoFromGrpc(req))
.doOnNext(v -> spanTag("req", req.toString()))
.map(bankAccount -> GetAllByBalanceResponse.newBuilder().setBankAccount(BankAccountMapper.toGrpc(bankAccount)).build()))
.timeout(Duration.ofMillis(TIMEOUT_MILLIS))
.doOnError(this::spanError)
.doOnNext(response -> log.info("bankAccount: {}", response.getBankAccount()));
}
@Override
@NewSpan
public Mono<GetAllByBalanceWithPaginationResponse> getAllByBalanceWithPagination(Mono<GetAllByBalanceWithPaginationRequest> request) {
return request.flatMap(req -> bankAccountService.findAllBankAccountsByBalance(BankAccountMapper.findByBalanceRequestDtoFromGrpc(req))
.doOnNext(v -> spanTag("req", req.toString()))
.map(BankAccountMapper::toPaginationGrpcResponse))
.timeout(Duration.ofMillis(TIMEOUT_MILLIS))
.doOnError(this::spanError)
.doOnNext(response -> log.info("response: {}", response.toString()));
}
private <T> T validate(T data) {
var errors = validator.validate(data);
if (!errors.isEmpty()) throw new ConstraintViolationException(errors);
return data;
}
private void spanTag(String key, String value) {
var span = tracer.currentSpan();
if (span != null) span.tag(key, value);
}
private void spanError(Throwable ex) {
var span = tracer.currentSpan();
if (span != null) span.error(ex);
}
}
Interceptors are a gRPC concept that allows apps to interact with incoming or outgoing gRPC calls.
They offer a way to enrich the request processing pipeline.
We can add gRPC interceptors, here we implement LogGrpcInterceptor:
@Slf4j
public class LogGrpcInterceptor implements ServerInterceptor {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
log.info("service: {}, method: {}, headers: {}", call.getMethodDescriptor().getServiceName(), call.getMethodDescriptor().getBareMethodName(), headers.toString());
return next.startCall(call, headers);
}
}
and add it to the global GrpcGlobalServerInterceptor:
@Configuration(proxyBeanMethods = false)
public class GlobalInterceptorConfiguration {
@GrpcGlobalServerInterceptor
public LogGrpcInterceptor logServerInterceptor() {
return new LogGrpcInterceptor();
}
}
The service layer of the microservice has a few methods, for example, working with lists of data it has two methods,
one which returns PageImpl used in unary method response and one returns Flow for gRPC streaming response method.
The current Spring version supports @Transactional annotation with R2DBC
The interface and implementation are below:
public interface BankAccountService {
Mono<BankAccount> createBankAccount(BankAccount bankAccount);
Mono<BankAccount> getBankAccountById(UUID id);
Mono<BankAccount> depositAmount(UUID id, BigDecimal amount);
Mono<BankAccount> withdrawAmount(UUID id, BigDecimal amount);
Flux<BankAccount> findBankAccountByBalanceBetween(FindByBalanceRequestDto request);
Mono<Page<BankAccount>> findAllBankAccountsByBalance(FindByBalanceRequestDto request);
}
@Slf4j
@Service
@RequiredArgsConstructor
public class BankAccountServiceImpl implements BankAccountService {
private final BankAccountRepository bankAccountRepository;
private final Tracer tracer;
@Override
@Transactional
@NewSpan
public Mono<BankAccount> createBankAccount(@SpanTag(key = "bankAccount") BankAccount bankAccount) {
return bankAccountRepository.save(bankAccount)
.doOnSuccess(savedBankAccount -> spanTag("savedBankAccount", savedBankAccount.toString()))
.doOnError(this::spanError);
}
@Override
@Transactional(readOnly = true)
@NewSpan
public Mono<BankAccount> getBankAccountById(@SpanTag(key = "id") UUID id) {
return bankAccountRepository.findById(id)
.doOnEach(v -> spanTag("id", id.toString()))
.switchIfEmpty(Mono.error(new BankAccountNotFoundException(id.toString())))
.doOnError(this::spanError);
}
@Override
@Transactional
@NewSpan
public Mono<BankAccount> depositAmount(@SpanTag(key = "id") UUID id, @SpanTag(key = "amount") BigDecimal amount) {
return bankAccountRepository.findById(id)
.switchIfEmpty(Mono.error(new BankAccountNotFoundException(id.toString())))
.flatMap(bankAccount -> bankAccountRepository.save(bankAccount.depositBalance(amount)))
.doOnError(this::spanError)
.doOnNext(bankAccount -> spanTag("bankAccount", bankAccount.toString()))
.doOnSuccess(bankAccount -> log.info("updated bank account: {}", bankAccount));
}
@Override
@Transactional
@NewSpan
public Mono<BankAccount> withdrawAmount(@SpanTag(key = "id") UUID id, @SpanTag(key = "amount") BigDecimal amount) {
return bankAccountRepository.findById(id)
.switchIfEmpty(Mono.error(new BankAccountNotFoundException(id.toString())))
.flatMap(bankAccount -> bankAccountRepository.save(bankAccount.withdrawBalance(amount)))
.doOnError(this::spanError)
.doOnNext(bankAccount -> spanTag("bankAccount", bankAccount.toString()))
.doOnSuccess(bankAccount -> log.info("updated bank account: {}", bankAccount));
}
@Override
@Transactional(readOnly = true)
@NewSpan
public Flux<BankAccount> findBankAccountByBalanceBetween(@SpanTag(key = "request") FindByBalanceRequestDto request) {
return bankAccountRepository.findBankAccountByBalanceBetween(request.min(), request.max(), request.pageable())
.doOnError(this::spanError);
}
@Override
@Transactional(readOnly = true)
@NewSpan
public Mono<Page<BankAccount>> findAllBankAccountsByBalance(@SpanTag(key = "request") FindByBalanceRequestDto request) {
return bankAccountRepository.findAllBankAccountsByBalance(request.min(), request.max(), request.pageable())
.doOnError(this::spanError)
.doOnSuccess(result -> log.info("result: {}", result.toString()));
}
private void spanTag(String key, String value) {
Optional.ofNullable(tracer.currentSpan()).ifPresent(span -> span.tag(key, value));
}
private void spanError(Throwable ex) {
Optional.ofNullable(tracer.currentSpan()).ifPresent(span -> span.error(ex));
}
}
R2DBC is an API which provides reactive, non-blocking APIs for relational databases.
Using this, you can have your reactive APIs in Spring Boot read and write information to the database in a reactive/asynchronous way.
The BankRepository is combination of ReactiveSortingRepository from spring data and our custom BankPostgresRepository implementation.
For our custom BankPostgresRepository implementation used here R2dbcEntityTemplate and DatabaseClient.
If we want to have same pagination response like JPA provide,
we have to manually create PageImpl.
public interface BankAccountRepository extends ReactiveSortingRepository<BankAccount, UUID>, BankAccountPostgresRepository {
Flux<BankAccount> findBankAccountByBalanceBetween(BigDecimal min, BigDecimal max, Pageable pageable);
}
public interface BankAccountPostgresRepository {
Mono<Page<BankAccount>> findAllBankAccountsByBalance(BigDecimal min, BigDecimal max, Pageable pageable);
}
@Slf4j
@Repository
@RequiredArgsConstructor
public class BankAccountPostgresRepositoryImpl implements BankAccountPostgresRepository {
private final DatabaseClient databaseClient;
private final R2dbcEntityTemplate template;
private final Tracer tracer;
@Override
@NewSpan
public Mono<Page<BankAccount>> findAllBankAccountsByBalance(@SpanTag(key = "min") BigDecimal min,
@SpanTag(key = "max") BigDecimal max,
@SpanTag(key = "pageable") Pageable pageable) {
var query = Query.query(Criteria.where(BALANCE).between(min, max)).with(pageable);
var listMono = template.select(query, BankAccount.class).collectList()
.doOnError(this::spanError)
.doOnSuccess(list -> spanTag("list", String.valueOf(list.size())));
var totalCountMono = databaseClient.sql("SELECT count(bank_account_id) as total FROM microservices.bank_accounts WHERE balance BETWEEN :min AND :max")
.bind("min", min)
.bind("max", max)
.fetch()
.one()
.doOnError(this::spanError)
.doOnSuccess(totalCount -> spanTag("totalCount", totalCount.toString()));
return Mono.zip(listMono, totalCountMono).map(tuple -> new PageImpl<>(tuple.getT1(), pageable, (Long) tuple.getT2().get("total")));
}
private void spanTag(String key, String value) {
Optional.ofNullable(tracer.currentSpan()).ifPresent(span -> span.tag(key, value));
}
private void spanError(Throwable ex) {
Optional.ofNullable(tracer.currentSpan()).ifPresent(span -> span.error(ex));
}
}
For errors handling gRPC starter provide us GrpcAdvice which marks a class to be checked up for exception handling methods,
@GrpcExceptionHandler marks the annotated method to be executed, in case of the specified exception being thrown,
status codes are good described here
@GrpcAdvice
@Slf4j
public class GrpcExceptionAdvice {
@GrpcExceptionHandler(RuntimeException.class)
public StatusException handleRuntimeException(RuntimeException ex) {
var status = Status.INTERNAL.withDescription(ex.getLocalizedMessage()).withCause(ex);
log.error("(GrpcExceptionAdvice) RuntimeException: ", ex);
return status.asException();
}
@GrpcExceptionHandler(BankAccountNotFoundException.class)
public StatusException handleBankAccountNotFoundException(BankAccountNotFoundException ex) {
var status = Status.NOT_FOUND.withDescription(ex.getLocalizedMessage()).withCause(ex);
log.error("(GrpcExceptionAdvice) BankAccountNotFoundException: ", ex);
return status.asException();
}
@GrpcExceptionHandler(InvalidAmountException.class)
public StatusException handleInvalidAmountException(InvalidAmountException ex) {
var status = Status.INVALID_ARGUMENT.withDescription(ex.getLocalizedMessage()).withCause(ex);
log.error("(GrpcExceptionAdvice) InvalidAmountException: ", ex);
return status.asException();
}
@GrpcExceptionHandler(DataAccessException.class)
public StatusException handleDataAccessException(DataAccessException ex) {
var status = Status.INVALID_ARGUMENT.withDescription(ex.getLocalizedMessage()).withCause(ex);
log.error("(GrpcExceptionAdvice) DataAccessException: ", ex);
return status.asException();
}
@GrpcExceptionHandler(ConstraintViolationException.class)
public StatusException handleConstraintViolationException(ConstraintViolationException ex) {
var status = Status.INVALID_ARGUMENT.withDescription(ex.getLocalizedMessage()).withCause(ex);
log.error("(GrpcExceptionAdvice) ConstraintViolationException: ", ex);
return status.asException();
}
@GrpcExceptionHandler(MethodArgumentNotValidException.class)
public StatusException handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
var status = Status.INVALID_ARGUMENT.withDescription(ex.getLocalizedMessage()).withCause(ex);
log.error("(GrpcExceptionAdvice) MethodArgumentNotValidException: ", ex);
return status.asException();
}
@GrpcExceptionHandler(IllegalArgumentException.class)
public StatusException handleIllegalArgumentException(IllegalArgumentException ex) {
var status = Status.INVALID_ARGUMENT.withDescription(ex.getLocalizedMessage()).withCause(ex);
log.error("(GrpcExceptionAdvice) IllegalArgumentException: ", ex);
return status.asException();
}
}
For working with gRPC available few UI clients, personally like to use BloomRPC,
another usefully tools is grpcurl and grpcui.
Next step let's deploy our microservice to k8s,
we can build a docker image in different ways, in this example using a simple multistage docker file:
FROM --platform=linux/arm64 azul/zulu-openjdk-alpine:17 as builder
ARG JAR_FILE=target/spring-webflux-grpc-0.0.1-SNAPSHOT.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract
FROM azul/zulu-openjdk-alpine:17
COPY --from=builder dependencies/ ./
COPY --from=builder snapshot-dependencies/ ./
COPY --from=builder spring-boot-loader/ ./
COPY --from=builder application/ ./
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher", "-XX:MaxRAMPercentage=75", "-XX:+UseG1GC"]
For working with k8s like to use Helm, deployment for the microservice is simple and has deployment itself, Service, ConfigMap
and ServiceMonitor.
The last one is required because for monitoring use kube-prometheus-stack helm chart
Microservice helm chart yaml file is:
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Values.microservice.name }}
labels:
app: {{ .Values.microservice.name }}
spec:
replicas: {{ .Values.microservice.replicas }}
template:
metadata:
name: {{ .Values.microservice.name }}
labels:
app: {{ .Values.microservice.name }}
spec:
containers:
- name: {{ .Values.microservice.name }}
image: {{ .Values.microservice.image }}
imagePullPolicy: Always
resources:
requests:
memory: {{ .Values.microservice.resources.requests.memory }}
cpu: {{ .Values.microservice.resources.requests.cpu }}
limits:
memory: {{ .Values.microservice.resources.limits.memory }}
cpu: {{ .Values.microservice.resources.limits.cpu }}
livenessProbe:
httpGet:
port: {{ .Values.microservice.livenessProbe.httpGet.port }}
path: {{ .Values.microservice.livenessProbe.httpGet.path }}
initialDelaySeconds: {{ .Values.microservice.livenessProbe.initialDelaySeconds }}
periodSeconds: {{ .Values.microservice.livenessProbe.periodSeconds }}
readinessProbe:
httpGet:
port: {{ .Values.microservice.readinessProbe.httpGet.port }}
path: {{ .Values.microservice.readinessProbe.httpGet.path }}
initialDelaySeconds: {{ .Values.microservice.readinessProbe.initialDelaySeconds }}
periodSeconds: {{ .Values.microservice.readinessProbe.periodSeconds }}
ports:
- containerPort: {{ .Values.microservice.ports.http.containerPort }}
name: {{ .Values.microservice.ports.http.name }}
- containerPort: {{ .Values.microservice.ports.grpc.containerPort}}
name: {{ .Values.microservice.ports.grpc.name }}
env:
- name: SPRING_APPLICATION_NAME
value: microservice_k8s
- name: JAVA_OPTS
value: "-XX:+UseG1GC -XX:MaxRAMPercentage=75"
- name: SERVER_PORT
valueFrom:
configMapKeyRef:
key: server_port
name: {{ .Values.microservice.name }}-config-map
- name: GRPC_SERVER_PORT
valueFrom:
configMapKeyRef:
key: grpc_server_port
name: {{ .Values.microservice.name }}-config-map
- name: SPRING_ZIPKIN_BASE_URL
valueFrom:
configMapKeyRef:
key: zipkin_base_url
name: {{ .Values.microservice.name }}-config-map
- name: SPRING_R2DBC_URL
valueFrom:
configMapKeyRef:
key: r2dbc_url
name: {{ .Values.microservice.name }}-config-map
- name: SPRING_FLYWAY_URL
valueFrom:
configMapKeyRef:
key: flyway_url
name: {{ .Values.microservice.name }}-config-map
restartPolicy: Always
terminationGracePeriodSeconds: {{ .Values.microservice.terminationGracePeriodSeconds }}
selector:
matchLabels:
app: {{ .Values.microservice.name }}
---
apiVersion: v1
kind: Service
metadata:
name: {{ .Values.microservice.name }}-service
labels:
app: {{ .Values.microservice.name }}
spec:
selector:
app: {{ .Values.microservice.name }}
ports:
- port: {{ .Values.microservice.service.httpPort }}
name: http
protocol: TCP
targetPort: http
- port: {{ .Values.microservice.service.grpcPort }}
name: grpc
protocol: TCP
targetPort: grpc
type: ClusterIP
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
labels:
release: monitoring
name: {{ .Values.microservice.name }}-service-monitor
namespace: default
spec:
selector:
matchLabels:
app: {{ .Values.microservice.name }}
endpoints:
- interval: 10s
port: http
path: /actuator/prometheus
namespaceSelector:
matchNames:
- default
---
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Values.microservice.name }}-config-map
data:
server_port: "8080"
grpc_server_port: "8000"
zipkin_base_url: zipkin:9411
r2dbc_url: "r2dbc:postgresql://postgres:5432/bank_accounts"
flyway_url: "jdbc:postgresql://postgres:5432/bank_accounts"
and Values.yaml file:
microservice:
name: spring-webflux-grpc-microservice
image: alexanderbryksin/spring_webflux_grpc_microservice:latest
replicas: 1
livenessProbe:
httpGet:
port: 8080
path: /actuator/health/liveness
initialDelaySeconds: 60
periodSeconds: 5
readinessProbe:
httpGet:
port: 8080
path: /actuator/health/readiness
initialDelaySeconds: 60
periodSeconds: 5
ports:
http:
name: http
containerPort: 8080
grpc:
name: grpc
containerPort: 8000
terminationGracePeriodSeconds: 20
service:
httpPort: 8080
grpcPort: 8000
resources:
requests:
memory: '6000Mi'
cpu: "3000m"
limits:
memory: '6000Mi'
cpu: "3000m"
As UI tool for working with k8s, personally like to use Lens.
More details and source code of the full project you can find GitHub repository here,
of course always in real-world projects, business logic and infrastructure code is much more complicated, and we have to implement many more necessary features.
I hope this article is usefully and helpfully, and be happy to receive any feedback or questions, feel free to contact me by email or any messengers :)
Top comments (0)