Showing posts with label spring. Show all posts
Showing posts with label spring. Show all posts

Friday 16 June 2023

Best micro-service gateways available in spring boot application

 When it comes to choosing the best microservice gateway in a Spring Boot application, there are several popular options available. The choice depends on your specific requirements and preferences. Here are a few notable microservice gateway options for Spring Boot:

  1. Spring Cloud Gateway: Spring Cloud Gateway is a lightweight, developer-friendly gateway built on top of Spring Boot and Spring WebFlux. It provides a powerful routing and filtering mechanism, making it easy to build scalable and resilient microservice architectures. Spring Cloud Gateway integrates well with the Spring ecosystem and supports features like rate limiting, circuit breaking, and service discovery.

    <!-- Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Cloud Starter Gateway --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <!-- Additional Spring Cloud dependencies --> <!-- Add other Spring Cloud dependencies as per your requirements --> </dependencies><dependencies>
  2. Netflix Zuul: Netflix Zuul is a battle-tested microservice gateway developed by Netflix. While it's not explicitly built on Spring Boot, it can be used with Spring Cloud to leverage its features. Zuul offers robust routing and filtering capabilities, integrates with Netflix Eureka for service discovery, and supports features like dynamic routing, request/response modification, and centralized configuration.

    <!-- Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Netflix Zuul Dependency --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <!-- Additional dependencies --> <!-- Add other dependencies as per your requirements --> </dependencies><dependencies>
  3. Kong: Kong is an open-source API gateway that can be used as a microservice gateway in Spring Boot applications. It provides features like routing, load balancing, authentication, rate limiting, and logging. Kong can be deployed as a standalone service or as part of a containerized infrastructure, and it offers various plugins to extend its functionality.<dependencies>

    <!-- Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Kong Dependency --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-kong</artifactId> </dependency> <!-- Additional dependencies --> <!-- Add other dependencies as per your requirements --> </dependencies>
  4. Ambassador: Ambassador is a Kubernetes-native API gateway that can be used with Spring Boot microservices running on Kubernetes. It provides features like edge routing, traffic management, authentication, and observability. Ambassador integrates well with Kubernetes and can leverage its service discovery and routing capabilities.<dependencies>

    <!-- Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Ambassador Dependency --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-kubernetes-ribbon</artifactId> </dependency> <!-- Additional dependencies --> <!-- Add other dependencies as per your requirements --> </dependencies>

These are just a few examples of microservice gateway options for Spring Boot. It's essential to evaluate your specific requirements, such as performance, scalability, security, and ease of integration, when choosing the best gateway for your project. Consider factors like community support, documentation, and compatibility with your existing infrastructure while making your decision.

Sunday 7 April 2019

Singleton Bean working in Spring

There are five main scopes for creating a bean in spring.
  1. singleton
  2. prototype
  3. request
  4. session
  5. global-session
 Singleton is default scope type if we don't mention any scope then its singleton by default. A general perception about spring singleton bean is that it behaves same as java singleton class but there are few fundamental differences. Spring IoC container creates only one object for singleton bean and store it in cache memory of all singleton beans. All requests for bean with same name or id will get the same object, It does not mean that class defined as bean is singleton also. lets take an example where we create two beans for same class with different bean name and id then spring will create two different objects in shared memory and will return object based upon name or id given in request.

@Configuration
public class AppConfig {
    @Bean(name={"sineltonTest"})
    @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
    public singeltonTest getSingeltonTest() {
        return new singeltonTest();
    }
   
    @Bean (name={"sineltonTest1"})
    @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
    public singeltonTest getSingeltonTest1() {
        return new singeltonTest();
    }
}

In above example spring will create two different objects for two beans ("sineltonTest", "sineltonTest1") in shared cache and if we use @Autowired with @Qualifire("singeltonTest1") then it will get object from cache created for bean with name "sineltonTest1" so bean is sigleton not the class. There is one more catch w.r.t singleton bean in spring that what happen when we create object of that class with new operator instead of @autowired? This question is asked basically to confuse the person in interview. The clear answer is that spring will not return object from cache now it depends upon exact implementation of the bean class if it is created as java singleton class then one instance is created and every time same instance will be returned otherwise every time new instance is created and returned.