Spring data mongodb config - spring

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

Related

Disable Spring Boot Configuration

How to disable Spring Boot auto configuration.I want to disable the data source auto configuration.
Try this two way:
Properties configuration
spring:
profiles: app-profile
autoconfigure:
exclude:
- org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
- org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
Class level configuration
#SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class SpringBootApp {
public static void main(String[] args) {
SpringApplication.run(SpringBootApp.class, args);
}
Reference:
https://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/reference/html/using-boot-auto-configuration.html

location of Bootstrap.properties in Spring Cloud Config application

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?

Eureka server doesn't discover service

I've just started to learn about microservices via spring cloud and to start with I tried to reproduce basic example from this article https://spring.io/blog/2015/07/14/microservices-with-spring. Here is my code:
Eureka server
#SpringBootApplication
#EnableEurekaServer
public class ServiceRegistryApplication {
public static void main(String[] args) {
System.setProperty("spring.config.name", "registration-server");
SpringApplication.run(ServiceRegistryApplication.class, args);
}
}
resources/registration-server.yml:
# Configure this Discovery Server
eureka:
instance:
hostname: localhost
client: # Not a client, don't register with yourself (unless running
# multiple discovery servers for redundancy)
registerWithEureka: false
fetchRegistry: false
server:
port: 1111 # HTTP (Tomcat) port
Sample service:
#SpringBootApplication
#EnableDiscoveryClient
public class AccountsServiceApplication {
public static void main(String[] args) {
System.setProperty("spring.config.name", "accounts-server");
SpringApplication.run(AccountsServiceApplication.class, args);
}
}
accounts-service.yml:
# Spring properties
spring:
application:
name: accounts-service
# Discovery Server Access
eureka:
client:
serviceUrl:
defaultZone: http://localhost:1111/eureka/
# HTTP Server
server:
port: 2222 # HTTP (Tomcat) port
But when I run both apps and go to the localhost:1111 I cannot see my service in the application list:
Could you tel me please what am I doing wrong?
EDIT
After I applied changes the following line appeared:
i have a great solution for you and it's simple
follow those steps:
1- Eureka Server
#SpringBootApplication
#EnableEurekaServer
public class ServiceRegistryApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRegistryApplication.class, args);
}
}
in application.properties specify those params
spring.application.name=eureka-server
server.port=1111
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
2- in Sample Service
#SpringBootApplication
#EnableDiscoveryClient
public class AccountsServiceApplication {
public static void main(String[] args) {
SpringApplication.run(AccountsServiceApplication.class, args);
}
}
in application.properties specify those params
spring.application.name=accounts-service
server.port=2222
eureka.client.service-url.default-zone=http://localhost:1111/eureka
and don't forget to remove all .yml properties files.

Logging using spring boot sl4j

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/.

Share configuration between Spring cloud config clients

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

Resources