[go: up one dir, main page]

0% found this document useful (0 votes)
57 views30 pages

06 Spring Cloud Config

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views30 pages

06 Spring Cloud Config

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Spring Cloud Config

Centralized, versioned configuration


management for distributed applications

Copyright Ken Krueger 2015


Objectives
● At the end of this module, you will be able to
– Explain what Spring Cloud Config is
– Build and Run and Spring Cloud Config Server
– Establish a Repository
– Build, Run, and Configure a Client

Copyright Ken Krueger 2015


Module Outline
● Configuration Management
– Challenges
– Desired Solution
● Spring Cloud Config
– Server Side
– Client Side
● Repository Organization

Copyright Ken Krueger 2015


What is
Application Configuration?
● Applications are more than just code
– Connections to resources, other applications

● Usually use external configuration to adjust software behavior


– Where resources are located
– How to connect to the DB
– Etc.

Copyright Ken Krueger 2015


Configuration Options
● Package configuration files with application
– Requires rebuild, restart

● Configuration files in common file system


– Unavailable in cloud

● Use environment variables


– Done differently on different platforms
– Large # of individual variables to manage / duplicate

● Use a cloud-vendor specific solution


– Coupling application to specific environment

Copyright Ken Krueger 2015


Other Challenges
● Microservices → large # of dependent services
Manual Work, Brittle
● Dynamic updates
– Changes to services or environment variables require
restage or restart
Deployment Activities
● Version control

Traceablity

Copyright Ken Krueger 2015


Desired Solution for Configuration
● Platform/Cloud-Independent solution
– Language-independent too
● Centralized
– Or a few discrete sources of our choosing
● Dynamic
– Ability to update settings while an application is running
● Controllable
– Same SCM choices we use with software
● Passive
– Services (Applications) should do most of the work themselves by
self-registering

Copyright Ken Krueger 2015


Solution:
● Spring Cloud Config
– Provides centralized, externalized, secured, easy-to-reach
source of application configuration
● Spring Cloud Bus
– Provides simple way to notify clients to config changes
● Spring Cloud Netflix Eureka
– Service Discovery – Allows applications to register
themselves as clients

Copyright Ken Krueger 2015


Module Outline
● Configuration Management
– Challenges
– Desired Solution
● Spring Cloud Config
– Server Side
– Client Side
● Repository Organization

Copyright Ken Krueger 2015


Spring Cloud Config
● Designates a centralized server to serve-up configuration information
– Configuration itself can be backed by source control

● Clients connect over HTTP and retrieve their configuration settings


– In addition to their own, internal sources of configuration

HTTP Client Application


ConfigServer
Config Server (Spring)
Config Server HTTP
H
TT
P Client Application
(Spring)

Backing Files
(Git, Flat Files, etc.) Client Application
(Another Technology)

Copyright Ken Krueger 2015


Module Outline
● Configuration Management
– Challenges
– Desired Solution
● Spring Cloud Config
– Server Side
– Client Side
● Repository Organization

Copyright Ken Krueger 2015


Spring Cloud Config Server
● Source available at GitHub:
https://github.com/spring-cloud-samples/configserver
● Or, it is reasonably easy to build your own

Copyright Ken Krueger 2015


Spring Cloud Config Server – Building, part 1
<parent>
● Include minimal dependencies in <groupId>org.springframework.boot</groupId>
your POM (or Gradle) <artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
– Spring Boot Starter Parent
</parent>
– Spring Cloud Starter Parent ...
– Spring Cloud Config Server <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Dalston.RELEASE</version>
...
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>

Copyright Ken Krueger 2015


Spring Cloud Config Server – Building, part 2
● application.yml – indicates location of configuration repository

---
spring:
cloud:
config:
server:
git:
uri: https://github.com/kennyk65/Microservices-With-Spring-Student-Files
searchPaths: ConfigData

– ...or application.properties

Copyright Ken Krueger 2015


