Zuulにはリボン負荷分散が付属しており、プロジェクト構成でデフォルトで有効になっています
最初にサービスセンターを作成します
2つのクライアント
サービスセンターとクライアントの作成:https://blog.csdn.net/qq_37203082/article/details/111031105
2つのクライアントの構成済みサービスIDは同じである必要があります
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
server:
port: 8889
spring:
application:
name: order-server
テストで異なるコンテンツを出力するには、テストインターフェイスのURLも同じである必要があります
クライアント1
@RestController
public class TestOrderController {
@RequestMapping("orderTest")
public String orderTest(){
return "this is service 1 order";
}
}
クライアント2
@RestController
public class TestOrderController {
@RequestMapping("orderTest")
public String orderTest(){
return "this is service 2 order";
}
}
ZUULゲートウェイ
Maven参照が増加し、残りの参照はクライアントの参照と同じになります
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
クラスを開始し、アノテーション@EnableZuulProxyを追加します
@SpringBootApplication
@EnableZuulProxy
public class SpringCloudZuulApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudZuulApplication.class, args);
}
}
構成ファイル
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
server:
port: 8080
spring:
application:
name: zuul-server
zuul:
routes:
api-a:
path: /server/**
service-id: order-server
起動後の4つのサービスの効果
インターフェイスをテストします。同じインターフェイスが2つの結果を返します。これは、両方のクライアントが入力したことを意味し、デフォルトでポーリング戦略が使用されるため、2つのクライアントが順番にアクセスして2つのノードを形成します。
起動シーケンス:サービスセンター、ズールゲートウェイ、クライアント1、クライアント2
他の戦略をフォローアップしてから、