How spring cloud defines validation interval under the Pivotal Cloud Foundry? - java

I'm working on application that get the DS from PCF (Pivotal Cloud FoundrY) User provided services. It's working properly but I want to understand how the validation interval is defined.
As Spring and PCF are managing my connection pool. I'd like to understand how that works.
public DataSource getProfileDS() {
PoolConfig poolConfig = new PoolConfig(5, 10, 30000);
DataSourceConfig dsConfig = new DataSourceConfig(poolConfig, null);
return connectionFactory().dataSource("profileDS", dsConfig);
}
Also, is there any way to set up the validation interval by my own like we are used to do under the tomcat?

How spring cloud defines validation interval under the Pivotal Cloud Foundry?
Spring Cloud Config will define a validation query that is appropriate for your relational database.
Examples:
MySQL
Oracle
Postgres
As to the validation interval, it does not look like that's being configured. Instead, the DBCP-like pools use testOnBorrow, and Hikari is configured to use connectionTestQuery. When testing before obtaining a connection from the pool, setting the validation interval is unnecessary.
Also, is there any way to set up the validation interval by my own like we are used to do under the tomcat?
Not if you're going to use Spring Cloud Connectors, but you don't have to use Spring Cloud Connectors. There's a couple of other ways you can do this.
Spring Boot exposes VCAP_SERVICES as properties like vcap.services.<name>.credentials.username. You could use those to manually define a DataSource. See here.
You can use the new java-cfenv library, which is intended to complement Spring Boot better.
Hope that helps!

Related

HikariCp metrics collection using micrometer in Java springboot application by configuring it in

community,
I have a requirement where I want to do HikariCp metrics collection using micrometre in Java spring boot application by configuring it. I was looking around but I could not find much help in the official documentation
https://github.com/brettwooldridge/HikariCP/tree/dev/src/main/java/com/zaxxer/hikari
Can anyone please help me with how to do that?
Note: In the official documentation of Hikari they have provided a way to set metric registry for dropwizard but I wanted to do it with Micrometre.
only data source autoconfigured is exposed for metrics in my case I have a custom Hikari data source configuration.
public myDataSource(){
DataSource datasource = this.createDataSource();// you can write your own implementation to create datasource. you can pass URL, username etc to create it.
this.setUpHikariWithMetrics();
}
public void setUpHikariWithMetrics() {
if (dataSource instanceof HikariDataSource) {
((HikariDataSource) dataSource).setMetricRegistry(metricRegistry);
}
}

How do I get data from a Flux in an AbstractGatewayFilterFactory

I have a ReactiveDiscoveryClient that provides a method Flux<ServiceInstance> getInstances(String serviceId). I want to use the result of that method in my GatewayFilterFactory's apply method. However,
ServiceInstance si = reactiveDiscoveryClient.getInstances(config.getServiceId()).block();
fails because the block operations are not allowed. Is there anyway around it?
What I've done is cheat since my ReactiveDiscoveryClient has an in-memory map that contains the services and I just provide an extra method that gets the value I need.
That's rare, as by default, those actions are allowed. From the Spring Cloud Documentation:
27.1. #EnableDiscoveryClient
Spring Cloud Commons provides the #EnableDiscoveryClient annotation. This looks for
implementations of the DiscoveryClient and ReactiveDiscoveryClient
interfaces with META-INF/spring.factories. Implementations of the
discovery client add a configuration class to spring.factories under
the org.springframework.cloud.client.discovery.EnableDiscoveryClient
key. Examples of DiscoveryClient implementations include Spring Cloud
Netflix Eureka, Spring Cloud Consul Discovery, and Spring Cloud
Zookeeper Discovery.
Spring Cloud will provide both the blocking and reactive service
discovery clients by default. You can disable the blocking and/or
reactive clients easily by setting
spring.cloud.discovery.blocking.enabled=false or
spring.cloud.discovery.reactive.enabled=false. To completely disable
service discovery you just need to set
spring.cloud.discovery.enabled=false.
At least for what it's told in the last paragraph, you should be able to perform a block(). Anyway, you could try by setting these params, as should be enough to be allowed to invoke it:
spring.cloud.discovery.blocking.enabled=true
spring.cloud.discovery.reactive.enabled=true
spring.cloud.discovery.enabled=true
Also, not sure if related, but note this warning as well (just in case)

