SpringBoot Cache: specific configuration for each cache - spring-boot

I am experimenting Cache in Spring Boot. At moment, I am using Caffeine implementation. From what I've read from the documentation, I've configured the caches in YAML as follows:
spring:
cache:
cache-names:
- skills
- profile-level-type
caffeine:
spec: maximumSize=50,expireAfterAccess=60s
Where skills and profile-level-type correspond to different #Services. It is working. However I would like something that more flexible. Apparently, I cannot make a configuration to a specific cache. I was thinking something about this:
spring:
cache:
caffeine:
skills: maximumSize=50,expireAfterAccess=60s
profile-level-type: maximumSize=5,expireAfterAccess=3600s
Is it possible? If not with Caffeine at least with another cache provider (JCache, EhCache, etc.)?

Related

Do SpringBoot Configuration Trees support refresh

Do SpringBoot Configuration Trees support refresh?
I have the following. If the /mnt/secrets/ volume changes does Spring automatically refresh Beans with #ConfigurationProperties?
spring:
application:
name: "foo"
# Read Secrets
config:
import:
- configtree:/mnt/secrets/
activate:
on-cloud-platform: kubernetes
Currently if a Kubernetes configmap or secret is modified/updated, it does not redeploy the pod automatically. There needs to be a manual deployment to pick the new changes.
This is currently a feature in progress to facilicate this. https://github.com/kubernetes/kubernetes/issues/22368
So going by above, can you see if your case falls on the same lines. If so, check if below helps.
From a Kubernetes perspective, you can use Reloader to look for changes and auto redeployment.
For now, use Reloader - https://github.com/stakater/Reloader
It watches if some change happens in ConfigMap and/or Secret; then performs a rolling upgrade on relevant DeploymentConfig, Deployment, Daemonset, Statefulset and Rollout
How to use it - https://github.com/stakater/Reloader#how-to-use-reloader

spring.config.import doesn't accept configserver option when in yaml

I have a bunch of services and recently migrated to new Spring Boot/Cloud. All but one works ok. The one complains like this:
***************************
APPLICATION FAILED TO START
***************************
Description:
Config data location 'configserver:http://localhost:8888/' does not exist
Action:
Check that the value 'configserver:http://localhost:8888/' at class path resource [application.yaml] - 5:13 is correct, or prefix it with 'optional:'
Making it optional makes service to completely skip remote configuration. Other services tells
Fetching config from server at : http://localhost:8888/
but this one misses it completely.
I've checked dependencies, they looks ok, tried to use latest spring-cloud-starter-config 3.0.5 but didn't change anything.
application.yaml looks like this:
spring:
application:
name: some-service
config:
import: "configserver:http://localhost:8888/"
cloud:
config:
fail-fast: true
retry:
max-attempts: 5000
initial-interval: 1500
multiplier: 1.5
All services has exactly the same config, just different names.
I have also another service which had similar problem, and switching from application.yaml to application.properties has fixed the problem. I don't like this solution, that's why I'm asking for help here. I suppose I miss some dependency or it has different version. I've even found similar problem, but no solution is visible: https://github.com/spring-cloud/spring-cloud-config/issues/1933
Edit: I've switched from yaml to properties format and it's working ok. Why yaml is not working?
So the problem was excessive application.properties in one of attached library. After removing this properties everything is working as expected.
Probably something in Spring Boot doesn't like such additional properties.

Number of objects cached in production using EhCache

Is there any way to know the number of objects cached in production?
I am using EhCache on my project and NewRelic for monitoring.
I don't know if that help me too or if there is integration for NewRelic with EhCache.
You could potentially use NewRelic's JMX integration, but also there is JCache API integration in NewRelic, which may instrument the right parts if you are using the JCache compatible features of EhCache.
If you want to export select JMX information, then you need to specify in your NewRelic config on the server as documented here.
For example you could create the following file in /opt/newrelic/extension/ehcache.yaml
name: EhCacheMetrics
version: 1.0
enabled: true
jmx:
- object_name: {see Metadata within VisualVM MBeans browser}
metrics:
- attributes: CacheHitPercentage, CacheHits, CacheMisses, ObjectCount
type: simple
You'll need to find out what is available which you can do by registering EhCache for JMX management as documented here.
There is a good Spring Boot sample here

Disable spring integration metrics

I have the spring boot application exposing actuator/prometheus endpoint for monitoring,
I want to turn off metrics for spring integration components, keeping for http requests only.
I've tried disabling it in configuration according to the documentation in this way:
management:
metrics:
enable:
spring:
integration: false
That did not work for me.
Could you help me?
Let's take a look into documentation again!
In addition to MeterFilter beans, it’s also possible to apply a limited set of customization on a per-meter basis using properties. Per-meter customizations apply to any all meter IDs that start with the given name. For example, the following will disable any meters that have an ID starting with example.remote:
management.metrics.enable.example.remote=false
But your YAML config is slightly different in regards spring.integration pattern to match. I think it has to be like this:
management:
metrics:
enable:
spring.integration: false

Spring Boot testing using in memory db

I have created a Spring web project using Spring Boot.
Would like to understand the practice around testing. I require an in memory embedded database say hsql or h2 for my junits with initial schema.sql.
And on the main application the database could be say mysql or oracle
In a non Spring Boot project, we would normally have a separate applicationcontext.xml one which is referred by the web app and for testing we would use applicationContext-text.xml
Now, in Spring boot as everything is created automatically and Spring Boot is opiniated too. Would like to know how do I setup having an embedded inmemory db for Junits and an external db like MySQL for the application.
One solution I can think of is using Profiles. with 2 properties file application.properties and application-test.properties. and use test profile for my junits.
Any recommendation on the approach I should take.
A profile is, indeed, the recommended approach. What I would do is probably make the in-memory implementation the "default" profile (it's harmless, in the sense that you never change any real data, so it's better to make that the default in case someone accidentally runs it against a real database). Personally, I prefer to put all the external configuration in a single application.yml file, but that's really up to you. In the external configuration you need to supply a valid driver class and URL, e.g.
spring:
datasource:
driverClassName: org.h2.Driver
url: jdbc:h2:mem:test;MODE=PostgreSQL
schema: classpath:/schema.sql
---
spring:
profiles: local
datasource:
url: jdbc:postgresql://localhost/test
username: root
password: changeme
driverClassName: org.postgresql.Driver
schema:
(Note that H2 has a postgres compatibility mode, so it is really nice as a complement to postgres in production.)

Resources