Spring Cloud Config Server serving nested plain text files - spring

I have a Config Server up and running, with the relevant application.yml setting of
spring:
application:
name: config
...
searchPaths:
- '{application}'
- '{application}/{profile}'
I would like to access a file that is not application.properties, something like myfile.txt. If I have the git layout
/myapp/nested/folder/myfile.txt
I want to be able to access that file. According to the Spring Config Server docs, I should be able to do this via /{name}/{profile}/{label}/{path}, but I can't get any paths to work.
TL;DR: How do I retrieve nested config files from a git repo via Config Server?

TL;DR: The documentation is wrong. The endpoint /{name}/{profile}/{label}/{path} should be written as /{application}/{profile}/{label}/{path}. You just need to make sure that one of your searchPaths/{path} can resolve your file.
First off, {label} is, as always, the git branch name (probably "master").
Second, {path} can can be an absolute path to the file. It uses /, i.e., myapp/nested/folder/myfile.txt, not (_) as is required in {application} or {label}.
Third, the search paths in the question are set to '{application}' and '{application}/{profile}', along with the default search path of /, the git repo's root directory. These define the places Config Server will search for a plain text file as:
/{expanded application}/{path}
/{expanded application}/{profile}/{path}
/{path}
Note that only {application} can be expanded into multiple folders with (_) and that only {path} can include multiple folders with /. For instance, with these searchPaths and a file located at /myapp/nested/folder/myfile.txt, the following requests are valid:
/asdf/asdf/master/myapp/nested/folder/myfile.txt
/myapp/asdf/master/nested/folder/myfile.txt
/myapp/nested/master/folder/myfile.txt
/myapp(_)nested(_)folder/asdf/master/myfile.txt
/myapp(_)nested/folder/master/myfile.txt
where asdf can be any arbitrary string.

Related

Spring cloud's config server plain text api with SVN and a default label

I have spring boot 2 app that acts as a config server with the following properties. Notice in particularly the "default-label" properties which is the empty string because we check out directly the folder that contains the files, and not some parent branch/folder.
spring:
application:
name: config-server
profiles:
include: subversion
cloud:
config:
server:
svn:
uri: https://...somesvnrepo.../project/trunk/config
username: fake
password: notreal
default-label:
basedir: C:\Users\John\Documents\Application\configserver_tmp
The contents of /trunk/config is straigthforward. There ae no subdirectories and just these files:
application.yml
application-dev.yml
myservice.yml
myservice-dev.yml
logback.xml
Serving the yml files works fine, but getting the logback.xml file using the "plain text api" not work at all.
Doing localhost:8888/appname/default/master/logback.xml gives the error "no label master found" which is true, I don't have that label. Any other combination of paths by omitting profiles or labels results in a 404 all the way up to just calling localhost:8888/logback.xml. Adding the ?useDefaultLabel request parameter makes no difference. Actually I don't understand the purpose of the appname, profile and label part of the url when the context is to get a plain text file that is not bound to any specific application, profile or label.
I found similar questions on the internet but they mention updating their spring boot version and then it worked for them. I'm already at the latest spring boot version (2.1.3-release).
Is this because I use SVN? Or because of of the default-label being empty?

How to upload to SFTP user's Home directory using Spring Integration

I'm trying to upload a file via SFTP using Spring Integration (version 4.1.2).
Does anyone know how to configure the sftp:outbound-channel-adapter so that the file gets uploaded automatically to user's home directory without indicating the full directory path in the remote-directory's attribute (ex: remote-directory="/home/sftp_user")?
The solution is that the remote-directory must be set as an empty string.
Please note that the following won't work as it fails the XML model validation:
<sftp:outbound-channel-adapter
...
remote-directory=""
...
/>
I ended up reading the remote-directory from a configuration property which I set as an empty string.

Loading multiple config file from spring cloud config server

