I was trying to develop Spring Cloud Config server with some properties. So on that site it was mentioned that replace the application.properties file into bootstrap.properties file.
Code is given as below,
#SpringBootApplication
#EnableConfigServer
public class ConfigserverApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigserverApplication.class, args);
}
}
and properties file ,
server.port = 8888
spring.cloud.config.server.native.searchLocations=file:///C:/configprop/
SPRING_PROFILES_ACTIVE=native
So where exactly this bootstrap.properties file resides in ? Is that complete replacement for application.properties or both exists in config server?
Related
I have a spring boot application, using spring-data-mongodb.
With the following MongoDB config in application.properties, the mongoRepository read/write documents into the correct database: product_db. Everything is correct so far.
#MongoDB Config
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.authentication-database=product_db
spring.data.mongodb.username=demo
spring.data.mongodb.password=demo
spring.data.mongodb.database=product_db
Then I introduced spring-cloud-config, and I moved the exact same MongoDB config to my-service.propertiesso the config client can get these from config server. The weird thing is that now mongoRepository read/write document from/into database: test
Where does spring-cloud-config-server specific the database name? How to config to ask mongoRepository to use the correct database?
Code snippet as following
Config Server
#SpringBootApplication
#EnableDiscoveryClient
#EnableConfigServer
public class ConfigServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServiceApplication.class, args);
}
}
application.properties for config server
server.port=2000
spring.application.name=config-server
eureka.client.service-url.defaultZone=http://localhost:8260/eureka
spring.profiles.active=native
spring.cloud.config.server.native.search-locations=file:///${user.home}/Project/configuration
my-test.properties
server.port=2100
spring.application.name=service-sample
eureka.client.service-url.defaultZone=http://localhost:8260/eureka
logging.level.org.springframework=INFO
#MongoDB Config
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.authentication-database=product_db
spring.data.mongodb.username=demo
spring.data.mongodb.password=demo
spring.data.mongodb.database=product_db
For my-service as the config client:
#EnableDiscoveryClient
#SpringBootApplication
#EnableMongoRepositories(basePackages = "com.mydemo.service.sample.repositories")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
bootstrap.properties for config client:
server.port=2100
spring.application.name=my-service
eureka.client.service-url.defaultZone=http://localhost:8260/eureka
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=config-server
As mentioned before, if I remove the bootstrap.properties, and use the following application.properties file for my-service, everything is good.
server.port=2100
spring.application.name=service-sample
eureka.client.service-url.defaultZone=http://localhost:8260/eureka
logging.level.org.springframework=INFO
#MongoDB Config
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.authentication-database=product_db
spring.data.mongodb.username=demo
spring.data.mongodb.password=demo
spring.data.mongodb.database=product_db
I have a Spring Boot project located in the path "C:\Personal Projects\Spring" and I want it to serve to browser static HTML file named index.html that is placed in "C:\Personal Projects\Game\build".
Therefore, I wrote the following code:
#SpringBootApplication
#EnableAutoConfiguration
public class Main {
public static void main(String[] args) throws IOException {
SpringApplication app = new SpringApplication(Main.class);
Properties properties = new Properties();
properties.setProperty("spring.resources.static-locations",
"C:\\Personal Projects\\Game\\build");
app.setDefaultProperties(properties);
app.run(args);
}
}
When I run the program and open browser for "localhost:8080/index.html" I get a 404 error.
Do you know what I'm doing wrong?
It should be:
properties.setProperty("spring.resources.static-locations",
"file:C:\\Personal Projects\\Game\\build");
In my application properties i have written the below for logging
logging.level.com.intro.dmp=INFO
logging.level.org.springframework.web=ERROR
logging.level.com.intro.dmp=ERROR
logging.file=application.log
and my Application is below , but it is not creating any log file rather displaying in the console . What is that i am missing here , it is reading application properties
#SpringBootApplication(scanBasePackages = "com.intro")
#PropertySource("file:src/main/resources/application.properties")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
logger.debug("--Application Started--");
logger.error("Check the main Articles");
logger.info("Checking files ");
}
}
Two changes will resolve your issue:
In application.properties file, you need to provide quotes to the file name. i.e.:logging.file='application.log'
You can remove #PropertySource annotation. Spring Boot will, by default, look for application.properties file at src/main/resources/.
I'm trying to share configuration between Spring Cloud clients with a Spring Cloud config server which have a file-based repository:
#Configuration
#EnableAutoConfiguration
#EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
// application.yml
server:
port: 8888
spring:
profiles:
active: native
test:
foo: world
One of my Spring Cloud client use the test.foo configuration, defined in the config server, and it is configured like below:
#SpringBootApplication
#RestController
public class HelloWorldServiceApplication {
#Value("${test.foo}")
private String foo;
#RequestMapping(path = "/", method = RequestMethod.GET)
#ResponseBody
public String helloWorld() {
return "Hello " + this.foo;
}
public static void main(String[] args) {
SpringApplication.run(HelloWorldServiceApplication.class, args);
}
}
// boostrap.yml
spring:
cloud:
config:
uri: ${SPRING_CONFIG_URI:http://localhost:8888}
fail-fast: true
// application.yml
spring:
application:
name: hello-world-service
Despite this configuration, the Environment in the Spring Cloud Client doesn't contains the test.foo entry (cf java.lang.IllegalArgumentException: Could not resolve placeholder 'test.foo')
However it's works perfectly if i put the properties in a hello-world-service.yml file, in my config server file-based repository.
Maven dependencies on Spring Cloud Brixton.M5 and Spring Boot 1.3.3.RELEASE with spring-cloud-starter-config and spring-cloud-config-server
From Spring Cloud documentation
With the "native" profile (local file system backend) it is
recommended that you use an explicit search location that isn’t part
of the server’s own configuration. Otherwise the application*
resources in the default search locations are removed because they are
part of the server.
So i should put the shared configuration in an external directory and add the path in the application.yml file of the config-server.
// application.yml
spring:
profiles:
active: native
cloud:
config:
server:
native:
search-locations: file:/Users/herau/config-repo
// /Users/herau/config-repo/application.yml
test:
foo: world
I have a Spring Boot application as follows:
#SpringBootApplication
#PropertySource(ignoreResourceNotFound=true,value={"classpath:application.properties","classpath:util-${spring.profiles.active}.properties"})
#ComponentScan("com.jmarts")
#EnableTransactionManagement
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Override
protected SpringApplicationBuilderconfigure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
I'm making use of spring profiles and based on active profile, a the correct environment specific file is loaded: utils-local.properties, utils-dev.properties, etc...
When profile is set through application.properties (spring), e.g. spring.profiles.active=local all works great, correct file (utils-local.properties)is loaded.
Providing profile through -D (gradle bootRun -Dspring.profiles.active=local) doesn't load profile. I was able to verify that the system properties is passed (print systemProperties)
I assume spring boot will register a PropertySourcesPlaceholderConfigurer if none is configured.
spring boot officially supports profile-specific properties using the naming convention application-{profile}.properties.
so you can remove "classpath:util-${spring.profiles.active}.properties" and add application-local.properties, application-dev.properties and so on in the classpath.