技術共有

SpringCloud -- Eureka クラスター

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

エウレカ登録センタークラスター

なぜクラスターなのか

登録センター サーバーが 1 つしかない場合は、単一障害点が発生し、高い同時実行で処理できないため、クラスターが必要です。

クラスタリングの方法

相互に登録するには 3 つの EurekaServer を準備します。つまり、各 EurekaServer は、それ自体を含むすべての EureakServer に登録する必要があり、サーバーとクライアントの両方として機能します。他のマイクロサービス (オーダー、ユーザー) は、登録アドレスをすべての EurekaServer に指定するだけで済みます。

コード:

ここで使用されるのは、クラスタリングの効果を実現するために複数の yml ファイルを構成する Eureka モジュールです。

スタートアップクラス

  1. package org.example;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. /**
  6. * @ClassName EurekaStart
  7. * @Author 23
  8. * @Date 2024/7/10 19:12
  9. * @Version 1.0
  10. * @Description TODO
  11. **/
  12. @SpringBootApplication
  13. @EnableEurekaServer
  14. public class EurekaStart {
  15. public static void main(String[] args) {
  16. SpringApplication.run(EurekaStart.class,args);
  17. }
  18. }

yml

  1. spring:
  2. profiles:
  3. active: peer3
  4. ---
  5. spring:
  6. profiles: peer1
  7. application:
  8. name: Eureka1
  9. eureka:
  10. instance:
  11. hostname: localhost
  12. client:
  13. serviceUrl:
  14. defaultZone: http://localhost:10070/eureka/
  15. server:
  16. port: 10070
  17. ---
  18. spring:
  19. profiles: peer2
  20. application:
  21. name: Eureka2
  22. eureka:
  23. instance:
  24. hostname: localhost
  25. client:
  26. serviceUrl:
  27. defaultZone: http://localhost:10071/eureka/
  28. server:
  29. port: 10071
  30. ---
  31. spring:
  32. profiles: peer3
  33. application:
  34. name: Eureka3
  35. eureka:
  36. instance:
  37. hostname: localhost
  38. client:
  39. serviceUrl:
  40. defaultZone: http://localhost:10072/eureka/
  41. server:
  42. port: 10072

ドキュメント

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <parent>
  5. <groupId>org.example</groupId>
  6. <artifactId>Springcloud-Netflix</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. </parent>
  9. <artifactId>Eureka-Service</artifactId>
  10. <packaging>jar</packaging>
  11. <name>Eureka-Service</name>
  12. <url>http://maven.apache.org</url>
  13. <properties>
  14. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  15. </properties>
  16. <dependencies>
  17. <dependency>
  18. <groupId>junit</groupId>
  19. <artifactId>junit</artifactId>
  20. <version>3.8.1</version>
  21. <scope>test</scope>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-web</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-test</artifactId>
  30. </dependency>
  31. <!-- Eureka服务端支持 -->
  32. <dependency>
  33. <groupId>org.springframework.cloud</groupId>
  34. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  35. </dependency>
  36. </dependencies>
  37. </project>

サービスの負荷分散

負荷分散が必要な理由

同時実行性を提供するために、同じサービス プロバイダーが複数 (コモディティ サービス) を展開できる場合があります。このクライアントは、呼び出し時に特定の責任あるバランシング戦略に従ってロード呼び出しを完了する必要があります。

サービスプロバイダーの構成

  1. spring:
  2. profiles:
  3. active: order2
  4. ---
  5. server:
  6. port: 10010
  7. spring:
  8. profiles: order1
  9. application:
  10. name: order-service
  11. eureka:
  12. client:
  13. service-url:
  14. defaultZone: http://localhost:10072/eureka
  15. instance:
  16. prefer-ip-address: true
  17. ---
  18. server:
  19. port: 10012
  20. spring:
  21. profiles: order2
  22. application:
  23. name: order-service
  24. eureka:
  25. client:
  26. service-url:
  27. defaultZone: http://localhost:10072/eureka
  28. instance:
  29. prefer-ip-address: true

