Reading key value from consul in springboot application - spring-boot

I want to read my external configs like database host and port from consul.
I want to access it in my Springboot application.
So I have created a springboot application and have added cloud config for consul configuration.
I have created an bootstrap.yml where I give my consul host and port and I am able to connect.
But I am not able to fetch any key-value pair from consul.
I have posted my bootstrap.yml below asd well.
Can somebody guide me how to do that
spring:
cloud:
consul:
host: localhost
port: 8500
config:
enabled: true
data-key: config/application/datakey
TIA

You can make use of ConsulClient and then use getKVValue method to read the kv configs you have set in consul.

Related

Fargate service discovery for eureka clients

We are planning to host our eureka discovery server in the same vpn as our fargate services. What configuration we have to provide in our client container configuration to enable them to seamlessly connect with our discovery server. The discovery server is not hosted on fargate , it is on separate EC2 machine.
You need to point the clients to the Eureka server including the following configuration on the client's application.yml:
spring:
application:
name: <client-name>
eureka:
client:
serviceUrl:
defaultZone: ${EUREKA_URI:http://localhost:8761/eureka}
The EUREKA_URI environment variable need to be set on containers with the Eureka address. The additional localhost URL allows apps run without containers on dev workstations along with a local copy of Eureka (just another boot app).

Accessing Gateway Properties from another microservice using Spring boot

I have Config Microservice , Employee Microservice , Gateway Microservice . application.yml will be loaded from Config Microservices for Employee and Gateway Microservice using the Spring.cloud.config.
i need to use one of the Gateway Property in the Employee Microservice . how to achieve this ?
Config Microservice contains below files
application.yml
server:
port: 8080
Gateway.yml
app:
access-table: true
Employee.yml
server:
port: 8081
In the Employee Microservice, Employee.yml is loaded using the Spring cloud config URI .
cloud:
config:
uri: http://localhost:8080
how can i access the access-table property from the gateway.yml in the Employee Microservices ?
i tired using name property but it overrides the other configs , i need to access only that property . is there a way?
cloud:
config:
uri: http://localhost:8080
name: Employee.yml,Gateway.yml
If you have any common properties that required in multiple micro-services then place those properties in config server application.yml.
This application.yml for all micro-services even if have any profile.

Access vault secret using spring-cloud-vault and use it in application.properties

I have a vault server hosted in Openshift and I have to access secrets from the Vault into my spring application.
My bootstrap.yml looks like this :
spring:
application:
name: application-name
profiles: dev
cloud:
vault:
fail-fast: true
host: HOST
port: 443
scheme: https
token: MY_TOKEN
authentication: TOKEN
kv:
enabled: true
backend: secret
profile-separator: '/'
application-name: application-name
I checked vault logs and able to make connection from spring application to vault.
I can access the secret using Value Property Source.
However, I want to populate the secret's value into application.properties to update properties like spring.datasource.username and spring.datasource.password.
Is there any way to access the secret directly from application.properties?
TL; DR: Yes, you can use Vault properties in application.(properties|yml). It's not recommended to use these in bootstrap.(properties.yml).
Spring Cloud comes with a Bootstrap context where configuration libraries (such as Spring Cloud Consul, Spring Cloud Config and Spring Cloud Vault) are initialized. These integrations fetch configuration and provide these as a parent PropertySources to your application. Spring Boot considers these (you have options to use these PropertySources with the highest/lowest priority) during property binding and when you resolve a property value using Environment.
When bootstrapping an application, then typically one of the first things that happen is property binding in #ConfigurationProperties objects. At the time when bootstrap.(properties|yml) is loaded, typically Spring Cloud Config integrations didn't run yet so at that time you don't see properties contributed by these libraries. Therefore, there's the split between bootstrap context and the actual application context.

Can Spring set datasource from service discovery?

Before I start trying to programmatically set datasource configuration in my app using service discovery, I would like to be sure this functionality is not already managed by Spring.
In my case I am using Consul as the discovery service. So I read http://cloud.spring.io/spring-cloud-consul/1.0.x/index.html but don't really find something to answer my question (prolly because of my flawed knowledge of Spring configuration itself).
Basically I would configure the discovery system in a bootstrap.yml file
spring:
cloud:
consul:
host: localhost
port: 8500
then in the application.yml I would set something like:
spring:
datasource:
url: jdbc:${consul.service.datasource}
Is this possible "natively". If not, what do I miss in the concepts involved?

Modify spring cloud config server in the client

I have a spring boot powered spring cloud application with a configuration server running seperately on port say 8001 on localhost.
Meanwhile, location has been specified in the config client applications/micro services as below in the bootstrap.yml file of the client project.
spring:
cloud:
config:
uri: http://localhost:8001
This works absolutely fine.
However when i want to deploy the whole application on different setups, i would need to run the config server on different IPs and Ports.
In that case i can not go and change the IP:Port information of the config server in all the projects, rebuild the jar and deploy them. In fact in most scenarios, Jenkins build the Jars by itself on different environment.
How can we handle such situation? Can we specify an environment variable in the bootstrap.yml, if yes how to do it?
Any suggestion?
Br,
AJ
You can definitely use environment variables in your bootstrap.yml file:
spring:
cloud:
config:
uri: http://${configServerHost}:${configServerPort}
When you launch your application you only have to add -DconfigServerHost=localhost -DconfigServerPort=8001

Resources