1。はじめに
マイクロサービスアーキテクチャシステムでは、通常、軽量のメッセージブローカーを使用して共通のメッセージトピックを構築して、システム内のすべてのマイクロサービスインスタンスを接続します。このトピックで生成されたメッセージは、すべてのインスタンスで聴き、消費されるため、メッセージバスと呼びます。
2。メッセージプロキシ
メッセージブローカーは、メッセージの検証、送信、ルーティングのアーキテクチャモデルです。アプリケーション間の通信スケジューリングとして機能し、アプリケーション間の依存関係を最小限に抑えるため、アプリケーションは通信プロセスを効率的に切り離すことができます。メッセージブローカーはミドルウェア製品です。そのコアは、メッセージを受信して配布するために使用され、設定されたメッセージ処理フローに基づいて正しいアプリケーションに転送するために使用されるメッセージルーティングプログラムです。独立した通信およびメッセージングプロトコルが含まれ、組織内および組織間のネットワーク通信を可能にします。プロキシを設計する目的は、アプリケーションからメッセージを渡し、特別な操作を実行できることです。以下は、メッセージプロキシを使用する必要があるエンタープライズアプリケーションのシナリオです。
以下など、誰もが使用できるオープンソース製品はすでにたくさんあります。
3。SpringCloud+rabbitmq
(1)rabbitmqの紹介とインストールは詳細ではありません。
(2)pom.xml
<Dependencies> <Dependency> <GroupId> org.springframework.boot </groupid> <artifactid> spring-boot-starter-amqp </artifactid> </dependency> <redency> <groupid> org.springframework.boot </groupid> <artifactid> </scartid> </scartid> </dependency> </dependencies>
(3)Application.yml
春:アプリケーション:名前:rabbitmq-hello rabbitmq:host:***。***。***。***ポート:5672ユーザー名:ゲストパスワード:ゲスト:ゲスト
(4)送信者
@component public class sender {private static final logger log = loggerfactory.getLogger(sender.class); @Autowiredプライベートamqptemplate amqptemplate; public void send(){string context = "hello" + new date(); log.info( "sender:" + context); this.amqptemplate.convertandsend( "hello"、context); }}(5)受信機
@Component @RabbitListener(Queues = "Hello")public class Receiver {private static final logger log = loggerfactory.getLogger(receiver.class); @rabbithandler public void process(string hello){log.info( "受信者:" + hello); }}(6)RabbitMQ構成クラスRabbitConfigを作成します
@configuration public class rabbitconfig {@bean public queue helloqueue(){return new queue( "hello"); }} (7)メッセージ制作を呼び出すためのユニットテストクラスを作成する
@runwith(springjunit4classrunner.class)@springboottest(classes = springcloudbusrabbitmqapplication.class)public class helloapplicationtests {@autowired private sender sender; @test public void hello()throws exception {sender.send(); }} (8)テスト、helloApplicationTestsを実行します
(9)ホストを訪問:15672
4。Config-Client(SpringCloudバスを統合)変換
(1)pom.xml
<Dependencies> <Dependency> <GroupId> org.springframework.cloud </groupid> <artifactid> spring-cloud-starter-config </artifactid> </dependency> <dependency> <groupid> org.springframework.boot </groupid> <artifactid> spring-webb < <groupid> org.springframework.cloud </groupid> <artifactid> spring-cloud-starter-eureka </artifactid> </dependency> <dependency> springframework.cloud </groupId> <artifactid> spring-cloud-starter-amqp </artifactid> </</artifactid> <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-starter-actuator </artifactid> </dependency> <依存関係> <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-startertest
(2)bootstrap.properties
spring.application.name = configspace spring.cloud.config.label = master spring.cloud.config.profige = dev spring.cloud.config.uri = http:// localhost:5588/eureka.client.serviceurl.defouldzone = http:////// localhost:5555/eureka spring.rabbitmq.host = 118.89.237.88 spring.rabbitmq.port = 5672 spring.rabbitmq.username = guest spring.rabbitmq.password = guest management.security.enabled = fals
(3)他の人を変更する必要はありません
V.テスト
(1)テスト準備
ポート5555、Eurekaserverのサービス登録センター。
ポート5588の分散構成センター、configserver。
2つの分散構成、ConfigClient、ポートは5589および5590です。 (2)http:// localhost:5589/fromにアクセスしてください
(3)http:// localhost:5590/fromにアクセスしてください
rabbitmq:
(4)倉庫に移動してパスワード値を変更します
from = git-dev-v1.0 by springcloud config-server username = springcloudパスワード= 1234567890
(5)リクエストhttp:// localhost:5589/bus/refreshまたはhttp:// localhost:5590/bus/refresh
リクエストが成功した後、config-clientは構成ファイルを読み直します。
(6)もう一度訪問します
投稿がリクエストされている場合:http:// localhost:5590/bus/reffresh、http:// localhost:5589/fromにアクセスしてください
さらに、/bus/refreshインターフェイスは、「/bus/refresh?destination = username:** "などの「ユーザー名」パラメーターを使用して、サービス名がIPアドレスに関係なくユーザー名のすべてのサービスを更新します。
(7)アーキテクチャ
(8)アーキテクチャの調整
SpringCloudバスの /バス /リフレッシュインターフェイスは、サービスおよびインスタンスの構成更新用のパラメーターを提供するため、当社のアーキテクチャもそれに応じて調整することができます。以前のアーキテクチャでは、特定のサービスのインスタンスにリクエストを送信するために必要なサービスの構成更新を行い、サービスクラスター全体の構成更新をトリガーします。機能を実装できますが、指定したアプリケーションインスタンスはクラスター内の他のアプリケーションインスタンスとは異なるため、クラスター内の複雑さが高まり、将来の運用とメンテナンス作業に有害になります。たとえば、サービスインスタンスを移行する必要がある場合は、Webフックなどの構成を変更する必要があります。したがって、サービスクラスターピアの各ノードを可能な限り変更する必要があります。
したがって、下の図に示すように、以前のアーキテクチャを調整しました。
次の変更が行われました。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。