Project Objectives
This is a sample project to demonstrate Spring Cloud Config and Service Discovery with Consul. The project demonstrates how to load Consul KV Store from a Git Repository using Git2Consul. In addition, the project demonstrates a simple Feign client, Ribbon client-side load-balancing, and dynamic property refreshing.
I could only get the project to work correctly when the following property was set to
yaml
orproperties
despite the documentation calling for the property to be set withfiles
.
spring.cloud.consul.config.format = yaml | properties
Remaining Objectives
- Integrate Vault to store and resolve secrets
- Refine the local development process (bypass bootstrap)
- Include Zuul | Cloud Gateway into this project
- Include Hystrix into this project
- Connect Git2Consul to a private repository. How will it be secure?
Running this project
- Start Docker Container for Consul (runs on port 8500)
docker-compose up
- Initialize Vault see below
- Run Application
VAULT_APP_TOKEN=app_token SPRING_PROFILES_ACTIVE=mypillar,holding ./start.sh
Or without Spring Profiles
./gradlew bootRun
Consul Key Value (KV) Structure
/ config / application / data
/ config / application-[spring-profile] / data
/ config / [microservice-name] / data
/ config / [microservice-name]-[spring-profile] / data
The application key:
/config/application/data
is considered global to all applications.
The application key:/config/application-[spring-profile]/data
is considered global to all applications with the specified spring profile.
Consul Requires that YAML is suffixed by 'data' key to work properly. The configuration above assumes that the key values are all YAML.
Useful Project Links
- Consul - localhost:8500
- Application Health Check
- Simple Rest Ping - Returns Alive!
- RestTemplate mapped to localhost:8080/ping - Returns Alive!
- Ribbon load balanced RestTemplate mapped to springconsuldemo - Returns Alive!
- Ribbon load balanced Feign client - Returns Alive!
- Endpoint to display the properties resolved from the Consul KV store
Resources
- Spring Cloud Consul
- Spring Cloud Consul - Reference
- Git2Consul Configuration Repo
- Article used as base for project
- Overriding Bootstrap Properties
- Overriding Remote Properties
- Multi-Profile YAML
- Cloud Config - Local Dev Mode
- Spring Profiles - Order of Precedence
- Applying multiple Spring profiles
- Good tip for disabling Bootstrap
- More Details on Bootstrap context
Cloud Config Requirements
- Need to support a directory structure approach, all properties are contained within a single continuous/master branch
- Resolve global configs (application), Pillar (profile), Infrastructure (profile), App (spring.application.name)
- Determine if Git2Consul will support yaml with multiple embedded profiles
-
Profiles: Environments: Local, Holding, Staging, Prod, LoadTest Infrastructure: MySQL, Kafka, Redis, etc. Pillar: Directory, Workflow, CIC Deployment Modes: REST, Kafka, Shadow Migration, Autobot, etc
Global Environment.Infrastructure.[cluster id] - ex. holding-kafka, holding-mysql-auditlog Pillar Application
-
#once you disable bootstrap then service discovery is disabled hence no feign @Loadbalanced annotated endpoint will fail #Also any name-resolved rest templates will not work. We would need to make more dynamic configuration to support disabling bootstrap.
#Stackoverflow #https://stackoverflow.com/questions/38251693/correct-way-to-load-values-properties-from-spring-cloud-consul
https://github.com/JorgeHernandezRamirez/SpringCloudConsul https://github.com/spring-cloud/spring-cloud-consul/blob/master/docs/src/main/asciidoc/spring-cloud-consul.adoc
http://cloud.spring.io/spring-cloud-consul/multi/multi_spring-cloud-consul-config.html http://cloud.spring.io/spring-cloud-consul/multi/multi_spring-cloud-consul-discovery.html
Initialize Vault
$ export VAULT_ADDRESS=http://127.0.0.1:8200
$ ./tools/unseal_vault.sh # copy app token from output
$ export VAULT_APP_TOKEN=token