一般的な負荷分散実装テクノロジー

リボン

RestTmplate を通じて、URL を使用してサービス呼び出しを完了します

コード実装 (サービス利用者側):

  1. package org.example.Controller;
  2. import org.example.domain.Order;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.cloud.client.ServiceInstance;
  5. import org.springframework.cloud.client.discovery.DiscoveryClient;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.PathVariable;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RestController;
  10. import org.springframework.web.client.RestTemplate;
  11. import java.util.Arrays;
  12. import java.util.List;
  13. /**
  14. * @ClassName UserController
  15. * @Author 23
  16. * @Date 2024/7/10 18:52
  17. * @Version 1.0
  18. * @Description TODO
  19. **/
  20. @RestController
  21. @RequestMapping("/User")
  22. public class UserControllerrlb {
  23. @Autowired
  24. private RestTemplate restTemplate;
  25. // @Autowired
  26. // private DiscoveryClient discoveryClient;
  27. @GetMapping("/getOrder/{id}")
  28. public List<Order> getOrderById(@PathVariable("id") Integer id) {
  29. // Order[] order = restTemplate.getForObject("http://localhost:10010/OrderService/user/" + id, Order[].class);
  30. // return Arrays.asList(order);
  31. // List<ServiceInstance> instances=discoveryClient.getInstances("order-service");//通过服务的名字去获取服务实例
  32. // ServiceInstance serviceInstance=instances.get(0);//定死只获取第一个服务实例对象
  33. // String ip=serviceInstance.getHost();//获取服务对象ip
  34. // int port=serviceInstance.getPort();//获取获取的实例对象的服务端口号
  35. Order[] orders=restTemplate.getForObject("http://Order-Service/OrderService/user/"+id,Order[].class);
  36. return Arrays.asList(orders);
  37. }
  38. }

リボン負荷分散呼び出し

リボンは、Netflix がリリースしたクラウド中間層サービスのオープンソース プロジェクトであり、その主な機能はクライアントの負荷分散アルゴリズムを提供することです。 リボン クライアント コンポーネントは、接続タイムアウト、再試行などの一連の完全な構成項目を提供します。簡単に言えば、リボンはクライアント ロード バランサーであり、構成ファイル内のロード バランサーの背後にあるすべてのマシンをリストすることができ、リボンは特定のルール (単純なポーリング、ランダム接続など) に基づいて接続を自動的に支援します。マシンでは、リボンを使用してカスタム負荷分散アルゴリズムを簡単に実装できます。 リボンは、特定のルールに従ってサービスの複数のサービス インスタンスの呼び出しの負荷分散を完了できるクライアント ロード バランサーです。これらのルールはカスタマイズもサポートします。

回路図:

OpenFeign 負荷分散

OpenFeign は、Web サービス呼び出しを容易にすることを目的とした宣言型 Web サービス クライアントです。 Feign は、HTTP リクエスト用のインターフェース テンプレートを提供します (アクセス アドレスがマークされています)。簡単なインターフェースを記述し、アノテーションを挿入することで、HTTP リクエストのパラメーター、形式、アドレス、その他の情報を定義できます。 OpenFeign は HTTP リクエストを完全にプロキシ (動的プロキシ) します。これをメソッドのように呼び出すだけで、サービス リクエストと関連処理が完了します。 Feign は、Ribbon と Hystrix (Hystrix については後で説明します) を統合するので、これら 2 つのコンポーネントを明示的に使用する必要がなくなりました。

負荷分散戦略

ルール

さまざまな IRule サブクラスを構成することにより、さまざまなロード バランシング戦略を選択できます。つまり、呼び出しを完了するための特定の戦略を持つサービスをサービス リストから選択できます。もちろんカスタマイズも可能です。したがって、負荷分散戦略は組み込み戦略とカスタム戦略に分けることができます。

組み込みの負荷分散戦略