I'm trying to setup a spring boot project with Spring Cloud Config with database backend. I've following things in my setup:
application.properties
spring.application.name=my-services-api
spring.datasource.url=jdbc:h2:~/test
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.platform=h2
spring.datasource.username=sa
spring.datasource.password=sa
spring.datasource.hikari.connection-timeout=5000
spring.datasource.hikari.maximum-pool-size=10
spring.profiles.active=jdbc
spring.cloud.config.uri=http://localhost:8080
spring.cloud.config.server.default-profile=production
spring.cloud.config.server.default-label=latest
spring.cloud.config.server.jdbc.sql=SELECT key, value FROM my_properties WHERE application=? AND profile=? AND label=?;
spring.cloud.config.server.jdbc.order=1
spring.h2.console.enabled=true
management.endpoints.web.exposure.include=refresh
message=default message
And I've #EnableConfigServer in application class. Also I've #RefreshScope in a controller where I'm trying to inject a value from my database.
#Value("${message}")
private String message;
And I've an entry in db.
APPLICATION |PROFILE |LABEL |KEY |VALUE
my-services-api |production |latest |message |Some New Hello
To refresh I'm doing a POST in /actuator/refresh with empty body which returns a success 200. But the value of message field is still the one coming from properties file and is not updated.
When I go a GET /my-services-api/production/latest, I see the new value in the response but message field is still not refreshed. Am I missing any configuration? Thanks.
I had to change the configuration to have a separate server and client:
On the server side, I have this in application.properties:
spring.application.name=my-services-api
spring.datasource.url=jdbc:h2:~/test
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.platform=h2
spring.datasource.username=sa
spring.datasource.password=sa
spring.datasource.hikari.connection-timeout=5000
spring.datasource.hikari.maximum-pool-size=10
spring.profiles.active=jdbc
spring.cloud.config.uri=http://localhost:8080
spring.cloud.config.server.default-profile=jdbc
spring.cloud.config.server.default-label=latest
spring.cloud.config.server.jdbc.sql=SELECT key, value FROM my_properties WHERE application=? AND profile=? AND label=?;
spring.cloud.config.server.jdbc.order=1
spring.h2.console.enabled=true
management.endpoints.web.exposure.include=refresh
logging.level.org.springframework.jdbc.core=TRACE
On the client side, I've a bootstrap.properties with:
spring.application.name=my-services-api
spring.cloud.config.uri=http://localhost:8080
spring.cloud.config.label=latest
spring.cloud.config.profile=jdbc
And application.properties with:
management.endpoints.web.exposure.include=refresh
To refresh, I did a POST call to /actuator/refresh on the client service.
Related
I use oracle DB in Spring Boot project. I want to set error logger for the database in Spring console. This configuration must be made in application.properties file I think. (This code example shows another oracle db properties). How can I do that?
oracledb.url = <oracle_url>
oracledb.user = <oracle_user>
oracledb.password = <oracle_password>
oracledb.poolname= <oracle_poolname>
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
This is going to be the spring back-end of a project that I am working on. I am able to create tables from my model classes in Postgres database however I wasn't able to put static data to the tables.
I created a data.sql file in resources with bunch of insert commands.
This is how my application.properties looks like:
spring.datasource.url=jdbc:postgresql://localhost:5432/springtest
spring.datasource.username=
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.format_sql=true
spring.datasource.initialization-mode=always
The compiler(Intellij) is crossing a line over
"spring.datasource.initialization-mode"
with the message:
Deprecated configuration property
'spring.datasource.initialization-mode'
It also suggest me to use the replacement key spring.sql.init.mode=always however this does not work as well. I need the program to execute the SQL commands in data.sql.
Since Spring Boot 2.5.0 by default, data.sql scripts are now run before Hibernate is initialized. This aligns the behavior of basic script-based initialization with that of Flyway and Liquibase. If you want to use data.sql to populate a schema created by Hibernate, set spring.jpa.defer-datasource-initialization to true.
Additional info: github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.5-Release-Notes
I was able to figure it out by changing
spring.datasource.initialization-mode=always
to
spring.sql.init.mode=always
and renaming data.sql to import.sql
I went through lots of tutorials regarding this but could not get this done.
Here is my table structure for this.
Application Profile Label prop_key value
masking dev latest test-property message
I have a cloud config server which should integrate with JDBC backend. Here is my application.properties in config server
server.port=8082
spring.application.name=masking
management.endpoints.web.exposure.include=*
spring.datasource.url=jdbc:postgresql://localhost:8000/finos?currentSchema=xlabs
spring.datasource.username=mufgdev
spring.datasource.password=XXX
spring.profiles.active=XXX
spring.cloud.config.server.jdbc.sql=SELECT prop_key,value from xlabs.properties where application=? and profile=? and label=?
spring.cloud.config.server.jdbc.order=1
With this configs if I enter http://localhost:8082/masking/dev/latest response will show the results as I want.
I want to consume properties in client side with the following configs in bootstrap.properties
spring.application.name=masking
spring.cloud.config.uri=http://localhost:8082
spring.cloud.config.label=latest
spring.cloud.config.profile=dev
And in my client side
#RestController
#RefreshScope
public class TestController {
#Value("${test-property}")
private String aConf;
#GetMapping("/message")
String message() {
String name =aConf ;
return name;
}
}
This gives java.lang.IllegalArgumentException: Could not resolve placeholder 'test-property' in value "${test-property}"
Can anyone comment on this?
Thanks.
This issue comes with the latest Spring boot release, All the above code segments steps all okay, But by default Spring has disabled bootstrap. So you have to enable them by adding
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
No need add for older versions of the Spring boot projects.
I have added H2DB in my springBoot application for unit testing purpose.
In application-test.properties I just added:
spring.datasource.name = h2db
spring.h2.console.enabled = true
Its working fine, saving the values.
But how it is working and how can I browse this DB?
Default properties for H2 in Spring Boot application are:
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
So it's not necessary to add them to your application.properties - Spring Boot create this DB itself.
If you want to get access to your H2 DB right from your IDE you have to make this setup.
Application.properties
spring.datasource.url=
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=
i placed this application.properties in resources folder.
Java Class
#Component
public class data{
#Autowired
private JdbcTemplate jdbcTemplate;
public void queryData(){
String sql = "select * from DEPOSIT";
jdbcTemplate = new JdbcTemplate();
jdbcTemplate.execute(sql);
}
}
I am getting
java.lang.Illegal Argument Exception:No Data Source Specified
I am getting this error message even though i specified data source in application.properties
I am using Spring Boot for this task. I Have added almost all the dependencies required in POM.
Not sure why i am not able to access data source. basically trying to access data from DB using Spring boot, MySQL, jdbcTemplate.
Not sure whats wrong here.
Do i have to add anything in the code so that data source can be specified in java class?
Add below properties to your application.properties file. This specifies the data source for your application. Do check if mysql is running on your machine before starting your application.
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/db
spring.datasource.username=yourusername
spring.datasource.password=yourpassword
For additional information refer to below link:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html