Hey I got to know how to check properties in config server i.e. http://{Config server URL}:{Port}/{ServiceID}/{Profile}
Right now I am facing a problem where I've to read multiple properties file from cloud config server. Ex: Right now I've to read properties file(huge file) from a single property file i.e. employee.properties
I don't want huge list of properties file in single folder. like
employee-dev.properties
employee-dit.properties
employee-sit.properties
employee-uat.properties
employee-preprod.properties
employee-prod.properties
customers-dev.properties
customers-dit.properties
customers-sit.properties
customers-uat.properties
customers-preprod.properties
customers-prod.properties
.
.
.
and soo on.
My requirement is I need to maitain a list of properties file and config server will provide me these values. like :
**{env}/employee.properties**
**{env}/customers.properties**
Here env could be any environment like dit, sit, prod etc.
Hope I'll get answer here.
If you don't want to have all properties files in an only folder, so you need to add this configuration on config server properties file.
1. Match all options
spring.cloud.config.server.native.searchLocations = [classpath:/, classpath:/config, classpath:/config/{application}, classpath:/config/{application}/{profile}]
2. Match only /application-name/application-name-profile.properties
spring.cloud.config.server.native.searchLocations = classpath:config/{application}
Then you'll have matched based on application name folder for example.
resources/config/application-name/application-name-properties.

Spring command line JSON config containing array

I am using Grails 3 Elasticsearch plugin with Springs external JSON configuration by setting spring.application.json as system property.
The properties are available in the application but I can't find a way to initialize an array properly.
What I am trying to accomplish is to override the default values of the hosts property specified in my application.yml:
environments:
development:
elasticSearch:
client:
hosts:
- {host: "myhost.com", port: 9300}
- {host: "anotherhost.com", port: 9300}
I am setting the property from the command line as follows:
-Dspring.application.json={"environments":{"development":{"elasticSearch":{"client":{"hosts":[{"host":"override1.com", "port":9000},{"host":"override2.com", "port":9100}]}}}}}
I would expect environments.development.elasticSearch.client.hosts to contain an array like it does when initialized from the application.yml, but in fact environments.development.elasticSearch.client containes host[0] and host[1], where each contains the host and the port. The host array from the yml file is still there.
How can I achieve the same behavior using the command line as with the application.yml file?
I believe you can do this the same way that you would if it was set in a .properties file, using a list:
-Denvironments.developmet.elasticSearch.client.hosts={"host":"override1.com", "port":9000},{"host":"override2.com", "port":9100}
and I believe you can also do it as an environment variable...
set ENVIRONMENTS_DEVELOPMENT_ELASTICSEARCH_CLIENT_HOSTS='{"host":"override1.com", "port":9000},{"host":"override2.com", "port":9100}'
There may need to be some quotes around parts of these depending on the shell you are in, the OS, etc.

How can I change config file path in Codeigniter?

I use Codeigniter framework , and you know when I try to load a config file then use it
I do something like that :
$this->load->config('myconfig', TRUE);
myconfig.php file is located inside application folder ( application/config/myconfig.php)
and use it like this :
$this->config->item('get_item', 'myconfig')
My question is : how can I change the location of myconfig file and use it properly ?
I want to put the config file(s) in out folder like this :
mysite -> system(folder)
mysite -> user_guide(folder)
mysite -> myConfigFiles(folder)
mysite -> myConfigFiles(folder) / myconfig.php
I need to do something like this :
$this->load->config(base_url.'myConfigFiles/myconfig', TRUE);
any help ?
Yes - it is possible to do this. The loader will accept ../../relative/paths. You can use a path relative from the default config directory (an absolute path will not work).
So let's say you have this structure (had a hard time following your description):
mysite
application
config <-- default directory
system
myConfigFiles
myconfig.php
You can just do this:
$this->load->config('../../myConfigFiles/myconfig', TRUE);
This works for pretty much everything - views, libraries, models, etc.
Note that with the introduction of the ENVIRONMENT constant in version 2.0.1, you can automatically check for config files within the config directory in another directory that matches the name of the current environment. This is really intended to be a convenience method for loading different files depending on if you are in production or development. I'm not 100% sure what your goals are, but this additional knowledge may also help you achieve them, or it may be totally irrelevant.
Really not sure WHY you would want to do this (and I wouldn't recommend it), but since all config files are is regular PHP files you can put a config file in the standard location that loads your extra config files. As an example:
mysite -> application -> config -> myconfigloader.php
then in myconfigloader.php put this:
<?php
require_once(APPPATH.'../myConfigFiles/myconfig.php');
So once you do
$this->load->config('myconfigloader', TRUE);
It will load everything in your myconfig.php file. Let me know if that works for you.

Resources