私の連絡先情報
郵便メール:
2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
登録センター サーバーが 1 つしかない場合は、単一障害点が発生し、高い同時実行で処理できないため、クラスターが必要です。
相互に登録するには 3 つの EurekaServer を準備します。つまり、各 EurekaServer は、それ自体を含むすべての EureakServer に登録する必要があり、サーバーとクライアントの両方として機能します。他のマイクロサービス (オーダー、ユーザー) は、登録アドレスをすべての EurekaServer に指定するだけで済みます。
ここで使用されるのは、クラスタリングの効果を実現するために複数の yml ファイルを構成する Eureka モジュールです。
- package org.example;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
-
- /**
- * @ClassName EurekaStart
- * @Author 23
- * @Date 2024/7/10 19:12
- * @Version 1.0
- * @Description TODO
- **/
- @SpringBootApplication
- @EnableEurekaServer
- public class EurekaStart {
- public static void main(String[] args) {
- SpringApplication.run(EurekaStart.class,args);
- }
- }
- spring:
- profiles:
- active: peer3
- ---
- spring:
- profiles: peer1
- application:
- name: Eureka1
- eureka:
- instance:
- hostname: localhost
- client:
- serviceUrl:
- defaultZone: http://localhost:10070/eureka/
- server:
- port: 10070
- ---
- spring:
- profiles: peer2
- application:
- name: Eureka2
- eureka:
- instance:
- hostname: localhost
- client:
- serviceUrl:
- defaultZone: http://localhost:10071/eureka/
- server:
- port: 10071
- ---
- spring:
- profiles: peer3
- application:
- name: Eureka3
- eureka:
- instance:
- hostname: localhost
- client:
- serviceUrl:
- defaultZone: http://localhost:10072/eureka/
- server:
- port: 10072
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.example</groupId>
- <artifactId>Springcloud-Netflix</artifactId>
- <version>1.0-SNAPSHOT</version>
- </parent>
-
- <artifactId>Eureka-Service</artifactId>
- <packaging>jar</packaging>
-
- <name>Eureka-Service</name>
- <url>http://maven.apache.org</url>
-
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- </dependency>
- <!-- Eureka服务端支持 -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
- </dependency>
- </dependencies>
- </project>
同時実行性を提供するために、同じサービス プロバイダーが複数 (コモディティ サービス) を展開できる場合があります。このクライアントは、呼び出し時に特定の責任あるバランシング戦略に従ってロード呼び出しを完了する必要があります。
- spring:
- profiles:
- active: order2
- ---
- server:
- port: 10010
- spring:
- profiles: order1
- application:
- name: order-service
- eureka:
- client:
- service-url:
- defaultZone: http://localhost:10072/eureka
- instance:
- prefer-ip-address: true
- ---
- server:
- port: 10012
- spring:
- profiles: order2
- application:
- name: order-service
- eureka:
- client:
- service-url:
- defaultZone: http://localhost:10072/eureka
- instance:
- prefer-ip-address: true
RestTmplate を通じて、URL を使用してサービス呼び出しを完了します
コード実装 (サービス利用者側):
- package org.example.Controller;
-
- import org.example.domain.Order;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.cloud.client.ServiceInstance;
- import org.springframework.cloud.client.discovery.DiscoveryClient;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.client.RestTemplate;
-
- import java.util.Arrays;
- import java.util.List;
-
- /**
- * @ClassName UserController
- * @Author 23
- * @Date 2024/7/10 18:52
- * @Version 1.0
- * @Description TODO
- **/
- @RestController
- @RequestMapping("/User")
- public class UserControllerrlb {
- @Autowired
- private RestTemplate restTemplate;
- // @Autowired
- // private DiscoveryClient discoveryClient;
- @GetMapping("/getOrder/{id}")
- public List<Order> getOrderById(@PathVariable("id") Integer id) {
- // Order[] order = restTemplate.getForObject("http://localhost:10010/OrderService/user/" + id, Order[].class);
- // return Arrays.asList(order);
- // List<ServiceInstance> instances=discoveryClient.getInstances("order-service");//通过服务的名字去获取服务实例
- // ServiceInstance serviceInstance=instances.get(0);//定死只获取第一个服务实例对象
- // String ip=serviceInstance.getHost();//获取服务对象ip
- // int port=serviceInstance.getPort();//获取获取的实例对象的服务端口号
- Order[] orders=restTemplate.getForObject("http://Order-Service/OrderService/user/"+id,Order[].class);
- return Arrays.asList(orders);
-
- }
- }
リボンは、Netflix がリリースしたクラウド中間層サービスのオープンソース プロジェクトであり、その主な機能はクライアントの負荷分散アルゴリズムを提供することです。 リボン クライアント コンポーネントは、接続タイムアウト、再試行などの一連の完全な構成項目を提供します。簡単に言えば、リボンはクライアント ロード バランサーであり、構成ファイル内のロード バランサーの背後にあるすべてのマシンをリストすることができ、リボンは特定のルール (単純なポーリング、ランダム接続など) に基づいて接続を自動的に支援します。マシンでは、リボンを使用してカスタム負荷分散アルゴリズムを簡単に実装できます。 リボンは、特定のルールに従ってサービスの複数のサービス インスタンスの呼び出しの負荷分散を完了できるクライアント ロード バランサーです。これらのルールはカスタマイズもサポートします。
回路図:
OpenFeign は、Web サービス呼び出しを容易にすることを目的とした宣言型 Web サービス クライアントです。 Feign は、HTTP リクエスト用のインターフェース テンプレートを提供します (アクセス アドレスがマークされています)。簡単なインターフェースを記述し、アノテーションを挿入することで、HTTP リクエストのパラメーター、形式、アドレス、その他の情報を定義できます。 OpenFeign は HTTP リクエストを完全にプロキシ (動的プロキシ) します。これをメソッドのように呼び出すだけで、サービス リクエストと関連処理が完了します。 Feign は、Ribbon と Hystrix (Hystrix については後で説明します) を統合するので、これら 2 つのコンポーネントを明示的に使用する必要がなくなりました。
さまざまな IRule サブクラスを構成することにより、さまざまなロード バランシング戦略を選択できます。つまり、呼び出しを完了するための特定の戦略を持つサービスをサービス リストから選択できます。もちろんカスタマイズも可能です。したがって、負荷分散戦略は組み込み戦略とカスタム戦略に分けることができます。