how to enable cache across spring boot app instances of same service? - spring-boot

We would like to enable caching in spring boot app.
I have followed and enabled it using following link:
https://howtodoinjava.com/spring-boot2/spring-boot-cache-example/
when I ran the spring boot app as multiple instances (on different ports like 8081, 8082, ...),
its creating each cache for each instance whenever its turn came.
how to enable cache across spring boot app instances ?

You can't. Each time you run the application, new server starts which maintains its own memory cache.
If you want a common cache, install a separate server, such as Redis, in your system and configure spring app with that. Then, it can be commonly accessed for all instances.

Related

(How) Can I run more than one Spring Boot application on the same server and port?

I have a Spring Boot web application that I'd like to split up into about six separate applications; one would provide the homepage and login at endpoints under "/", and the others would each claim a subdirectory path ("/subsystem1", "/subsystem2", etc...). I have a pretty clear idea how I could use JWT to pass authentication/authorization from the login app to the others.
The main reason for this is so that each subsystem can be modified or updated without shutting down the others. And organizationally, so we don't have to subject the entire app to a QA process when only one subsystem is changed.
Is it possible to set up multiple Spring Boot instances to run on the same server at the same time and the same port, with different paths/directories to their endpoints? How?
I was unable to find any duplicate question, but here are two related questions that may offer clues:
From Is there a standard way to customize the deploy path in Spring Boot? I learned that I can set the application property server.servlet.context-path to prefix the whole application with a subdirectory name (e.g. "/subsystem1"). But I still can't run two apps at the same time, even if both claim different subdirectories. Spring Boot reports "Web server failed to start. Port 8080 was already in use."
There's Multiple Spring-boot applications running on one Tomcat
but I'd prefer to use standalone Spring applications with their embedded Tomcat instances rather than the less-recommended WAR packaging and deployment to an external Tomcat container.
This one looks promising -- Deploying Multiple Spring Boot Web Applications in Single Server -- but the answers focus on whether standalone or Tomcat container deployments are better, and doesn't touch on the "how-to" question.
Acceptable answers:
If, as ekalin suggests, it is impossible to have multiple Spring Boot apps listen to the same port, here are a couple of ideas I have brainstormed (but don't know how to accomplish):
Perhaps the instances could be running on different ports but the main app (the one with the login page) could "forward" or redirect to the other apps in some way that hides their true URLs? E.g. "localhost:8080/subsystem1" would be an alias for "localhost:8081/".
Perhaps the applications could each have their own Docker containers, all running within a shared Docker network, and we use Docker somehow to map each URL path to the right app? Could this be set up with docker-compose?
We set up a proxy server of some kind that remaps URL paths to the separate applications.
You can't have more than one application listening on a port. How would the kernel which application to send the packages to?
You could run an http server such as nginx listening on 8080, and each application in a different port, and then proxy the requests based on the URL to the desired application.

Spring boot admin listing kubernetes internal urls. Not able to navigate to the application page

Problem
Trying to use Spring boot admin to do a deep monitoring of spring boot micro services running in Kubernetes.
Spring boot admin listing the micro services but pointing to the internal IPs.
Spring boot admin application listing page showing the internal IP
The application details page has almost zero info
Details
Kubernetes 1.15
Spring boot applications are getting discovered by Spring boot admin using Spring cloud discovery
spring-cloud-kubernetes version 1.1.0.RELEASE
The problem is that the IPs are of internal pod network and would not be accessible to the users in any real world scenario.
Any hints on how to approach this scenario ? Any alternatives ?
Also I was wondering how spring boot admin would behave in case of pods with more than one replica. I think it is close to impossible to point to a unique pod replica through ingress or node port.
Hack I am working on
If I can start another pod which exposes the Linux desktop to the end user. From a browser of this desktop, user may be able to access the pod network ips. It is just a wild thought as a hack.
Spring Boot Admin register each application/client based on its name by below property.
spring.boot.admin.client.instance.name=${spring.application.name}
If all your pods have same name it can register based on individual ips by enabling perfer-ip property (which is false by default):
spring.boot.admin.client.instance.prefer-ip=true
In your case, you want to SBA to register based on the Kubernetes load balanced url, then service-base-url property should be set the corresponding application's url.
spring.boot.admin.client.instance.service-base-url=http://myapp.com

How to use EhCache (local) and RedisCache (remote) in the same spring boot project?

I have a spring boot project that need local and remote caching. I am trying to use ehcache for local and Redis for remote, but I can't configure both to work together.
I am also trying to use PubSub as Redis Caching, because this one will be seen by others computers.
At this moment, Redis is storing all caching instead of just what I want to.

Discovering Hazelcast instance in a Spring Boot eco-system

Background:
We have a set of about 15 Spring Boot applications as microservices. They all run as Docker containers, and run as clusters of one or more instances. We also use Spring Cloud Netflix components such as Eureka and discover the running application instances from the client using Feign/Ribbon.
Question:
As part of the POC exercises, we tested with Redis and Hazelcast for caching and Spring Boot configuration storage. Everything works using Spring Boot, Spring Cloud and Redis/Hazelcast Java client libraries. However, when we deploy Hazelcast in a multi-node peer-to-peer cluster, Hazelcast seems to require a "known" IP address/hostname and the accessible port to be available in the Java client's configuration (with or without Spring). Typically, when Hazelcast is deployed in a multi-instance cluster on ephemeral VM instances (for example, EC2), the IP address and the port information is not available. So we thought of two possible solutions:
Find a way to run Hazelcast as a Spring Boot application, and register it with Eureka as a Discovery Client. That way other Spring Boot applications can use Eureka to discover an instance of Hazelcast dynamically
Find a way in Hazelcast so that it can publish it's IP address and port information dynamically to a central Key/Value store
If anyone has played around with Hazelcast to be able to do either/both of the possible solutions, it would be great if you can share more information on that. If there is a third approach that'd work better, I will be eager to know that as well.

Using of Spring Security in Cloud & autoscaling of web application

Some Web Application is managed by Spring 4.0 Framework, the Spring Security 3.2 is used also to authenticate users with remember Me feature.
The remember Me and Security is realized by JDBC Support (the needed data are saved in database).
A lot of Spring Beans is used, that are created as "spring" singletons
This Web Applicaton runs in TOMCAT7 Servlet Container, that installed in "classic" Host Sever.
This web application will be runs in production within TOMCAT7, that managed by some Cloud Provider - either in AWS Elastic Beanstalk or in EC2 Instance direct with instaled TOMCAT with autoscaling
That means, that at the first moment runs only ONE EC2 Instance, that has running TOMCAT Server 1. This server has initialized Spring Beans, holding in JVM 1.
But at "peak time" the second instance of EC2 will be started. The TOMCAT2 Server will be started also.
Is it possible, that a USER1, that was authenticated at first moment on TOMCAT1, have a problem with authentication and other business operations realized in WEb Application, if the load balancer routes the user1'Requests after start of TOMMCAT2 to TOMCAT2???
i don't know, whether Spring 4.0 or Spring Security are stateless by default.
You can consider using Elastic Load Balancer sticky sessions for your application.
Read about using Sticky Sesions here:
http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/US_StickySessions.html
Read about using Sticky Sessions with Elastic Beanstalk here:
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.managing.elb.html
Read about Stickiness policy here:
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/command-options.html

Resources