I have setup oracle as a database, I'm connected to it and can do CRUD operations using spring boot, however, the data.sql file in the resources folder doesn't seem to get excuted
spring.jpa.database-platform=org.hibernate.dialect.Oracle12cDialect
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto=none
spring.jpa.generate-ddl=false
spring.datasource.initialization-mode=always
this is my jpa properties, and this is my jpa dependency
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
You have to add
spring.sql.init.mode=always
Here's a guide to this topic: https://www.baeldung.com/spring-boot-data-sql-and-schema-sql
Related
I'm interested is it possible to disable features into Spring Boot/Cloud which are not used?
If there is no way to remove a lot of the necessary features? Do you know some guide how I can make a custom build?
I'm interested how Spring Boot can be optimized for read-time processing.
You can exclude features as tomcat from starter's dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>tomcat-embed-el</artifactId>
<groupId>org.apache.tomcat.embed</groupId>
</exclusion>
<exclusion>
<artifactId>tomcat-embed-core</artifactId>
<groupId>org.apache.tomcat.embed</groupId>
</exclusion>
<exclusion>
<artifactId>tomcat-embed-websocket</artifactId>
<groupId>org.apache.tomcat.embed</groupId>
</exclusion>
</exclusions>
</dependency>
Or similar for Logback by removing from build or classpath:
Spring Boot tries to work with whatever is in the classpath, so if you don't want logback, take it off the classpath.
Add exclusion to both the spring-boot-starter and spring-boot-starter-web to resolve the conflict.
Or cloud config/discovery:
#Disable discovery
spring.cloud.discovery.enabled = false
#Disable cloud config and config discovery
spring.cloud.config.discovery.enabled = false
spring.cloud.config.enabled = false
What are the features are you expecting to disable ?
If you feel any dependency is not used try tag in your pom.xml. So that you can cut down some dependency and make you application lightweight.
I have configured H2 as follows for my springboot app:
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
# Enabling H2 Console
spring.h2.console.enabled=true
# Custom H2 Console URL
spring.h2.console.path=/h2
When I go to log in it says it cannot find the database. I am typing explicitly jdbc:h2:mem:testdb like this:
What else can I do to troubleshoot? I do have a data.sql file which is creating the schema and putting in sample data.
I read through some other tutorials which mentioned including jdbc or jpa in maven/gradle. When I added either of these then H2 seemed to work just fine.
Had same problems, what I did to make this work was:
application.properties:
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.datasource.url=jdbc:h2:mem:uniqueNewName <<testdb did not work!
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
springspring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
pom.xml:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc10 -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc10</artifactId>
<version>19.8.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
Don't ask me why, but I had to add jbdc and spring-boot-starter-data-jpa to my dependencies to make this work.
I have a spring cloud microservices project which uses Spring Cloud Config Server for managing configurations and Eureka Server for service discovery.
My application was doing great until I wanted to add a new microservice with keycloak. This new microservice is simply a rest API for my Vue frontend application and user managament is expected to be handled by Keycloak.
The new service runs OK and registers itself to Eureka until I add keycloak dependencies to the project. Application does not crash or throw any errors, startsup fine and registers itself to Eureka but on the Spring Boot Admin server panel I see that the application is down.
Here is my .properties file for the new service.
eureka.instance.preferIpAddress=true
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
eureka.instance.leaseRenewalIntervalInSeconds=3
eureka.client.eureka-connection-idle-timeout-seconds=3
eureka.client.fetchRegistry=true
spring.boot.admin.client.url=http://localhost:6060
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
server.port=8082
keycloak.auth-server-url=http://localhost:8080/auth
keycloak.realm=microservices
keycloak.resource=microservices-app
keycloak.public-client=true
keycloak.security-constraints[0].authRoles[0]=user
keycloak.security-constraints[0].securityCollections[0].patterns[0]=/*
keycloak.cors=true
Here are my dependencies for the new service.
...
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-spring-boot-starter</artifactId>
<version>4.8.3.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.keycloak.bom</groupId>
<artifactId>keycloak-adapter-bom</artifactId>
<version>4.8.3.Final</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Spring cloud version is Hoxton.SR1
Spring boot version is 2.2.2.RELEASE
I have tried adding Spring Security and making java configurations using KeycloakAuthenticationProvider but didn't help.
Throughout this project, I had many weird bugs caused by Spring Cloud version or Spring Cloud-Spring Boot Admin interaction so changing version or adding one little parameter to the configuration files usually did the trick, I am suspecting this Keycloak issue will be solved the same way.
Spring Boot Admin uses actuator endpoints, try to make them unprotected:
keycloak.security-constraints[0].authRoles[0]=user
keycloak.security-constraints[0].securityCollections[0].patterns[0]=/*
keycloak.security-constraints[0].securityCollections[1].patterns[0]=/actuator
keycloak.security-constraints[0].securityCollections[1].patterns[1]=/actuator/*
im trying to connect my Spring Boot app to a Cassandra 2.2.8 cluster on EC2 instances (2 nodes).
my use is tracing with Sleuth and Zipkin.
when the tracing start, the driver always point to localhost :
com.datastax.driver.core.Cluster : New Cassandra host localhost/127.0.0.1:9042 added
this is my application.properties
spring.datasource.url=jdbc:cassandra://url-node-1:9042
spring.datasource.contactPoints=url-node-1,url-node-2
zipkin.storage.type=cassandra
spring.datasource.initialize=true
spring.datasource.continue-on-error=true
spring.sleuth.enabled=false
and this is my pom.xml :
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-storage-cassandra</artifactId>
<version>1.17.1</version>
<exclusions>
<exclusion>
<artifactId>cassandra-driver-core</artifactId>
<groupId>com.datastax.cassandra</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-mapping</artifactId>
<version>3.1.0</version>
</dependency>
Zipkin's connection to cassandra is independent from the normal spring setup. We use some very specific setup. you'll want to set properties in the namespace of zipkin.storage.cassandra
https://github.com/openzipkin/zipkin/blob/master/zipkin-server/src/main/resources/zipkin-server-shared.yml#L40
Before spring boot 1.4 migration, i was able to keep the log4j.xml outside of the jar using the application.properties config [logging.config=conf/log4j.xml]. The config folder and application jar resides under same folder.
After spring boot 1.4 migration, the log4j.xml is not get picked and it throws filenotfound exception. Does spring 1.4 loads the config and log xml differently from previous version?
spring-boot is probably trying to use its default logger, logback. You need to exclude logback or spring-boot-starter-logging.
Spring documentation on how-to-logging:
Spring Boot supports Log4j 2 for logging configuration if it is on the
classpath. If you are using the starters for assembling dependencies
that means you have to exclude Logback and then include log4j 2
instead.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>