How to define a Shared DataSource in Spring Cloud Config

Would it be possible to set up a DataSource using Spring Cloud in which open JDBC connections could be injected into all of my Spring Boot applications?
Something kind of like a JNDI server lookup? If so, can someone provide some examples or a description on how to use this type of configuration?
You could use a Spring Cloud bootstrap configuration to create a DataSource. I don't see much value in doing it that way over a normal Spring Boot autoconfiguration though. Link: http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-developing-auto-configuration.
One solution I found out was to set all datasources info into the properties files which will supplied by Spring Cloud Config Server to applications clients. So the aplications clients which creates DataSources gets from remote properties those values.

Dynamic configuration in a java/camel/spring/jpa application

I have written an application using java, camel, spring, shiro, c3p0 and jpa.
This application needs to connect to some web services and some db and it has now a static configuration using classic spring propertyplaceholders and .prop property files.
I inject properties in java classes using #Value annotations and I define datasources using spring with ${} placeholders.
In the configuration there are url,username,password for web services and database,url,username,password for datasource.
Now I need to do a dynamic/multi tenant configuration. I mean that each "customer" can have his set of passwords and that these login/passwords can change over time.
Using shiro I can add to the Subject some data, so I can add current properties to it and get them where I need.
But how can I continue to use #value annotations?
And, most important question, how can I change datasources parameters at runtime?
I see in c3p0 documentation that using getConnection(username,password) with a new pair of username and password creates a new pool and close the old. But I do not use getConnection because only the EntityManager uses datasource.
Please help me!
Thanks,
Mario
After a lot of searching I think I can do in this way:
for properties use DynamicCombinedConfiguration from commons configuration, but I do not know how to tell it to read the tenant id from Shiro Subject
for JPA use AbstractDataSource from spring, but again I do not know if I can read the tenant id from Shiro Subject
Can you tell me if I am pointing in the right direction?
Thanks again,
Mario

How can I use JDBCTemplate from Spring JDBC in a Swing desktop application?

I use a lot of JDBC code in my Swing desktop application. Now I read about JDBCTemplate from Spring in Spring in Action and it looks like a nice API for working with JDBC.
But Spring JDBC seem to need some XML configuration files for beans. Is there any way I can use JDBCTemplate without these XML configuration files (e.g. with annotations)? Or how can I use this JdbcTemplate in a Swing desktop application for database access?
You can have a Spring Context without XML by creating a #Configuration anotated java class and create the Spring Context using the AnnotationConfigApplicationContext class to load the config
see Spring JavaConfig for a code sample.
While using Spring as the backbone of your application certainly has merit, and indeed annotation-based configuration can free you from 'XML hell', if you just want to use JdbcTemplate 'raw' there's nothing preventing you from doing so.
Just make sure you supply it with a valid DataSource, such as PGPoolingDataSource for example, if you're using PostgreSQL. If your JDBC vendor does not provide a DataSource implementation, then feel free to use Spring's SimpleDriverDataSource.
For example:
DataSource ds = new SimpleDriverDataSource(LegacyDriver.class,
"jdbc:legacy://database", "username", "password");
JdbcTemplate jdbc = new JdbcTemplate(ds);
// Use jdbc to do stuff
Even though its technically possible - it would defeat the purpose and design of Spring based application.
My suggestion would be to start using Spring framework as a backbone for you application. I promise you that your application will only benefit from using it (better design, clear separation of concerns, better testability etc ). Setting up JbdcTemplate using Spring context is almost trivial.
You already reading 'Spring in Action' - just start using it :)
Take a look at Spring Reference Documentation - the best Spring resource, period

Categories