Spring cloud config server unable to find file from local git repo? - spring

Spring cloud server not able to fetch file from local git repository. I have created git repo in my local directory. I have created two files in git repo and committed, one file name is accountdetails.yml file and another file name is account-details.yml file.
If I try to send request http::localhost:8086/accountdetails.yml file. I am getting whitelabel error page.
If I send request http::localhost:8086/account-details.yml file. I am able to get the yml file response from spring config server.
My question is why we should hypen in accounts-detail.yml file? It is mandatory to use application-name.
SpringConfigApplication.java
#SpringBootApplication
#EnableConfigServer
public class SpringconfigApplication {
public static void main(String[] args) {
SpringApplication.run(SpringconfigApplication.class, args);
}
}
application.yml
server:
port: 8086
spring:
cloud:
config:
server:
git:
uri: "file://D:/Javaexamples/configrepos"
default-label: master
accountdetails.yml
config:
application: "spring accounts details application "
version: 1.0
account-details.yml
config:
application: "spring config server application"
version: 1.0

Related

How to build a Docker image for client service of Spring Cloud Config Server

I have built a Docker image for cloud config service. I have another service employee for which I want to build a Docker image. This client service will read data source configuration properties from the config server
I am able to connect the employee service successfully with config service on localhost without Docker. The problem is only that I am not able to build a Docker image for the employee service.
On running mvn package dockerfile:build command for the employee service I get the following error
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Below is the bootstrap.yml file located at src/main/resources directory
spring:
application:
name: employee
profiles:
active: dev
cloud:
config:
uri: http://configserver:8071
Config service's bootstrap.yml file
spring:
application:
name: config-server
profiles:
active: native,dev
cloud:
config:
server:
#Local configuration: This locations can either of classpath or locations in the filesystem.
native:
#Reads from a specific filesystem folder
search-locations: classpath:/config
server:
port: 8071
employee.properties file in config directory
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
employee-dev.properties file
spring.datasource.url=jdbc:mysql://db:3306/employee?allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
This is probably because the container of config server with hostname configserver is not up. I am not sure what hostname I should provide. Even if I want to build the image of employee service by starting the container of config server, it won't be on configserver host but on localhost. This is probably why the build is failing?
I want to build the Docker image of employee service so that its container can be fired up using the docker-compose up command
Edit: This question is similar but I already have the spring-cloud-starter-config dependency added in my client service and it still gives me the same error on build
Edit 2
You don't have to separately build a Docker image for the client service at all!
When I ran docker-compose up command I noticed the following line in output
c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://configserver:8071
But I was still not sure if it was actually reading the data from the config server. So I purposely added incorrect database credentials in config server properties file for my service and the service startup failed! Now I am sure that my employee service is reading the data from the config server
I am still not sure how can I achieve creating a new Docker image for my employee service.
Did you try changing the URL param db to actual host of vhostname ?
db =change it to IP addess or accessable load balancer URL
spring.datasource.url=jdbc:mysql://db:3306/employee?allowPublicKeyRetrieval=true&useSSL=false

Spring Cloud Config - bootstraping compositie configuration

Is it possible to bootstrap spring config service with composite configuration.
Example:
Final configuration (placed on the git repo )
spring:
cloud:
config:
server:
composite:
- type: git
uri: <uri1>
- type: git
uri: <uri2>
After I setup env variable to bootstrap:
SPRING_CLOUD_CONFIG_SERVER_BOOTSTRAP = true
and provide setup to fetch configuration from repository spring config is not adding new configuration for additional repositories.
Is it possible to configure spring config server to bootstrap from one repository and then serve configuration from different one(s)?

Spring Cloud Config Client multiple property files retrieve from same repo

I have a requirement to retrieve the property files with environment name distinguishing those.
Config server is listening to this repo (https://github.com/tpande1/spring-cloud-config-repo/tree/master) which has : config-client-dev.properties, config-client-sbx.properties, config-client-test.properties and config-client-prod.properties
In Client Server i have the below config to read from the above github repo. How can i read these different properties with the environment specified in my Client Application?
Sample Rest Code:
#GetMapping("/message") //Pick message(SBX, DEV, Test, PROD) from the propertyfile from github
public String getMessage() {
return message;
}
bootstrap.yml:
spring:
profiles: dev
application:
name: config-client
cloud:
config:
uri: http://localhost:1000 //Config server port
profile: sbx, dev, test, prod
management.endpoints.web.exposure.include: "*"
You can add multiple active profiles in your bootstrap yaml file. In the option where you have spring.profiles.active and in your config server, you can add search patterns for each profile.
As mentioned in the below link in Pattern matching section.
https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_server.html

How to ask Spring Cloud Config server to checkout configuration from specific branch?

I have following Spring cloud config application.yml:
spring:
application:
name: configserver
cloud:
config:
server:
git:
uri: https://xyz#bitbucket.org/xyz/microservices-configs.git
username: xyz
password: xyz
basedir: target/configs
server:
port: 8881
Following is my bootstrap.yml of user microservice:
spring:
application:
name: userservice
cloud:
config:
uri: http://localhost:8881/
Scenario - 1
When I hit config server in browser like this:
http://localhost:8881/development/userservice-development.yml
It serves file properly. and when I look at basedir i.e. target/config, I see:
- userservice.yml
- gateway.yml
Exactly what I wanted, Since I added this two files only in development branch.
Scenario - 2
When I run my userservice microservice project using following command:
mvn clean spring-boot:run -Dspring.profiles.active=development
It fetches the right file from git, but it checkout from master branch ! but not from the development branch as I am expecting. am I expecting right ? (FYI I have both development and production yml in master branch)
So the question is, how do we go for using config server ? Is there any configuration which we can set to fetch yml from that particular branch only ? I believe we need to set some label, because as per documentation, default label is master. Can anyone let me know how do we go for setting label in above scenario ?
According to the documentation, the configuration you want to set in your config client is:
spring.cloud.config.label=mybranch
Where mybranch is an existing branch in your git repo.
You can specify the default branch (more generally, Git label) that a config server uses if a client does not specify the label, via property spring.cloud.config.server.git.default-label, perhaps this is what you are after? Certainly solves the issue for me!
If only use the branch in a yml file just configure:
spring:
cloud:
config:
server:
git:
uri: https://gitlab.com/somerepo.git
username: someuser
password: somepass
default-label: branchname
Config server designed to use profile to separate environment.
Example:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
The branching make configuration inconsistency.
Concept of config server is based on 12-factor config (http://12factor.net/config ) .
Check it out for detail reason.

Spring cloud config directory structure

I have a working Spring cloud config server who provides configuration from a git repository. All configurations are stored in the root directory in the repository. They are named {name}-{profile}.yml.
I have changed the directory structure to {name}/{profile}.yml
When I curl http://configserver:8888/appname/myprofile the config server I get a json response but the propertySources is empty.
My spring cloud server config
spring:
cloud:
config:
server:
git:
uri: http://git#git.host/scm/cas/application-config.git
You need to add cloud.config.server.git.searchPaths to the configuration server application.yml.
cloud:
config:
server:
git:
searchPaths: <directory>

Resources