In einer verteilten Microservice-Architektur besitzt jeder Service
eigene Konfigurationsparameter: Datenbankverbindungen, Fremd-APIs,
Timeouts und Feature-Flags. Diese Konfigurationen in jedem Service lokal
in application.properties zu pflegen, führt zu
Konfigurationsabweichungen (Configuration Drift), Sicherheitsrisiken
(Zugangsdaten im Quellcode) und Neustarts bei jeder
Parameteränderung.
In dieser Aufgabe lernen Sie Spring Cloud Config kennen:
default, qa, prod) trennen.@RefreshScope
geladen werden.spring-cloud-config-starter.zipDas Archiv enthält drei Verzeichnisse:
configserver/: Ein vorkonfigurierter Spring Cloud
Config Server (Port 8888)config-client/: Eine Beispiel-Client-Anwendung mit
Thymeleaf-View und Actuatorschool-spring-cloudconfig-wa-tolls/: Lokales
Konfigurationsverzeichnis mit Profil-Propertiesflowchart TD
Git["Zentrales Git-Repository / Verzeichnis<br>(s1rates.properties, s1rates-qa..)"]
Server["Config-Server (Port 8888)<br>@EnableConfigServer"]
Client["Config-Client (Port 8080)<br>@RefreshScope"]
Git -->|"liest Properties bei Start & Refresh"| Server
Server -->|"HTTP GET /s1rates/default (liefert Key-Value)"| Client
spring-cloud-config-starter.zip
und öffnen Sie das Projekt in IntelliJ IDEA.configserver.ConfigserverApplication.java. Beachten Sie die Annotation
@EnableConfigServer:
@SpringBootApplication
@EnableConfigServer
public class ConfigserverApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigserverApplication.class, args);
}
}src/main/resources/application.properties
des Config-Servers:
server.port=8888
spring.cloud.config.server.git.uri=file://${user.dir}/school-spring-cloudconfig-wa-tolls
Hinweis: Der Server kann ein entferntes Git-Repository oder ein lokales
Verzeichnis verwenden.ConfigserverApplication.curl http://localhost:8888/s1rates/defaultDer Config-Server folgt einer festen URL- und Dateinamen-Konvention:
URL-Muster:
http://<host>:8888/<application-name>/<profile>[/<label>]
s1rates.properties: Gilt für alle Profile der
Applikation s1rates.s1rates-qa.properties: Überschreibt Werte spezifisch
für das Profil qa.application.properties: Globale Standardwerte für alle
angebundenen Microservices.config-client.src/main/resources/application.properties des Clients:
spring.application.name=s1rates
spring.profiles.active=default
spring.config.import=configserver:http://localhost:8888
Wichtig: spring.application.name bestimmt, welchen
Konfigurationssatz der Client beim Server anfragt.RateController.java:
@Controller
@RefreshScope
public class RateController {
@Value("${rateamount:1.00}")
private String rateamount;
@Value("${lanes:1}")
private String lanes;
@GetMapping("/rate")
public String getRate(Model model) {
model.addAttribute("rateamount", rateamount);
model.addAttribute("lanes", lanes);
return "rateview";
}
}ConfigClientApplication (Port
8080).http://localhost:8080/rate auf.
@RefreshScope)In traditionellen Anwendungen erfordert jede Einstellungsänderung
einen Neustart des Service. Mit Spring Boot Actuator und
@RefreshScope lassen sich Konfigurationen im laufenden
Betrieb aktualisieren.
school-spring-cloudconfig-wa-tolls/s1rates.properties den
Wert von rateamount (z. B. auf 4.20).git add s1rates.properties
git commit -m "Update rateamount to 4.20"http://localhost:8888/s1rates/default den neuen Wert?http://localhost:8080/rate
auf.POST-Request an den
Actuator-Refresh-Endpunkt des Clients:
curl -X POST http://localhost:8080/actuator/refreshhttp://localhost:8080/rate im Browser neu:
Passwörter oder API-Schlüssel gehören nicht im Klartext in Konfigurationsdateien.
application.properties):
encrypt.key=MeinGeheimesSchluesselWort123!/encrypt:
curl http://localhost:8888/encrypt -d "SuperSecretPassword"{cipher} in Ihre Properties einfügen:
db.password={cipher}c8a49ef2...Halten Sie Ihre Antworten und Erkenntnisse in einer kurzen Notiz oder Markdown-Datei fest:
/s1rates/default./rate vor und nach dem
Aufruf von /actuator/refresh.spring.application.name beim Client
erforderlich, wenn Spring Cloud Config genutzt wird?@RefreshScope gegenüber einem
Neustart bei einem System mit 50 Microservice-Instanzen?