Posts

Showing posts from July, 2024

Docker diff between CMD,RUN and ENTRYPOINT

 In Docker, ENTRYPOINT , RUN , and CMD are instructions used in a Dockerfile to define the behavior of the container. Each serves a different purpose: RUN Purpose :          RUN is used to execute commands               during the build process of the Docker image. Usage :                 It installs software packages, sets up the environment, and performs any necessary                                            configuration. The commands executed with RUN are not preserved after the image is built; they                contribute to the layers of the final image. Example : FROM ubuntu:latest RUN apt-get update && apt-get install -y curl CMD Purpose :  CMD specifies...

Understanding the spring boot container

Spring Boot simplifies the process of building and deploying applications  by providing an embedded server and a suite of pre-configured tools.  Here's a detailed overview to help you understand how Spring Boot handles containers and deployments: What is a Spring Boot Container? Embedded Server : Spring Boot applications come with an embedded server (such as Tomcat, Jetty, or Undertow).  This means you don't need to deploy your application to an external application server; the server is included in the application package. This allows you to run your application as a standalone Java application ( java -jar myapp.jar ). Dependency Management : Spring Boot provides a set of starter dependencies to simplify dependency management.  For example, spring-boot-starter-web includes all dependencies required to create a web application. These starters make it easy to get started with a variety of application types and components without needing to manually configure each dep...

Spring BeanNameAware interface

The BeanNameAware interface in Spring is part of the Spring Framework's mechanism  for providing beans with their own bean names.  It is an interface that a bean can implement to be aware of its own name within the Spring container. BeanNameAware Interface The BeanNameAware interface has a single method: public interface BeanNameAware { void setBeanName(String name); } When a bean implements this interface, Spring will call the setBeanName method with the name of the bean as defined in the Spring configuration. Usage of BeanNameAware Implementing the BeanNameAware interface can be useful in scenarios where a bean needs to know its own name for logging, debugging, or any other purpose. Here is an example to illustrate its use: import org.springframework.beans.factory.BeanNameAware; import org.springframework.stereotype.Component; @Component public class MyBean implements BeanNameAware { private String beanName; @Override public void setBeanName(String...

Spring Boot Autowiring

 In Spring Boot, autowiring is a feature that allows Spring to automatically resolve and inject collaborating beans (dependencies) into your beans.  There are several types of autowiring available in Spring Boot: Types of Autowiring No Autowiring ( no or default ) : The default setting where no autowiring is performed. Dependencies need to be explicitly specified using ref attribute in the bean configuration. By Type ( autowire="byType" ) : Spring container injects the dependency by matching the data type of the property to be autowired with the beans defined in the configuration file. If a bean of the matching type is found, it is injected. If there is more than one bean of that type, an error will be thrown unless a qualifier is specified. By Name ( autowire="byName" ) : Similar to byType, but the container looks for a bean with the same name as the property to be autowired. If a match is found, it injects the bean. If no matching bean is found, no injection occ...

Try With Resource block in java 7

 In Java, the try-with-resources statement  is a try statement that declares one or more resources.  A resource is an object that must be closed  after the program is finished with it.  The try-with-resources statement ensures  that each resource is closed at the end of the statement.  This feature was introduced in Java 7. import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class TryWithResourcesExample {     public static void main(String[] args) {         // Path to the file to be read         String filePath = "example.txt";         // Using try-with-resources to ensure the BufferedReader is closed automatically                   // FileReader::new with string filePath                     // BufferedReade::new with FileRe...

Zipkin with Spring Boot Example

Here’s a basic example of how you might instrument a simple web service with Zipkin in Java using Spring Boot and Sleuth: Add Dependencies : pom.xml <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> Configuration spring:   zipkin:     base-url: http://localhost:9411   sleuth:     sampler:       probability: 1.0 Creating Spans :

Zipkin and Instrumentation

 Here are key concepts and components of Zipkin and instrumentation: Key Concepts Span :  A span represents a single operation within a trace.  It includes information such as  the operation name, start and end timestamps,  and tags.(captured as event data) Trace :  A trace is a collection of spans  that share the same trace ID.  It represents the end-to-end latency of an entire request,  potentially across multiple services. Annotations :  These are timestamped records used to  denote events within a span, such as sending or receiving a message. Binary Annotations :  Key-value pairs associated with spans  that provide additional context. Components Instrumentation :  This involves adding code to your application to create spans and traces.  Many libraries and frameworks have built-in support for Zipkin, making it easier to instrument applications. Collector :  This component receives spans from instrumented...

ZIpkin

 Zipkin is an open-source distributed tracing system  designed to help gather timing data needed  to troubleshoot latency problems in microservice architectures.  Here's a brief overview of its key features and components: Key Features: Distributed Tracing:             Zipkin captures trace data from various services in a distributed system, allowing you to trace                   requests as they move from one service to another Latency Analysis:             It helps identify latency issues and bottlenecks by showing the duration of each service call within           a trace. Visualization:             Provides visual representations of trace data, making it easier to understand the flow of requests                across services. Inst...

Spring Boot Task Execution and Scheduling

If an  Executor  bean is not present in the context, Spring Boot auto-configures and adds a  ThreadPoolTaskExecutor  with sensible defaults. And it automatically associates to asynchronous task execution ( @EnableAsync ) and Spring MVC asynchronous request processing. If you have defined a custom  Executor  in the context, regular task execution (that is  @EnableAsync ) will use it transparently but the Spring MVC support will not be configured as it requires an  AsyncTaskExecutor  implementation (named  applicationTaskExecutor ). Depending on your target arrangement, you could change your  Executor  into a  ThreadPoolTaskExecutor  or define both a  ThreadPoolTaskExecutor  and an  AsyncConfigurer  wrapping your custom  Executor . The auto-configured  TaskExecutorBuilder  allows you to easily create instances that reproduce what the auto-configuration does by default.