![見出し画像](https://assets.st-note.com/production/uploads/images/116087745/rectangle_large_type_2_2634745f0b59a1e54c0bdc184b28aa9d.jpeg?width=1200)
【中央化された設定サーバー】 Spring Cloud Config Server - Springboot3 第6回
はじめに
こんにちは、今日は「Spring Cloud Config Server」を勉強します!「Spring Cloud Config Server」 を使用する場合、設定ファイルを変更するたびに各インスタンスでマイクロサービスを再起動する必要はありません。ですので、中央化された設定サーバーとも言われます。
![](https://assets.st-note.com/img/1694595758896-wFJu9BWBhj.png?width=1200)
config-server プロジェクトの作成
![](https://assets.st-note.com/img/1694595758510-Q1sYaBalVh.png?width=1200)
![](https://assets.st-note.com/img/1694595756617-aOI5h7DeGk.png?width=1200)
<ConfigServerApplication>
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
@EnableConfigServerのアノテーションを付きます。
config-server を Eureka Clientとして登録
spring.application.name=CONFIG-SERVER
server.port=8888
eureka.instance.client.serverUrl.defaultZone=http://localhost:8761/eureka
config-server の Git ロケーションの設定
spring.application.name=CONFIG-SERVER
server.port=8888
eureka.instance.client.serverUrl.defaultZone=http://localhost:8761/eureka
spring.cloud.config.server.git.uri=https://github.com/{myaccount}/springboot-microservices.git
spring.cloud.config.server.git.clone-on-start=true
spring.cloud.config.server.git.default-label=main
![](https://assets.st-note.com/img/1694595756412-2buffxHhXC.png?width=1200)
リファクタリング: department-service
![](https://assets.st-note.com/img/1694595757837-nOwmTqYxEt.png?width=1200)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
department-service <pom.xml>に依存性追加
![](https://assets.st-note.com/img/1694595758177-hwzxfiobfU.png?width=1200)
Githubではアプリケーションnameを削除て、
springboot-microservicesのrootフォルダにコミット
spring.config.import=optional:configserver:http://localhost:8888
department-service <pom.xml>に追加します。
![](https://assets.st-note.com/img/1694595757199-lxN2Zo0AgX.png?width=1200)
リファクタリング: employment-service
department-serviceと同じ流れで依存性を追加します。
注意すべき点はConfig-Serverを再起動しないといけないのです。
「/actuator/refresh」の実装
![](https://assets.st-note.com/img/1694595755541-a93vS6V25h.png)
@RefreshScope
@RestController
public class MessageController {
@Value("${spring.boot.message}")
private String message;
@GetMapping("message")
public String message() {
return message;
}
}
「message()」メソッドは「/message」エンドポイントを通じて「spring.boot.message」環境属性値を露出するRESTfulAPIを提供します。 これらのエンドポイントは、環境プロパティが変更されるたびに新しい値を取得できるように@RefreshScopeアノテーションを使用して動的に更新できます。
@RefreshScope
このアノテーションは、Spring Cloud Config サーバーと一緒に使用される際、環境属性の変更を動的に適用するために使用されます。 プロパティ値が変更されると、アプリケーションを再起動することなく変更された値を取得できます。
@Value("${spring.boot.message}")
このアノテーションはSpringの@Valueアノテーションを使用してspring.boot.message環境属性値を注入します。 これにより、message変数に該当プロパティの値を取得できます。
![](https://assets.st-note.com/img/1694595993268-bJgqqcuhXi.png?width=1200)
![](https://assets.st-note.com/img/1694595760146-Dw9ttsEoNw.png?width=1200)
うまくできた。
department-serviceやconfig-serverを再起動しなくて、Githubのmessageの修正
![](https://assets.st-note.com/img/1694596025896-tKK8A2Z5xl.png?width=1200)
![](https://assets.st-note.com/img/1694596051310-IPkMHjrYT5.png?width=1200)
![](https://assets.st-note.com/img/1694595760427-HG694WNReG.png?width=1200)
つまり、設定ファイルを変更するたびにマイクロサービスを再起動する必要はありません。
![](https://assets.st-note.com/img/1694596574736-GhsWmVsDp4.png?width=1200)
Githubのアドレス
https://github.com/Commonerd/springboot-microservices
最後に
システムメンテナンスをしていた時、隣の同僚がAPサーバーを切らずにあるURLを飛ばして環境設定を変えるというのを見たことがあります。 当時、とても不思議でこれが何なのか聞いてみましたが、マイクロサービスに対する全体的な理解がなくて聞いてもよく理解できませんでした。 今回の勉強をきっかけに、Config Serverの便利さだけでなく、マイクロサービスの全体的な可用性を高める方法だということも理解できてとても有益な勉強になりました!
エンジニアファーストの会社 株式会社CRE-CO
ソンさん
【参考】
[Udemy] Building Microservices with Spring Boot & Spring Cloud