Micronaut 客戶端負(fù)載均衡

2023-03-13 09:25 更新

當(dāng)從 Consul、Eureka 或其他服務(wù)發(fā)現(xiàn)服務(wù)器發(fā)現(xiàn)服務(wù)時,DiscoveryClient 會發(fā)出可用 ServiceInstance 的列表。

默認(rèn)情況下,Micronaut 使用此列表中的服務(wù)器自動執(zhí)行 Round Robin 客戶端負(fù)載平衡。這與重試建議相結(jié)合,為您的微服務(wù)基礎(chǔ)設(shè)施增加了額外的彈性。

負(fù)載平衡由 LoadBalancer 接口處理,該接口有一個 LoadBalancer.select() 方法,該方法返回一個發(fā)出 ServiceInstance 的 Publisher。

返回 Publisher 是因為選擇 ServiceInstance 的過程可能會導(dǎo)致網(wǎng)絡(luò)操作,具體取決于所采用的服務(wù)發(fā)現(xiàn)策略。

LoadBalancer 接口的默認(rèn)實現(xiàn)是 DiscoveryClientRoundRobinLoadBalancer。您可以用另一種實現(xiàn)替換此策略,以自定義在 Micronaut 中處理客戶端負(fù)載平衡的方式,因為有許多不同的方法可以優(yōu)化負(fù)載平衡。

例如,您可能希望在特定區(qū)域中的服務(wù)之間進行負(fù)載平衡,或者在具有最佳總體響應(yīng)時間的服務(wù)器之間進行負(fù)載平衡。

要替換 LoadBalancer,請定義一個替換 DiscoveryClientLoadBalancerFactory 的 bean。

事實上,這正是 Netflix Ribbon 支持所做的,在下一節(jié)中描述。

Netflix Ribbon 支持

使用 CLI

如果您使用 Micronaut CLI 創(chuàng)建項目,請?zhí)峁?nbsp;netflix-ribbon 功能以在您的項目中配置 Netflix 功能區(qū):

$ mn create-app my-app --features netflix-ribbon

Netflix Ribbon 是 Netflix 使用的進程間通信庫,支持可定制的負(fù)載平衡策略。

如果您的應(yīng)用程序執(zhí)行客戶端負(fù)載平衡的方式需要更大的靈活性,您可以使用 Micronaut 的 Netflix Ribbon 支持。

要將功能區(qū)支持添加到您的應(yīng)用程序,請將 netflix-ribbon 配置添加到您的構(gòu)建中:

 Gradle Maven 
implementation("io.micronaut.netflix:micronaut-netflix-ribbon")
<dependency>
    <groupId>io.micronaut.netflix</groupId>
    <artifactId>micronaut-netflix-ribbon</artifactId>
</dependency>

LoadBalancer 實現(xiàn)現(xiàn)在將是 RibbonLoadBalancer 實例。

可以使用配置中的功能區(qū)命名空間設(shè)置功能區(qū)的配置選項。例如在您的配置文件中(例如 application.yml):

Configuring Ribbon

 Properties Yaml  Toml  Groovy  Hocon  JSON 
ribbon.VipAddress=test
ribbon.ServerListRefreshInterval=2000
ribbon:
  VipAddress: test
  ServerListRefreshInterval: 2000
[ribbon]
  VipAddress="test"
  ServerListRefreshInterval=2000
ribbon {
  VipAddress = "test"
  ServerListRefreshInterval = 2000
}
{
  ribbon {
    VipAddress = "test"
    ServerListRefreshInterval = 2000
  }
}
{
  "ribbon": {
    "VipAddress": "test",
    "ServerListRefreshInterval": 2000
  }
}

每個發(fā)現(xiàn)的客戶端也可以在 ribbon.clients 下配置。例如給定一個 @Client(id = "hello-world") 你可以配置 Ribbon 設(shè)置:

Per Client Ribbon Settings

 Properties Yaml  Toml  Groovy  Hocon  JSON 
ribbon.clients.hello-world.VipAddress=test
ribbon.clients.hello-world.ServerListRefreshInterval=2000
ribbon:
  clients:
    hello-world:
      VipAddress: test
      ServerListRefreshInterval: 2000
[ribbon]
  [ribbon.clients]
    [ribbon.clients.hello-world]
      VipAddress="test"
      ServerListRefreshInterval=2000
ribbon {
  clients {
    helloWorld {
      VipAddress = "test"
      ServerListRefreshInterval = 2000
    }
  }
}
{
  ribbon {
    clients {
      hello-world {
        VipAddress = "test"
        ServerListRefreshInterval = 2000
      }
    }
  }
}
{
  "ribbon": {
    "clients": {
      "hello-world": {
        "VipAddress": "test",
        "ServerListRefreshInterval": 2000
      }
    }
  }
}

默認(rèn)情況下,Micronaut 為每個將 Ribbon 與 Micronaut 的 DiscoveryClient 集成的客戶端注冊一個 DiscoveryClientServerList。


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號