Spring Cloud Config server
Spring Cloud Config server
Spring Cloud Config to serwer, który wspiera zarządzanie konfiguracją mikroserwisów. Do projektu opartego o Mavena i Spring Boota dodajemy zależności w pliku pom.xml:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency><!--?prettify linenums=true?-->
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR4</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
W pliku konfiguracyjnym application.yml dodajemy:
spring: cloud: config: server: git: uri:path-to-git-repository server: port: 8888
W repozytorium GITa tworzymy i zapisujemy pliki konfiguracyjne dla odpowiednich mikroserwisów np:
app-1.properties
Plik app-1.properties powinien mieć przykładową zawartość:
message = my configuration from spring cloud server
Włączamy Spring Cloud Server dodając adnotację EnableConfigServer :
@SpringBootApplication @EnableConfigServer public class SpringCloudServerApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudServerApplication.class, args); } }
W projekcie mikroserwisu dodajemy zależności:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR4</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
Tworzymy REST kontroler:
@RefreshScope @RestController class WebController { @Value("${message}") private String message; @RequestMapping("/message") String getMessage() { return message; } }
W pliku application.properties dodajemy:
spring.application.name = app-1 spring.cloud.config.uri = http://localhost:8888 management.endpoints.web.exposure.include = * server.port = 8082
Dowolnym klientem REST wysyłamy żądanie POST pod podany niżej adres aby odświerzyć konfigurację:
http://localhost:8082/refresh
Każda zmiana na serwerze w pliku app-1.properties wymaga wykonania commita do repozytorium GITa. Po zatwierdzeniu zmiany należy odświerzyć ponownie konfigurację. Odświerzanie konfiguracji dla każdego mikroserwisu z osobna jest wadą przedstawionego rozwiązania. Z pomocą przychodzi projekt Spring Cloud Bus…
Leave a comment