SpringBoot App CLI start up
- Run Spring Boot app using Maven
Spring Boot provide the plugin with maven
go to your project directory and run
>> mvn spring-boot:run
- Run Spring Boot app with java -jar command
>> java -jar target/app-0.0.1-SNAPSHOT.jar
- Run Spring Boot App with Gradle
>> gradle bootRun
Spring Cloud architecture
API gateway
A gateway can take care of securing and routing messages, hiding services, throttling load, and many other useful things. Spring Cloud Gateway gives control of API layer, integrating Spring Cloud service discovery and client-side load-balancing solutions to simplify configuration and maintenance.
Service Registry
A service registry is a database used to keep track of the available instances of each microservice in an application.
The service registry needs to be updated each time a new service comes online and whenever a service is taken offline or becomes unavailable.
Spring Bean Scopes
singleton - only one instance of the spring bean will be created for the spring container. This is the default spring bean scope.
If a scope is set to singleton, the Spring IoC container creates exactly one instance of the object defined by that bean definition.This single instance is stored in a cache of such singleton beans, and all subsequent requests and references for that named bean return the cached object.
When the bean scope is set to prototype , the Spring IoC container creates a new bean instance of an object every time a bean is requested.
Prototype scope is used for all beans that are stateful, while the singleton scope should be used for stateless beans.
@Bean is just for the metadata definition to create the bean(equivalent to tag).
@Autowired is to inject the dependancy into a bean(equivalent to ref XML tag/attribute)
Classes annotated with @Controller , @RestController , @Repository or @Service are singletons by default.
Is singleton bean thread safe?
Because the singleton isn't thread-safe, calls to its prototype methods may also run concurrently. When several threads share the singleton, the single instance of the prototype that Spring injects to that singleton will also be shared.
Why do we use @autowired annotation?
Spring @Autowired annotation is used for automatic dependency injection. Spring framework is built on dependency injection and we inject the class dependencies through spring bean configuration file.
What is the difference between @bean and @component?
@Component is a class-level annotation, but @Bean is at the method level, so @Component is only an option when a class's source code is editable.
@Bean can always be used, but it's more verbose.
@Component is compatible with Spring's auto-detection, but @Bean requires manual class instantiation.
What is difference between Java singleton and Spring singleton?
Spring singleton is different than Java singleton.
In java, one instance of the bean is created per JVM whereas in spring, one instance of the bean is created per application context.
What is singleton vs request scope?
Request scoped beans are short living instances of a class, they will be created when a new request comes in.
What is singleton vs request scope?
Request scoped beans are short living instances of a class, they will be created when a new request comes in.
Singleton beans live the entire lifetime of your application.
Note: If you have a multi user application with several sessions, all users will access the same instance of your beans, if they are singletons.
eg:
public class SingleObject {
private static SingleObject instance = new SingleObject();
private SingleObject(){}
public static SingleObject getInstance(){
return instance;
}
}
Spring Boot is a project that is built on the top of the Spring Framework.
Spring Boot is a Spring module that provides the RAD (Rapid Application Development) feature to the Spring framework.
Spring Boot is the combination of Spring Framework and Embedded Servers.
Limitations of Spring Boot
Spring Boot can use dependencies that are not going to be used in the application, these dependencies increase the size of the application.
Spring Boot provides a number of starters that allow us to add jars in the classpath.
Spring Boot built-in starters make development easier and rapid.
Spring Boot Starters are the dependency descriptors.
All the starters follow a similar naming pattern: spring-boot-starter-*, where * denotes a particular type of application.
spring-boot-starter-actuator = provides production-ready features to help monitor and manage your application.
Spring Data JPA
Spring Data is a high-level Spring Source project.
Spring Data JPA adds a layer on the top of JPA.
Spring Data JPA handles most of the complexity of JDBC-based database access and ORM (Object Relational Mapping).
Spring Data JPA uses all features defined by JPA specification, especially the entity, association mappings, and JPA's query capabilities.
Spring Data JPA adds its own features such as the no-code implementation of the repository pattern and the creation of database queries from the method name.
Spring Data JPA provides three repositories are as follows:
1) CrudRepository
2) PagingAndSortingRepository
3) JpaRepository
Spring Boot Starter Actuator
There are three main features of Spring Boot Actuator:
1) Endpoints - allows us to monitor and interact with the application.
2) Metrics - provides dimensional metrics by integrating with the micrometer.
3) Audit -provides a flexible audit framework that publishes events to an AuditEventRepository. It automatically publishes the authentication events if spring-security is in execution.
Enable actuator by injecting the dependency spring-boot-starter-actuator in the pom.xml file.
Spring Boot DevTools pick up the changes and restart the application.
Spring Boot DevTools provides the following features:
1) Property Defaults
2) Automatic Restart
3) LiveReload
4) Remote Debug Tunneling
5) Remote Update and Restart
Spring Boot Caching
In Spring, the cache abstraction is a mechanism that allows consistent use of various caching methods with minimal impact on the code.
Cache Abstraction
The cache abstraction mechanism applies to Java methods.
The main objective of using cache abstraction is to reduce the number of executions based on the information present in the cache.
It applies to expensive methods such as CPU or IO bound.
Types of Caching
There are four types of caching are as follows:
1) In-memory Caching [eg: Memcached and Redis]
2) Database Caching
3) Web server Caching
4) CDN Caching - Content Delivery Network.
Spring Boot Cache Annotations
@EnableCaching [class-level annotation]
@CacheConfig(cacheNames={"employee"}) [class-level annotation]
@CachePut or @CacheEvict
@Cacheable [skips the method execution]
@CacheEvict [method level]
@CachePut [runs the method]
dependency - spring-boot-starter-cache
Fault Tolerance
The solution to use a fallback in case of failure of a microservice, is called fault tolerance.
Fault tolerance can be achieved with the help of a circuit breaker. It is a pattern that wraps requests to external services and detects when they fail.
If a failure is detected, the circuit breaker opens.
All the subsequent requests immediately return an error instead of making requests to the unhealthy service.
It monitors and detects the service which is down and misbehaves with other services.
It rejects calls until it becomes healthy again.
Hystrix
Hystrix is a library that controls the interaction between microservices to provide latency and fault tolerance.
dependency - spring-cloud-starter-netflix-hystrix
@EnableHystrix
@HystrixCommand(fallbackMethod="fallbackRetrieveConfigurations")
Comments
Post a Comment