Spring Cloud Config Server – Building, part 3
● Add @EnableConfigServer
@SpringBootApplication
@EnableConfigServer
public class Application {

public static void main(String[] args) {


SpringApplication.run(Application.class, args);
}

● That's It!

Copyright Ken Krueger 2015


Module Outline
● Configuration Management
– Challenges
– Desired Solution
● Spring Cloud Config
– Server Side
– Client Side
● Repository Organization

Copyright Ken Krueger 2015


The Client Side – Building part 1
● Use the Spring Boot Starter parent as a Parent POM:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>

● ...And use Dependency management section for Spring Cloud:


<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Dalston.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

Copyright Ken Krueger 2015


The Client Side – Building Part 2
● Include the Spring Cloud Starter for config:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

● Configure application name and server location in


bootstrap.properties / yml
● So it is examined early in the startup process
# bootstrap.properties:
spring.application.name: lucky-word
spring.cloud.config.uri: http://localhost:8001

● That's It!
– Client connects at startup for additional configuration settings.

Copyright Ken Krueger 2015


The Client Side
● How Properties work in Spring Applications
– Spring apps have an Environment object
– Environment object contains multiple PropertySources
● Typically populated from environment variables, system properties, JNDI,
developer-specified property files, etc.
– Spring Cloud Config Client library simply adds another
PropertySource
● By connecting to server over HTTP
● http://<server>:<port>/<spring.application.name>/<profile>
– Result: Properties described by server become part of client
application's environment

Copyright Ken Krueger 2015


Client-Side Startup Process
Two Stages:

● Bootstrap:
– Application context is loaded using bootstrap.yml (or .properties)
– Application connects to Config server, loads more properties

● Main:
– A “child” application context is loaded using application.yml (or properties)
– All properties from parent/bootstrap Environment are available on startup.

● Recommendation: Use bootstrap.yml / .properties only for settings that


MUST be set early (application name, URI of config server, etc.

Copyright Ken Krueger 2015


Module Outline
● Configuration Management
– Challenges
– Desired Solution
● Spring Cloud Config
– Server Side
– Client Side
● Repository Organization

Copyright Ken Krueger 2015


EnvironmentRepository - Choices
● Spring Cloud Config Server uses an
EnvironmentRepository
– Two implementations available: Git and Native (local files)
● Implement EnvironmentRepository to use other
sources.

Copyright Ken Krueger 2015


Environment Repository - Organization
● Configuration file naming convention:
– <spring.application.name>-<profile>.yml
● Or .properties (yml takes precedence)
– spring.application.name – set by client application's bootstrap.yml (or
.properties)
– profile – Client's spring.profiles.active
● (set various ways)
● Obtain settings from server:
– http://<server>:<port>/<spring.application.name>/<profile>
– Spring Cloud clients do this automatically on startup

Copyright Ken Krueger 2015


Environment Repository – Organization Example
● Assume client application named “lucky-word” and profile
set to “northamerica”
– Spring client (automatically) requests
● /lucky-word/northamerica
Ignored (profile is set)

lucky-word-default.yml Included (second precedent)


lucky-word.yml
Included (first precedent)
lucky-word-northamerica.yml
lucky-word-europe.yml
lucky-word.properties Ignored (different profile set)
another-app.yml

Included (third precedent)


Ignored (different app)

Copyright Ken Krueger 2015


.yml vs .properties
● Settings can be stored in either YAML or standard Java
properties files
– Both have advantages
– Config server will favor .yml over .properties

# .yml file
# .properties file
---
spring.config.name=aaa
spring:
spring.config.location=bbb
config:
spring.profiles.active=ccc
name: aaa
spring.profiles.include=ddd
location: bbb
some.other.property=fff
profiles:
active: ccc
include: ddd
some.other.property: fff

Copyright Ken Krueger 2015


Profiles
● YAML Format can hold multiple profiles in a single file

# lucky-word-east.properties # luckyword.yml
lucky-word: Clover ---
spring:
profiles: east
lucky-word: Clover
# lucky-word-west.properties ---
lucky-word: Rabbit's Foot spring:
profiles: west
lucky-word: Rabbit's Foot

Copyright Ken Krueger 2015


What about non-Java / non-Spring
Clients?
● Spring Cloud Server exposes properties over simple HTTP
interface
– http://<server>:<port>/<spring.application.name>/<profile>
● Reasonably easy to call server from any application
– Just not as automated as Spring.

Copyright Ken Krueger 2015


What if the Config Server is Down?
● Spring Cloud Config Server should typically run on
several instances
– So downtime should be a non-issue
● Client application can control policy of how to handle
missing config server
– spring.cloud.config.failFast=true
– Default is false
● Config Server settings override local settings
– Strategy: provide local fallback settings.

Copyright Ken Krueger 2015


Summary
● Spring Cloud Config offers centralized, versioned
configuration for distributed applications
● Spring Cloud Config Server – Easy to Build

– Backed by repository (Git or native) with .yml or


.properties
● Spring Cloud Config Client –
– Accesses Server, adds another PropertySource

Copyright Ken Krueger 2015


Exercise

Setup your own Spring Cloud


Config Server, Client, and Repository

Instructions: Student Files, Lab 3

Copyright Ken Krueger 2015

You might also like