According to this (and also this):
The Spring Environment has an API for this, but you would normally set a System property (spring.profiles.active) or an OS environment variable (SPRING_PROFILES_ACTIVE).
I am using version 2.7.3 of Spring Boot:
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
We have a src/main/resources/application.properties file like this:
spring.datasource.driver-class-name=com.teradata.jdbc.TeraDriver
spring.jpa.hibernate.ddl-auto=none
spring.jpa.open-in-view=false
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.TeradataDialect
#---
spring.config.activate.on-profile=local
spring.datasource.username=
spring.datasource.url=jdbc:teradata://TDTEST/TMODE=ANSI,CHARSET=UTF8
spring.datasource.password=
#---
spring.config.activate.on-profile=prod
spring.datasource.username=
spring.datasource.url=
We built a fatjar using the maven shade plugin. But when we try to run it like this:
SPRING_PROFILES_ACTIVE=local java -ea -jar target/td-api-1.0-SNAPSHOT.jar
We get this message:
17:09:42.663 [main] INFO cdao.dpde.app.App - No active profile set, falling back to 1 default profile: "default"
Why? How can I fix this?
Adjust your application.yml/properties to the following
spring:
application:
name: service-name
profiles:
active: ${PROFILE:local}
and run the following command
java -jar td-api-1.0-SNAPSHOT.jar --PROFILE=environement
the advantage of the above is you can default your profile to a fallback profile if the environment was not provided.
The command above takes into consideration that you are inside of the /target dir
Related
I am trying to enable the actuator on an existing Spring Boot 2 application, which I thought would be straightforward as I have done few times for the application I created from scratch. However, somehow tomcat (as default server) not startup with the actuator port configured in the properties, when the application starts.
Here are snippets of the config and pom
server:
port: 8085
management:
port: 9085
<spring-boot.version>2.3.5.RELEASE</spring-boot.version>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
In the log of application startup, I can see the server port has been bound to the application but didn't see the actuator one specified in the properties.
18:32:08.435 [main] INFO o.a.c.h.Http11NioProtocol - Starting ProtocolHandler ["http-nio-8085"]
18:32:08.524 [main] INFO o.s.b.w.e.t.TomcatWebServer - Tomcat started on port(s): 8085 (http) with context path ''
I would expect to see a port binding like the log below right after the one above.
2021-01-20 17:01:35.636 INFO 20044 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 9085 (http)
I have checked the port has not been used by any other application. Did I miss anything else?
From your posting info, you should add some setting to enable endpoints. I give a sample. please try. By another word, management.port is used in spring boot 1.x.
In simple way, you just enable all endpoints and expose all. for security reason, you should make management.endpoints.enabled-by-default to false. then enable endpoint you want to expose.
management:
server:
port: 9085
endpoints:
enabled-by-default: true
web:
# base-path: /mgmt # you can change /actuator to another name
exposure:
include: "*"
# endpoint:
# refresh:
# enabled: true
# loggers:
# enabled: true
# env:
# enabled: true
I am trying to access the spring cloud config server but it is down. I still want to test the application using local application.properties. Is there any way i could do this?
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
bootstrap.yml
server:
port: 8086
spring:
application:
name: PromoMS
security:
basic:
enabled: false
management:
security:
enabled: false
cloud:
config:
fail-fast: true
discovery:
enabled: true
service-id: ms-config-server
I am getting following exception
java.lang.IllegalStateException: No instances found of configserver (ms-config-server)
at org.springframework.cloud.config.client.ConfigServerInstanceProvider.getConfigServerInstances(ConfigServerInstanceProvider.java:48) ~[spring-cloud-config-client-2.2.2.RELEASE.jar:2.2.2.RELEASE]
To disable spring-cloud-config you and use your local properties, you need to add this property to your bootstrap.yml:
spring.cloud.config.enabled=false
And then enable it only when you need to run your application via application args, something like this:
java -jar your-application-name.jar --spring.cloud.config.enabled=true
I am using postgresSQL instance on google cloud platform in spring boot app with spring data JPA.
But i am not able to connect to postgresSQL instance at the time of deployment.
I am not really sure on what dependency is required for this and what is the application properties configuration.
here is the code.
application.properties
spring.datasource.url=jdbc:postgresql://google/postgres?cloudSqlInstance=<instance-name>&socketFactory=com.google.cloud.sql.postgres.SocketFactory
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
pom.xml
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>postgres-socket-factory</artifactId>
<version>1.0.12</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-sql-postgresql</artifactId>
<version>1.1.0.RELEASE</version>
</dependency>
app.yml for deployment on flex environment:
runtime: java
env: flex
service: payment2
handlers:
- url: /.*
script: this field is required, but ignored
beta_settings:
cloud_sql_instances: <instance-name>
Thanks in advance.Please help!!!
I'm trying to connect my spring boot application with MySQL server.
I have followed the documentation properly but still, I'm getting some issue in connection
Following exception is throwing every time
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
In order for me to get around this error I have to log into MySql with the username & password stored in my application.properties file using MySql Workbench. After doing so the error no longer occurs. This only happens when trying to Run my spring boot project after restarting MySql server.
UPDATE: Adding "&allowPublicKeyRetrieval=true" to my spring.datasource.url in the application.properties file solved the problem. I figured this out after I noticed another error in the logs: "MySQL Public Key Retrieval is not allowed"
spring.datasource.url=jdbc:mysql://localhost:3306/db_example?autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
First of all restart MySQL server and retry again...
If it is not...
Did you add application.properties details and JPA, MySQL dependency?
Please Could you show pom.xml file and application.properties file.
appication.properties
spring.mvc.view.prefix=/WEB-INF/JSP/ #jsp file path
spring.mvc.view.suffix=.jsp
#Hibernate
spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useSSL=false
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.generate-ddl=true
#JPA
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
pom.xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Thank you...!
I am able to use RABBITMQ and MYSQLSERVICES which is on pivotal.While binding services I am able to get the Credentials and using that credentials in my application.properties for spring data jpa project.
But this configuration that I am using is hard-coded in application.Properties To Make this configuration dynamically I came to know that we can use vcap services provided by pivotal.
So want to use run-time credentials for rabbimq and mysql.
My Code is below for reference.
File: application.propeties
rabbitmq.host=hostname
rabbitmq.virtual-host=vhost
rabbitmq.username=username
rabbitmq.password=password
rabbit.mainqueue=queue name
rabbit.errorqueue=erro queue name
spring.datasource.url=jdbc:mysql://hostname:postno
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
server.port=8000
The below is the repository file
package com.redistomysql.consumer.repo;
import org.springframework.data.jpa.repository.JpaRepository;
public interface tblemployee_personal_infoRepository extends JpaRepository<tblemployee_personal_info, Long> {
}
Any help would be appreciated.
The link for reference **http://www.java-allandsundry.com/2016/05/approaches-to-binding-spring-boot.html**
Set this configuration in application-cloud.yml for Mysql
---
spring:
datasource:
url: ${vcap.services.mydb.credentials.jdbcUrl}
username: ${vcap.services.mydb.credentials.username}
password: ${vcap.services.mydb.credentials.password}
The config for rabbitMq:
System.getEnv("VCAP_SERVICES")
The dependencies in pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-spring-service-connector</artifactId>
<version>1.2.4.RELEASE</version>
</dependency>
<!-- If you intend to deploy the app on Cloud Foundry, add the following -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-cloudfoundry-connector</artifactId>
<version>1.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-heroku-connector</artifactId>
<version>1.2.4.RELEASE</version>
</dependency>
**The manifest.yml**
---
applications:
- name: redistomysql-consumer
path: target/redistomysql-consumer-0.0.1-SNAPSHOT.jar
memory: 1024M
env:
JAVA_OPTS: -Djava.security.egd=file:/dev/./urandom
SPRING_PROFILES_ACTIVE: cloud
services:
- es-mysql-db
- es-consumer-rabbitmq-service
buildpack: https://github.com/cloudfoundry/java-buildpack.git
env:
JBP_CONFIG_SPRING_AUTO_RECONFIGURATION: '{enabled: false}'