![見出し画像](https://assets.st-note.com/production/uploads/images/116723430/rectangle_large_type_2_9d5bc93c1a768c1922812ec2e0021103.jpeg?width=800)
【マイクロサービスの安定的設定管理】Spring Cloud Bus, Docker, RabbitMQ - Springboot3 第7回
はじめに
こんにちは、今日はマイクロサービスの安定的設定管理として、「Spring Cloud Bus」を勉強します。キーワドは「メッセージブローカ(Message Broker)」になります!
「Spring Cloud Bus」とは?
「Spring Cloud Config Server」は一つの問題点がありますが、それは設定変化をリロードするためには「/refresh」エンドポイントを手動で行う必要があるということです。 これは、複数のアプリケーションがある場合はまったく実用的ではありません。 100個のマイクロサービスがあるとしましょう。 いちいち100個の「/refresh」エンドポイントを呼び出すべきでしょうか? 誰でも考えてみても無理です。
この時に使われるのが「Spring Cloud Bus」モジュールです。 「Spring Cloud Bus」モジュールは、「メッセージブローカー」を通じて多数のアプリケーションと接続するために使用されます。 また、設定変化をブロードキャストすることができる。
![](https://assets.st-note.com/img/1695196293589-T2QbrnKHdV.png?width=800)
メッセージコンポーネントブローカーとして「RabbitMQ」と「Apache Kafka」が広く使われます。 マイクロサービスはメッセージブロッカーを「Subscribe」し、一つのマイクロサービスに設定変化が発生すると、メッセージブロッカーは自分を「subscribe」している他のマイクロサービスにその変化を「broadcast」します。 この時、「busrefresh」というエンドポイントを使います。
実装手順
1. bus AMQP 依存性追加
department-service/pom.xml
employee-service/pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
2. Dockerを通じてRabbitMQインストール
Dockerインストール
![](https://assets.st-note.com/img/1695176522478-JHtVjK69Jg.png?width=800)
![](https://assets.st-note.com/img/1695176614877-EKZ1A84hTD.png?width=800)
![](https://assets.st-note.com/img/1695178478646-LO2GeGXNMp.png?width=800)
![](https://assets.st-note.com/img/1695184165926-th4RwmvRD2.png?width=800)
インストール中いろいろ問題がありました;;;
Docker Desktopは起動するがカーネルが古いと怒られた。
![](https://assets.st-note.com/img/1695185376478-NvywJf6PgK.png)
![](https://assets.st-note.com/img/1695185453329-gdrX713YM9.png)
![](https://assets.st-note.com/img/1695185557799-mhLqwD4kDm.png?width=800)
「Sign In」ボタンを押して、IDを登録して、ログインします。
「https://hub.docker.com/」で 「rabbitmq」のバージョンをチェックします。
![](https://assets.st-note.com/img/1695187226162-lwK2Ak4ezp.png?width=800)
![](https://assets.st-note.com/img/1695187504636-YnTVoNUJBe.png)
![](https://assets.st-note.com/img/1695187557961-LrMsILK26R.png?width=800)
![](https://assets.st-note.com/img/1695187704951-TIGgz2eAQt.png?width=800)
![](https://assets.st-note.com/img/1695188123231-IOl4ZHF8qF.png?width=800)
![](https://assets.st-note.com/img/1695188203231-dXvWJyPuNr.png?width=800)
3. RabbitMQ 設定
department-serviceのapplication.properties
![](https://assets.st-note.com/img/1695188489911-yzLtDDNsRv.png?width=800)
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
employee-serviceのapplication.properties
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
4. employee-serviceのREST API 生成
![](https://assets.st-note.com/img/1695191693054-8PYrA9jbqq.png)
department-serviceの「MessageController」のコード
@RefreshScope
@RestController
public class MessageController {
@Value("${spring.boot.message}")
private String message;
@GetMapping("message")
public String message() {
return message;
}
}
employee-serviceの「MessageController」のコード。エンドポイントが少し変更されました。
@RefreshScope
@RestController
public class MessageController {
@Value("${spring.boot.message}")
private String message;
@GetMapping("/users/message")
public String message() {
return message;
}
}
![](https://assets.st-note.com/img/1695192778043-awkaEo5fad.png?width=800)
![](https://assets.st-note.com/img/1695193045377-QT8DdIKNat.png?width=800)
![](https://assets.st-note.com/img/1695193286652-Y0YZZCz31V.png)
![](https://assets.st-note.com/img/1695193571016-5fRyECHvmt.png?width=800)
![](https://assets.st-note.com/img/1695193355694-IED4y5NwSA.png)
5. department-serviceとemployee-service設定変更後、「/busrefresh」
![](https://assets.st-note.com/img/1695194401162-ZAyN6IL7RV.png?width=800)
![](https://assets.st-note.com/img/1695194478543-2eSmuu6Kjs.png?width=800)
![](https://assets.st-note.com/img/1695194695208-pLdtcpX7Rt.png?width=800)
![](https://assets.st-note.com/img/1695195090227-FsDQkgYABr.png)
![](https://assets.st-note.com/img/1695195071047-RAC5YjhHnK.png)
![](https://assets.st-note.com/img/1695198332995-keEPy6XSJm.png?width=800)
最後に
「Spring Cloud Bus」を使用することで、マイクロサービスアーキテクチャの設定管理を効率的に行う方法を学びました。これにより、複数のアプリケーションがある場合でも設定変更を手動で更新する手間を省くことができ、スムーズな運用が可能となります。また、「RabbitMQ」や「Apache Kafka」などのメッセージブローカーを活用することで、設定変更をブロードキャストし、各マイクロサービスに即座に反映させることができます。これにより、システム全体の安定性と信頼性が向上し、開発者は設定管理にかかる負担を軽減できます。
このように、「Spring Cloud Bus」はマイクロサービスアーキテクチャの設定管理において強力なツールとなり、効果的な運用をサポートします。私も、今後は複数のサービスを連動して開発するときは、こんな方法をつかって安定的な設定管理をしたいと思います!
エンジニアファーストの会社 株式会社CRE-CO
ソンさん
【参考】
[Udemy] Building Microservices with Spring Boot & Spring Cloud
この記事が気に入ったらサポートをしてみませんか?